Skip to content

Commit 4c2e9df

Browse files
Andy FerrisChris Foster
authored andcommitted
The perspective transformation
* Can convert 3D positions to 2D screen locations using PerspectiveMap
1 parent 9b92f28 commit 4c2e9df

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/CoordinateTransformations.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ export SphericalFromCartesian, CartesianFromSpherical,
2828
# Common transformations
2929
export AbstractAffineMap
3030
export AffineMap, LinearMap, Translation
31+
export PerspectiveMap
3132

3233
include("core.jl")
3334
include("coordinatesystems.jl")
3435
include("affine.jl")
36+
include("perspective.jl")
3537

3638
# Deprecations
3739
export transform

src/perspective.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
PerspectiveMap()
3+
4+
Construct a perspective transformation. The persepective transformation takes,
5+
e.g., a point in 3D space and "projects" it onto a 2D virtual screen of an ideal
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).
8+
9+
This transformation is designed to be used in composition with other coordinate
10+
transformations, defining e.g. the position and orientation of the camera. For
11+
example:
12+
13+
cam_transform = PerspectiveMap() ∘ AffineMap(cam_rotation, -cam_position)
14+
screen_points = map(cam_transform, points)
15+
"""
16+
immutable PerspectiveMap <: Transformation
17+
end
18+
19+
function (::PerspectiveMap)(v::AbstractVector)
20+
scale = 1/v[end]
21+
return [v[i] * scale for i in 1:length(v)-1]
22+
end
23+
24+
@inline function (::PerspectiveMap)(v::StaticVector)
25+
return pop(v) * inv(v[end])
26+
end

0 commit comments

Comments
 (0)