Bläddra i källkod

fixed bug in slice

Former-commit-id: 1345823aed13daad42673a3da7b1beefda53dccc
jalec 13 år sedan
förälder
incheckning
906a074123
1 ändrade filer med 18 tillägg och 46 borttagningar
  1. 18 46
      slice.h

+ 18 - 46
slice.h

@@ -36,6 +36,7 @@ namespace igl
 }
 }
 
 
 // Implementation
 // Implementation
+#include <vector>
 
 
 template <typename T>
 template <typename T>
 inline void igl::slice(
 inline void igl::slice(
@@ -52,32 +53,23 @@ inline void igl::slice(
   assert(R.maxCoeff() < xm);
   assert(R.maxCoeff() < xm);
   assert(C.minCoeff() >= 0);
   assert(C.minCoeff() >= 0);
   assert(C.maxCoeff() < xn);
   assert(C.maxCoeff() < xn);
+
   // Build reindexing maps for columns and rows, -1 means not in map
   // Build reindexing maps for columns and rows, -1 means not in map
-  Eigen::Matrix<int,Eigen::Dynamic,1> RI;
+  std::vector<std::vector<int> > RI;
   RI.resize(xm);
   RI.resize(xm);
-  // initialize to -1
-  for(int i = 0;i<xm;i++)
-  {
-    RI(i) = -1;
-  }
   for(int i = 0;i<ym;i++)
   for(int i = 0;i<ym;i++)
   {
   {
-    RI(R(i)) = i;
+    RI[R(i)].push_back(i);
   }
   }
-  Eigen::Matrix<int,Eigen::Dynamic,1> CI;
+  std::vector<std::vector<int> > CI;
   CI.resize(xn);
   CI.resize(xn);
   // initialize to -1
   // initialize to -1
-  for(int i = 0;i<xn;i++)
-  {
-    CI(i) = -1;
-  }
   for(int i = 0;i<yn;i++)
   for(int i = 0;i<yn;i++)
   {
   {
-    CI(C(i)) = i;
+    CI[C(i)].push_back(i);
   }
   }
   // Resize output
   // Resize output
-  Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> 
-    dyn_Y(ym,yn);
+  Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(ym,yn);
   // Take a guess at the number of nonzeros (this assumes uniform distribution
   // Take a guess at the number of nonzeros (this assumes uniform distribution
   // not banded or heavily diagonal)
   // not banded or heavily diagonal)
   dyn_Y.reserve((X.nonZeros()/(X.rows()*X.cols())) * (ym*yn));
   dyn_Y.reserve((X.nonZeros()/(X.rows()*X.cols())) * (ym*yn));
@@ -87,9 +79,13 @@ inline void igl::slice(
     // Iterate over inside
     // Iterate over inside
     for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
     for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
     {
     {
-      if(RI(it.row()) >= 0 && CI(it.col()) >= 0)
+      std::vector<int>::iterator rit, cit;
+      for(rit = RI[it.row()].begin();rit != RI[it.row()].end(); rit++)
       {
       {
-        dyn_Y.coeffRef(RI(it.row()),CI(it.col())) = it.value();
+        for(cit = CI[it.row()].begin();cit != CI[it.row()].end(); cit++)
+        {
+          dyn_Y.coeffRef(*rit,*cit) = it.value();
+        }
       }
       }
     }
     }
   }
   }
@@ -111,39 +107,15 @@ inline void igl::slice(
   assert(R.maxCoeff() < xm);
   assert(R.maxCoeff() < xm);
   assert(C.minCoeff() >= 0);
   assert(C.minCoeff() >= 0);
   assert(C.maxCoeff() < xn);
   assert(C.maxCoeff() < xn);
-  // Build reindexing maps for columns and rows, -1 means not in map
-  Eigen::Matrix<int,Eigen::Dynamic,1> RI;
-  RI.resize(xm);
-  // initialize to -1
-  for(int i = 0;i<xm;i++)
-  {
-    RI(i) = -1;
-  }
-  for(int i = 0;i<ym;i++)
-  {
-    RI(R(i)) = i;
-  }
-  Eigen::Matrix<int,Eigen::Dynamic,1> CI;
-  CI.resize(xn);
-  // initialize to -1
-  for(int i = 0;i<xn;i++)
-  {
-    CI(i) = -1;
-  }
-  for(int i = 0;i<yn;i++)
-  {
-    CI(C(i)) = i;
-  }
+
   // Resize output
   // Resize output
   Y.resize(ym,yn);
   Y.resize(ym,yn);
-  for(int i = 0;i<xm;i++)
+  // loop over output rows, then columns
+  for(int i = 0;i<ym;i++)
   {
   {
-    for(int j = 0;j<xn;j++)
+    for(int j = 0;j<yn;j++)
     {
     {
-      if(RI(i) >= 0 && CI(j) >= 0)
-      {
-        Y(RI(i),CI(j)) = X(i,j);
-      }
+      Y(i,j) = X(R(i),C(j));
     }
     }
   }
   }
 }
 }