This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
/
composite.py
243 lines (186 loc) · 7.08 KB
/
composite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import numpy as np
from blmath.numerics import vx
def convert_33_to_44(matrix):
'''
Transform from:
array([[1., 2., 3.],
[2., 3., 4.],
[5., 6., 7.]])
to:
array([[1., 2., 3., 0.],
[2., 3., 4., 0.],
[5., 6., 7., 0.],
[0., 0., 0., 1.]])
'''
if matrix.shape != (3, 3):
raise ValueError("Expected 3x3 matrix, got: %s" % matrix.shape)
result = np.pad(matrix, ((0, 1), (0, 1)), mode='constant')
result[3][3] = 1
return result
def convert_44_to_33(matrix):
'''
Transform from:
array([[1., 2., 3., 0.],
[2., 3., 4., 0.],
[5., 6., 7., 0.],
[0., 0., 0., 1.]])
to:
array([[1., 2., 3.],
[2., 3., 4.],
[5., 6., 7.]])
'''
if matrix.shape != (4, 4):
raise ValueError("Expected 4x4 matrix, got: %s" % matrix.shape)
return matrix[:3, :3]
class CompositeTransform(object):
'''
Composite transform using homogeneous coordinates.
Example usage
-------------
transform = CompositeTransform()
transform.scale(10)
transform.reorient(up=[0, 1, 0], look=[-1, 0, 0])
transform.translate([0, -2.5, 0])
transformed_scan = Mesh(v=transform(scan.v), f=scan.f)
...
untransformed_alignment = Mesh(
v=transform(alignment.v, reverse=True),
f=alignment.f
)
Backround
---------
- Computer Graphics: Principles and Practice, Hughes, van Dam, McGuire,
Sklar, Foley
- http://gamedev.stackexchange.com/questions/72044/why-do-we-use-4x4-matrices-to-transform-things-in-3d
'''
def __init__(self):
# List of tuples, containing forward and reverse matrices.
self.transforms = []
def __call__(self, points, from_range=None, reverse=False):
'''
points: Points to transform, as a 3xn array.
range: The indices of the subset of the transformations to apply.
e.g. (0, 2), (2, 4). The default is to apply them all.
reverse: When `True` applies the selected transformations in reverse.
This has no effect on how range is interpreted, only whether the
selected transformations apply in the forward or reverse mode.
'''
matrix = self.matrix_for(from_range=from_range, reverse=reverse)
return vx.unpad(np.dot(matrix, vx.pad_with_ones(points).T).T)
def matrix_for(self, from_range=None, reverse=False):
'''
Return a 4x4 transformation matrix representation.
range: The min and max indices of the subset of the transformations to
apply. e.g. (0, 2), (2, 4). Inclusive of the min value, exclusive of
the max value. The default is to apply them all.
reverse: When `True` returns a matrix for the inverse transform.
This has no effect on how range is interpreted, only whether the
forward or reverse matrices are used.
'''
import six
if from_range is not None:
start, stop = from_range # from_range is defined as None, a non-sequence, but when it's not None, it's always a sequence. pylint: disable=unpacking-non-sequence
selected_transforms = self.transforms[start:stop]
else:
selected_transforms = self.transforms
# The transpose of a product of matrices equals the products of each
# transpose in reverse order.
matrices = [
reverse_matrix if reverse else forward_matrix.T
for forward_matrix, reverse_matrix in selected_transforms
]
if not len(matrices): # pylint: disable=len-as-condition
return np.eye(4)
matrix = six.moves.reduce(np.dot, matrices)
return matrix if reverse else matrix.T
def append_transform4(self, forward, reverse=None):
'''
Append an arbitrary transformation, defined by 4x4 forward and reverse
matrices.
The new transformation is added to the end. Return its index.
'''
if reverse is None:
reverse = np.linalg.inv(forward)
new_index = len(self.transforms)
self.transforms.append((forward, reverse))
return new_index
def append_transform3(self, forward, reverse=None):
'''
Append an arbitrary transformation, defined by 3x3 forward and reverse
matrices.
The new transformation is added to the end. Return its index.
'''
args = (forward,) if reverse is None else (forward, reverse)
return self.append_transform4(*map(convert_33_to_44, args))
def scale(self, factor):
'''
Scale by the given factor.
factor: A float or int.
Forward:
[[ s_0, 0, 0, 0 ],
[ 0, s_1, 0, 0 ],
[ 0, 0, s_2, 0 ],
[ 0, 0, 0, 1 ]]
Reverse:
[[ 1/s_0, 0, 0, 0 ],
[ 0, 1/s_1, 0, 0 ],
[ 0, 0, 1/s_2, 0 ],
[ 0, 0, 0, 1 ]]
'''
if factor <= 0:
raise ValueError('Scale factor should be greater than zero')
forward3 = np.diag(np.repeat(factor, 3))
reverse3 = np.diag(np.repeat(1./factor, 3))
return self.append_transform3(forward3, reverse3)
def convert_units(self, from_units, to_units):
'''
Convert the mesh from one set of units to another.
These calls are equivalent:
- composite.convert_units(from_units='cm', to_units='m')
- composite.scale(.01)
'''
from blmath import units
factor = units.factor(
from_units=from_units,
to_units=to_units,
units_class='length'
)
self.scale(factor)
def translate(self, vector):
'''
Translate by the vector provided.
vector: A 3x1 vector.
Forward:
[[ 1, 0, 0, v_0 ],
[ 0, 1, 0, v_1 ],
[ 0, 0, 1, v_2 ],
[ 0, 0, 0, 1 ]]
Reverse:
[[ 1, 0, 0, -v_0 ],
[ 0, 1, 0, -v_1 ],
[ 0, 0, 1, -v_2 ],
[ 0, 0, 0, 1 ]]
'''
vector = np.asarray(vector)
forward = np.eye(4)
forward[:, -1][:-1] = vector
reverse = np.eye(4)
reverse[:, -1][:-1] = -vector
return self.append_transform4(forward, reverse)
def reorient(self, up, look):
'''
Reorient using up and look.
'''
from blmath.geometry.transform import rotation_from_up_and_look
forward3 = rotation_from_up_and_look(up, look)
# The inverse of a rotation matrix is its transpose.
return self.append_transform3(forward3, forward3.T)
def rotate(self, rot):
'''
Rotate by either an explicit matrix or a rodrigues vector
'''
from blmath.geometry.transform.rodrigues import as_rotation_matrix
rot = np.asarray(rot)
rot = as_rotation_matrix(rot)
# The inverse of a rotation matrix is its transpose.
return self.append_transform3(rot, rot.T)