17#ifndef IGNITION_MATH_MASSMATRIX3_HH_
18#define IGNITION_MATH_MASSMATRIX3_HH_
24#include <ignition/math/config.hh>
35 inline namespace IGNITION_MATH_VERSION_NAMESPACE
55 : mass(_mass), Ixxyyzz(_ixxyyzz), Ixyxzyz(_ixyxzyz)
71 public:
bool Mass(
const T &_m)
93 const T &_ixy,
const T &_ixz,
const T &_iyz)
95 this->Ixxyyzz.Set(_ixx, _iyy, _izz);
96 this->Ixyxzyz.Set(_ixy, _ixz, _iyz);
104 return this->Ixxyyzz;
111 return this->Ixyxzyz;
119 this->Ixxyyzz = _ixxyyzz;
128 this->Ixyxzyz = _ixyxzyz;
136 return this->Ixxyyzz[0];
143 return this->Ixxyyzz[1];
150 return this->Ixxyyzz[2];
157 return this->Ixyxzyz[0];
164 return this->Ixyxzyz[1];
171 return this->Ixyxzyz[2];
177 public:
bool IXX(
const T &_v)
186 public:
bool IYY(
const T &_v)
195 public:
bool IZZ(
const T &_v)
204 public:
bool IXY(
const T &_v)
213 public:
bool IXZ(
const T &_v)
222 public:
bool IYZ(
const T &_v)
233 this->Ixxyyzz[0], this->Ixyxzyz[0], this->Ixyxzyz[1],
234 this->Ixyxzyz[0], this->Ixxyyzz[1], this->Ixyxzyz[2],
235 this->Ixyxzyz[1], this->Ixyxzyz[2], this->Ixxyyzz[2]);
245 this->Ixxyyzz.Set(_moi(0, 0), _moi(1, 1), _moi(2, 2));
247 0.5*(_moi(0, 1) + _moi(1, 0)),
248 0.5*(_moi(0, 2) + _moi(2, 0)),
249 0.5*(_moi(1, 2) + _moi(2, 1)));
258 this->mass = _massMatrix.
Mass();
281 return !(*
this == _m);
291 return (this->mass > 0) &&
293 (this->
IXX()*this->
IYY() - std::pow(this->
IXY(), 2) > 0) &&
294 (this->
MOI().Determinant() > 0);
313 return _moments[0] > 0 &&
316 _moments[0] + _moments[1] > _moments[2] &&
317 _moments[1] + _moments[2] > _moments[0] &&
318 _moments[2] + _moments[0] > _moments[1];
335 T tol = _tol * this->Ixxyyzz.Max();
339 return this->Ixxyyzz;
350 T c = Id[0]*Id[1] - std::pow(Ip[0], 2)
351 + Id[0]*Id[2] - std::pow(Ip[1], 2)
352 + Id[1]*Id[2] - std::pow(Ip[2], 2);
354 T d = Id[0]*std::pow(Ip[2], 2)
355 + Id[1]*std::pow(Ip[1], 2)
356 + Id[2]*std::pow(Ip[0], 2)
358 - 2*Ip[0]*Ip[1]*Ip[2];
360 T p = std::pow(b, 2) - 3*c;
369 if (p < std::pow(tol, 2))
373 T q = 2*std::pow(b, 3) - 9*b*c - 27*d;
377 T delta = acos(
clamp<T>(0.5 * q / std::pow(p, 1.5), -1, 1));
380 T moment0 = (b + 2*sqrt(p) * cos(delta / 3.0)) / 3.0;
381 T moment1 = (b + 2*sqrt(p) * cos((delta + 2*
IGN_PI)/3.0)) / 3.0;
382 T moment2 = (b + 2*sqrt(p) * cos((delta - 2*
IGN_PI)/3.0)) / 3.0;
383 sort3(moment0, moment1, moment2);
401 T tol = _tol * this->Ixxyyzz.Max();
403 if (moments.
Equal(this->Ixxyyzz, tol) ||
429 Vector2<T> f1(this->Ixyxzyz[0], -this->Ixyxzyz[1]);
430 Vector2<T> f2(this->Ixxyyzz[1] - this->Ixxyyzz[2],
431 -2*this->Ixyxzyz[2]);
435 Vector2<T> momentsDiff(moments[0] - moments[1],
436 moments[1] - moments[2]);
439 int unequalMoment = -1;
440 if (
equal<T>(momentsDiff[0], 0, std::abs(tol)))
442 else if (
equal<T>(momentsDiff[1], 0, std::abs(tol)))
445 if (unequalMoment >= 0)
450 T momentsDiff3 = moments[1] - moments[unequalMoment];
454 T s = (this->Ixxyyzz[0] - moments[unequalMoment]) / momentsDiff3;
460 T phi2 = acos(
clamp<T>(ClampedSqrt(s), -1, 1));
469 math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
497 Vector2<T> g1a(0, 0.5*momentsDiff3 * sin(2*phi2));
500 math::Angle phi11a(Angle2(g1a, tol) - Angle2(f1, tol));
505 Vector2<T> g1b(0, 0.5*momentsDiff3 * sin(-2*phi2));
508 math::Angle phi11b(Angle2(g1b, tol) - Angle2(f1, tol));
514 T erra = std::pow(sin(phi1) - sin(phi11a.
Radian()), 2)
515 + std::pow(cos(phi1) - cos(phi11a.
Radian()), 2);
516 T errb = std::pow(sin(phi1) - sin(phi11b.
Radian()), 2)
517 + std::pow(cos(phi1) - cos(phi11b.
Radian()), 2);
537 if (unequalMoment == 0)
545 T v = (std::pow(this->Ixyxzyz[0], 2) + std::pow(this->Ixyxzyz[1], 2)
546 +(this->Ixxyyzz[0] - moments[2])
547 *(this->Ixxyyzz[0] + moments[2] - moments[0] - moments[1]))
548 / ((moments[1] - moments[2]) * (moments[2] - moments[0]));
551 if (v < std::abs(tol))
560 w = (this->Ixxyyzz[0] - moments[2] + (moments[2] - moments[1])*v)
561 / ((moments[0] - moments[1]) * v);
566 T phi2 = acos(
clamp<T>(ClampedSqrt(v), -1, 1));
568 T phi3 = acos(
clamp<T>(ClampedSqrt(w), -1, 1));
573 0.5* (moments[0]-moments[1])*ClampedSqrt(v)*sin(2*phi3),
574 0.5*((moments[0]-moments[1])*w + moments[1]-moments[2])*sin(2*phi2));
576 (moments[0]-moments[1])*(1 + (v-2)*w) + (moments[1]-moments[2])*v,
577 (moments[0]-moments[1])*sin(phi2)*sin(2*phi3));
592 if (f1small && f2small)
603 math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
610 math::Angle phi11(Angle2(g1, tol) - Angle2(f1, tol));
618 math::Angle phi11(Angle2(g1, tol) - Angle2(f1, tol));
621 math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
623 T err = std::pow(sin(phi11.
Radian()) - sin(phi12.
Radian()), 2)
624 + std::pow(cos(phi11.
Radian()) - cos(phi12.
Radian()), 2);
631 math::Angle phi11a(Angle2(g1a, tol) - Angle2(f1, tol));
632 math::Angle phi12a(0.5*(Angle2(g2a, tol) - Angle2(f2, tol)));
635 T erra = std::pow(sin(phi11a.
Radian()) - sin(phi12a.
Radian()), 2)
636 + std::pow(cos(phi11a.
Radian()) - cos(phi12a.
Radian()), 2);
641 signsPhi23.
Set(-1, 1);
648 math::Angle phi11b(Angle2(g1b, tol) - Angle2(f1, tol));
649 math::Angle phi12b(0.5*(Angle2(g2b, tol) - Angle2(f2, tol)));
652 T errb = std::pow(sin(phi11b.
Radian()) - sin(phi12b.
Radian()), 2)
653 + std::pow(cos(phi11b.
Radian()) - cos(phi12b.
Radian()), 2);
658 signsPhi23.
Set(1, -1);
665 math::Angle phi11c(Angle2(g1c, tol) - Angle2(f1, tol));
666 math::Angle phi12c(0.5*(Angle2(g2c, tol) - Angle2(f2, tol)));
669 T errc = std::pow(sin(phi11c.
Radian()) - sin(phi12c.
Radian()), 2)
670 + std::pow(cos(phi11c.
Radian()) - cos(phi12c.
Radian()), 2);
675 signsPhi23.
Set(-1, -1);
680 phi2 *= signsPhi23[0];
681 phi3 *= signsPhi23[1];
699 const T _tol = 1e-6)
const
719 _size.
X(sqrt(6*(moments.
Y() + moments.
Z() - moments.
X()) / this->mass));
720 _size.
Y(sqrt(6*(moments.
Z() + moments.
X() - moments.
Y()) / this->mass));
721 _size.
Z(sqrt(6*(moments.
X() + moments.
Y() - moments.
Z()) / this->mass));
764 if (this->
Mass() <= 0 || _size.
Min() <= 0 ||
772 T x2 = std::pow(_size.
X(), 2);
773 T y2 = std::pow(_size.
Y(), 2);
774 T z2 = std::pow(_size.
Z(), 2);
775 L(0, 0) = this->mass / 12.0 * (y2 + z2);
776 L(1, 1) = this->mass / 12.0 * (z2 + x2);
777 L(2, 2) = this->mass / 12.0 * (x2 + y2);
796 if (_mass <= 0 || _length <= 0 || _radius <= 0 ||
817 if (this->
Mass() <= 0 || _length <= 0 || _radius <= 0 ||
824 T radius2 = std::pow(_radius, 2);
826 L(0, 0) = this->mass / 12.0 * (3*radius2 + std::pow(_length, 2));
828 L(2, 2) = this->mass / 2.0 * radius2;
840 if (_mass <= 0 || _radius <= 0)
855 if (this->
Mass() <= 0 || _radius <= 0)
861 T radius2 = std::pow(_radius, 2);
863 L(0, 0) = 0.4 * this->mass * radius2;
864 L(1, 1) = 0.4 * this->mass * radius2;
865 L(2, 2) = 0.4 * this->mass * radius2;
872 private:
static inline T ClampedSqrt(
const T &_x)
884 private:
static T Angle2(
const Vector2<T> &_v,
const T _eps = 1e-6)
888 return atan2(_v[1], _v[0]);
897 private: Vector3<T> Ixxyyzz;
902 private: Vector3<T> Ixyxzyz;
#define IGN_PI
Define IGN_PI, IGN_PI_2, and IGN_PI_4.
Definition Helpers.hh:174
#define IGN_PI_2
Definition Helpers.hh:175
An angle and related functions.
Definition Angle.hh:48
void Normalize()
Normalize the angle in the range -Pi to Pi.
void Radian(double _radian)
Set the value from an angle in radians.
A class for inertial information about a rigid body consisting of the scalar mass and a 3x3 symmetric...
Definition MassMatrix3.hh:43
bool operator==(const MassMatrix3< T > &_m) const
Equality comparison operator.
Definition MassMatrix3.hh:269
Vector3< T > OffDiagonalMoments() const
Get the off-diagonal moments of inertia (Ixy, Ixz, Iyz).
Definition MassMatrix3.hh:109
bool IsPositive() const
Verify that inertia values are positive definite.
Definition MassMatrix3.hh:287
T Mass() const
Get the mass.
Definition MassMatrix3.hh:79
bool EquivalentBox(Vector3< T > &_size, Quaternion< T > &_rot, const T _tol=1e-6) const
Get dimensions and rotation offset of uniform box with equivalent mass and moment of inertia.
Definition MassMatrix3.hh:697
bool IYZ(const T &_v)
Set IYZ.
Definition MassMatrix3.hh:222
bool MOI(const Matrix3< T > &_moi)
Sets Moments of Inertia (MOI) from a Matrix3.
Definition MassMatrix3.hh:243
MassMatrix3()
Default Constructor.
Definition MassMatrix3.hh:45
bool IXZ(const T &_v)
Set IXZ.
Definition MassMatrix3.hh:213
Matrix3< T > MOI() const
returns Moments of Inertia as a Matrix3
Definition MassMatrix3.hh:230
Vector3< T > DiagonalMoments() const
Get the diagonal moments of inertia (Ixx, Iyy, Izz).
Definition MassMatrix3.hh:102
bool SetFromCylinderZ(const T _length, const T _radius, const Quaternion< T > &_rot)
Set inertial properties based on equivalent cylinder aligned with Z axis using the current mass value...
Definition MassMatrix3.hh:811
Quaternion< T > PrincipalAxesOffset(const T _tol=1e-6) const
Compute rotational offset of principal axes.
Definition MassMatrix3.hh:398
bool operator!=(const MassMatrix3< T > &_m) const
Inequality test operator.
Definition MassMatrix3.hh:279
T IXX() const
Get IXX.
Definition MassMatrix3.hh:134
Vector3< T > PrincipalMoments(const T _tol=1e-6) const
Compute principal moments of inertia, which are the eigenvalues of the moment of inertia matrix.
Definition MassMatrix3.hh:332
MassMatrix3(const T &_mass, const Vector3< T > &_ixxyyzz, const Vector3< T > &_ixyxzyz)
Constructor.
Definition MassMatrix3.hh:52
bool IYY(const T &_v)
Set IYY.
Definition MassMatrix3.hh:186
virtual ~MassMatrix3()
Destructor.
Definition MassMatrix3.hh:66
bool SetFromSphere(const T _mass, const T _radius)
Set inertial properties based on mass and equivalent sphere.
Definition MassMatrix3.hh:837
bool IZZ(const T &_v)
Set IZZ.
Definition MassMatrix3.hh:195
bool Mass(const T &_m)
Set the mass.
Definition MassMatrix3.hh:71
bool SetFromBox(const Vector3< T > &_size, const Quaternion< T > &_rot=Quaternion< T >::Identity)
Set inertial properties based on equivalent box using the current mass value.
Definition MassMatrix3.hh:759
MassMatrix3(const MassMatrix3< T > &_m)
Copy constructor.
Definition MassMatrix3.hh:60
bool IXX(const T &_v)
Set IXX.
Definition MassMatrix3.hh:177
bool SetFromBox(const T _mass, const Vector3< T > &_size, const Quaternion< T > &_rot=Quaternion< T >::Identity)
Set inertial properties based on mass and equivalent box.
Definition MassMatrix3.hh:740
bool DiagonalMoments(const Vector3< T > &_ixxyyzz)
Set the diagonal moments of inertia (Ixx, Iyy, Izz).
Definition MassMatrix3.hh:117
bool OffDiagonalMoments(const Vector3< T > &_ixyxzyz)
Set the off-diagonal moments of inertia (Ixy, Ixz, Iyz).
Definition MassMatrix3.hh:126
bool IsValid() const
Verify that inertia values are positive definite and satisfy the triangle inequality.
Definition MassMatrix3.hh:301
T IZZ() const
Get IZZ.
Definition MassMatrix3.hh:148
bool InertiaMatrix(const T &_ixx, const T &_iyy, const T &_izz, const T &_ixy, const T &_ixz, const T &_iyz)
Set the moment of inertia matrix.
Definition MassMatrix3.hh:92
T IXZ() const
Get IXZ.
Definition MassMatrix3.hh:162
static bool ValidMoments(const Vector3< T > &_moments)
Verify that principal moments are positive and satisfy the triangle inequality.
Definition MassMatrix3.hh:311
T IYY() const
Get IYY.
Definition MassMatrix3.hh:141
bool SetFromSphere(const T _radius)
Set inertial properties based on equivalent sphere using the current mass value.
Definition MassMatrix3.hh:852
T IYZ() const
Get IYZ.
Definition MassMatrix3.hh:169
bool SetFromCylinderZ(const T _mass, const T _length, const T _radius, const Quaternion< T > &_rot=Quaternion< T >::Identity)
Set inertial properties based on mass and equivalent cylinder aligned with Z axis.
Definition MassMatrix3.hh:789
bool IXY(const T &_v)
Set IXY.
Definition MassMatrix3.hh:204
MassMatrix3 & operator=(const MassMatrix3< T > &_massMatrix)
Equal operator.
Definition MassMatrix3.hh:256
T IXY() const
Get IXY.
Definition MassMatrix3.hh:155
A 3x3 matrix class.
Definition Quaternion.hh:32
Matrix3< T > Transposed() const
Return the transpose of this matrix.
Definition Matrix3.hh:478
A quaternion class.
Definition Quaternion.hh:38
Quaternion< T > Inverse() const
Get the inverse of this quaternion.
Definition Quaternion.hh:131
Two dimensional (x, y) vector.
Definition Vector2.hh:33
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition Vector2.hh:85
void Set(T _x, T _y)
Set the contents of the vector.
Definition Vector2.hh:106
The Vector3 class represents the generic vector containing 3 elements.
Definition Vector3.hh:40
T Sum() const
Return the sum of the values.
Definition Vector3.hh:89
void Min(const Vector3< T > &_v)
Set this vector's components to the minimum of itself and the passed in vector.
Definition Vector3.hh:287
T Z() const
Get the z value.
Definition Vector3.hh:661
bool Equal(const Vector3 &_v, const T &_tol) const
Equality test with tolerance.
Definition Vector3.hh:558
T Y() const
Get the y value.
Definition Vector3.hh:654
T X() const
Get the x value.
Definition Vector3.hh:647
void sort3(T &_a, T &_b, T &_c)
Sort three numbers, such that _a <= _b <= _c.
Definition Helpers.hh:601
MassMatrix3< float > MassMatrix3f
Definition MassMatrix3.hh:906
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition Helpers.hh:395
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition Helpers.hh:545
MassMatrix3< double > MassMatrix3d
Definition MassMatrix3.hh:905