moments.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2022 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "moments.h"
  9. // C++17 would avoid this with an if constexpr below
  10. //
  11. // This makes it so that m1 can be:
  12. // - RowVector3d
  13. // - Vector3d
  14. // - RowVectorXd
  15. // - Eigen::VectorXd
  16. namespace igl
  17. {
  18. template <bool SingleRow, bool SingleCol> struct moments_resize_3;
  19. template <> struct moments_resize_3<true,false>
  20. {
  21. template <typename Derivedm1>
  22. static void run(Eigen::PlainObjectBase<Derivedm1>& m1)
  23. {
  24. static_assert(Derivedm1::ColsAtCompileTime == Eigen::Dynamic ||
  25. Derivedm1::ColsAtCompileTime == 3,"#cols must be 3 or dynamic");
  26. m1.resize(1,3);
  27. }
  28. };
  29. template <> struct moments_resize_3<false,true>
  30. {
  31. template <typename Derivedm1>
  32. static void run(Eigen::PlainObjectBase<Derivedm1>& m1)
  33. {
  34. static_assert(Derivedm1::RowsAtCompileTime == Eigen::Dynamic ||
  35. Derivedm1::RowsAtCompileTime == 3,"#rows must be 3 or dynamic");
  36. m1.resize(3,1);
  37. }
  38. };
  39. }
  40. template <
  41. typename DerivedV,
  42. typename DerivedF,
  43. typename Derivedm0,
  44. typename Derivedm1,
  45. typename Derivedm2>
  46. IGL_INLINE void igl::moments(
  47. const Eigen::MatrixBase<DerivedV>& V,
  48. const Eigen::MatrixBase<DerivedF>& F,
  49. Derivedm0 & m0,
  50. Eigen::PlainObjectBase<Derivedm1>& m1,
  51. Eigen::PlainObjectBase<Derivedm2>& m2)
  52. {
  53. assert(V.cols() == 3 && "V should be #V by 3");
  54. typedef typename Derivedm2::Scalar Scalar;
  55. m0 = 0;
  56. moments_resize_3<Derivedm1::RowsAtCompileTime == 1,Derivedm1::ColsAtCompileTime == 1>::run(m1);
  57. m1 << 0,0,0;
  58. Scalar _xx=0;
  59. Scalar _yy=0;
  60. Scalar _zz=0;
  61. Scalar _yx=0;
  62. Scalar _zx=0;
  63. Scalar _zy=0;
  64. for(int f = 0;f<F.rows();f++)
  65. {
  66. // "Computing the Moment of Inertia of a Solid Defined by a Triangle Mesh"
  67. // (The attached code has a sign bug in I, fixed below.)
  68. const Scalar x1 = V(F(f,0),0);
  69. const Scalar y1 = V(F(f,0),1);
  70. const Scalar z1 = V(F(f,0),2);
  71. const Scalar x2 = V(F(f,1),0);
  72. const Scalar y2 = V(F(f,1),1);
  73. const Scalar z2 = V(F(f,1),2);
  74. const Scalar x3 = V(F(f,2),0);
  75. const Scalar y3 = V(F(f,2),1);
  76. const Scalar z3 = V(F(f,2),2);
  77. // Signed volume
  78. const Scalar v =
  79. x1*y2*z3 + y1*z2*x3 + x2*y3*z1 - (x3*y2*z1 + x2*y1*z3 + y3*z2*x1);
  80. // Contribution to the mass
  81. m0 += v;
  82. // Contribution to the centroid
  83. const Scalar x4 = x1 + x2 + x3;
  84. const Scalar y4 = y1 + y2 + y3;
  85. const Scalar z4 = z1 + z2 + z3;
  86. m1(0) += (v * x4);
  87. m1(1) += (v * y4);
  88. m1(2) += (v * z4);
  89. // Contribution to moment of inertia monomials
  90. _xx += v * (x1*x1 + x2*x2 + x3*x3 + x4*x4);
  91. _yy += v * (y1*y1 + y2*y2 + y3*y3 + y4*y4);
  92. _zz += v * (z1*z1 + z2*z2 + z3*z3 + z4*z4);
  93. _yx += v * (y1*x1 + y2*x2 + y3*x3 + y4*x4);
  94. _zx += v * (z1*x1 + z2*x2 + z3*x3 + z4*x4);
  95. _zy += v * (z1*y1 + z2*y2 + z3*y3 + z4*y4);
  96. }
  97. m0 /= 6.0;
  98. m1 /= 24.0;
  99. const double r = 1.0/120.0;
  100. m2.setZero(3,3);
  101. m2(1,0) = m1(1)*m1(0)/m0 - _yx * r;
  102. m2(2,0) = m1(2)*m1(0)/m0 - _zx * r;
  103. m2(2,1) = m1(2)*m1(1)/m0 - _zy * r;
  104. m2(0,1) = m2(1,0);
  105. m2(0,2) = m2(2,0);
  106. m2(1,2) = m2(2,1);
  107. _xx = _xx * r - m1(0)*m1(0)/m0;
  108. _yy = _yy * r - m1(1)*m1(1)/m0;
  109. _zz = _zz * r - m1(2)*m1(2)/m0;
  110. m2(0,0) = _yy + _zz;
  111. m2(1,1) = _zz + _xx;
  112. m2(2,2) = _xx + _yy;
  113. }
  114. #ifdef IGL_STATIC_LIBRARY
  115. // Explicit template instantiation
  116. template void igl::moments<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&);
  117. template void igl::moments<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double, Eigen::Matrix<double, 1, 3, 0, 3, 1>, Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 3, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&);
  118. template void igl::moments<Eigen::Matrix<double, -1, 3, 0, -1, -1>, Eigen::Matrix<int, -1, 3, 0, -1, -1>, double, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, -1> > const&, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&);
  119. template void igl::moments<Eigen::Matrix<double, -1, 3, 0, -1, -1>, Eigen::Matrix<int, -1, 3, 0, -1, -1>, double, Eigen::Matrix<double, 1, 3, 0, 3, 1>, Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, -1> > const&, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 3, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&);
  120. template void igl::moments<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 1, -1, -1>, double, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 1, -1, -1> > const&, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&);
  121. template void igl::moments<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 1, -1, -1>, double, Eigen::Matrix<double, 1, 3, 0, 3, 1>, Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 1, -1, -1> > const&, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 3, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&);
  122. template void igl::moments<Eigen::Matrix<double, -1, 3, 1, -1, -1>, Eigen::Matrix<int, -1, 3, 1, -1, -1>, double, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, -1> > const&, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&);
  123. template void igl::moments<Eigen::Matrix<double, -1, 3, 1, -1, -1>, Eigen::Matrix<int, -1, 3, 1, -1, -1>, double, Eigen::Matrix<double, 1, 3, 0, 3, 1>, Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, -1> > const&, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 3, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> >&);
  124. #endif