Ignition Math

API Reference

4.0.0
MassMatrix3.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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/config.hh>
25 #include "ignition/math/Helpers.hh"
27 #include "ignition/math/Vector2.hh"
28 #include "ignition/math/Vector3.hh"
29 #include "ignition/math/Matrix3.hh"
30 
31 namespace ignition
32 {
33  namespace math
34  {
35  // Inline bracket to help doxygen filtering.
36  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
37  //
42  template<typename T>
44  {
46  public: MassMatrix3() : mass(0)
47  {}
48 
53  public: MassMatrix3(const T &_mass,
54  const Vector3<T> &_ixxyyzz,
55  const Vector3<T> &_ixyxzyz)
56  : mass(_mass), Ixxyyzz(_ixxyyzz), Ixyxzyz(_ixyxzyz)
57  {}
58 
61  public: MassMatrix3(const MassMatrix3<T> &_m)
62  : mass(_m.Mass()), Ixxyyzz(_m.DiagonalMoments()),
63  Ixyxzyz(_m.OffDiagonalMoments())
64  {}
65 
67  public: virtual ~MassMatrix3() {}
68 
72  public: bool Mass(const T &_m)
73  {
74  this->mass = _m;
75  return this->IsValid();
76  }
77 
80  public: T Mass() const
81  {
82  return this->mass;
83  }
84 
93  public: bool InertiaMatrix(const T &_ixx, const T &_iyy, const T &_izz,
94  const T &_ixy, const T &_ixz, const T &_iyz)
95  {
96  this->Ixxyyzz.Set(_ixx, _iyy, _izz);
97  this->Ixyxzyz.Set(_ixy, _ixz, _iyz);
98  return this->IsValid();
99  }
100 
103  public: Vector3<T> DiagonalMoments() const
104  {
105  return this->Ixxyyzz;
106  }
107 
111  {
112  return this->Ixyxzyz;
113  }
114 
118  public: bool DiagonalMoments(const Vector3<T> &_ixxyyzz)
119  {
120  this->Ixxyyzz = _ixxyyzz;
121  return this->IsValid();
122  }
123 
127  public: bool OffDiagonalMoments(const Vector3<T> &_ixyxzyz)
128  {
129  this->Ixyxzyz = _ixyxzyz;
130  return this->IsValid();
131  }
132 
135  public: T IXX() const
136  {
137  return this->Ixxyyzz[0];
138  }
139 
142  public: T IYY() const
143  {
144  return this->Ixxyyzz[1];
145  }
146 
149  public: T IZZ() const
150  {
151  return this->Ixxyyzz[2];
152  }
153 
156  public: T IXY() const
157  {
158  return this->Ixyxzyz[0];
159  }
160 
163  public: T IXZ() const
164  {
165  return this->Ixyxzyz[1];
166  }
167 
170  public: T IYZ() const
171  {
172  return this->Ixyxzyz[2];
173  }
174 
178  public: bool IXX(const T &_v)
179  {
180  this->Ixxyyzz.X(_v);
181  return this->IsValid();
182  }
183 
187  public: bool IYY(const T &_v)
188  {
189  this->Ixxyyzz.Y(_v);
190  return this->IsValid();
191  }
192 
196  public: bool IZZ(const T &_v)
197  {
198  this->Ixxyyzz.Z(_v);
199  return this->IsValid();
200  }
201 
205  public: bool IXY(const T &_v)
206  {
207  this->Ixyxzyz.X(_v);
208  return this->IsValid();
209  }
210 
214  public: bool IXZ(const T &_v)
215  {
216  this->Ixyxzyz.Y(_v);
217  return this->IsValid();
218  }
219 
223  public: bool IYZ(const T &_v)
224  {
225  this->Ixyxzyz.Z(_v);
226  return this->IsValid();
227  }
228 
231  public: Matrix3<T> MOI() const
232  {
233  return Matrix3<T>(
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]);
237  }
238 
244  public: bool MOI(const Matrix3<T> &_moi)
245  {
246  this->Ixxyyzz.Set(_moi(0, 0), _moi(1, 1), _moi(2, 2));
247  this->Ixyxzyz.Set(
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();
252  }
253 
257  public: MassMatrix3 &operator=(const MassMatrix3<T> &_massMatrix)
258  {
259  this->mass = _massMatrix.Mass();
260  this->Ixxyyzz = _massMatrix.DiagonalMoments();
261  this->Ixyxzyz = _massMatrix.OffDiagonalMoments();
262 
263  return *this;
264  }
265 
270  public: bool operator==(const MassMatrix3<T> &_m) const
271  {
272  return equal<T>(this->mass, _m.Mass()) &&
273  (this->Ixxyyzz == _m.DiagonalMoments()) &&
274  (this->Ixyxzyz == _m.OffDiagonalMoments());
275  }
276 
280  public: bool operator!=(const MassMatrix3<T> &_m) const
281  {
282  return !(*this == _m);
283  }
284 
288  public: bool IsPositive() const
289  {
290  // Check if mass and determinants of all upper left submatrices
291  // of moment of inertia matrix are positive
292  return (this->mass > 0) &&
293  (this->IXX() > 0) &&
294  (this->IXX()*this->IYY() - std::pow(this->IXY(), 2) > 0) &&
295  (this->MOI().Determinant() > 0);
296  }
297 
302  public: bool IsValid() const
303  {
304  return this->IsPositive() && ValidMoments(this->PrincipalMoments());
305  }
306 
312  public: static bool ValidMoments(const Vector3<T> &_moments)
313  {
314  return _moments[0] > 0 &&
315  _moments[1] > 0 &&
316  _moments[2] > 0 &&
317  _moments[0] + _moments[1] > _moments[2] &&
318  _moments[1] + _moments[2] > _moments[0] &&
319  _moments[2] + _moments[0] > _moments[1];
320  }
321 
333  public: Vector3<T> PrincipalMoments(const T _tol = 1e-6) const
334  {
335  // Compute tolerance relative to maximum value of inertia diagonal
336  T tol = _tol * this->Ixxyyzz.Max();
337  if (this->Ixyxzyz.Equal(Vector3<T>::Zero, tol))
338  {
339  // Matrix is already diagonalized, return diagonal moments
340  return this->Ixxyyzz;
341  }
342 
343  // Algorithm based on http://arxiv.org/abs/1306.6291v4
344  // A Method for Fast Diagonalization of a 2x2 or 3x3 Real Symmetric
345  // Matrix, by Maarten Kronenburg
346  Vector3<T> Id(this->Ixxyyzz);
347  Vector3<T> Ip(this->Ixyxzyz);
348  // b = Ixx + Iyy + Izz
349  T b = Id.Sum();
350  // c = Ixx*Iyy - Ixy^2 + Ixx*Izz - Ixz^2 + Iyy*Izz - Iyz^2
351  T c = Id[0]*Id[1] - std::pow(Ip[0], 2)
352  + Id[0]*Id[2] - std::pow(Ip[1], 2)
353  + Id[1]*Id[2] - std::pow(Ip[2], 2);
354  // d = Ixx*Iyz^2 + Iyy*Ixz^2 + Izz*Ixy^2 - Ixx*Iyy*Izz - 2*Ixy*Ixz*Iyz
355  T d = Id[0]*std::pow(Ip[2], 2)
356  + Id[1]*std::pow(Ip[1], 2)
357  + Id[2]*std::pow(Ip[0], 2)
358  - Id[0]*Id[1]*Id[2]
359  - 2*Ip[0]*Ip[1]*Ip[2];
360  // p = b^2 - 3c
361  T p = std::pow(b, 2) - 3*c;
362 
363  // At this point, it is important to check that p is not close
364  // to zero, since its inverse is used to compute delta.
365  // In equation 4.7, p is expressed as a sum of squares
366  // that is only zero if the matrix is diagonal
367  // with identical principal moments.
368  // This check has no test coverage, since this function returns
369  // immediately if a diagonal matrix is detected.
370  if (p < std::pow(tol, 2))
371  return b / 3.0 * Vector3<T>::One;
372 
373  // q = 2b^3 - 9bc - 27d
374  T q = 2*std::pow(b, 3) - 9*b*c - 27*d;
375 
376  // delta = acos(q / (2 * p^(1.5)))
377  // additionally clamp the argument to [-1,1]
378  T delta = acos(clamp<T>(0.5 * q / std::pow(p, 1.5), -1, 1));
379 
380  // sort the moments from smallest to largest
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);
385  return Vector3<T>(moment0, moment1, moment2);
386  }
387 
399  public: Quaternion<T> PrincipalAxesOffset(const T _tol = 1e-6) const
400  {
401  // Compute tolerance relative to maximum value of inertia diagonal
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))))
407  {
408  // matrix is already aligned with principal axes
409  // or all three moments are approximately equal
410  // return identity rotation
412  }
413 
414  // Algorithm based on http://arxiv.org/abs/1306.6291v4
415  // A Method for Fast Diagonalization of a 2x2 or 3x3 Real Symmetric
416  // Matrix, by Maarten Kronenburg
417  // A real, symmetric matrix can be diagonalized by an orthogonal matrix
418  // (due to the finite-dimensional spectral theorem
419  // https://en.wikipedia.org/wiki/Spectral_theorem
420  // #Hermitian_maps_and_Hermitian_matrices ),
421  // and another name for orthogonal matrix is rotation matrix.
422  // Section 5 of the paper shows how to compute Euler angles
423  // phi1, phi2, and phi3 that map to a rotation matrix.
424  // In some cases, there are multiple possible values for a given angle,
425  // such as phi1, that are denoted as phi11, phi12, phi11a, phi12a, etc.
426  // Similar variable names are used to the paper so that the paper
427  // can be used as an additional reference.
428 
429  // f1, f2 defined in equations 5.5, 5.6
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]);
433 
434  // Check if two moments are equal, since different equations are used
435  // The moments vector is already sorted, so just check adjacent values.
436  Vector2<T> momentsDiff(moments[0] - moments[1],
437  moments[1] - moments[2]);
438 
439  // index of unequal moment
440  int unequalMoment = -1;
441  if (equal<T>(momentsDiff[0], 0, std::abs(tol)))
442  unequalMoment = 2;
443  else if (equal<T>(momentsDiff[1], 0, std::abs(tol)))
444  unequalMoment = 0;
445 
446  if (unequalMoment >= 0)
447  {
448  // moments[1] is the repeated value
449  // it is not equal to moments[unequalMoment]
450  // momentsDiff3 = lambda - lambda3
451  T momentsDiff3 = moments[1] - moments[unequalMoment];
452  // eq 5.21:
453  // s = cos(phi2)^2 = (A_11 - lambda3) / (lambda - lambda3)
454  // s >= 0 since A_11 is in range [lambda, lambda3]
455  T s = (this->Ixxyyzz[0] - moments[unequalMoment]) / momentsDiff3;
456  // set phi3 to zero for repeated moments (eq 5.23)
457  T phi3 = 0;
458  // phi2 = +- acos(sqrt(s))
459  // start with just the positive value
460  // also clamp the acos argument to prevent NaN's
461  T phi2 = acos(clamp<T>(ClampedSqrt(s), -1, 1));
462 
463  // The paper defines variables phi11 and phi12
464  // which are candidate values of angle phi1.
465  // phi12 is straightforward to compute as a function of f2 and g2.
466  // eq 5.25:
467  Vector2<T> g2(momentsDiff3 * s, 0);
468  // combining eq 5.12 and 5.14, and subtracting psi2
469  // instead of multiplying by its rotation matrix:
470  math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
471  phi12.Normalize();
472 
473  // The paragraph prior to equation 5.16 describes how to choose
474  // the candidate value of phi1 based on the length
475  // of the f1 and f2 vectors.
476  // * When |f1| != 0 and |f2| != 0, then one should choose the
477  // value of phi2 so that phi11 = phi12
478  // * When |f1| == 0 and f2 != 0, then phi1 = phi12
479  // phi11 can be ignored, and either sign of phi2 can be used
480  // * The case of |f2| == 0 can be ignored at this point in the code
481  // since having a repeated moment when |f2| == 0 implies that
482  // the matrix is diagonal. But this function returns a unit
483  // quaternion for diagonal matrices, so we can assume |f2| != 0
484  // See MassMatrix3.ipynb for a more complete discussion.
485  //
486  // Since |f2| != 0, we only need to consider |f1|
487  // * |f1| == 0: phi1 = phi12
488  // * |f1| != 0: choose phi2 so that phi11 == phi12
489  // In either case, phi1 = phi12,
490  // and the sign of phi2 must be chosen to make phi11 == phi12
491  T phi1 = phi12.Radian();
492 
493  bool f1small = f1.SquaredLength() < std::pow(tol, 2);
494  if (!f1small)
495  {
496  // a: phi2 > 0
497  // eq. 5.24
498  Vector2<T> g1a(0, 0.5*momentsDiff3 * sin(2*phi2));
499  // combining eq 5.11 and 5.13, and subtracting psi1
500  // instead of multiplying by its rotation matrix:
501  math::Angle phi11a(Angle2(g1a, tol) - Angle2(f1, tol));
502  phi11a.Normalize();
503 
504  // b: phi2 < 0
505  // eq. 5.24
506  Vector2<T> g1b(0, 0.5*momentsDiff3 * sin(-2*phi2));
507  // combining eq 5.11 and 5.13, and subtracting psi1
508  // instead of multiplying by its rotation matrix:
509  math::Angle phi11b(Angle2(g1b, tol) - Angle2(f1, tol));
510  phi11b.Normalize();
511 
512  // choose sign of phi2
513  // based on whether phi11a or phi11b is closer to phi12
514  // use sin and cos to account for angle wrapping
515  T erra = std::pow(sin(phi1) - sin(phi11a.Radian()), 2)
516  + std::pow(cos(phi1) - cos(phi11a.Radian()), 2);
517  T errb = std::pow(sin(phi1) - sin(phi11b.Radian()), 2)
518  + std::pow(cos(phi1) - cos(phi11b.Radian()), 2);
519  if (errb < erra)
520  {
521  phi2 *= -1;
522  }
523  }
524 
525  // I determined these arguments using trial and error
526  Quaternion<T> result = Quaternion<T>(-phi1, -phi2, -phi3).Inverse();
527 
528  // Previous equations assume repeated moments are at the beginning
529  // of the moments vector (moments[0] == moments[1]).
530  // We have the vectors sorted by size, so it's possible that the
531  // repeated moments are at the end (moments[1] == moments[2]).
532  // In this case (unequalMoment == 0), we apply an extra
533  // rotation that exchanges moment[0] and moment[2]
534  // Rotation matrix = [ 0 0 1]
535  // [ 0 1 0]
536  // [-1 0 0]
537  // That is equivalent to a 90 degree pitch
538  if (unequalMoment == 0)
539  result *= Quaternion<T>(0, IGN_PI_2, 0);
540 
541  return result;
542  }
543 
544  // No repeated principal moments
545  // eq 5.1:
546  T v = (std::pow(this->Ixyxzyz[0], 2) + std::pow(this->Ixyxzyz[1], 2)
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]));
550  // value of w depends on v
551  T w;
552  if (v < std::abs(tol))
553  {
554  // first sentence after eq 5.4:
555  // "In the case that v = 0, then w = 1."
556  w = 1;
557  }
558  else
559  {
560  // eq 5.2:
561  w = (this->Ixxyyzz[0] - moments[2] + (moments[2] - moments[1])*v)
562  / ((moments[0] - moments[1]) * v);
563  }
564  // initialize values of angle phi1, phi2, phi3
565  T phi1 = 0;
566  // eq 5.3: start with positive value
567  T phi2 = acos(clamp<T>(ClampedSqrt(v), -1, 1));
568  // eq 5.4: start with positive value
569  T phi3 = acos(clamp<T>(ClampedSqrt(w), -1, 1));
570 
571  // compute g1, g2 for phi2,phi3 >= 0
572  // equations 5.7, 5.8
573  Vector2<T> g1(
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));
576  Vector2<T> g2(
577  (moments[0]-moments[1])*(1 + (v-2)*w) + (moments[1]-moments[2])*v,
578  (moments[0]-moments[1])*sin(phi2)*sin(2*phi3));
579 
580  // The paragraph prior to equation 5.16 describes how to choose
581  // the candidate value of phi1 based on the length
582  // of the f1 and f2 vectors.
583  // * The case of |f1| == |f2| == 0 implies a repeated moment,
584  // which should not be possible at this point in the code
585  // * When |f1| != 0 and |f2| != 0, then one should choose the
586  // value of phi2 so that phi11 = phi12
587  // * When |f1| == 0 and f2 != 0, then phi1 = phi12
588  // phi11 can be ignored, and either sign of phi2, phi3 can be used
589  // * When |f2| == 0 and f1 != 0, then phi1 = phi11
590  // phi12 can be ignored, and either sign of phi2, phi3 can be used
591  bool f1small = f1.SquaredLength() < std::pow(tol, 2);
592  bool f2small = f2.SquaredLength() < std::pow(tol, 2);
593  if (f1small && f2small)
594  {
595  // this should never happen
596  // f1small && f2small implies a repeated moment
597  // return invalid quaternion
599  return Quaternion<T>::Zero;
600  }
601  else if (f1small)
602  {
603  // use phi12 (equations 5.12, 5.14)
604  math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
605  phi12.Normalize();
606  phi1 = phi12.Radian();
607  }
608  else if (f2small)
609  {
610  // use phi11 (equations 5.11, 5.13)
611  math::Angle phi11(Angle2(g1, tol) - Angle2(f1, tol));
612  phi11.Normalize();
613  phi1 = phi11.Radian();
614  }
615  else
616  {
617  // check for when phi11 == phi12
618  // eqs 5.11, 5.13:
619  math::Angle phi11(Angle2(g1, tol) - Angle2(f1, tol));
620  phi11.Normalize();
621  // eqs 5.12, 5.14:
622  math::Angle phi12(0.5*(Angle2(g2, tol) - Angle2(f2, tol)));
623  phi12.Normalize();
624  T err = std::pow(sin(phi11.Radian()) - sin(phi12.Radian()), 2)
625  + std::pow(cos(phi11.Radian()) - cos(phi12.Radian()), 2);
626  phi1 = phi11.Radian();
627  math::Vector2<T> signsPhi23(1, 1);
628  // case a: phi2 <= 0
629  {
630  Vector2<T> g1a = Vector2<T>(1, -1) * g1;
631  Vector2<T> g2a = Vector2<T>(1, -1) * g2;
632  math::Angle phi11a(Angle2(g1a, tol) - Angle2(f1, tol));
633  math::Angle phi12a(0.5*(Angle2(g2a, tol) - Angle2(f2, tol)));
634  phi11a.Normalize();
635  phi12a.Normalize();
636  T erra = std::pow(sin(phi11a.Radian()) - sin(phi12a.Radian()), 2)
637  + std::pow(cos(phi11a.Radian()) - cos(phi12a.Radian()), 2);
638  if (erra < err)
639  {
640  err = erra;
641  phi1 = phi11a.Radian();
642  signsPhi23.Set(-1, 1);
643  }
644  }
645  // case b: phi3 <= 0
646  {
647  Vector2<T> g1b = Vector2<T>(-1, 1) * g1;
648  Vector2<T> g2b = Vector2<T>(1, -1) * g2;
649  math::Angle phi11b(Angle2(g1b, tol) - Angle2(f1, tol));
650  math::Angle phi12b(0.5*(Angle2(g2b, tol) - Angle2(f2, tol)));
651  phi11b.Normalize();
652  phi12b.Normalize();
653  T errb = std::pow(sin(phi11b.Radian()) - sin(phi12b.Radian()), 2)
654  + std::pow(cos(phi11b.Radian()) - cos(phi12b.Radian()), 2);
655  if (errb < err)
656  {
657  err = errb;
658  phi1 = phi11b.Radian();
659  signsPhi23.Set(1, -1);
660  }
661  }
662  // case c: phi2,phi3 <= 0
663  {
664  Vector2<T> g1c = Vector2<T>(-1, -1) * g1;
665  Vector2<T> g2c = g2;
666  math::Angle phi11c(Angle2(g1c, tol) - Angle2(f1, tol));
667  math::Angle phi12c(0.5*(Angle2(g2c, tol) - Angle2(f2, tol)));
668  phi11c.Normalize();
669  phi12c.Normalize();
670  T errc = std::pow(sin(phi11c.Radian()) - sin(phi12c.Radian()), 2)
671  + std::pow(cos(phi11c.Radian()) - cos(phi12c.Radian()), 2);
672  if (errc < err)
673  {
674  err = errc;
675  phi1 = phi11c.Radian();
676  signsPhi23.Set(-1, -1);
677  }
678  }
679 
680  // apply sign changes
681  phi2 *= signsPhi23[0];
682  phi3 *= signsPhi23[1];
683  }
684 
685  // I determined these arguments using trial and error
686  return Quaternion<T>(-phi1, -phi2, -phi3).Inverse();
687  }
688 
698  public: bool EquivalentBox(Vector3<T> &_size,
699  Quaternion<T> &_rot,
700  const T _tol = 1e-6) const
701  {
702  if (!this->IsPositive())
703  {
704  // inertia is not positive, cannot compute equivalent box
705  return false;
706  }
707 
708  Vector3<T> moments = this->PrincipalMoments(_tol);
709  if (!ValidMoments(moments))
710  {
711  // principal moments don't satisfy the triangle identity
712  return false;
713  }
714 
715  // The reason for checking that the principal moments satisfy
716  // the triangle inequality
717  // I1 + I2 - I3 >= 0
718  // is to ensure that the arguments to sqrt in these equations
719  // are positive and the box size is real.
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));
723 
724  _rot = this->PrincipalAxesOffset(_tol);
725 
726  if (_rot == Quaternion<T>::Zero)
727  {
728  // _rot is an invalid quaternion
730  return false;
731  }
732 
733  return true;
734  }
735 
741  public: bool SetFromBox(const T _mass,
742  const Vector3<T> &_size,
744  {
745  // Check that _mass and _size are strictly positive
746  // and that quatenion is valid
747  if (_mass <= 0 || _size.Min() <= 0 || _rot == Quaternion<T>::Zero)
748  {
749  return false;
750  }
751  this->Mass(_mass);
752  return this->SetFromBox(_size, _rot);
753  }
754 
760  public: bool SetFromBox(const Vector3<T> &_size,
762  {
763  // Check that _mass and _size are strictly positive
764  // and that quatenion is valid
765  if (this->Mass() <= 0 || _size.Min() <= 0 ||
766  _rot == Quaternion<T>::Zero)
767  {
768  return false;
769  }
770 
771  // Diagonal matrix L with principal moments
772  Matrix3<T> L;
773  T x2 = std::pow(_size.X(), 2);
774  T y2 = std::pow(_size.Y(), 2);
775  T z2 = std::pow(_size.Z(), 2);
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);
779  Matrix3<T> R(_rot);
780  return this->MOI(R * L * R.Transposed());
781  }
782 
790  public: bool SetFromCylinderZ(const T _mass,
791  const T _length,
792  const T _radius,
794  {
795  // Check that _mass, _radius and _length are strictly positive
796  // and that quatenion is valid
797  if (_mass <= 0 || _length <= 0 || _radius <= 0 ||
798  _rot == Quaternion<T>::Zero)
799  {
800  return false;
801  }
802  this->Mass(_mass);
803  return this->SetFromCylinderZ(_length, _radius, _rot);
804  }
805 
812  public: bool SetFromCylinderZ(const T _length,
813  const T _radius,
814  const Quaternion<T> &_rot)
815  {
816  // Check that _mass and _size are strictly positive
817  // and that quatenion is valid
818  if (this->Mass() <= 0 || _length <= 0 || _radius <= 0 ||
819  _rot == Quaternion<T>::Zero)
820  {
821  return false;
822  }
823 
824  // Diagonal matrix L with principal moments
825  T radius2 = std::pow(_radius, 2);
826  Matrix3<T> L;
827  L(0, 0) = this->mass / 12.0 * (3*radius2 + std::pow(_length, 2));
828  L(1, 1) = L(0, 0);
829  L(2, 2) = this->mass / 2.0 * radius2;
830  Matrix3<T> R(_rot);
831  return this->MOI(R * L * R.Transposed());
832  }
833 
838  public: bool SetFromSphere(const T _mass, const T _radius)
839  {
840  // Check that _mass and _radius are strictly positive
841  if (_mass <= 0 || _radius <= 0)
842  {
843  return false;
844  }
845  this->Mass(_mass);
846  return this->SetFromSphere(_radius);
847  }
848 
853  public: bool SetFromSphere(const T _radius)
854  {
855  // Check that _mass and _radius are strictly positive
856  if (this->Mass() <= 0 || _radius <= 0)
857  {
858  return false;
859  }
860 
861  // Diagonal matrix L with principal moments
862  T radius2 = std::pow(_radius, 2);
863  Matrix3<T> L;
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;
867  return this->MOI(L);
868  }
869 
873  private: static inline T ClampedSqrt(const T &_x)
874  {
875  if (_x <= 0)
876  return 0;
877  return sqrt(_x);
878  }
879 
885  private: static T Angle2(const Vector2<T> &_v, const T _eps = 1e-6)
886  {
887  if (_v.SquaredLength() < std::pow(_eps, 2))
888  return 0;
889  return atan2(_v[1], _v[0]);
890  }
891 
893  private: T mass;
894 
898  private: Vector3<T> Ixxyyzz;
899 
903  private: Vector3<T> Ixyxzyz;
904  };
905 
908  }
909  }
910 }
911 #endif
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&#39;s commonly used to ke...
Definition: Vector3.hh:40
MassMatrix3(const MassMatrix3< T > &_m)
Copy constructor.
Definition: MassMatrix3.hh:61
T pow(T... args)
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&#39;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
Definition: Angle.hh:39
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