17 #ifndef _IGNITION_MATRIX4_HH_
18 #define _IGNITION_MATRIX4_HH_
44 memset(this->data, 0,
sizeof(this->data[0][0])*16);
51 memcpy(this->data, _m.data,
sizeof(this->data[0][0])*16);
71 public:
Matrix4(T _v00, T _v01, T _v02, T _v03,
72 T _v10, T _v11, T _v12, T _v13,
73 T _v20, T _v21, T _v22, T _v23,
74 T _v30, T _v31, T _v32, T _v33)
76 this->
Set(_v00, _v01, _v02, _v03,
77 _v10, _v11, _v12, _v13,
78 _v20, _v21, _v22, _v23,
79 _v30, _v31, _v32, _v33);
88 this->
Set(1 - 2*qt.
Y()*qt.
Y() - 2 *qt.
Z()*qt.
Z(),
89 2 * qt.
X()*qt.
Y() - 2*qt.
Z()*qt.
W(),
90 2 * qt.
X() * qt.
Z() + 2 * qt.
Y() * qt.
W(),
93 2 * qt.
X() * qt.
Y() + 2 * qt.
Z() * qt.
W(),
94 1 - 2*qt.
X()*qt.
X() - 2 * qt.
Z()*qt.
Z(),
95 2 * qt.
Y() * qt.
Z() - 2 * qt.
X() * qt.
W(),
98 2 * qt.
X() * qt.
Z() - 2 * qt.
Y() * qt.
W(),
99 2 * qt.
Y() * qt.
Z() + 2 * qt.
X() * qt.
W(),
100 1 - 2 * qt.
X()*qt.
X() - 2 * qt.
Y()*qt.
Y(),
127 T _v00, T _v01, T _v02, T _v03,
128 T _v10, T _v11, T _v12, T _v13,
129 T _v20, T _v21, T _v22, T _v23,
130 T _v30, T _v31, T _v32, T _v33)
132 this->data[0][0] = _v00;
133 this->data[0][1] = _v01;
134 this->data[0][2] = _v02;
135 this->data[0][3] = _v03;
137 this->data[1][0] = _v10;
138 this->data[1][1] = _v11;
139 this->data[1][2] = _v12;
140 this->data[1][3] = _v13;
142 this->data[2][0] = _v20;
143 this->data[2][1] = _v21;
144 this->data[2][2] = _v22;
145 this->data[2][3] = _v23;
147 this->data[3][0] = _v30;
148 this->data[3][1] = _v31;
149 this->data[3][2] = _v32;
150 this->data[3][3] = _v33;
162 this->data[0][0] = _axis.
X()*_axis.
X()*C + c;
163 this->data[0][1] = _axis.
X()*_axis.
Y()*C - _axis.
Z()*s;
164 this->data[0][2] = _axis.
X()*_axis.
Z()*C + _axis.
Y()*s;
166 this->data[1][0] = _axis.
Y()*_axis.
X()*C + _axis.
Z()*s;
167 this->data[1][1] = _axis.
Y()*_axis.
Y()*C + c;
168 this->data[1][2] = _axis.
Y()*_axis.
Z()*C - _axis.
X()*s;
170 this->data[2][0] = _axis.
Z()*_axis.
X()*C - _axis.
Y()*s;
171 this->data[2][1] = _axis.
Z()*_axis.
Y()*C + _axis.
X()*s;
172 this->data[2][2] = _axis.
Z()*_axis.
Z()*C + c;
179 this->data[0][3] = _t.
X();
180 this->data[1][3] = _t.
Y();
181 this->data[2][3] = _t.
Z();
190 this->data[0][3] = _x;
191 this->data[1][3] = _y;
192 this->data[2][3] = _z;
199 return Vector3<T>(this->data[0][3], this->data[1][3], this->data[2][3]);
206 return Vector3<T>(this->data[0][0], this->data[1][1], this->data[2][2]);
216 T trace = this->data[0][0] + this->data[1][1] + this->data[2][2];
220 root = sqrt(trace + 1.0);
222 root = 1.0 / (2.0 * root);
223 q.
X((this->data[2][1] - this->data[1][2]) * root);
224 q.
Y((this->data[0][2] - this->data[2][0]) * root);
225 q.
Z((this->data[1][0] - this->data[0][1]) * root);
229 static unsigned int s_iNext[3] = {1, 2, 0};
231 if (this->data[1][1] > this->data[0][0])
233 if (this->data[2][2] > this->data[i][i])
235 unsigned int j = s_iNext[i];
236 unsigned int k = s_iNext[j];
238 root = sqrt(this->data[i][i] - this->data[j][j] -
239 this->data[k][k] + 1.0);
243 root = 1.0 / (2.0 * root);
244 b = (this->data[j][i] + this->data[i][j]) * root;
245 c = (this->data[k][i] + this->data[i][k]) * root;
250 case 0: q.
X(a);
break;
251 case 1: q.
Y(a);
break;
252 case 2: q.
Z(a);
break;
257 case 0: q.
X(b);
break;
258 case 1: q.
Y(b);
break;
259 case 2: q.
Z(b);
break;
264 case 0: q.
X(c);
break;
265 case 1: q.
Y(c);
break;
266 case 2: q.
Z(c);
break;
269 q.
W((this->data[k][j] - this->data[j][k]) * root);
284 T m31 = this->data[2][0];
285 T m11 = this->data[0][0];
286 T m12 = this->data[0][1];
287 T m13 = this->data[0][2];
288 T m32 = this->data[2][1];
289 T m33 = this->data[2][2];
290 T m21 = this->data[1][0];
292 if (std::abs(m31) >= 1.0)
301 euler.
X(atan2(m12, m13));
302 euler2.
X(atan2(m12, m13));
308 euler.
X(atan2(-m12, -m13));
309 euler2.
X(atan2(-m12, -m13));
317 euler.
X(atan2(m32 / cos(euler.
Y()), m33 / cos(euler.
Y())));
318 euler2.
X(atan2(m32 / cos(euler2.
Y()), m33 / cos(euler2.
Y())));
320 euler.
Z(atan2(m21 / cos(euler.
Y()), m11 / cos(euler.
Y())));
321 euler2.
Z(atan2(m21 / cos(euler2.
Y()), m11 / cos(euler2.
Y())));
341 this->data[0][0] = _s.
X();
342 this->data[1][1] = _s.
Y();
343 this->data[2][2] = _s.
Z();
344 this->data[3][3] = 1.0;
351 public:
void Scale(T _x, T _y, T _z)
353 this->data[0][0] = _x;
354 this->data[1][1] = _y;
355 this->data[2][2] = _z;
356 this->data[3][3] = 1.0;
363 return equal(this->data[3][0], static_cast<T>(0)) &&
364 equal(this->data[3][1], static_cast<T>(0)) &&
365 equal(this->data[3][2], static_cast<T>(0)) &&
366 equal(this->data[3][3], static_cast<T>(1));
378 return Vector3<T>(this->data[0][0]*_v.
X() + this->data[0][1]*_v.
Y() +
379 this->data[0][2]*_v.
Z() + this->data[0][3],
380 this->data[1][0]*_v.
X() + this->data[1][1]*_v.
Y() +
381 this->data[1][2]*_v.
Z() + this->data[1][3],
382 this->data[2][0]*_v.
X() + this->data[2][1]*_v.
Y() +
383 this->data[2][2]*_v.
Z() + this->data[2][3]);
391 T v0, v1, v2, v3, v4, v5, t00, t10, t20, t30;
394 v0 = this->data[2][0]*this->data[3][1] -
395 this->data[2][1]*this->data[3][0];
396 v1 = this->data[2][0]*this->data[3][2] -
397 this->data[2][2]*this->data[3][0];
398 v2 = this->data[2][0]*this->data[3][3] -
399 this->data[2][3]*this->data[3][0];
400 v3 = this->data[2][1]*this->data[3][2] -
401 this->data[2][2]*this->data[3][1];
402 v4 = this->data[2][1]*this->data[3][3] -
403 this->data[2][3]*this->data[3][1];
404 v5 = this->data[2][2]*this->data[3][3] -
405 this->data[2][3]*this->data[3][2];
407 t00 = +(v5*this->data[1][1] -
408 v4*this->data[1][2] + v3*this->data[1][3]);
409 t10 = -(v5*this->data[1][0] -
410 v2*this->data[1][2] + v1*this->data[1][3]);
411 t20 = +(v4*this->data[1][0] -
412 v2*this->data[1][1] + v0*this->data[1][3]);
413 t30 = -(v3*this->data[1][0] -
414 v1*this->data[1][1] + v0*this->data[1][2]);
416 T invDet = 1 / (t00 * this->data[0][0] + t10 * this->data[0][1] +
417 t20 * this->data[0][2] + t30 * this->data[0][3]);
419 r(0, 0) = t00 * invDet;
420 r(1, 0) = t10 * invDet;
421 r(2, 0) = t20 * invDet;
422 r(3, 0) = t30 * invDet;
424 r(0, 1) = -(v5*this->data[0][1] -
425 v4*this->data[0][2] + v3*this->data[0][3]) * invDet;
426 r(1, 1) = +(v5*this->data[0][0] -
427 v2*this->data[0][2] + v1*this->data[0][3]) * invDet;
428 r(2, 1) = -(v4*this->data[0][0] -
429 v2*this->data[0][1] + v0*this->data[0][3]) * invDet;
430 r(3, 1) = +(v3*this->data[0][0] -
431 v1*this->data[0][1] + v0*this->data[0][2]) * invDet;
433 v0 = this->data[1][0]*this->data[3][1] -
434 this->data[1][1]*this->data[3][0];
435 v1 = this->data[1][0]*this->data[3][2] -
436 this->data[1][2]*this->data[3][0];
437 v2 = this->data[1][0]*this->data[3][3] -
438 this->data[1][3]*this->data[3][0];
439 v3 = this->data[1][1]*this->data[3][2] -
440 this->data[1][2]*this->data[3][1];
441 v4 = this->data[1][1]*this->data[3][3] -
442 this->data[1][3]*this->data[3][1];
443 v5 = this->data[1][2]*this->data[3][3] -
444 this->data[1][3]*this->data[3][2];
446 r(0, 2) = +(v5*this->data[0][1] -
447 v4*this->data[0][2] + v3*this->data[0][3]) * invDet;
448 r(1, 2) = -(v5*this->data[0][0] -
449 v2*this->data[0][2] + v1*this->data[0][3]) * invDet;
450 r(2, 2) = +(v4*this->data[0][0] -
451 v2*this->data[0][1] + v0*this->data[0][3]) * invDet;
452 r(3, 2) = -(v3*this->data[0][0] -
453 v1*this->data[0][1] + v0*this->data[0][2]) * invDet;
455 v0 = this->data[2][1]*this->data[1][0] -
456 this->data[2][0]*this->data[1][1];
457 v1 = this->data[2][2]*this->data[1][0] -
458 this->data[2][0]*this->data[1][2];
459 v2 = this->data[2][3]*this->data[1][0] -
460 this->data[2][0]*this->data[1][3];
461 v3 = this->data[2][2]*this->data[1][1] -
462 this->data[2][1]*this->data[1][2];
463 v4 = this->data[2][3]*this->data[1][1] -
464 this->data[2][1]*this->data[1][3];
465 v5 = this->data[2][3]*this->data[1][2] -
466 this->data[2][2]*this->data[1][3];
468 r(0, 3) = -(v5*this->data[0][1] -
469 v4*this->data[0][2] + v3*this->data[0][3]) * invDet;
470 r(1, 3) = +(v5*this->data[0][0] -
471 v2*this->data[0][2] + v1*this->data[0][3]) * invDet;
472 r(2, 3) = -(v4*this->data[0][0] -
473 v2*this->data[0][1] + v0*this->data[0][3]) * invDet;
474 r(3, 3) = +(v3*this->data[0][0] -
475 v1*this->data[0][1] + v0*this->data[0][2]) * invDet;
485 memcpy(this->data, _mat.data,
sizeof(this->data[0][0])*16);
494 this->data[0][0] = _mat(0, 0);
495 this->data[0][1] = _mat(0, 1);
496 this->data[0][2] = _mat(0, 2);
498 this->data[1][0] = _mat(1, 0);
499 this->data[1][1] = _mat(1, 1);
500 this->data[1][2] = _mat(1, 2);
502 this->data[2][0] = _mat(2, 0);
503 this->data[2][1] = _mat(2, 1);
504 this->data[2][2] = _mat(2, 2);
515 this->data[0][0] * _m2(0, 0) +
516 this->data[0][1] * _m2(1, 0) +
517 this->data[0][2] * _m2(2, 0) +
518 this->data[0][3] * _m2(3, 0),
520 this->data[0][0] * _m2(0, 1) +
521 this->data[0][1] * _m2(1, 1) +
522 this->data[0][2] * _m2(2, 1) +
523 this->data[0][3] * _m2(3, 1),
525 this->data[0][0] * _m2(0, 2) +
526 this->data[0][1] * _m2(1, 2) +
527 this->data[0][2] * _m2(2, 2) +
528 this->data[0][3] * _m2(3, 2),
530 this->data[0][0] * _m2(0, 3) +
531 this->data[0][1] * _m2(1, 3) +
532 this->data[0][2] * _m2(2, 3) +
533 this->data[0][3] * _m2(3, 3),
535 this->data[1][0] * _m2(0, 0) +
536 this->data[1][1] * _m2(1, 0) +
537 this->data[1][2] * _m2(2, 0) +
538 this->data[1][3] * _m2(3, 0),
540 this->data[1][0] * _m2(0, 1) +
541 this->data[1][1] * _m2(1, 1) +
542 this->data[1][2] * _m2(2, 1) +
543 this->data[1][3] * _m2(3, 1),
545 this->data[1][0] * _m2(0, 2) +
546 this->data[1][1] * _m2(1, 2) +
547 this->data[1][2] * _m2(2, 2) +
548 this->data[1][3] * _m2(3, 2),
550 this->data[1][0] * _m2(0, 3) +
551 this->data[1][1] * _m2(1, 3) +
552 this->data[1][2] * _m2(2, 3) +
553 this->data[1][3] * _m2(3, 3),
555 this->data[2][0] * _m2(0, 0) +
556 this->data[2][1] * _m2(1, 0) +
557 this->data[2][2] * _m2(2, 0) +
558 this->data[2][3] * _m2(3, 0),
560 this->data[2][0] * _m2(0, 1) +
561 this->data[2][1] * _m2(1, 1) +
562 this->data[2][2] * _m2(2, 1) +
563 this->data[2][3] * _m2(3, 1),
565 this->data[2][0] * _m2(0, 2) +
566 this->data[2][1] * _m2(1, 2) +
567 this->data[2][2] * _m2(2, 2) +
568 this->data[2][3] * _m2(3, 2),
570 this->data[2][0] * _m2(0, 3) +
571 this->data[2][1] * _m2(1, 3) +
572 this->data[2][2] * _m2(2, 3) +
573 this->data[2][3] * _m2(3, 3),
575 this->data[3][0] * _m2(0, 0) +
576 this->data[3][1] * _m2(1, 0) +
577 this->data[3][2] * _m2(2, 0) +
578 this->data[3][3] * _m2(3, 0),
580 this->data[3][0] * _m2(0, 1) +
581 this->data[3][1] * _m2(1, 1) +
582 this->data[3][2] * _m2(2, 1) +
583 this->data[3][3] * _m2(3, 1),
585 this->data[3][0] * _m2(0, 2) +
586 this->data[3][1] * _m2(1, 2) +
587 this->data[3][2] * _m2(2, 2) +
588 this->data[3][3] * _m2(3, 2),
590 this->data[3][0] * _m2(0, 3) +
591 this->data[3][1] * _m2(1, 3) +
592 this->data[3][2] * _m2(2, 3) +
593 this->data[3][3] * _m2(3, 3));
602 this->data[0][0]*_vec.
X() + this->data[0][1]*_vec.
Y() +
603 this->data[0][2]*_vec.
Z() + this->data[0][3],
604 this->data[1][0]*_vec.
X() + this->data[1][1]*_vec.
Y() +
605 this->data[1][2]*_vec.
Z() + this->data[1][3],
606 this->data[2][0]*_vec.
X() + this->data[2][1]*_vec.
Y() +
607 this->data[2][2]*_vec.
Z() + this->data[2][3]);
614 public:
inline const T &
operator()(
size_t _row,
size_t _col)
const
616 if (_row >= 4 || _col >= 4)
618 return this->data[_row][_col];
628 if (_row >= 4 || _col >= 4)
630 return this->data[_row][_col];
665 return !(*
this == _m);
703 _in.setf(std::ios_base::skipws);
705 _in >> d[0] >> d[1] >> d[2] >> d[3]
706 >> d[4] >> d[5] >> d[6] >> d[7]
707 >> d[8] >> d[9] >> d[10] >> d[11]
708 >> d[12] >> d[13] >> d[14] >> d[15];
710 _m.
Set(d[0], d[1], d[2], d[3],
711 d[4], d[5], d[6], d[7],
712 d[8], d[9], d[10], d[11],
713 d[12], d[13], d[14], d[15]);
718 private: T data[4][4];
ignition/math/AffineException.hh
Definition: AffineException.hh:30
Matrix4< T > Inverse() const
Return the inverse matrix.
Definition: Matrix4.hh:389
static const Matrix4< T > Identity
Identity matrix.
Definition: Matrix4.hh:36
T & operator()(size_t _row, size_t _col)
Get a mutable version the value at the specified row, column index.
Definition: Matrix4.hh:626
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:188
const T & operator()(size_t _row, size_t _col) const
Get the value at the specified row, column index.
Definition: Matrix4.hh:614
const Matrix4< T > & operator=(const Matrix3< T > &_mat)
Equal operator for 3x3 matrix.
Definition: Matrix4.hh:492
Matrix4< int > Matrix4i
Definition: Matrix4.hh:735
Matrix4(const Matrix4< T > &_m)
Copy constructor.
Definition: Matrix4.hh:49
Encapsulates a position and rotation in three space.
Definition: Pose3.hh:30
friend std::istream & operator>>(std::istream &_in, ignition::math::Matrix4< T > &_m)
Stream extraction operator.
Definition: Matrix4.hh:699
A 4x4 matrix class.
Definition: Matrix4.hh:33
const T & Y() const
Get the y component.
Definition: Quaternion.hh:750
static const Matrix4< T > Zero
Zero matrix.
Definition: Matrix4.hh:39
void Scale(const Vector3< T > &_s)
Set the scale.
Definition: Matrix4.hh:339
T X() const
Get the x value.
Definition: Vector3.hh:545
const T & Z() const
Get the z component.
Definition: Quaternion.hh:757
Vector3< T > Scale() const
Get the scale values as a Vector3<T>
Definition: Matrix4.hh:204
T Y() const
Get the y value.
Definition: Vector3.hh:552
A 3x3 matrix class.
Definition: Matrix3.hh:32
Matrix4()
Constructor.
Definition: Matrix4.hh:42
Vector3< T > operator*(const Vector3< T > &_vec) const
Multiplication operator.
Definition: Matrix4.hh:599
Matrix4(T _v00, T _v01, T _v02, T _v03, T _v10, T _v11, T _v12, T _v13, T _v20, T _v21, T _v22, T _v23, T _v30, T _v31, T _v32, T _v33)
Constructor.
Definition: Matrix4.hh:71
bool IsAffine() const
Return true if the matrix is affine.
Definition: Matrix4.hh:361
bool operator==(const Matrix4< T > &_m) const
Equality operator.
Definition: Matrix4.hh:637
T Z() const
Get the z value.
Definition: Vector3.hh:559
Exception that is thrown when an out-of-bounds index is encountered.
Definition: IndexException.hh:30
void Axis(const Vector3< T > &_axis, T _angle)
Set the upper-left 3x3 matrix from an axis and angle.
Definition: Matrix4.hh:156
Quaternion< T > Rotation() const
Get the rotation as a quaternion.
Definition: Matrix4.hh:211
Vector3< T > EulerRotation(bool _firstSolution) const
Get the rotation as a Euler angles.
Definition: Matrix4.hh:279
bool operator!=(const Matrix4< T > &_m) const
Inequality test operator.
Definition: Matrix4.hh:663
void Scale(T _x, T _y, T _z)
Set the scale.
Definition: Matrix4.hh:351
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:37
Vector3< T > TransformAffine(const Vector3< T > &_v) const
Perform an affine transformation.
Definition: Matrix4.hh:373
Matrix4< float > Matrix4f
Definition: Matrix4.hh:737
void Translate(const Vector3< T > &_t)
Set the translational values [ (0, 3) (1, 3) (2, 3) ].
Definition: Matrix4.hh:177
virtual ~Matrix4()
Destructor.
Definition: Matrix4.hh:107
void Translate(T _x, T _y, T _z)
Set the translational values [ (0, 3) (1, 3) (2, 3) ].
Definition: Matrix4.hh:188
Matrix4< T > operator*(const Matrix4< T > &_m2) const
Multiplication operator.
Definition: Matrix4.hh:512
Pose3< T > Pose() const
Get the transformation as math::Pose.
Definition: Matrix4.hh:332
const T & W() const
Get the w component.
Definition: Quaternion.hh:736
Vector3< T > Translation() const
Get the translational values as a Vector3.
Definition: Matrix4.hh:197
void Set(T _v00, T _v01, T _v02, T _v03, T _v10, T _v11, T _v12, T _v13, T _v20, T _v21, T _v22, T _v23, T _v30, T _v31, T _v32, T _v33)
Change the values.
Definition: Matrix4.hh:126
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Helpers.hh:177
Matrix4< double > Matrix4d
Definition: Matrix4.hh:736
void Normalize()
Normalize the quaternion.
Definition: Quaternion.hh:206
A quaternion class.
Definition: Quaternion.hh:31
Matrix4(const Quaternion< T > &_q)
Construct Matrix4 from a quaternion.
Definition: Matrix4.hh:84
#define IGN_PI
Define IGN_PI, IGN_PI_2, and IGN_PI_4.
Definition: Helpers.hh:59
Matrix4< T > & operator=(const Matrix4< T > &_mat)
Equal operator.
Definition: Matrix4.hh:483
friend std::ostream & operator<<(std::ostream &_out, const ignition::math::Matrix4< T > &_m)
Stream insertion operator.
Definition: Matrix4.hh:672
const T & X() const
Get the x component.
Definition: Quaternion.hh:743