Browse Source

better overloads

Former-commit-id: 194d7e4e7611bb2496901d2005c3aefadbbc6f98
Alec Jacobson 10 years ago
parent
commit
ef69b90899
2 changed files with 82 additions and 23 deletions
  1. 43 18
      include/igl/remove_unreferenced.cpp
  2. 39 5
      include/igl/remove_unreferenced.h

+ 43 - 18
include/igl/remove_unreferenced.cpp

@@ -20,24 +20,61 @@ IGL_INLINE void igl::remove_unreferenced(
   Eigen::PlainObjectBase<DerivedNF> &NF,
   Eigen::PlainObjectBase<DerivedI> &I)
 {
+  Eigen::Matrix<typename DerivedI::Scalar,Eigen::Dynamic,1> J;
+  remove_unreferenced(V,F,NV,NF,I,J);
+}
 
-  // Mark referenced vertices
-  Eigen::MatrixXi mark = Eigen::MatrixXi::Zero(V.rows(),1);
+template <
+  typename DerivedV,
+  typename DerivedF,
+  typename DerivedNV,
+  typename DerivedNF,
+  typename DerivedI,
+  typename DerivedJ>
+IGL_INLINE void igl::remove_unreferenced(
+  const Eigen::PlainObjectBase<DerivedV> &V,
+  const Eigen::PlainObjectBase<DerivedF> &F,
+  Eigen::PlainObjectBase<DerivedNV> &NV,
+  Eigen::PlainObjectBase<DerivedNF> &NF,
+  Eigen::PlainObjectBase<DerivedI> &I,
+  Eigen::PlainObjectBase<DerivedJ> &J)
+{
+  const size_t n = V.rows();
+  remove_unreferenced(n,F,I,J);
+  NF = F;
+  for_each(NF.data(),NF.data()+NF.size(),[&I](int & a){a=I(a);});
+  slice(V,J,1,NV);
+}
 
+template <
+  typename DerivedF,
+  typename DerivedI,
+  typename DerivedJ>
+IGL_INLINE void igl::remove_unreferenced(
+  const size_t n,
+  const Eigen::PlainObjectBase<DerivedF> &F,
+  Eigen::PlainObjectBase<DerivedI> &I,
+  Eigen::PlainObjectBase<DerivedJ> &J)
+{
+  // Mark referenced vertices
+  typedef Eigen::Matrix<bool,Eigen::Dynamic,1> MatrixXb;
+  MatrixXb mark = MatrixXb::Zero(n,1);
   for(int i=0; i<F.rows(); ++i)
   {
     for(int j=0; j<F.cols(); ++j)
     {
       if (F(i,j) != -1)
+      {
         mark(F(i,j)) = 1;
+      }
     }
   }
 
   // Sum the occupied cells
-  int newsize = mark.sum();
+  int newsize = mark.count();
 
-  NV.resize(newsize,V.cols());
-  I.resize(V.rows(),1);
+  I.resize(n,1);
+  J.resize(newsize,1);
 
   // Do a pass on the marked vector and remove the unreferenced vertices
   int count = 0;
@@ -45,8 +82,8 @@ IGL_INLINE void igl::remove_unreferenced(
   {
     if (mark(i) == 1)
     {
-      NV.row(count) = V.row(i);
       I(i) = count;
+      J(count) = i;
       count++;
     }
     else
@@ -54,18 +91,6 @@ IGL_INLINE void igl::remove_unreferenced(
       I(i) = -1;
     }
   }
-
-  NF.resize(F.rows(),F.cols());
-
-  // Apply I on F
-  for (int i=0; i<F.rows(); ++i)
-  {
-    Eigen::RowVectorXi t(F.cols());
-    for (int j=0; j<F.cols(); ++j)
-      t(j) = I(F(i,j));
-
-    NF.row(i) = t;
-  }
 }
 
 #ifdef IGL_STATIC_LIBRARY

+ 39 - 5
include/igl/remove_unreferenced.h

@@ -18,14 +18,16 @@
 #include <Eigen/Core>
 namespace igl 
 {
-  // [ NV, NF ] = remove_unreferenced( V,F)
   // Remove unreferenced vertices from V, updating F accordingly
   //
   // Input:
-  // V,F: mesh description
-  //
-  // Output:
-  // NV, NF: new mesh without unreferenced vertices
+  //   V  #V by dim list of mesh vertex positions
+  //   F  #F by ss list of simplices (Values of -1 are quitely skipped)
+  // Outputs:
+  //   NV  #NV by dim list of mesh vertex positions
+  //   NF  #NF by ss list of simplices
+  //   IM  #V by 1 list of indices such that: NF = IM(F) and NT = IM(T)
+  //      and V(find(IM<=size(NV,1)),:) = NV
   //
   template <
     typename DerivedV,
@@ -39,6 +41,38 @@ namespace igl
     Eigen::PlainObjectBase<DerivedNV> &NV,
     Eigen::PlainObjectBase<DerivedNF> &NF,
     Eigen::PlainObjectBase<DerivedI> &I);
+  template <
+    typename DerivedV,
+    typename DerivedF,
+    typename DerivedNV,
+    typename DerivedNF,
+    typename DerivedI,
+    typename DerivedJ>
+  IGL_INLINE void remove_unreferenced(
+    const Eigen::PlainObjectBase<DerivedV> &V,
+    const Eigen::PlainObjectBase<DerivedF> &F,
+    Eigen::PlainObjectBase<DerivedNV> &NV,
+    Eigen::PlainObjectBase<DerivedNF> &NF,
+    Eigen::PlainObjectBase<DerivedI> &I,
+    Eigen::PlainObjectBase<DerivedJ> &J);
+  // Inputs:
+  //   n  number of vertices (possibly greater than F.maxCoeff()+1)
+  //   F  #F by ss list of simplices
+  // Outputs:
+  //   IM  #V by 1 list of indices such that: NF = IM(F) and NT = IM(T)
+  //      and V(find(IM<=size(NV,1)),:) = NV
+  //   J  #RV by 1 list, such that RV = V(J,:)
+  //   
+  template <
+    typename DerivedF,
+    typename DerivedI,
+    typename DerivedJ>
+  IGL_INLINE void remove_unreferenced(
+    const size_t n,
+    const Eigen::PlainObjectBase<DerivedF> &F,
+    Eigen::PlainObjectBase<DerivedI> &I,
+    Eigen::PlainObjectBase<DerivedJ> &J);
+
 }
 
 #ifndef IGL_STATIC_LIBRARY