Skip to content

Commit cf3ca22

Browse files
committed
Eager composition and AbstractLinearTransformation
1 parent d739e81 commit cf3ca22

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

src/commontransformations.jl

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
abstract AbstractAffineTransformation <: Transformation
2+
abstract AbstractLinearTransformation <: AbstractAffineTransformation
23

3-
4-
# Composite affine transformation.
5-
"""
6-
A composite of two affine transformations
7-
"""
8-
immutable ComposedAffineTransformation{T1 <: AbstractAffineTransformation, T2 <: AbstractAffineTransformation} <: AbstractAffineTransformation
9-
comp::ComposedTransformation{T1,T2}
10-
end
11-
12-
ComposedAffineTransformation(t1, t2) = ComposedAffineTransformation(ComposedTransformation(t1,t2))
13-
14-
# The repetition below is required since we can't inherit both from
15-
# AbstractAffineTransformation and AbstractComposedTransformation. (Needs
16-
# traits!)
17-
Base.show(io::IO, trans::ComposedAffineTransformation) = show(io, trans.comp)
18-
@compat @inline (trans::ComposedAffineTransformation)(x) = trans.comp(x)
19-
Base.inv(trans::ComposedAffineTransformation) = ComposedAffineTransformation(inv(trans.comp))
20-
transform_deriv(trans::ComposedAffineTransformation, x) = transform_deriv(trans.comp, x)
21-
transform_deriv_params(trans::ComposedAffineTransformation, x) = transform_deriv_params(trans.comp, x)
22-
23-
compose(t1::AbstractAffineTransformation, t2::AbstractAffineTransformation) = ComposedAffineTransformation(t1,t2)
24-
4+
# Helper to compute a zeroed input point for an affine transformation
5+
# FIXME: deal with general input dimension and type.
6+
zeroed_input(trans::AbstractAffineTransformation) = Vec(0,0,0)
257

268
# transform_deriv for AbstractAffineTransformation is independent of the point
279
# about which we're linearizing.
2810
function transform_deriv(trans::AbstractAffineTransformation)
29-
x = Vec(0,0,0) # FIXME: deal with general input dimension and type.
30-
transform_deriv(trans, x)
11+
transform_deriv(trans, zeroed_input(trans))
3112
end
3213

14+
3315
#-------------------------------------------------------------------------------
3416
# Linear Transformations
3517

36-
immutable LinearTransformation{MatrixT} <: AbstractAffineTransformation
18+
"""
19+
LinearTransformation <: AbstractLinearTransformation
20+
21+
A general linear transformation, constructed using `LinearTransformation(M)`
22+
for any matrix `M`. Other abstract linear transformations can be converted
23+
into a general linear transformation using `LinearTransformation(trans)`
24+
"""
25+
immutable LinearTransformation{MatrixT} <: AbstractLinearTransformation
3726
M::MatrixT
3827
end
3928

29+
LinearTransformation(trans::LinearTransformation) = trans
30+
31+
function LinearTransformation(trans::AbstractLinearTransformation)
32+
LinearTransformation(transform_deriv(trans, zeroed_input(trans)))
33+
end
34+
4035
Base.show(io::IO, trans::LinearTransformation) = print(io, "LinearTransformation($(trans.M))")
4136

4237
@compat function (trans::LinearTransformation)(x)
@@ -47,7 +42,12 @@ Base.inv(trans::LinearTransformation) = LinearTransformation(inv(trans.M))
4742

4843
compose(t1::LinearTransformation, t2::LinearTransformation) = LinearTransformation(t1.M*t2.M)
4944

50-
transform_deriv(trans::LinearTransformation) = trans.M
45+
transform_deriv(trans::LinearTransformation, x) = trans.M
46+
47+
48+
function compose(t1::AbstractLinearTransformation, t2::AbstractLinearTransformation)
49+
LinearTransformation(t1) LinearTransformation(t2)
50+
end
5151

5252

5353
#-------------------------------------------------------------------------------
@@ -72,11 +72,15 @@ immutable AffineTransformation{MatrixT, VectorT} <: AbstractAffineTransformation
7272
end
7373

7474
function AffineTransformation(trans::AbstractAffineTransformation)
75-
# FIXME: How do we deal with general input dimension and type?
76-
x = Vec(0,0,0)
75+
x = zeroed_input(trans)
7776
AffineTransformation(transform_deriv(trans, x), trans(x))
7877
end
7978

79+
function AffineTransformation(trans::AbstractLinearTransformation)
80+
x = zeroed_input(trans)
81+
AffineTransformation(transform_deriv(trans, x), x)
82+
end
83+
8084
function AffineTransformation(trans::Transformation, x)
8185
AffineTransformation(transform_deriv(trans, x), trans(x))
8286
end
@@ -100,6 +104,10 @@ function compose(t1::AffineTransformation, t2::AffineTransformation)
100104
AffineTransformation(t1.M*t2.M, t1.v + t1.M*t2.v)
101105
end
102106

107+
function compose(t1::AbstractAffineTransformation, t2::AbstractAffineTransformation)
108+
AffineTransformation(t1) AffineTransformation(t2)
109+
end
110+
103111
"""
104112
affine_decomposition_T_of_L(trans)
105113
@@ -175,7 +183,7 @@ end
175183
Construct the `Rotation2D` transformation for rotating 2D Cartesian points
176184
(i.e. `FixedVector{2}`s) about the origin.
177185
"""
178-
immutable Rotation2D{T} <: AbstractAffineTransformation
186+
immutable Rotation2D{T} <: AbstractLinearTransformation
179187
angle::T
180188
sin::T
181189
cos::T
@@ -251,7 +259,7 @@ compose(t1::Rotation2D, t2::Rotation2D) = Rotation2D(t1.angle + t2.angle)
251259
Construct the `Rotation` transformation for rotating 3D Cartesian points
252260
(i.e. `FixedVector{3}`s) about the origin. I
253261
"""
254-
immutable Rotation{R, T} <: AbstractAffineTransformation
262+
immutable Rotation{R, T} <: AbstractLinearTransformation
255263
rotation::R
256264
matrix::Mat{3,3,T} # Should we enforce this storage, or merely "suggest" it
257265
end
@@ -391,7 +399,7 @@ the Z axis).
391399
392400
(see also `Rotation`, `RotationYZ`, `RotationZX` and `euler_rotation`)
393401
"""
394-
immutable RotationXY{T} <: AbstractAffineTransformation
402+
immutable RotationXY{T} <: AbstractLinearTransformation
395403
angle::T
396404
sin::T
397405
cos::T
@@ -405,7 +413,7 @@ the X axis).
405413
406414
(see also `Rotation`, `RotationXY`, `RotationZX` and `euler_rotation`)
407415
"""
408-
immutable RotationYZ{T} <: AbstractAffineTransformation
416+
immutable RotationYZ{T} <: AbstractLinearTransformation
409417
angle::T
410418
sin::T
411419
cos::T
@@ -419,7 +427,7 @@ the Y axis).
419427
420428
(see also `Rotation`, `RotationXY`, `RotationYZ` and `euler_rotation`)
421429
"""
422-
immutable RotationZX{T} <: AbstractAffineTransformation
430+
immutable RotationZX{T} <: AbstractLinearTransformation
423431
angle::T
424432
sin::T
425433
cos::T

0 commit comments

Comments
 (0)