Skip to content

Commit cd615e1

Browse files
authored
Merge pull request #21 from timholy/pull-request/7e99e237
Add `recenter` convenience function
2 parents 529d950 + 9c49fa5 commit cd615e1

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ composed_inv = inv(composed)
5656
composed_inv(z) == x
5757
```
5858

59+
For any transformation, we can shift the origin to a new point using `recenter`:
60+
```julia
61+
rot_around_x = recenter(rot, x)
62+
```
63+
Now `rot_around_x` is a rotation around the point `x = SVector(1.0, 2.0, 3.0)`.
64+
65+
5966
Finally, we can construct a matrix describing how the components of `z`
6067
differentiates with respect to components of `x`:
6168
```julia

src/CoordinateTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export RotMatrix, Quat, SpQuat, AngleAxis, RodriguesVec,
1212
RotXYZ, RotYXZ, RotZXY, RotXZY, RotYZX, RotZYX
1313

1414
# Core methods
15-
export compose, , transform_deriv, transform_deriv_params
15+
export compose, , transform_deriv, transform_deriv_params, recenter
1616
export Transformation, IdentityTransformation
1717

1818
# 2D coordinate systems and their transformations

src/affine.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,7 @@ function Base.:(==)(t1::LinearMap, t2::AffineMap)
202202
0 == vecnorm(t2.v)
203203
end
204204

205+
recenter(trans::AbstractMatrix, origin::AbstractVector) = recenter(LinearMap(trans), origin)
206+
205207
transform_deriv(trans::AffineMap, x) = trans.m
206208
# TODO transform_deriv_params

src/core.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ end
7070
Base.inv(trans::ComposedTransformation) = inv(trans.t2) inv(trans.t1)
7171
Base.inv(trans::IdentityTransformation) = trans
7272

73+
"""
74+
recenter(trans::Union{AbstractMatrix,Transformation}, origin::AbstractVector) -> ctrans
75+
76+
Return a new transformation `ctrans` such that point `origin` serves
77+
as the origin-of-coordinates for `trans`. For example, if `trans` is a
78+
rotation matrix, then `ctrans` is a rotation around `origin`.
79+
"""
80+
function recenter(trans::Transformation, origin::AbstractVector)
81+
Translation(origin) trans Translation(-origin)
82+
end
83+
7384

7485
"""
7586
transform_deriv(trans::Transformation, x)

test/affine.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ CoordinateTransformations.transform_deriv(::SquareMe, x0) = diagm(2*x0)
8888
@test m2 == eye(2)
8989
end
9090

91+
@testset "Recenter" begin
92+
M = [1 2; 3 4]
93+
for origin in ([5,-3], @SVector([5,-3]))
94+
c = recenter(M, origin)
95+
@test c(origin) == origin
96+
@test c(zero(origin)) == [6,-6]
97+
end
98+
end
99+
91100
#=
92101
@testset "Rotation2D on Polar" begin
93102
p = Polar(2.0, 1.0)

0 commit comments

Comments
 (0)