MassMatrix3.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef IGNITION_MATH_MASSMATRIX3_HH_
18 #define IGNITION_MATH_MASSMATRIX3_HH_
19 
20 #include <algorithm>
21 #include <string>
22 #include <vector>
23 
24 #include "ignition/math/Helpers.hh"
26 #include "ignition/math/Vector2.hh"
27 #include "ignition/math/Vector3.hh"
28 #include "ignition/math/Matrix3.hh"
29 
30 namespace ignition
31 {
32  namespace math
33  {
38  template<typename T>
40  {
42  public: MassMatrix3() : mass(0)
43  {}
44 
49  public: MassMatrix3(const T &_mass,
50  const Vector3<T> &_ixxyyzz,
51  const Vector3<T> &_ixyxzyz)
52  : mass(_mass), Ixxyyzz(_ixxyyzz), Ixyxzyz(_ixyxzyz)
53  {}
54 
57  public: MassMatrix3(const MassMatrix3<T> &_m)
58  : mass(_m.Mass()), Ixxyyzz(_m.DiagonalMoments()),
59  Ixyxzyz(_m.OffDiagonalMoments())
60  {}
61 
63  public: virtual ~MassMatrix3() {}
64 
68  public: bool Mass(const T &_m)
69  {
70  this->mass = _m;
71  return this->IsValid();
72  }
73 
76  public: T Mass() const
77  {
78  return this->mass;
79  }
80 
89  public: bool InertiaMatrix(const T &_ixx, const T &_iyy, const T &_izz,
90  const T &_ixy, const T &_ixz, const T &_iyz)
91  {
92  this->Ixxyyzz.Set(_ixx, _iyy, _izz);
93  this->Ixyxzyz.Set(_ixy, _ixz, _iyz);
94  return this->IsValid();
95  }
96 
99  public: Vector3<T> DiagonalMoments() const
100  {
101  return this->Ixxyyzz;
102  }
103 
107  {
108  return this->Ixyxzyz;
109  }
110 
114  public: bool DiagonalMoments(const Vector3<T> &_ixxyyzz)
115  {
116  this->Ixxyyzz = _ixxyyzz;
117  return this->IsValid();
118  }
119 
123  public: bool OffDiagonalMoments(const Vector3<T> &_ixyxzyz)
124  {
125  this->Ixyxzyz = _ixyxzyz;
126  return this->IsValid();
127  }
128 
131  public: T IXX() const
132  {
133  return this->Ixxyyzz[0];
134  }
135 
138  public: T IYY() const
139  {
140  return this->Ixxyyzz[1];
141  }
142 
145  public: T IZZ() const
146  {
147  return this->Ixxyyzz[2];
148  }
149 
152  public: T IXY() const
153  {
154  return this->Ixyxzyz[0];
155  }
156 
159  public: T IXZ() const
160  {
161  return this->Ixyxzyz[1];
162  }
163 
166  public: T IYZ() const
167  {
168  return this->Ixyxzyz[2];
169  }
170 
174  public: bool IXX(const T &_v)
175  {
176  this->Ixxyyzz.X(_v);
177  return this->IsValid();
178  }
179 
183  public: bool IYY(const T &_v)
184  {
185  this->Ixxyyzz.Y(_v);
186  return this->IsValid();
187  }
188 
192  public: bool IZZ(const T &_v)
193  {
194  this->Ixxyyzz.Z(_v);
195  return this->IsValid();
196  }
197 
201  public: bool IXY(const T &_v)
202  {
203  this->Ixyxzyz.X(_v);
204  return this->IsValid();
205  }
206 
210  public: bool IXZ(const T &_v)
211  {
212  this->Ixyxzyz.Y(_v);
213  return this->IsValid();
214  }
215 
219  public: bool IYZ(const T &_v)
220  {
221  this->Ixyxzyz.Z(_v);
222  return this->IsValid();
223  }
224 
227  public: Matrix3<T> MOI() const
228  {
229  return Matrix3<T>(
230  this->Ixxyyzz[0], this->Ixyxzyz[0], this->Ixyxzyz[1],
231  this->Ixyxzyz[0], this->Ixxyyzz[1], this->Ixyxzyz[2],
232  this->Ixyxzyz[1], this->Ixyxzyz[2], this->Ixxyyzz[2]);
233  }
234 
240  public: bool MOI(const Matrix3<T> &_moi)
241  {
242  this->Ixxyyzz.Set(_moi(0, 0), _moi(1, 1), _moi(2, 2));
243  this->Ixyxzyz.Set(
244  0.5*(_moi(0, 1) + _moi(1, 0)),
245  0.5*(_moi(0, 2) + _moi(2, 0)),
246  0.5*(_moi(1, 2) + _moi(2, 1)));
247  return this->IsValid();
248  }
249 
253  public: MassMatrix3 &operator=(const MassMatrix3<T> &_massMatrix)
254  {
255  this->mass = _massMatrix.Mass();
256  this->Ixxyyzz = _massMatrix.DiagonalMoments();
257  this->Ixyxzyz = _massMatrix.OffDiagonalMoments();
258 
259  return *this;
260  }
261 
266  public: bool operator==(const MassMatrix3<T> &_m) const
267  {
268  return equal<T>(this->mass, _m.Mass()) &&
269  (this->Ixxyyzz == _m.DiagonalMoments()) &&
270  (this->Ixyxzyz == _m.OffDiagonalMoments());
271  }
272 
276  public: bool operator!=(const MassMatrix3<T> &_m) const
277  {
278  return !(*this == _m);
279  }
280 
284  public: bool IsPositive() const
285  {
286  // Check if mass and determinants of all upper left submatrices
287  // of moment of inertia matrix are positive
288  return (this->mass > 0) &&
289  (this->IXX() > 0) &&
290  (this->IXX()*this->IYY() - std::pow(this->IXY(), 2) > 0) &&
291  (this->MOI().Determinant() > 0);
292  }
293 
298  public: bool IsValid() const
299  {
300  return this->IsPositive() && ValidMoments(this->PrincipalMoments());
301  }
302 
308  public: static bool ValidMoments(const Vector3<T> &_moments)
309  {
310  return _moments[0] > 0 &&
311  _moments[1] > 0 &&
312  _moments[2] > 0 &&
313  _moments[0] + _moments[1] > _moments[2] &&
314  _moments[1] + _moments[2] > _moments[0] &&
315  _moments[2] + _moments[0] > _moments[1];
316  }
317 
329  public: Vector3<T> PrincipalMoments(const T _tol = 1e-6) const
330  {
331  // Compute tolerance relative to maximum value of inertia diagonal
332  T tol = _tol * this->Ixxyyzz.Max();
333  if (this->Ixyxzyz.Equal(Vector3<T>::Zero, tol))
334  {
335  // Matrix is already diagonalized, return diagonal moments
336  return this->Ixxyyzz;
337  }
338 
339  // Algorithm based on http://arxiv.org/abs/1306.6291v4
340  // A Method for Fast Diagonalization of a 2x2 or 3x3 Real Symmetric
341  // Matrix, by Maarten Kronenburg
342  Vector3<T> Id(this->Ixxyyzz);
343  Vector3<T> Ip(this->Ixyxzyz);
344  // b = Ixx + Iyy + Izz
345  T b = Id.Sum();
346  // c = Ixx*Iyy - Ixy^2 + Ixx*Izz - Ixz^2 + Iyy*Izz - Iyz^2
347  T c = Id[0]*Id[1] - std::pow(Ip[0], 2)
348  + Id[0]*Id[2] - std::pow(Ip[1], 2)
349  + Id[1]*Id[2] - std::pow(Ip[2], 2);
350  // d = Ixx*Iyz^2 + Iyy*Ixz^2 + Izz*Ixy^2 - Ixx*Iyy*Izz - 2*Ixy*Ixz*Iyz
351  T d = Id[0]*std::pow(Ip[2], 2)
352  + Id[1]*std::pow(Ip[1], 2)
353  + Id[2]*std::pow(Ip[0], 2)
354  - Id[0]*Id[1]*Id[2]
355  - 2*Ip[0]*Ip[1]*Ip[2];
356  // p = b^2 - 3c
357  T p = std::pow(b, 2) - 3*c;
358 
359  // At this point, it is important to check that p is not close
360  // to zero, since its inverse is used to compute delta.
361  // In equation 4.7, p is expressed as a sum of squares
362  // that is only zero if the matrix is diagonal
363  // with identical principal moments.
364  // This check has no test coverage, since this function returns
365  // immediately if a diagonal matrix is detected.
366  if (p < std::pow(tol, 2))
367  return b / 3.0 * Vector3<T>::One;
368 
369  // q = 2b^3 - 9bc - 27d
370  T q = 2*std::pow(b, 3) - 9*b*c - 27*d;
371 
372  // delta = acos(q / (2 * p^(1.5)))
373  // additionally clamp the argument to [-1,1]
374  T delta = acos(clamp<T>(0.5 * q / std::pow(p, 1.5), -1, 1));
375 
376  // sort the moments from smallest to largest
377  T moment0 = (b + 2*sqrt(p) * cos(delta / 3.0)) / 3.0;
378  T moment1 = (b + 2*sqrt(p) * cos((delta + 2*IGN_PI)/3.0)) / 3.0;
379  T moment2 = (b + 2*sqrt(p) * cos((delta - 2*IGN_PI)/3.0)) / 3.0;
380  sort3(moment0, moment1, moment2);
381  return Vector3<T>(moment0, moment1, moment2);
382  }
383 
395  public: Quaternion<T> PrincipalAxesOffset(const T _tol = 1e-6) const
396  {
397  // Compute tolerance relative to maximum value of inertia diagonal
398  T tol = _tol * this->Ixxyyzz.Max();
399  Vector3<T> moments = this->PrincipalMoments(tol);
400  if (moments.Equal(this->Ixxyyzz, tol))
401  {
402  // matrix is already aligned with principal axes
403  // this includes case when all three moments are
404  // approximately equal
405  // return identity rotation
407  }
408 
409  // Algorithm based on http://arxiv.org/abs/1306.6291v4
410  // A Method for Fast Diagonalization of a 2x2 or 3x3 Real Symmetric
411  // Matrix, by Maarten Kronenburg
412  // A real, symmetric matrix can be diagonalized by an orthogonal matrix
413  // (due to the finite-dimensional spectral theorem
414  // https://en.wikipedia.org/wiki/Spectral_theorem
415  // #Hermitian_maps_and_Hermitian_matrices ),
416  // and another name for orthogonal matrix is rotation matrix.
417  // Section 5 of the paper shows how to compute Euler angles
418  // phi1, phi2, and phi3 that map to a rotation matrix.
419  // In some cases, there are multiple possible values for a given angle,
420  // such as phi1, that are denoted as phi11, phi12, phi11a, phi12a, etc.
421  // Similar variable names are used to the paper so that the paper
422  // can be used as an additional reference.
423 
424  // f1, f2 defined in equations 5.5, 5.6
425  Vector2<T> f1(this->Ixyxzyz[0], -this->Ixyxzyz[1]);
426  Vector2<T> f2(this->Ixxyyzz[1] - this->Ixxyyzz[2],
427  -2*this->Ixyxzyz[2]);
428 
429  // Check if two moments are equal, since different equations are used
430  // The moments vector is already sorted, so just check adjacent values.
431  Vector2<T> momentsDiff(moments[0] - moments[1],
432  moments[1] - moments[2]);
433 
434  // index of unequal moment
435  int unequalMoment = -1;
436  if (equal<T>(momentsDiff[0], 0, std::abs(tol)))
437  unequalMoment = 2;
438  else if (equal<T>(momentsDiff[1], 0, std::abs(tol)))
439  unequalMoment = 0;
440 
441  if (unequalMoment >= 0)
442  {
443  // moments[1] is the repeated value
444  // it is not equal to moments[unequalMoment]
445  // momentsDiff3 = lambda - lambda3
446  T momentsDiff3 = moments[1] - moments[unequalMoment];
447  // eq 5.21:
448  // s = cos(phi2)^2 = (A_11 - lambda3) / (lambda - lambda3)
449  // s >= 0 since A_11 is in range [lambda, lambda3]
450  T s = (this->Ixxyyzz[0] - moments[unequalMoment]) / momentsDiff3;
451  // set phi3 to zero for repeated moments (eq 5.23)
452  T phi3 = 0;
453  // phi2 = +- acos(sqrt(s))
454  // start with just the positive value
455  // also clamp the acos argument to prevent NaN's
456  T phi2 = acos(clamp<T>(ClampedSqrt(s), -1, 1));
457 
458  // The paper defines variables phi11 and phi12
459  // which are candidate values of angle phi1.
460  // phi12 is straightforward to compute as a function of f2 and g2.
461  // eq 5.25:
462  Vector2<T> g2(momentsDiff3 * s, 0);
463  // combining eq 5.12 and 5.14, and subtracting psi2
464  // instead of multiplying by its rotation matrix:
465  math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
466  phi12.Normalize();
467 
468  // The paragraph prior to equation 5.16 describes how to choose
469  // the candidate value of phi1 based on the length
470  // of the f1 and f2 vectors.
471  // * When |f1| != 0 and |f2| != 0, then one should choose the
472  // value of phi2 so that phi11 = phi12
473  // * When |f1| == 0 and f2 != 0, then phi1 = phi12
474  // phi11 can be ignored, and either sign of phi2 can be used
475  // * The case of |f2| == 0 can be ignored at this point in the code
476  // since having a repeated moment when |f2| == 0 implies that
477  // the matrix is diagonal. But this function returns a unit
478  // quaternion for diagonal matrices, so we can assume |f2| != 0
479  // See MassMatrix3.ipynb for a more complete discussion.
480  //
481  // Since |f2| != 0, we only need to consider |f1|
482  // * |f1| == 0: phi1 = phi12
483  // * |f1| != 0: choose phi2 so that phi11 == phi12
484  // In either case, phi1 = phi12,
485  // and the sign of phi2 must be chosen to make phi11 == phi12
486  T phi1 = phi12.Radian();
487 
488  bool f1small = f1.SquaredLength() < std::pow(tol, 2);
489  if (!f1small)
490  {
491  // a: phi2 > 0
492  // eq. 5.24
493  Vector2<T> g1a(0, 0.5*momentsDiff3 * sin(2*phi2));
494  // combining eq 5.11 and 5.13, and subtracting psi1
495  // instead of multiplying by its rotation matrix:
496  math::Angle phi11a(Angle2(g1a, tol) - Angle2(f1, tol));
497  phi11a.Normalize();
498 
499  // b: phi2 < 0
500  // eq. 5.24
501  Vector2<T> g1b(0, 0.5*momentsDiff3 * sin(-2*phi2));
502  // combining eq 5.11 and 5.13, and subtracting psi1
503  // instead of multiplying by its rotation matrix:
504  math::Angle phi11b(Angle2(g1b, tol) - Angle2(f1, tol));
505  phi11b.Normalize();
506 
507  // choose sign of phi2
508  // based on whether phi11a or phi11b is closer to phi12
509  // use sin and cos to account for angle wrapping
510  T erra = std::pow(sin(phi1) - sin(phi11a.Radian()), 2)
511  + std::pow(cos(phi1) - cos(phi11a.Radian()), 2);
512  T errb = std::pow(sin(phi1) - sin(phi11b.Radian()), 2)
513  + std::pow(cos(phi1) - cos(phi11b.Radian()), 2);
514  if (errb < erra)
515  {
516  phi2 *= -1;
517  }
518  }
519 
520  // I determined these arguments using trial and error
521  Quaternion<T> result = Quaternion<T>(-phi1, -phi2, -phi3).Inverse();
522 
523  // Previous equations assume repeated moments are at the beginning
524  // of the moments vector (moments[0] == moments[1]).
525  // We have the vectors sorted by size, so it's possible that the
526  // repeated moments are at the end (moments[1] == moments[2]).
527  // In this case (unequalMoment == 0), we apply an extra
528  // rotation that exchanges moment[0] and moment[2]
529  // Rotation matrix = [ 0 0 1]
530  // [ 0 1 0]
531  // [-1 0 0]
532  // That is equivalent to a 90 degree pitch
533  if (unequalMoment == 0)
534  result *= Quaternion<T>(0, IGN_PI_2, 0);
535 
536  return result;
537  }
538 
539  // No repeated principal moments
540  // eq 5.1:
541  T v = (std::pow(this->Ixyxzyz[0], 2) + std::pow(this->Ixyxzyz[1], 2)
542  +(this->Ixxyyzz[0] - moments[2])
543  *(this->Ixxyyzz[0] + moments[2] - moments[0] - moments[1]))
544  / ((moments[1] - moments[2]) * (moments[2] - moments[0]));
545  // value of w depends on v
546  T w;
547  if (v < std::abs(tol))
548  {
549  // first sentence after eq 5.4:
550  // "In the case that v = 0, then w = 1."
551  w = 1;
552  }
553  else
554  {
555  // eq 5.2:
556  w = (this->Ixxyyzz[0] - moments[2] + (moments[2] - moments[1])*v)
557  / ((moments[0] - moments[1]) * v);
558  }
559  // initialize values of angle phi1, phi2, phi3
560  T phi1 = 0;
561  // eq 5.3: start with positive value
562  T phi2 = acos(clamp<T>(ClampedSqrt(v), -1, 1));
563  // eq 5.4: start with positive value
564  T phi3 = acos(clamp<T>(ClampedSqrt(w), -1, 1));
565 
566  // compute g1, g2 for phi2,phi3 >= 0
567  // equations 5.7, 5.8
568  Vector2<T> g1(
569  0.5* (moments[0]-moments[1])*ClampedSqrt(v)*sin(2*phi3),
570  0.5*((moments[0]-moments[1])*w + moments[1]-moments[2])*sin(2*phi2));
571  Vector2<T> g2(
572  (moments[0]-moments[1])*(1 + (v-2)*w) + (moments[1]-moments[2])*v,
573  (moments[0]-moments[1])*sin(phi2)*sin(2*phi3));
574 
575  // The paragraph prior to equation 5.16 describes how to choose
576  // the candidate value of phi1 based on the length
577  // of the f1 and f2 vectors.
578  // * The case of |f1| == |f2| == 0 implies a repeated moment,
579  // which should not be possible at this point in the code
580  // * When |f1| != 0 and |f2| != 0, then one should choose the
581  // value of phi2 so that phi11 = phi12
582  // * When |f1| == 0 and f2 != 0, then phi1 = phi12
583  // phi11 can be ignored, and either sign of phi2, phi3 can be used
584  // * When |f2| == 0 and f1 != 0, then phi1 = phi11
585  // phi12 can be ignored, and either sign of phi2, phi3 can be used
586  bool f1small = f1.SquaredLength() < std::pow(tol, 2);
587  bool f2small = f2.SquaredLength() < std::pow(tol, 2);
588  if (f1small && f2small)
589  {
590  // this should never happen
591  // f1small && f2small implies a repeated moment
592  // return invalid quaternion
593  return Quaternion<T>::Zero;
594  }
595  else if (f1small)
596  {
597  // use phi12 (equations 5.12, 5.14)
598  math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
599  phi12.Normalize();
600  phi1 = phi12.Radian();
601  }
602  else if (f2small)
603  {
604  // use phi11 (equations 5.11, 5.13)
605  math::Angle phi11(Angle2(g1, tol) - Angle2(f1, tol));
606  phi11.Normalize();
607  phi1 = phi11.Radian();
608  }
609  else
610  {
611  // check for when phi11 == phi12
612  // eqs 5.11, 5.13:
613  math::Angle phi11(Angle2(g1, tol) - Angle2(f1, tol));
614  phi11.Normalize();
615  // eqs 5.12, 5.14:
616  math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
617  phi12.Normalize();
618  T err = std::pow(sin(phi11.Radian()) - sin(phi12.Radian()), 2)
619  + std::pow(cos(phi11.Radian()) - cos(phi12.Radian()), 2);
620  phi1 = phi11.Radian();
621  math::Vector2<T> signsPhi23(1, 1);
622  // case a: phi2 <= 0
623  {
624  Vector2<T> g1a = Vector2<T>(1, -1) * g1;
625  Vector2<T> g2a = Vector2<T>(1, -1) * g2;
626  math::Angle phi11a(Angle2(g1a, tol) - Angle2(f1, tol));
627  math::Angle phi12a(0.5*(Angle2(g2a, tol) - Angle2(f2, tol)));
628  phi11a.Normalize();
629  phi12a.Normalize();
630  T erra = std::pow(sin(phi11a.Radian()) - sin(phi12a.Radian()), 2)
631  + std::pow(cos(phi11a.Radian()) - cos(phi12a.Radian()), 2);
632  if (erra < err)
633  {
634  err = erra;
635  phi1 = phi11a.Radian();
636  signsPhi23.Set(-1, 1);
637  }
638  }
639  // case b: phi3 <= 0
640  {
641  Vector2<T> g1b = Vector2<T>(-1, 1) * g1;
642  Vector2<T> g2b = Vector2<T>(1, -1) * g2;
643  math::Angle phi11b(Angle2(g1b, tol) - Angle2(f1, tol));
644  math::Angle phi12b(0.5*(Angle2(g2b, tol) - Angle2(f2, tol)));
645  phi11b.Normalize();
646  phi12b.Normalize();
647  T errb = std::pow(sin(phi11b.Radian()) - sin(phi12b.Radian()), 2)
648  + std::pow(cos(phi11b.Radian()) - cos(phi12b.Radian()), 2);
649  if (errb < err)
650  {
651  err = errb;
652  phi1 = phi11b.Radian();
653  signsPhi23.Set(1, -1);
654  }
655  }
656  // case c: phi2,phi3 <= 0
657  {
658  Vector2<T> g1c = Vector2<T>(-1, -1) * g1;
659  Vector2<T> g2c = g2;
660  math::Angle phi11c(Angle2(g1c, tol) - Angle2(f1, tol));
661  math::Angle phi12c(0.5*(Angle2(g2c, tol) - Angle2(f2, tol)));
662  phi11c.Normalize();
663  phi12c.Normalize();
664  T errc = std::pow(sin(phi11c.Radian()) - sin(phi12c.Radian()), 2)
665  + std::pow(cos(phi11c.Radian()) - cos(phi12c.Radian()), 2);
666  if (errc < err)
667  {
668  err = errc;
669  phi1 = phi11c.Radian();
670  signsPhi23.Set(-1, -1);
671  }
672  }
673 
674  // apply sign changes
675  phi2 *= signsPhi23[0];
676  phi3 *= signsPhi23[1];
677  }
678 
679  // I determined these arguments using trial and error
680  return Quaternion<T>(-phi1, -phi2, -phi3).Inverse();
681  }
682 
692  public: bool EquivalentBox(Vector3<T> &_size,
693  Quaternion<T> &_rot,
694  const T _tol = 1e-6) const
695  {
696  if (!this->IsPositive())
697  {
698  // inertia is not positive, cannot compute equivalent box
699  return false;
700  }
701 
702  Vector3<T> moments = this->PrincipalMoments(_tol);
703  if (!ValidMoments(moments))
704  {
705  // principal moments don't satisfy the triangle identity
706  return false;
707  }
708 
709  // The reason for checking that the principal moments satisfy
710  // the triangle inequality
711  // I1 + I2 - I3 >= 0
712  // is to ensure that the arguments to sqrt in these equations
713  // are positive and the box size is real.
714  _size.X(sqrt(6*(moments.Y() + moments.Z() - moments.X()) / this->mass));
715  _size.Y(sqrt(6*(moments.Z() + moments.X() - moments.Y()) / this->mass));
716  _size.Z(sqrt(6*(moments.X() + moments.Y() - moments.Z()) / this->mass));
717 
718  _rot = this->PrincipalAxesOffset(_tol);
719 
720  if (_rot == Quaternion<T>::Zero)
721  {
722  // _rot is an invalid quaternion
723  return false;
724  }
725 
726  return true;
727  }
728 
734  public: bool SetFromBox(const T _mass,
735  const Vector3<T> &_size,
737  {
738  // Check that _mass and _size are strictly positive
739  // and that quatenion is valid
740  if (_mass <= 0 || _size.Min() <= 0 || _rot == Quaternion<T>::Zero)
741  {
742  return false;
743  }
744  this->Mass(_mass);
745  return this->SetFromBox(_size, _rot);
746  }
747 
753  public: bool SetFromBox(const Vector3<T> &_size,
755  {
756  // Check that _mass and _size are strictly positive
757  // and that quatenion is valid
758  if (this->Mass() <= 0 || _size.Min() <= 0 ||
759  _rot == Quaternion<T>::Zero)
760  {
761  return false;
762  }
763 
764  // Diagonal matrix L with principal moments
765  Matrix3<T> L;
766  T x2 = std::pow(_size.X(), 2);
767  T y2 = std::pow(_size.Y(), 2);
768  T z2 = std::pow(_size.Z(), 2);
769  L(0, 0) = this->mass / 12.0 * (y2 + z2);
770  L(1, 1) = this->mass / 12.0 * (z2 + x2);
771  L(2, 2) = this->mass / 12.0 * (x2 + y2);
772  Matrix3<T> R(_rot);
773  return this->MOI(R * L * R.Transposed());
774  }
775 
783  public: bool SetFromCylinderZ(const T _mass,
784  const T _length,
785  const T _radius,
787  {
788  // Check that _mass, _radius and _length are strictly positive
789  // and that quatenion is valid
790  if (_mass <= 0 || _length <= 0 || _radius <= 0 ||
791  _rot == Quaternion<T>::Zero)
792  {
793  return false;
794  }
795  this->Mass(_mass);
796  return this->SetFromCylinderZ(_length, _radius, _rot);
797  }
798 
805  public: bool SetFromCylinderZ(const T _length,
806  const T _radius,
807  const Quaternion<T> &_rot)
808  {
809  // Check that _mass and _size are strictly positive
810  // and that quatenion is valid
811  if (this->Mass() <= 0 || _length <= 0 || _radius <= 0 ||
812  _rot == Quaternion<T>::Zero)
813  {
814  return false;
815  }
816 
817  // Diagonal matrix L with principal moments
818  T radius2 = std::pow(_radius, 2);
819  Matrix3<T> L;
820  L(0, 0) = this->mass / 12.0 * (3*radius2 + std::pow(_length, 2));
821  L(1, 1) = L(0, 0);
822  L(2, 2) = this->mass / 2.0 * radius2;
823  Matrix3<T> R(_rot);
824  return this->MOI(R * L * R.Transposed());
825  }
826 
831  public: bool SetFromSphere(const T _mass, const T _radius)
832  {
833  // Check that _mass and _radius are strictly positive
834  if (_mass <= 0 || _radius <= 0)
835  {
836  return false;
837  }
838  this->Mass(_mass);
839  return this->SetFromSphere(_radius);
840  }
841 
846  public: bool SetFromSphere(const T _radius)
847  {
848  // Check that _mass and _radius are strictly positive
849  if (this->Mass() <= 0 || _radius <= 0)
850  {
851  return false;
852  }
853 
854  // Diagonal matrix L with principal moments
855  T radius2 = std::pow(_radius, 2);
856  Matrix3<T> L;
857  L(0, 0) = 0.4 * this->mass * radius2;
858  L(1, 1) = 0.4 * this->mass * radius2;
859  L(2, 2) = 0.4 * this->mass * radius2;
860  return this->MOI(L);
861  }
862 
866  private: static inline T ClampedSqrt(const T &_x)
867  {
868  if (_x <= 0)
869  return 0;
870  return sqrt(_x);
871  }
872 
878  private: static T Angle2(const Vector2<T> &_v, const T _eps = 1e-6)
879  {
880  if (_v.SquaredLength() < std::pow(_eps, 2))
881  return 0;
882  return atan2(_v[1], _v[0]);
883  }
884 
886  private: T mass;
887 
891  private: Vector3<T> Ixxyyzz;
892 
896  private: Vector3<T> Ixyxzyz;
897  };
898 
901  }
902 }
903 #endif
An angle and related functions.
Definition: Angle.hh:44
bool operator==(const MassMatrix3< T > &_m) const
Equality comparison operator.
Definition: MassMatrix3.hh:266
T IXY() const
Get IXY.
Definition: MassMatrix3.hh:152
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:753
bool SetFromSphere(const T _mass, const T _radius)
Set inertial properties based on mass and equivalent sphere.
Definition: MassMatrix3.hh:831
T Mass() const
Get the mass.
Definition: MassMatrix3.hh:76
bool IYZ(const T &_v)
Set IYZ.
Definition: MassMatrix3.hh:219
static bool ValidMoments(const Vector3< T > &_moments)
Verify that principal moments are positive and satisfy the triangle inequality.
Definition: MassMatrix3.hh:308
MassMatrix3()
Default Constructor.
Definition: MassMatrix3.hh:42
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:783
bool operator!=(const MassMatrix3< T > &_m) const
Inequality test operator.
Definition: MassMatrix3.hh:276
T Sum() const
Return the sum of the values.
Definition: Vector3.hh:87
T IXZ() const
Get IXZ.
Definition: MassMatrix3.hh:159
MassMatrix3(const MassMatrix3< T > &_m)
Copy constructor.
Definition: MassMatrix3.hh:57
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:103
void Normalize()
Normalize the angle in the range -Pi to Pi.
bool OffDiagonalMoments(const Vector3< T > &_ixyxzyz)
Set the off-diagonal moments of inertia (Ixy, Ixz, Iyz).
Definition: MassMatrix3.hh:123
bool IZZ(const T &_v)
Set IZZ.
Definition: MassMatrix3.hh:192
A class for inertial information about a rigid body consisting of the scalar mass and a 3x3 symmetric...
Definition: MassMatrix3.hh:39
Two dimensional (x, y) vector.
Definition: Vector2.hh:29
MassMatrix3 & operator=(const MassMatrix3< T > &_massMatrix)
Equal operator.
Definition: MassMatrix3.hh:253
Vector3< T > OffDiagonalMoments() const
Get the off-diagonal moments of inertia (Ixy, Ixz, Iyz).
Definition: MassMatrix3.hh:106
Vector3< T > DiagonalMoments() const
Get the diagonal moments of inertia (Ixx, Iyy, Izz).
Definition: MassMatrix3.hh:99
T X() const
Get the x value.
Definition: Vector3.hh:639
bool IsPositive() const
Verify that inertia values are positive definite.
Definition: MassMatrix3.hh:284
bool DiagonalMoments(const Vector3< T > &_ixxyyzz)
Set the diagonal moments of inertia (Ixx, Iyy, Izz).
Definition: MassMatrix3.hh:114
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:329
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:89
T Y() const
Get the y value.
Definition: Vector3.hh:646
void Radian(double _radian)
Set the value from an angle in radians.
A 3x3 matrix class.
Definition: Matrix3.hh:35
Matrix3< T > MOI() const
returns Moments of Inertia as a Matrix3
Definition: MassMatrix3.hh:227
void Min(const Vector3< T > &_v)
Set this vector&#39;s components to the minimum of itself and the passed in vector.
Definition: Vector3.hh:285
bool SetFromSphere(const T _radius)
Set inertial properties based on equivalent sphere using the current mass value.
Definition: MassMatrix3.hh:846
T IZZ() const
Get IZZ.
Definition: MassMatrix3.hh:145
bool IXY(const T &_v)
Set IXY.
Definition: MassMatrix3.hh:201
T Z() const
Get the z value.
Definition: Vector3.hh:653
bool IYY(const T &_v)
Set IYY.
Definition: MassMatrix3.hh:183
T IYZ() const
Get IYZ.
Definition: MassMatrix3.hh:166
bool IXZ(const T &_v)
Set IXZ.
Definition: MassMatrix3.hh:210
Matrix3< T > Transposed() const
Return the transpose of this matrix.
Definition: Matrix3.hh:458
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:37
MassMatrix3< double > MassMatrix3d
Definition: MassMatrix3.hh:899
virtual ~MassMatrix3()
Destructor.
Definition: MassMatrix3.hh:63
bool IsValid() const
Verify that inertia values are positive definite and satisfy the triangle inequality.
Definition: MassMatrix3.hh:298
MassMatrix3(const T &_mass, const Vector3< T > &_ixxyyzz, const Vector3< T > &_ixyxzyz)
Constructor.
Definition: MassMatrix3.hh:49
bool MOI(const Matrix3< T > &_moi)
Sets Moments of Inertia (MOI) from a Matrix3.
Definition: MassMatrix3.hh:240
void Normalize()
Normalize the vector length.
Definition: Vector2.hh:89
MassMatrix3< float > MassMatrix3f
Definition: MassMatrix3.hh:900
bool Equal(const Vector3 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector3.hh:556
T IXX() const
Get IXX.
Definition: MassMatrix3.hh:131
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:734
#define IGN_PI_2
Definition: Helpers.hh:140
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:805
bool IXX(const T &_v)
Set IXX.
Definition: MassMatrix3.hh:174
Quaternion< T > PrincipalAxesOffset(const T _tol=1e-6) const
Compute rotational offset of principal axes.
Definition: MassMatrix3.hh:395
Definition: AffineException.hh:30
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:692
A quaternion class.
Definition: Matrix3.hh:30
T IYY() const
Get IYY.
Definition: MassMatrix3.hh:138
#define IGN_PI
Define IGN_PI, IGN_PI_2, and IGN_PI_4.
Definition: Helpers.hh:139
bool Mass(const T &_m)
Set the mass.
Definition: MassMatrix3.hh:68
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition: Vector2.hh:82
void sort3(T &_a, T &_b, T &_c)
Sort three numbers, such that _a <= _b <= _c.
Definition: Helpers.hh:385