-
Notifications
You must be signed in to change notification settings - Fork 5
/
tiny_spatial_transform.h
294 lines (248 loc) · 9.45 KB
/
tiny_spatial_transform.h
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TINY_SPATIAL_TRANSFORM_H
#define TINY_SPATIAL_TRANSFORM_H
#include "tiny_matrix3x3.h"
#include "tiny_spatial_motion_vector.h"
#include "tiny_vector3.h"
/**
* We follow the spatial algebra from Featherstone, but use right-handed
* transformation matrix applications so that the rotations need to be
* transposed and the transformation matrices are multiplied from right to left.
*/
template <typename TinyScalar, typename TinyConstants>
class TinySpatialTransform {
public:
enum eOutputOperation { None = 0, Add = 1, Subtract = 2 };
typedef ::TinyMatrix3x3<TinyScalar, TinyConstants> TinyMatrix3x3;
typedef ::TinyVector3<TinyScalar, TinyConstants> TinyVector3;
typedef ::TinySpatialMotionVector<TinyScalar, TinyConstants>
TinySpatialMotionVector;
TinyVector3 m_translation;
TinyMatrix3x3 m_rotation;
TinySpatialTransform() { set_identity(); }
void set_identity() {
m_translation.set_zero();
m_rotation.set_identity();
}
void set_zero() {
m_translation.set_zero();
m_rotation.set_zero();
}
/**
* X1*X2 = plx(E1*E2, r2 + E2T*r1)
*/
TinySpatialTransform operator*(const TinySpatialTransform& t) const {
TinySpatialTransform tr = *this;
tr.m_translation += m_rotation * t.m_translation;
tr.m_rotation *= t.m_rotation;
return tr;
}
TinySpatialTransform& operator+=(const TinySpatialTransform& t) {
m_rotation += t.m_rotation;
m_translation += t.m_translation;
return *this;
}
// template <typename SpatialVectorType>
// void adj_st_multiply(const SpatialVectorType& R, const TinySpatialTransform& a,
// TinySpatialTransform& adj_t1, SpatialVectorType& adj_t2) const {
// const TinyMatrix3x3& E1 = m_rotation;
// const TinyVector3& r1 = m_translation;
// const TinyMatrix3x3& E2 = a.m_rotation;
// const TinyVector3& r2 = a.m_translation;
// const TinyMatrix3x3& E0 = R.m_rotation;
// const TinyVector3& r0 = R.m_translation;
// adj_t1.m_rotation += E0*E2.transpose();
// adj_t1.m_translation += E2.transpose()*r0;
// adj_t2.m_rotation += E1.transpose()*E0+TinyMatrix3x3::vvt(r0, r1);
// adj_t2.m_translation += r0;
// }
template <typename SpatialVectorType>
void adj_st_multiply(const SpatialVectorType& R, const TinySpatialTransform& a,
TinySpatialTransform& adj_t1, SpatialVectorType& adj_t2) const {
const TinyMatrix3x3& E1 = m_rotation;
const TinyVector3& r1 = m_translation;
const TinyMatrix3x3& E2 = a.m_rotation;
const TinyVector3& r2 = a.m_translation;
const TinyMatrix3x3& E0 = R.m_rotation;
const TinyVector3& r0 = R.m_translation;
adj_t1.m_rotation += E0*E2.transpose()+TinyMatrix3x3::vvt(r0, r2);
adj_t1.m_translation += r0;
adj_t2.m_rotation += E1.transpose()*E0;
adj_t2.m_translation += E1.transpose()*r0;
}
void print(const char* name) const {
printf("%s\n", name);
double x = TinyConstants::getDouble(m_translation.getX());
double y = TinyConstants::getDouble(m_translation.getY());
double z = TinyConstants::getDouble(m_translation.getZ());
printf("translation: %f,%f,%f\n", x, y, z);
printf("rotation:\n");
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
double v = TinyConstants::getDouble(m_rotation[r][c]);
printf("%f, ", v);
}
printf("\n");
}
}
template <typename SpatialVectorType>
SpatialVectorType transformRotateOnly(const SpatialVectorType& vecIn) const {
SpatialVectorType outVec;
outVec.m_topVec = m_rotation * vecIn.m_topVec;
outVec.m_bottomVec = m_rotation * vecIn.m_bottomVec;
return outVec;
}
TinyVector3 apply(const TinyVector3& point) const {
return m_rotation * point + m_translation;
}
void adj_apply(const TinyVector3& R, const TinyVector3& point,
TinySpatialTransform& Rst, TinyVector3& Rpoint) const {
Rpoint += m_rotation.transpose() * R;
Rst.m_rotation += TinyMatrix3x3::vvt(R, point);
Rst.m_translation += R;
}
TinyVector3 apply_inverse(const TinyVector3& point) const {
return m_rotation.transpose() * (point - m_translation);
}
TinySpatialTransform get_inverse() const {
TinySpatialTransform inv;
inv.m_rotation = m_rotation.transpose();
inv.m_translation = inv.m_rotation * -m_translation;
return inv;
}
/**
* V = mv(w, v)
* X*V = mv(E*w, E*(v - r x w))
*/
template <typename SpatialVectorType>
SpatialVectorType apply(const SpatialVectorType& inVec) const {
SpatialVectorType outVec;
TinyVector3 rxw = inVec.m_topVec.cross(m_translation);
TinyVector3 v_rxw = inVec.m_bottomVec + rxw;
TinyVector3 tmp3 = m_rotation.transpose() * v_rxw;
TinyVector3 tmp4 = m_rotation.transpose() * inVec.m_topVec;
outVec.m_topVec = tmp4;
outVec.m_bottomVec = tmp3;
return outVec;
}
/**
* V = mv(w, v)
* X*V = mv(E*w, E*(v - r x w))
*/
template <typename SpatialVectorType>
void adj_st_apply(const SpatialVectorType& R, const SpatialVectorType& a,
TinySpatialTransform& adj_t, SpatialVectorType& adj_a) const {
const TinyVector3& a1 = a.m_topVec;
const TinyVector3& a2 = a.m_bottomVec;
const TinyVector3& b1 = R.m_topVec;
const TinyVector3& b2 = R.m_bottomVec;
const TinyMatrix3x3& E = m_rotation;
const TinyVector3& r = m_translation;
adj_t.m_rotation += TinyMatrix3x3::vvt(a1, b1)+TinyMatrix3x3::vvt(a2-r.cross(a1), b2);
adj_t.m_translation += (E*b2).cross(a1);
adj_a.m_topVec += E*b1-(E*b2).cross(r);
adj_a.m_bottomVec += E*b2;
}
/**
* V = mv(w, v)
* inv(X)*V = mv(ET*w, ET*v + r x (ET*w))
*/
template <typename SpatialVectorType>
SpatialVectorType apply_inverse(const SpatialVectorType& inVec) const {
SpatialVectorType outVec;
outVec.m_topVec = m_rotation * inVec.m_topVec;
outVec.m_bottomVec =
m_rotation * inVec.m_bottomVec + m_translation.cross(outVec.m_topVec);
return outVec;
}
template <typename SpatialVectorType>
void adj_st_apply_inverse(const SpatialVectorType& R, const SpatialVectorType& a,
TinySpatialTransform& adj_t, SpatialVectorType& adj_a) const {
const TinyVector3& a1 = a.m_topVec;
const TinyVector3& a2 = a.m_bottomVec;
const TinyVector3& b1 = R.m_topVec;
const TinyVector3& b2 = R.m_bottomVec;
const TinyMatrix3x3& E = m_rotation;
const TinyVector3& r = m_translation;
adj_t.m_rotation += TinyMatrix3x3::vvt(b1+b2.cross(r), a1)
+TinyMatrix3x3::vvt(b2, a2);
adj_t.m_translation += -(b2).cross(E*a1);
adj_a.m_topVec += E.transpose()*(b1+r.cross(b2));
adj_a.m_bottomVec += E.transpose()*b2;
}
/**
* F = fv(n, f)
* XT*F = fv(ETn + rxETf, ETf)
*/
template <typename SpatialVectorType>
SpatialVectorType apply_transpose(const SpatialVectorType& inVec) const {
SpatialVectorType outVec;
outVec.m_bottomVec = m_rotation * inVec.m_bottomVec;
outVec.m_topVec = m_rotation * inVec.m_topVec;
outVec.m_topVec +=
TinyVectorCrossMatrix(m_translation) * outVec.m_bottomVec;
return outVec;
}
/**
* F = fv(n, f)
* XT*F = fv(ETn + rxETf, ETf)
*/
template <typename SpatialVectorType>
void adj_st_apply_trans(const SpatialVectorType& R, const SpatialVectorType& a,
TinySpatialTransform& adj_t, SpatialVectorType& adj_a) const {
const TinyVector3& a1 = a.m_topVec;
const TinyVector3& a2 = a.m_bottomVec;
const TinyVector3& b1 = R.m_topVec;
const TinyVector3& b2 = R.m_bottomVec;
const TinyMatrix3x3& E = m_rotation;
const TinyVector3& r = m_translation;
adj_t.m_rotation += TinyMatrix3x3::vvt(b1, a1)+TinyMatrix3x3::vvt(b1.cross(r)+b2, a2);
adj_t.m_translation += -b1.cross(E*a2);
adj_a.m_topVec += E.transpose()*b1;
adj_a.m_bottomVec += E.transpose()*(b1.cross(r)+b2);
}
/**
* F = fv(n, f)
* X^* F = fv(E(n - rxf), Ef)
*/
template <typename SpatialVectorType>
SpatialVectorType apply_inverse_transpose(
const SpatialVectorType& inVec) const {
const TinyVector3& n = inVec.m_topVec;
const TinyVector3& f = inVec.m_bottomVec;
SpatialVectorType outVec;
outVec.m_topVec = m_rotation.transpose() * (n - m_translation.cross(f));
outVec.m_bottomVec = m_rotation.transpose() * f;
return outVec;
}
/**
* F = fv(n, f)
* X^* F = fv(E(n - rxf), Ef)
*/
// template <typename SpatialVectorType>
// SpatialVectorType adj_st_apply_inverse_transpose(
// const SpatialVectorType& R, const SpatialVectorType& a,
// TinySpatialTransform& adj_t, SpatialVectorType& adj_a) const {
// const TinyVector3& n = inVec.m_topVec;
// const TinyVector3& f = inVec.m_bottomVec;
// SpatialVectorType outVec;
// outVec.m_topVec = m_rotation.transpose() * (n - m_translation.cross(f));
// outVec.m_bottomVec = m_rotation.transpose() * f;
// return outVec;
// }
};
#endif // TINY_SPATIAL_TRANSFORM_H