Skip to content

Commit

Permalink
Sparse grid bfs (libigl#942)
Browse files Browse the repository at this point in the history
* 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: 1dbd225
  • Loading branch information
fwilliams authored and alecjacobson committed Oct 15, 2018
1 parent ae3df06 commit b2e8d0b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
28 changes: 18 additions & 10 deletions include/igl/sparse_voxel_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions include/igl/sparse_voxel_grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tutorial/715_MeshImplicitFunction/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b2e8d0b

Please sign in to comment.