|
4 | 4 | Construct a perspective transformation. The persepective transformation takes,
|
5 | 5 | e.g., a point in 3D space and "projects" it onto a 2D virtual screen of an ideal
|
6 | 6 | pinhole camera (at distance `1` away from the camera). The camera is oriented
|
7 |
| -towards the positive-Z axis (or in general, along the final dimension). |
| 7 | +towards the positive-Z axis (or in general, along the final dimension) and the |
| 8 | +sign of the `x` and `y` components is preserved for objects in front of the |
| 9 | +camera (objects behind the camera are also projected and therefore inverted - it |
| 10 | +is up to the user to cull these as necessary). |
8 | 11 |
|
9 | 12 | This transformation is designed to be used in composition with other coordinate
|
10 | 13 | transformations, defining e.g. the position and orientation of the camera. For
|
11 | 14 | example:
|
12 | 15 |
|
13 | 16 | cam_transform = PerspectiveMap() ∘ inv(AffineMap(cam_rotation, cam_position))
|
14 | 17 | screen_points = map(cam_transform, points)
|
| 18 | +
|
| 19 | +(see also `cameramap`) |
15 | 20 | """
|
16 | 21 | immutable PerspectiveMap <: Transformation
|
17 | 22 | end
|
|
24 | 29 | @inline function (::PerspectiveMap)(v::StaticVector)
|
25 | 30 | return pop(v) * inv(v[end])
|
26 | 31 | end
|
| 32 | + |
| 33 | +Base.@pure Base.isapprox(::PerspectiveMap, ::PerspectiveMap; kwargs...) = true |
| 34 | + |
| 35 | +""" |
| 36 | + cameramap(property = value, ...) |
| 37 | +
|
| 38 | +Create a transformation that takes points in real space (e.g. 3D) and projects |
| 39 | +them through a perspective transformation onto the focal plane of an ideal |
| 40 | +(pinhole) camera with the given properties. |
| 41 | +
|
| 42 | +All properties are optional. Valid properties include: |
| 43 | +
|
| 44 | + * `focal_length` (in physical units) |
| 45 | + * `pixel_size` (in physical units) or `pixel_size_x` and `pixel_size_y` |
| 46 | + * `offset_x` and `offset_y` (in pixels) |
| 47 | + * `origin` (a vector) and `orientation` (a rotation matrix) |
| 48 | +
|
| 49 | +By default, the camera looks towards the postive-`z` axis from `(0,0,0)` and |
| 50 | +the sign of the `x` and `y` components is preserved for objects in front of the |
| 51 | +camera (objects behind the camera are also projected and therefor inverted - it |
| 52 | +is up to the user to cull these as necessary). |
| 53 | +
|
| 54 | +If `origin` and `orientation` are specified, the camera is translated to `origin` |
| 55 | +and rotated by `orientation` before the perspective map is applied. |
| 56 | +
|
| 57 | +(see also `PerspectiveMap`) |
| 58 | +""" |
| 59 | +function cameramap(;focal_length = nothing, |
| 60 | + pixel_size = nothing, |
| 61 | + pixel_size_x = nothing, |
| 62 | + pixel_size_y = nothing, |
| 63 | + offset_x = nothing, |
| 64 | + offset_y = nothing, |
| 65 | + origin = nothing, |
| 66 | + orientation = nothing) |
| 67 | + |
| 68 | + trans = PerspectiveMap() |
| 69 | + |
| 70 | + if pixel_size === nothing # (this form of if-else-end is handled well by the compiler... the author is looking forward to v0.6 where !(::Bool) is pure...) |
| 71 | + else |
| 72 | + pixel_size_x = pixel_size |
| 73 | + pixel_size_y = pixel_size |
| 74 | + end |
| 75 | + |
| 76 | + # Apply camera rotations, if necessary |
| 77 | + if origin === nothing && orientation === nothing |
| 78 | + else |
| 79 | + trans = trans ∘ inv(AffineMap(orientation, origin)) |
| 80 | + end |
| 81 | + |
| 82 | + # Apply camera scaling, if necessary |
| 83 | + if isa(focal_length, Void) && isa(pixel_size_x, Void) && isa(pixel_size_y, Void) |
| 84 | + else |
| 85 | + trans = LinearMap(UniformScaling(focal_length/pixel_size)) ∘ trans |
| 86 | + end |
| 87 | + |
| 88 | + # Apply pixel offset, if necessary |
| 89 | + if isa(offset_x, Void) && isa(offset_y, Void) |
| 90 | + else |
| 91 | + trans = Translation(SVector(-offset_x, -offset_y)) ∘ trans |
| 92 | + end |
| 93 | + |
| 94 | + return trans |
| 95 | +end |
0 commit comments