فهرست منبع

Sparse grid bfs (#942)

* Add a utility function which computes a sparse set of epsilon-sized grid cells surrounding an input surface. The function takes a point on the surface as input and finds all those epsilon grid cells that intersect the surface using a breadth first search over the grid space.

* Add a utility function which computes a sparse set of epsilon-sized grid cells surrounding an input surface. The function takes a point on the surface as input and finds all those epsilon grid cells that intersect the surface using a breadth first search over the grid space.


Former-commit-id: 1dbd225d993a5417c7d3bedc3a6ba354cc256956
Francis Williams 6 سال پیش
والد
کامیت
b2e8d0b3d3
3فایلهای تغییر یافته به همراه23 افزوده شده و 15 حذف شده
  1. 18 10
      include/igl/sparse_voxel_grid.cpp
  2. 4 4
      include/igl/sparse_voxel_grid.h
  3. 1 1
      tutorial/715_MeshImplicitFunction/main.cpp

+ 18 - 10
include/igl/sparse_voxel_grid.cpp

@@ -5,14 +5,14 @@
 #include <vector>
 
 
-template <typename DerivedS, typename DerivedP0, typename DerivedV, typename DerivedI>
+template <typename DerivedP0, typename Func, typename DerivedS, typename DerivedV, typename DerivedI>
 IGL_INLINE void igl::sparse_voxel_grid(const Eigen::MatrixBase<DerivedP0>& p0,
-                                       const std::function<typename DerivedS::Scalar(const DerivedP0&)>& scalarFunc,
+                                       const Func& scalarFunc,
                                        const double eps,
+                                       const int expected_number_of_cubes,
                                        Eigen::PlainObjectBase<DerivedS>& CS,
                                        Eigen::PlainObjectBase<DerivedV>& CV,
-                                       Eigen::PlainObjectBase<DerivedI>& CI,
-                                       int expected_number_of_cubes)
+                                       Eigen::PlainObjectBase<DerivedI>& CI)
 {
   typedef typename DerivedV::Scalar ScalarV;
   typedef typename DerivedS::Scalar ScalarS;
@@ -38,16 +38,23 @@ IGL_INLINE void igl::sparse_voxel_grid(const Eigen::MatrixBase<DerivedP0>& p0,
 
   ScalarV half_eps = 0.5 * eps;
 
-  std::vector<IndexRowVector> CI_vector(expected_number_of_cubes);
-  std::vector<VertexRowVector> CV_vector(8*expected_number_of_cubes);
-  std::vector<ScalarS> CS_vector(8*expected_number_of_cubes);
+  std::vector<IndexRowVector> CI_vector;
+  std::vector<VertexRowVector> CV_vector;
+  std::vector<ScalarS> CS_vector;
+  CI_vector.reserve(expected_number_of_cubes);
+  CV_vector.reserve(8 * expected_number_of_cubes);
+  CS_vector.reserve(8 * expected_number_of_cubes);
 
   // Track visisted neighbors
-  std::unordered_map<Eigen::RowVector3i, int, IndexRowVectorHash> visited(6*expected_number_of_cubes);
+  std::unordered_map<Eigen::RowVector3i, int, IndexRowVectorHash> visited;
+  visited.reserve(6 * expected_number_of_cubes);
+  visited.max_load_factor(0.5);
 
   // BFS Queue
-  std::vector<Eigen::RowVector3i> queue(expected_number_of_cubes*8);
+  std::vector<Eigen::RowVector3i> queue;
+  queue.reserve(expected_number_of_cubes * 8);
   queue.push_back(Eigen::RowVector3i(0, 0, 0));
+
   while (queue.size() > 0)
   {
     Eigen::RowVector3i pi = queue.back();
@@ -136,5 +143,6 @@ IGL_INLINE void igl::sparse_voxel_grid(const Eigen::MatrixBase<DerivedP0>& p0,
 
 
 #ifdef IGL_STATIC_LIBRARY
-template void igl::sparse_voxel_grid<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, std::function<Eigen::Matrix<double, -1, 1, 0, -1, 1>::Scalar (Eigen::Matrix<double, 1, 3, 1, 1, 3> const&)> const&, double, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, int);
+template void igl::sparse_voxel_grid<class Eigen::Matrix<double, -1, -1, 0, -1, -1>, class std::function<double(class Eigen::Matrix<double, -1, -1, 0, -1, -1> const &)>, class Eigen::Matrix<double, -1, 1, 0, -1, 1>, class Eigen::Matrix<double, -1, -1, 0, -1, -1>, class Eigen::Matrix<int, -1, -1, 0, -1, -1> >(class Eigen::MatrixBase<class Eigen::Matrix<double, -1, -1, 0, -1, -1> > const &, class std::function<double(class Eigen::Matrix<double, -1, -1, 0, -1, -1> const &)> const &, double, int, class Eigen::PlainObjectBase<class Eigen::Matrix<double, -1, 1, 0, -1, 1> > &, class Eigen::PlainObjectBase<class Eigen::Matrix<double, -1, -1, 0, -1, -1> > &, class Eigen::PlainObjectBase<class Eigen::Matrix<int, -1, -1, 0, -1, -1> > &);
+template void igl::sparse_voxel_grid<Eigen::Matrix<double, 1, 3, 1, 1, 3>, std::function<double (Eigen::Matrix<double, 1, 3, 1, 1, 3> const&)>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, std::function<double (Eigen::Matrix<double, 1, 3, 1, 1, 3> const&)> const&, double, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
 #endif

+ 4 - 4
include/igl/sparse_voxel_grid.h

@@ -31,14 +31,14 @@ namespace igl {
   //   CV  #cube-vertices by 3 list of cube vertex positions
   //   CI  #number of cubes by 8 list of indexes into CS and CV. Each row represents a cube
   //
-  template <typename DerivedS, typename DerivedP0, typename DerivedV, typename DerivedI>
+  template <typename DerivedP0, typename Func, typename DerivedS, typename DerivedV, typename DerivedI>
   IGL_INLINE void sparse_voxel_grid(const Eigen::MatrixBase<DerivedP0>& p0,
-                                    const std::function<typename DerivedS::Scalar(const DerivedP0&)>& scalarFunc,
+                                    const Func& scalarFunc,
                                     const double eps,
+                                    const int expected_number_of_cubes,
                                     Eigen::PlainObjectBase<DerivedS>& CS,
                                     Eigen::PlainObjectBase<DerivedV>& CV,
-                                    Eigen::PlainObjectBase<DerivedI>& CI,
-                                    int expected_number_of_cubes=1024);
+                                    Eigen::PlainObjectBase<DerivedI>& CI);
 
 }
 #ifndef IGL_STATIC_LIBRARY

+ 1 - 1
tutorial/715_MeshImplicitFunction/main.cpp

@@ -34,7 +34,7 @@ int main(int argc, char * argv[])
   Eigen::MatrixXi CI;
 
   // Construct the voxel grid, populating CS, CV, and CI
-  igl::sparse_voxel_grid(p0, scalar_func, eps, CS, CV, CI);
+  igl::sparse_voxel_grid(p0, scalar_func, eps, 1024 /*expected_number_of_cubes*/, CS, CV, CI);
 
   // Given the sparse voxel grid, use Marching Cubes to construct a triangle mesh of the surface
   Eigen::MatrixXi F;