17 #ifndef IGNITION_MATH_MASSMATRIX3_HH_ 18 #define IGNITION_MATH_MASSMATRIX3_HH_ 24 #include <ignition/math/config.hh> 36 inline namespace IGNITION_MATH_VERSION_NAMESPACE {
56 : mass(_mass), Ixxyyzz(_ixxyyzz), Ixyxzyz(_ixyxzyz)
62 : mass(_m.Mass()), Ixxyyzz(_m.DiagonalMoments()),
63 Ixyxzyz(_m.OffDiagonalMoments())
72 public:
bool Mass(
const T &_m)
75 return this->IsValid();
94 const T &_ixy,
const T &_ixz,
const T &_iyz)
96 this->Ixxyyzz.Set(_ixx, _iyy, _izz);
97 this->Ixyxzyz.Set(_ixy, _ixz, _iyz);
98 return this->IsValid();
105 return this->Ixxyyzz;
112 return this->Ixyxzyz;
120 this->Ixxyyzz = _ixxyyzz;
121 return this->IsValid();
129 this->Ixyxzyz = _ixyxzyz;
130 return this->IsValid();
137 return this->Ixxyyzz[0];
144 return this->Ixxyyzz[1];
151 return this->Ixxyyzz[2];
158 return this->Ixyxzyz[0];
165 return this->Ixyxzyz[1];
172 return this->Ixyxzyz[2];
178 public:
bool IXX(
const T &_v)
181 return this->IsValid();
187 public:
bool IYY(
const T &_v)
190 return this->IsValid();
196 public:
bool IZZ(
const T &_v)
199 return this->IsValid();
205 public:
bool IXY(
const T &_v)
208 return this->IsValid();
214 public:
bool IXZ(
const T &_v)
217 return this->IsValid();
223 public:
bool IYZ(
const T &_v)
226 return this->IsValid();
234 this->Ixxyyzz[0], this->Ixyxzyz[0], this->Ixyxzyz[1],
235 this->Ixyxzyz[0], this->Ixxyyzz[1], this->Ixyxzyz[2],
236 this->Ixyxzyz[1], this->Ixyxzyz[2], this->Ixxyyzz[2]);
246 this->Ixxyyzz.Set(_moi(0, 0), _moi(1, 1), _moi(2, 2));
248 0.5*(_moi(0, 1) + _moi(1, 0)),
249 0.5*(_moi(0, 2) + _moi(2, 0)),
250 0.5*(_moi(1, 2) + _moi(2, 1)));
251 return this->IsValid();
259 this->mass = _massMatrix.
Mass();
272 return equal<T>(this->mass, _m.
Mass()) &&
282 return !(*
this == _m);
292 return (this->mass > 0) &&
294 (this->IXX()*this->IYY() -
std::pow(this->IXY(), 2) > 0) &&
295 (this->MOI().Determinant() > 0);
304 return this->IsPositive() && ValidMoments(this->PrincipalMoments());
314 return _moments[0] > 0 &&
317 _moments[0] + _moments[1] > _moments[2] &&
318 _moments[1] + _moments[2] > _moments[0] &&
319 _moments[2] + _moments[0] > _moments[1];
336 T tol = _tol * this->Ixxyyzz.Max();
340 return this->Ixxyyzz;
351 T c = Id[0]*Id[1] -
std::pow(Ip[0], 2)
359 - 2*Ip[0]*Ip[1]*Ip[2];
370 if (p < std::pow(tol, 2))
374 T q = 2*
std::pow(b, 3) - 9*b*c - 27*d;
378 T delta = acos(clamp<T>(0.5 * q / std::pow(p, 1.5), -1, 1));
381 T moment0 = (b + 2*sqrt(p) * cos(delta / 3.0)) / 3.0;
382 T moment1 = (b + 2*sqrt(p) * cos((delta + 2*
IGN_PI)/3.0)) / 3.0;
383 T moment2 = (b + 2*sqrt(p) * cos((delta - 2*
IGN_PI)/3.0)) / 3.0;
384 sort3(moment0, moment1, moment2);
402 T tol = _tol * this->Ixxyyzz.Max();
403 Vector3<T> moments = this->PrincipalMoments(tol);
404 if (moments.
Equal(this->Ixxyyzz, tol) ||
405 (math::equal<T>(moments[0], moments[1], std::abs(tol)) &&
406 math::equal<T>(moments[0], moments[2], std::abs(tol))))
430 Vector2<T> f1(this->Ixyxzyz[0], -this->Ixyxzyz[1]);
431 Vector2<T> f2(this->Ixxyyzz[1] - this->Ixxyyzz[2],
432 -2*this->Ixyxzyz[2]);
436 Vector2<T> momentsDiff(moments[0] - moments[1],
437 moments[1] - moments[2]);
440 int unequalMoment = -1;
441 if (equal<T>(momentsDiff[0], 0, std::abs(tol)))
443 else if (equal<T>(momentsDiff[1], 0, std::abs(tol)))
446 if (unequalMoment >= 0)
451 T momentsDiff3 = moments[1] - moments[unequalMoment];
455 T s = (this->Ixxyyzz[0] - moments[unequalMoment]) / momentsDiff3;
461 T phi2 = acos(clamp<T>(ClampedSqrt(s), -1, 1));
470 math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
498 Vector2<T> g1a(0, 0.5*momentsDiff3 * sin(2*phi2));
501 math::Angle phi11a(Angle2(g1a, tol) - Angle2(f1, tol));
506 Vector2<T> g1b(0, 0.5*momentsDiff3 * sin(-2*phi2));
509 math::Angle phi11b(Angle2(g1b, tol) - Angle2(f1, tol));
538 if (unequalMoment == 0)
547 +(this->Ixxyyzz[0] - moments[2])
548 *(this->Ixxyyzz[0] + moments[2] - moments[0] - moments[1]))
549 / ((moments[1] - moments[2]) * (moments[2] - moments[0]));
552 if (v < std::abs(tol))
561 w = (this->Ixxyyzz[0] - moments[2] + (moments[2] - moments[1])*v)
562 / ((moments[0] - moments[1]) * v);
567 T phi2 = acos(clamp<T>(ClampedSqrt(v), -1, 1));
569 T phi3 = acos(clamp<T>(ClampedSqrt(w), -1, 1));
574 0.5* (moments[0]-moments[1])*ClampedSqrt(v)*sin(2*phi3),
575 0.5*((moments[0]-moments[1])*w + moments[1]-moments[2])*sin(2*phi2));
577 (moments[0]-moments[1])*(1 + (v-2)*w) + (moments[1]-moments[2])*v,
578 (moments[0]-moments[1])*sin(phi2)*sin(2*phi3));
593 if (f1small && f2small)
604 math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
611 math::Angle phi11(Angle2(g1, tol) - Angle2(f1, tol));
619 math::Angle phi11(Angle2(g1, tol) - Angle2(f1, tol));
622 math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
632 math::Angle phi11a(Angle2(g1a, tol) - Angle2(f1, tol));
633 math::Angle phi12a(0.5*(Angle2(g2a, tol) - Angle2(f2, tol)));
641 phi1 = phi11a.Radian();
642 signsPhi23.
Set(-1, 1);
649 math::Angle phi11b(Angle2(g1b, tol) - Angle2(f1, tol));
650 math::Angle phi12b(0.5*(Angle2(g2b, tol) - Angle2(f2, tol)));
658 phi1 = phi11b.Radian();
659 signsPhi23.
Set(1, -1);
666 math::Angle phi11c(Angle2(g1c, tol) - Angle2(f1, tol));
667 math::Angle phi12c(0.5*(Angle2(g2c, tol) - Angle2(f2, tol)));
676 signsPhi23.
Set(-1, -1);
681 phi2 *= signsPhi23[0];
682 phi3 *= signsPhi23[1];
700 const T _tol = 1e-6)
const 702 if (!this->IsPositive())
708 Vector3<T> moments = this->PrincipalMoments(_tol);
709 if (!ValidMoments(moments))
720 _size.
X(sqrt(6*(moments.
Y() + moments.
Z() - moments.
X()) / this->mass));
721 _size.
Y(sqrt(6*(moments.
Z() + moments.
X() - moments.
Y()) / this->mass));
722 _size.
Z(sqrt(6*(moments.
X() + moments.
Y() - moments.
Z()) / this->mass));
724 _rot = this->PrincipalAxesOffset(_tol);
752 return this->SetFromBox(_size, _rot);
765 if (this->Mass() <= 0 || _size.
Min() <= 0 ||
776 L(0, 0) = this->mass / 12.0 * (y2 + z2);
777 L(1, 1) = this->mass / 12.0 * (z2 + x2);
778 L(2, 2) = this->mass / 12.0 * (x2 + y2);
797 if (_mass <= 0 || _length <= 0 || _radius <= 0 ||
803 return this->SetFromCylinderZ(_length, _radius, _rot);
818 if (this->Mass() <= 0 || _length <= 0 || _radius <= 0 ||
827 L(0, 0) = this->mass / 12.0 * (3*radius2 +
std::pow(_length, 2));
829 L(2, 2) = this->mass / 2.0 * radius2;
841 if (_mass <= 0 || _radius <= 0)
846 return this->SetFromSphere(_radius);
856 if (this->Mass() <= 0 || _radius <= 0)
864 L(0, 0) = 0.4 * this->mass * radius2;
865 L(1, 1) = 0.4 * this->mass * radius2;
866 L(2, 2) = 0.4 * this->mass * radius2;
873 private:
static inline T ClampedSqrt(
const T &_x)
885 private:
static T Angle2(
const Vector2<T> &_v,
const T _eps = 1e-6)
889 return atan2(_v[1], _v[0]);
bool IYY(const T &_v)
Set IYY.
Definition: MassMatrix3.hh:187
An angle and related functions.
Definition: Angle.hh:48
Vector3< T > OffDiagonalMoments() const
Get the off-diagonal moments of inertia (Ixy, Ixz, Iyz).
Definition: MassMatrix3.hh:110
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:107
bool IXY(const T &_v)
Set IXY.
Definition: MassMatrix3.hh:205
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:760
T IXY() const
Get IXY.
Definition: MassMatrix3.hh:156
bool OffDiagonalMoments(const Vector3< T > &_ixyxzyz)
Set the off-diagonal moments of inertia (Ixy, Ixz, Iyz).
Definition: MassMatrix3.hh:127
static bool ValidMoments(const Vector3< T > &_moments)
Verify that principal moments are positive and satisfy the triangle inequality.
Definition: MassMatrix3.hh:312
virtual ~MassMatrix3()
Destructor.
Definition: MassMatrix3.hh:67
bool IYZ(const T &_v)
Set IYZ.
Definition: MassMatrix3.hh:223
bool IZZ(const T &_v)
Set IZZ.
Definition: MassMatrix3.hh:196
bool IXX(const T &_v)
Set IXX.
Definition: MassMatrix3.hh:178
void Normalize()
Normalize the vector length.
Definition: Vector2.hh:93
bool SetFromSphere(const T _radius)
Set inertial properties based on equivalent sphere using the current mass value.
Definition: MassMatrix3.hh:853
bool DiagonalMoments(const Vector3< T > &_ixxyyzz)
Set the diagonal moments of inertia (Ixx, Iyy, Izz).
Definition: MassMatrix3.hh:118
A class for inertial information about a rigid body consisting of the scalar mass and a 3x3 symmetric...
Definition: MassMatrix3.hh:43
Two dimensional (x, y) vector.
Definition: Vector2.hh:33
bool Equal(const Vector3 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector3.hh:559
bool IsPositive() const
Verify that inertia values are positive definite.
Definition: MassMatrix3.hh:288
void Radian(double _radian)
Set the value from an angle in radians.
MassMatrix3< float > MassMatrix3f
Definition: MassMatrix3.hh:907
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:812
MassMatrix3(const T &_mass, const Vector3< T > &_ixxyyzz, const Vector3< T > &_ixyxzyz)
Constructor.
Definition: MassMatrix3.hh:53
T Mass() const
Get the mass.
Definition: MassMatrix3.hh:80
T X() const
Get the x value.
Definition: Vector3.hh:648
bool IXZ(const T &_v)
Set IXZ.
Definition: MassMatrix3.hh:214
T Z() const
Get the z value.
Definition: Vector3.hh:662
bool SetFromSphere(const T _mass, const T _radius)
Set inertial properties based on mass and equivalent sphere.
Definition: MassMatrix3.hh:838
T IXZ() const
Get IXZ.
Definition: MassMatrix3.hh:163
T Y() const
Get the y value.
Definition: Vector3.hh:655
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:333
T IYZ() const
Get IYZ.
Definition: MassMatrix3.hh:170
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:790
A 3x3 matrix class.
Definition: Matrix3.hh:39
bool Mass(const T &_m)
Set the mass.
Definition: MassMatrix3.hh:72
T IXX() const
Get IXX.
Definition: MassMatrix3.hh:135
bool IsValid() const
Verify that inertia values are positive definite and satisfy the triangle inequality.
Definition: MassMatrix3.hh:302
Quaternion< T > PrincipalAxesOffset(const T _tol=1e-6) const
Compute rotational offset of principal axes.
Definition: MassMatrix3.hh:399
bool operator==(const MassMatrix3< T > &_m) const
Equality comparison operator.
Definition: MassMatrix3.hh:270
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:698
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:93
The Vector3 class represents the generic vector containing 3 elements. Since it's commonly used to ke...
Definition: Vector3.hh:40
MassMatrix3(const MassMatrix3< T > &_m)
Copy constructor.
Definition: MassMatrix3.hh:61
Matrix3< T > Transposed() const
Return the transpose of this matrix.
Definition: Matrix3.hh:479
MassMatrix3()
Default Constructor.
Definition: MassMatrix3.hh:46
T IYY() const
Get IYY.
Definition: MassMatrix3.hh:142
#define IGN_PI_2
Definition: Helpers.hh:175
void Normalize()
Normalize the angle in the range -Pi to Pi.
Vector3< T > DiagonalMoments() const
Get the diagonal moments of inertia (Ixx, Iyy, Izz).
Definition: MassMatrix3.hh:103
void Min(const Vector3< T > &_v)
Set this vector's components to the minimum of itself and the passed in vector.
Definition: Vector3.hh:288
T Sum() const
Return the sum of the values.
Definition: Vector3.hh:90
Matrix3< T > MOI() const
returns Moments of Inertia as a Matrix3
Definition: MassMatrix3.hh:231
A quaternion class.
Definition: Matrix3.hh:34
#define IGN_PI
Define IGN_PI, IGN_PI_2, and IGN_PI_4. This was put here for Windows support.
Definition: Helpers.hh:174
bool MOI(const Matrix3< T > &_moi)
Sets Moments of Inertia (MOI) from a Matrix3. Symmetric component of input matrix is used by averagin...
Definition: MassMatrix3.hh:244
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:741
bool operator!=(const MassMatrix3< T > &_m) const
Inequality test operator.
Definition: MassMatrix3.hh:280
T IZZ() const
Get IZZ.
Definition: MassMatrix3.hh:149
MassMatrix3 & operator=(const MassMatrix3< T > &_massMatrix)
Equal operator.
Definition: MassMatrix3.hh:257
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition: Vector2.hh:86
void sort3(T &_a, T &_b, T &_c)
Sort three numbers, such that _a <= _b <= _c.
Definition: Helpers.hh:602
MassMatrix3< double > MassMatrix3d
Definition: MassMatrix3.hh:906