17 #ifndef GZ_MATH_MATRIX3_HH_
18 #define GZ_MATH_MATRIX3_HH_
26 #include <gz/math/config.hh>
31 inline namespace GZ_MATH_VERSION_NAMESPACE {
33 template <
typename T>
class Quaternion;
105 std::memset(this->data, 0,
sizeof(this->data[0][0])*9);
122 public: constexpr
Matrix3(T _v00, T _v01, T _v02,
123 T _v10, T _v11, T _v12,
124 T _v20, T _v21, T _v22)
125 : data{{_v00, _v01, _v02},
137 this->Set(1 - 2*qt.
Y()*qt.
Y() - 2 *qt.
Z()*qt.
Z(),
138 2 * qt.
X()*qt.
Y() - 2*qt.
Z()*qt.
W(),
139 2 * qt.
X() * qt.
Z() + 2 * qt.
Y() * qt.
W(),
140 2 * qt.
X() * qt.
Y() + 2 * qt.
Z() * qt.
W(),
141 1 - 2*qt.
X()*qt.
X() - 2 * qt.
Z()*qt.
Z(),
142 2 * qt.
Y() * qt.
Z() - 2 * qt.
X() * qt.
W(),
143 2 * qt.
X() * qt.
Z() - 2 * qt.
Y() * qt.
W(),
144 2 * qt.
Y() * qt.
Z() + 2 * qt.
X() * qt.
W(),
145 1 - 2 * qt.
X()*qt.
X() - 2 * qt.
Y()*qt.
Y());
155 public:
void Set(
size_t _row,
size_t _col, T _v)
171 public:
void Set(T _v00, T _v01, T _v02,
172 T _v10, T _v11, T _v12,
173 T _v20, T _v21, T _v22)
175 this->data[0][0] = _v00;
176 this->data[0][1] = _v01;
177 this->data[0][2] = _v02;
178 this->data[1][0] = _v10;
179 this->data[1][1] = _v11;
180 this->data[1][2] = _v12;
181 this->data[2][0] = _v20;
182 this->data[2][1] = _v21;
183 this->data[2][2] = _v22;
192 public:
void GZ_DEPRECATED(7) Axes(const
Vector3<T> &_xAxis,
196 this->SetAxes(_xAxis, _yAxis, _zAxis);
207 this->SetCol(0, _xAxis);
208 this->SetCol(1, _yAxis);
209 this->SetCol(2, _zAxis);
216 public:
void GZ_DEPRECATED(7) Axis(const
Vector3<T> &_axis, T _angle)
218 this->SetFromAxisAngle(_axis, _angle);
230 this->data[0][0] = _axis.
X()*_axis.
X()*C + c;
231 this->data[0][1] = _axis.
X()*_axis.
Y()*C - _axis.
Z()*s;
232 this->data[0][2] = _axis.
X()*_axis.
Z()*C + _axis.
Y()*s;
234 this->data[1][0] = _axis.
Y()*_axis.
X()*C + _axis.
Z()*s;
235 this->data[1][1] = _axis.
Y()*_axis.
Y()*C + c;
236 this->data[1][2] = _axis.
Y()*_axis.
Z()*C - _axis.
X()*s;
238 this->data[2][0] = _axis.
Z()*_axis.
X()*C - _axis.
Y()*s;
239 this->data[2][1] = _axis.
Z()*_axis.
Y()*C + _axis.
X()*s;
240 this->data[2][2] = _axis.
Z()*_axis.
Z()*C + c;
250 public:
void GZ_DEPRECATED(7) From2Axes(
253 this->SetFrom2Axes(_v1, _v2);
265 if (_v1LengthSquared <= 0.0)
268 this->Set(1, 0, 0, 0, 1, 0, 0, 0, 1);
273 if (_v2LengthSquared <= 0.0)
276 this->Set(1, 0, 0, 0, 1, 0, 0, 0, 1);
280 const T dot = _v1.
Dot(_v2) / sqrt(_v1LengthSquared * _v2LengthSquared);
281 if (fabs(dot - 1.0) <= 1e-6)
284 this->Set(1, 0, 0, 0, 1, 0, 0, 0, 1);
287 else if (fabs(dot + 1.0) <= 1e-6)
290 this->Set(-1, 0, 0, 0, -1, 0, 0, 0, -1);
296 this->SetFromAxisAngle(cross, acos(dot));
304 public:
void GZ_DEPRECATED(7) Col(
unsigned int _c, const
Vector3<T> &_v)
306 this->SetCol(_c, _v);
315 unsigned int c =
clamp(_c, 0u, 2u);
317 this->data[0][c] = _v.
X();
318 this->data[1][c] = _v.
Y();
319 this->data[2][c] = _v.
Z();
333 this->data[0][0] - _m(0, 0),
334 this->data[0][1] - _m(0, 1),
335 this->data[0][2] - _m(0, 2),
336 this->data[1][0] - _m(1, 0),
337 this->data[1][1] - _m(1, 1),
338 this->data[1][2] - _m(1, 2),
339 this->data[2][0] - _m(2, 0),
340 this->data[2][1] - _m(2, 1),
341 this->data[2][2] - _m(2, 2));
350 this->data[0][0]+_m(0, 0),
351 this->data[0][1]+_m(0, 1),
352 this->data[0][2]+_m(0, 2),
353 this->data[1][0]+_m(1, 0),
354 this->data[1][1]+_m(1, 1),
355 this->data[1][2]+_m(1, 2),
356 this->data[2][0]+_m(2, 0),
357 this->data[2][1]+_m(2, 1),
358 this->data[2][2]+_m(2, 2));
367 _s * this->data[0][0], _s * this->data[0][1], _s * this->data[0][2],
368 _s * this->data[1][0], _s * this->data[1][1], _s * this->data[1][2],
369 _s * this->data[2][0], _s * this->data[2][1], _s * this->data[2][2]);
379 this->data[0][0]*_m(0, 0)+
380 this->data[0][1]*_m(1, 0)+
381 this->data[0][2]*_m(2, 0),
383 this->data[0][0]*_m(0, 1)+
384 this->data[0][1]*_m(1, 1)+
385 this->data[0][2]*_m(2, 1),
387 this->data[0][0]*_m(0, 2)+
388 this->data[0][1]*_m(1, 2)+
389 this->data[0][2]*_m(2, 2),
392 this->data[1][0]*_m(0, 0)+
393 this->data[1][1]*_m(1, 0)+
394 this->data[1][2]*_m(2, 0),
396 this->data[1][0]*_m(0, 1)+
397 this->data[1][1]*_m(1, 1)+
398 this->data[1][2]*_m(2, 1),
400 this->data[1][0]*_m(0, 2)+
401 this->data[1][1]*_m(1, 2)+
402 this->data[1][2]*_m(2, 2),
405 this->data[2][0]*_m(0, 0)+
406 this->data[2][1]*_m(1, 0)+
407 this->data[2][2]*_m(2, 0),
409 this->data[2][0]*_m(0, 1)+
410 this->data[2][1]*_m(1, 1)+
411 this->data[2][2]*_m(2, 1),
413 this->data[2][0]*_m(0, 2)+
414 this->data[2][1]*_m(1, 2)+
415 this->data[2][2]*_m(2, 2));
425 this->data[0][0]*_vec.
X() + this->data[0][1]*_vec.
Y() +
426 this->data[0][2]*_vec.
Z(),
427 this->data[1][0]*_vec.
X() + this->data[1][1]*_vec.
Y() +
428 this->data[1][2]*_vec.
Z(),
429 this->data[2][0]*_vec.
X() + this->data[2][1]*_vec.
Y() +
430 this->data[2][2]*_vec.
Z());
452 _m(0, 0)*_v.
X() + _m(1, 0)*_v.
Y() + _m(2, 0)*_v.
Z(),
453 _m(0, 1)*_v.
X() + _m(1, 1)*_v.
Y() + _m(2, 1)*_v.
Z(),
454 _m(0, 2)*_v.
X() + _m(1, 2)*_v.
Y() + _m(2, 2)*_v.
Z());
464 return equal<T>(this->data[0][0], _m(0, 0), _tol)
465 && equal<T>(this->data[0][1], _m(0, 1), _tol)
466 && equal<T>(this->data[0][2], _m(0, 2), _tol)
467 && equal<T>(this->data[1][0], _m(1, 0), _tol)
468 && equal<T>(this->data[1][1], _m(1, 1), _tol)
469 && equal<T>(this->data[1][2], _m(1, 2), _tol)
470 && equal<T>(this->data[2][0], _m(2, 0), _tol)
471 && equal<T>(this->data[2][1], _m(2, 1), _tol)
472 && equal<T>(this->data[2][2], _m(2, 2), _tol);
480 return this->Equal(_m,
static_cast<T
>(1e-6));
496 return !(*
this == _m);
523 T t0 = this->data[2][2]*this->data[1][1]
524 - this->data[2][1]*this->data[1][2];
526 T t1 = -(this->data[2][2]*this->data[1][0]
527 -this->data[2][0]*this->data[1][2]);
529 T t2 = this->data[2][1]*this->data[1][0]
530 - this->data[2][0]*this->data[1][1];
532 return t0 * this->data[0][0]
533 + t1 * this->data[0][1]
534 + t2 * this->data[0][2];
541 T t0 = this->data[2][2]*this->data[1][1] -
542 this->data[2][1]*this->data[1][2];
544 T t1 = -(this->data[2][2]*this->data[1][0] -
545 this->data[2][0]*this->data[1][2]);
547 T t2 = this->data[2][1]*this->data[1][0] -
548 this->data[2][0]*this->data[1][1];
550 T invDet = 1.0 / (t0 * this->data[0][0] +
551 t1 * this->data[0][1] +
552 t2 * this->data[0][2]);
556 - (this->data[2][2] * this->data[0][1] -
557 this->data[2][1] * this->data[0][2]),
558 + (this->data[1][2] * this->data[0][1] -
559 this->data[1][1] * this->data[0][2]),
561 + (this->data[2][2] * this->data[0][0] -
562 this->data[2][0] * this->data[0][2]),
563 - (this->data[1][2] * this->data[0][0] -
564 this->data[1][0] * this->data[0][2]),
566 - (this->data[2][1] * this->data[0][0] -
567 this->data[2][0] * this->data[0][1]),
568 + (this->data[1][1] * this->data[0][0] -
569 this->data[1][0] * this->data[0][1]));
575 std::swap(this->data[0][1], this->data[1][0]);
576 std::swap(this->data[0][2], this->data[2][0]);
577 std::swap(this->data[1][2], this->data[2][1]);
585 this->data[0][0], this->data[1][0], this->data[2][0],
586 this->data[0][1], this->data[1][1], this->data[2][1],
587 this->data[0][2], this->data[1][2], this->data[2][2]);
598 for (
auto i : {0, 1, 2})
600 for (
auto j : {0, 1, 2})
602 if (!(i == 0 && j == 0))
621 _in.
setf(std::ios_base::skipws);
623 _in >> d[0] >> d[1] >> d[2]
624 >> d[3] >> d[4] >> d[5]
625 >> d[6] >> d[7] >> d[8];
629 _m.
Set(d[0], d[1], d[2],
637 private: T data[3][3];
643 constexpr Matrix3<T> gMatrix3Identity(
649 constexpr Matrix3<T> gMatrix3Zero(