Browse Source

centroid only worked for fixed size input (#2340)

* template for variable size and failing test

* fix

* static asserts
Alec Jacobson 1 year ago
parent
commit
8185a213d0
2 changed files with 44 additions and 1 deletions
  1. 11 1
      include/igl/centroid.cpp
  2. 33 0
      tests/include/igl/centroid.cpp

+ 11 - 1
include/igl/centroid.cpp

@@ -23,7 +23,16 @@ IGL_INLINE void igl::centroid(
   assert(F.cols() == 3 && "F should contain triangles.");
   assert(F.cols() == 3 && "F should contain triangles.");
   assert(V.cols() == 3 && "V should contain 3d points.");
   assert(V.cols() == 3 && "V should contain 3d points.");
   const int m = F.rows();
   const int m = F.rows();
-  cen.setZero();
+  // static assert that cen is either a 3d rowvector a 3d vector or a variable
+  // size vector
+  static_assert(Derivedc::IsVectorAtCompileTime,"cen should be a vector");
+  static_assert(
+      Derivedc::RowsAtCompileTime == 3 || 
+      Derivedc::RowsAtCompileTime == -1 || 
+      Derivedc::ColsAtCompileTime == 3 || 
+      Derivedc::ColsAtCompileTime == -1,
+      "cen should be sizeable to 3 vector");
+  cen.setZero(3);
   vol = 0;
   vol = 0;
   // loop over faces
   // loop over faces
   for(int f = 0;f<m;f++)
   for(int f = 0;f<m;f++)
@@ -70,4 +79,5 @@ template void igl::centroid<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix
 template void igl::centroid<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<float, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 1, 3, 1, 1, 3> >&);
 template void igl::centroid<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<float, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 1, 3, 1, 1, 3> >&);
 template void igl::centroid<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
 template void igl::centroid<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
 template void igl::centroid<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
 template void igl::centroid<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
+template void igl::centroid<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 1, -1, 1, 1, -1>>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1>> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1>> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1>>&);
 #endif
 #endif

+ 33 - 0
tests/include/igl/centroid.cpp

@@ -0,0 +1,33 @@
+#include <test_common.h>
+#include <igl/centroid.h>
+#include <iostream>
+
+TEST_CASE("centroid: ", "[igl]" )
+{
+  Eigen::MatrixXd V(4,3);
+  V<<0,0,0,
+    1,0,0,
+    0,1,0,
+    0,0,1;
+  Eigen::MatrixXi F(4,3);
+    F<< 
+    0,1,3,
+    0,2,1,
+    0,3,2,
+    1,2,3;
+  {
+    Eigen::RowVector3d c;
+    igl::centroid(V,F,c);
+    REQUIRE(c(0) == Approx(0.25));
+    REQUIRE(c(1) == Approx(0.25));
+    REQUIRE(c(2) == Approx(0.25));
+  }
+  {
+    Eigen::RowVectorXd c;
+    igl::centroid(V,F,c);
+    REQUIRE(c(0) == Approx(0.25));
+    REQUIRE(c(1) == Approx(0.25));
+    REQUIRE(c(2) == Approx(0.25));
+  }
+}
+