53 #define ACG_MATRIX4X4_C 59 #include "Matrix4x4T.hh" 60 #include "../Utils/NumLimitsT.hh" 69 #define MAT(m,r,c) ((m)[(r)+((c)<<2)]) 70 #define M(r,w) (MAT(mat_,r,w)) 76 template <
typename Scalar>
81 #define RHS(row,col) MAT(_rhs.mat_, row,col) 82 #define TMP(row,col) MAT(tmp.mat_, row,col) 85 Scalar mi0, mi1, mi2, mi3;
88 for (i = 0; i < 4; i++) {
89 mi0=M(i,0); mi1=M(i,1); mi2=M(i,2); mi3=M(i,3);
90 TMP(i,0) = mi0*RHS(0,0) + mi1*RHS(1,0) + mi2*RHS(2,0) + mi3*RHS(3,0);
91 TMP(i,1) = mi0*RHS(0,1) + mi1*RHS(1,1) + mi2*RHS(2,1) + mi3*RHS(3,1);
92 TMP(i,2) = mi0*RHS(0,2) + mi1*RHS(1,2) + mi2*RHS(2,2) + mi3*RHS(3,2);
93 TMP(i,3) = mi0*RHS(0,3) + mi1*RHS(1,3) + mi2*RHS(2,3) + mi3*RHS(3,3);
106 template <
typename Scalar>
111 #define RHS(row,col) MAT(_rhs.mat_, row,col) 114 Scalar mi0, mi1, mi2, mi3;
116 for (i = 0; i < 4; i++)
118 mi0=M(i,0); mi1=M(i,1); mi2=M(i,2); mi3=M(i,3);
119 M(i,0) = mi0 * RHS(0,0) + mi1 * RHS(1,0) + mi2 * RHS(2,0) + mi3 * RHS(3,0);
120 M(i,1) = mi0 * RHS(0,1) + mi1 * RHS(1,1) + mi2 * RHS(2,1) + mi3 * RHS(3,1);
121 M(i,2) = mi0 * RHS(0,2) + mi1 * RHS(1,2) + mi2 * RHS(2,2) + mi3 * RHS(3,2);
122 M(i,3) = mi0 * RHS(0,3) + mi1 * RHS(1,3) + mi2 * RHS(2,3) + mi3 * RHS(3,3);
134 template <
typename Scalar>
139 #define RHS(row,col) MAT(_rhs.mat_, row,col) 141 Scalar m0i, m1i, m2i, m3i;
144 m0i = M(0,i); m1i = M(1,i); m2i = M(2,i); m3i = M(3,i);
145 M(0,i) = RHS(0,0)*m0i + RHS(0,1)*m1i + RHS(0,2)*m2i + RHS(0,3)*m3i;
146 M(1,i) = RHS(1,0)*m0i + RHS(1,1)*m1i + RHS(1,2)*m2i + RHS(1,3)*m3i;
147 M(2,i) = RHS(2,0)*m0i + RHS(2,1)*m1i + RHS(2,2)*m2i + RHS(2,3)*m3i;
148 M(3,i) = RHS(3,0)*m0i + RHS(3,1)*m1i + RHS(3,2)*m2i + RHS(3,3)*m3i;
158 template <
typename Scalar>
159 template <
typename T>
165 M(0,0)*_v[0] + M(0,1)*_v[1] + M(0,2)*_v[2] + M(0,3)*_v[3],
166 M(1,0)*_v[0] + M(1,1)*_v[1] + M(1,2)*_v[2] + M(1,3)*_v[3],
167 M(2,0)*_v[0] + M(2,1)*_v[1] + M(2,2)*_v[2] + M(2,3)*_v[3],
168 M(3,0)*_v[0] + M(3,1)*_v[1] + M(3,2)*_v[2] + M(3,3)*_v[3]);
175 template <
typename Scalar>
179 for (
int i = 0; i < 4; ++i) {
180 for (
int j = 0; j < 4; ++j) {
192 template <
typename Scalar>
193 template <
typename T>
198 Scalar x = M(0,0)*_v[0] + M(0,1)*_v[1] + M(0,2)*_v[2] + M(0,3);
199 Scalar y = M(1,0)*_v[0] + M(1,1)*_v[1] + M(1,2)*_v[2] + M(1,3);
200 Scalar z = M(2,0)*_v[0] + M(2,1)*_v[1] + M(2,2)*_v[2] + M(2,3);
201 Scalar w = M(3,0)*_v[0] + M(3,1)*_v[1] + M(3,2)*_v[2] + M(3,3);
215 template <
typename Scalar>
216 template <
typename T>
221 Scalar x = M(0,0)*_v[0] + M(0,1)*_v[1] + M(0,2)*_v[2];
222 Scalar y = M(1,0)*_v[0] + M(1,1)*_v[1] + M(1,2)*_v[2];
223 Scalar z = M(2,0)*_v[0] + M(2,1)*_v[1] + M(2,2)*_v[2];
231 template <
typename Scalar>
237 *m++ = 0.0; *m++ = 0.0; *m++ = 0.0; *m++ = 0.0;
238 *m++ = 0.0; *m++ = 0.0; *m++ = 0.0; *m++ = 0.0;
239 *m++ = 0.0; *m++ = 0.0; *m++ = 0.0; *m++ = 0.0;
240 *m++ = 0.0; *m++ = 0.0; *m++ = 0.0; *m = 0.0;
247 template <
typename Scalar>
253 *m++ = 1.0; *m++ = 0.0; *m++ = 0.0; *m++ = 0.0;
254 *m++ = 0.0; *m++ = 1.0; *m++ = 0.0; *m++ = 0.0;
255 *m++ = 0.0; *m++ = 0.0; *m++ = 1.0; *m++ = 0.0;
256 *m++ = 0.0; *m++ = 0.0; *m++ = 0.0; *m = 1.0;
263 template <
typename Scalar>
269 for(
int i=0; i<4; i++ )
271 for(
int j=i+1; j<4; j++ )
274 MAT(mat_,i,j) = MAT(mat_,j,i);
288 template <
typename Scalar>
293 #define SWAP_ROWS(a, b) { Scalar *_tmp = a; (a)=(b); (b)=_tmp; } 296 Scalar m0, m1, m2, m3, s;
297 Scalar *r0, *r1, *r2, *r3;
299 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
301 r0[0] = M(0,0); r0[1] = M(0,1);
302 r0[2] = M(0,2); r0[3] = M(0,3);
303 r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0;
305 r1[0] = M(1,0); r1[1] = M(1,1);
306 r1[2] = M(1,2); r1[3] = M(1,3);
307 r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0;
309 r2[0] = M(2,0); r2[1] = M(2,1);
310 r2[2] = M(2,2); r2[3] = M(2,3);
311 r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0;
313 r3[0] = M(3,0); r3[1] = M(3,1);
314 r3[2] = M(3,2); r3[3] = M(3,3);
315 r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
319 if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
320 if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
321 if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
322 if (0.0 == r0[0])
return false;
326 m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
327 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
328 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
329 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
331 if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
333 if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
335 if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
337 if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
341 if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
342 if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
343 if (0.0 == r1[1])
return false;
347 m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
348 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
349 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
350 s = r1[4];
if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
351 s = r1[5];
if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
352 s = r1[6];
if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
353 s = r1[7];
if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
357 if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
358 if (0.0 == r2[2])
return false;
369 if (0.0 == r3[3])
return false;
372 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
376 r2[4] = s * (r2[4] - r3[4] * m2); r2[5] = s * (r2[5] - r3[5] * m2);
377 r2[6] = s * (r2[6] - r3[6] * m2); r2[7] = s * (r2[7] - r3[7] * m2);
379 r1[4] -= r3[4] * m1; r1[5] -= r3[5] * m1;
380 r1[6] -= r3[6] * m1; r1[7] -= r3[7] * m1;
382 r0[4] -= r3[4] * m0; r0[5] -= r3[5] * m0;
383 r0[6] -= r3[6] * m0; r0[7] -= r3[7] * m0;
387 r1[4] = s * (r1[4] - r2[4] * m1); r1[5] = s * (r1[5] - r2[5] * m1);
388 r1[6] = s * (r1[6] - r2[6] * m1); r1[7] = s * (r1[7] - r2[7] * m1);
390 r0[4] -= r2[4] * m0; r0[5] -= r2[5] * m0;
391 r0[6] -= r2[6] * m0; r0[7] -= r2[7] * m0;
395 r0[4] = s * (r0[4] - r1[4] * m0); r0[5] = s * (r0[5] - r1[5] * m0);
396 r0[6] = s * (r0[6] - r1[6] * m0); r0[7] = s * (r0[7] - r1[7] * m0);
398 M(0,0) = r0[4]; M(0,1) = r0[5];
399 M(0,2) = r0[6]; M(0,3) = r0[7];
400 M(1,0) = r1[4]; M(1,1) = r1[5];
401 M(1,2) = r1[6]; M(1,3) = r1[7];
402 M(2,0) = r2[4]; M(2,1) = r2[5];
403 M(2,2) = r2[6]; M(2,3) = r2[7];
404 M(3,0) = r3[4]; M(3,1) = r3[5];
405 M(3,2) = r3[6]; M(3,3) = r3[7];
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
bool invert()
matrix inversion (returns true on success)
Namespace providing different geometric functions concerning angles.
void clear()
sets all elements to zero
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x',y',z',1) = M * (x,y,z,1)
void transpose()
transpose matrix
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
void identity()
setup an identity matrix
VectorT< T, 3 > transform_vector(const VectorT< T, 3 > &_v) const
transform vector (x',y',z',0) = A * (x,y,z,0)