소스 검색

fix moments to work with more input types

Alec Jacobson 2 년 전
부모
커밋
d326251896
2개의 변경된 파일36개의 추가작업 그리고 1개의 파일을 삭제
  1. 35 0
      include/igl/moments.cpp
  2. 1 1
      include/igl/moments.h

+ 35 - 0
include/igl/moments.cpp

@@ -8,6 +8,38 @@
 
 
 #include "moments.h"
 #include "moments.h"
 
 
+// C++17 would avoid this with an if constexpr below
+//
+// This makes it so that m1 can be:
+//   - RowVector3d
+//   - Vector3d
+//   - RowVectorXd
+//   - VectorXd
+namespace igl
+{
+  template <bool SingleRow, bool SingleCol> struct moments_resize_3;
+  template <> struct moments_resize_3<true,false>
+  {
+    template <typename Derivedm1>
+    static void run(Eigen::PlainObjectBase<Derivedm1>& m1)
+    {
+      static_assert(Derivedm1::ColsAtCompileTime == Eigen::Dynamic || 
+          Derivedm1::ColsAtCompileTime == 3,"#cols must be 3 or dynamic");
+      m1.resize(1,3);
+    }
+  };
+  template <> struct moments_resize_3<false,true>
+  {
+    template <typename Derivedm1>
+    static void run(Eigen::PlainObjectBase<Derivedm1>& m1)
+    {
+      static_assert(Derivedm1::RowsAtCompileTime == Eigen::Dynamic || 
+          Derivedm1::RowsAtCompileTime == 3,"#rows must be 3 or dynamic");
+      m1.resize(3,1);
+    }
+  };
+}
+
 template <
 template <
   typename DerivedV, 
   typename DerivedV, 
   typename DerivedF, 
   typename DerivedF, 
@@ -21,9 +53,12 @@ IGL_INLINE void igl::moments(
   Eigen::PlainObjectBase<Derivedm1>& m1,
   Eigen::PlainObjectBase<Derivedm1>& m1,
   Eigen::PlainObjectBase<Derivedm2>& m2)
   Eigen::PlainObjectBase<Derivedm2>& m2)
 {
 {
+  assert(V.cols() == 3 && "V should be #V by 3");
   typedef typename Derivedm2::Scalar Scalar;
   typedef typename Derivedm2::Scalar Scalar;
 
 
   m0 = 0;
   m0 = 0;
+  moments_resize_3<Derivedm1::RowsAtCompileTime == 1,Derivedm1::ColsAtCompileTime == 1>::run(m1);
+
   m1 << 0,0,0;
   m1 << 0,0,0;
   Scalar _xx=0;
   Scalar _xx=0;
   Scalar _yy=0;
   Scalar _yy=0;

+ 1 - 1
include/igl/moments.h

@@ -13,7 +13,7 @@ namespace igl
 {
 {
   /// Computes the moments of mass for a solid object bound by a triangle mesh.
   /// Computes the moments of mass for a solid object bound by a triangle mesh.
   /// 
   /// 
-  /// @param[in] V  #V by dim list of rest domain positions
+  /// @param[in] V  #V by 3 list of rest domain positions
   /// @param[in] F  #F by 3 list of triangle indices into V
   /// @param[in] F  #F by 3 list of triangle indices into V
   /// @param[out] m0  zeroth moment of mass, total signed volume of solid.
   /// @param[out] m0  zeroth moment of mass, total signed volume of solid.
   /// @param[out] m1  first moment of mass, center of mass (centroid) times total mass
   /// @param[out] m1  first moment of mass, center of mass (centroid) times total mass