march_cube.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2021 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "march_cube.h"
  9. // Something bad is happening when I made this a function. Maybe
  10. // something is not inlining? It ends up 1.25× slower than if the code is pasted
  11. // into the respective functions in igl::marching_cubes
  12. //
  13. // Even if I make it a lambda with no arguments (all capture by reference [&])
  14. // and call it immediately I get a 1.25× slow-down.
  15. //
  16. // Maybe keeping it out of a function allows the compiler to optimize with the
  17. // loop? But then I guess that measn this function is not getting inlined? Or
  18. // that it's not getting optimized after inlining?
  19. //
  20. template <
  21. typename DerivedGV,
  22. typename Scalar,
  23. typename Index,
  24. typename DerivedV,
  25. typename DerivedF>
  26. IGL_INLINE void igl::march_cube(
  27. const DerivedGV & GV,
  28. const Eigen::Matrix<Scalar,8,1> & cS,
  29. const Eigen::Matrix<Index,8,1> & cI,
  30. const Scalar & isovalue,
  31. Eigen::PlainObjectBase<DerivedV> &V,
  32. Index & n,
  33. Eigen::PlainObjectBase<DerivedF> &F,
  34. Index & m,
  35. std::unordered_map<int64_t,int> & E2V)
  36. {
  37. // These consts get stored reasonably
  38. #include "marching_cubes_tables.h"
  39. // Seems this is also successfully inlined
  40. const auto ij2vertex =
  41. [&E2V,&V,&n,&GV]
  42. (const Index & i, const Index & j, const Scalar & t)->Index
  43. {
  44. // Seems this is successfully inlined.
  45. const auto ij2key = [](int32_t i,int32_t j)
  46. {
  47. if(i>j){ std::swap(i,j); }
  48. std::int64_t ret = 0;
  49. ret |= i;
  50. ret |= static_cast<std::int64_t>(j) << 32;
  51. return ret;
  52. };
  53. const auto key = ij2key(i,j);
  54. const auto it = E2V.find(key);
  55. int v = -1;
  56. if(it == E2V.end())
  57. {
  58. // new vertex
  59. if(n==V.rows()){ V.conservativeResize(V.rows()*2+1,V.cols()); }
  60. V.row(n) = GV.row(i) + t*(GV.row(j) - GV.row(i));
  61. v = n;
  62. E2V[key] = v;
  63. n++;
  64. }else
  65. {
  66. v = it->second;
  67. }
  68. return v;
  69. };
  70. int c_flags = 0;
  71. for(int c = 0; c < 8; c++)
  72. {
  73. if(cS(c) > isovalue){ c_flags |= 1<<c; }
  74. }
  75. //Find which edges are intersected by the surface
  76. int e_flags = aiCubeEdgeFlags[c_flags];
  77. //If the cube is entirely inside or outside of the surface, then there will be no intersections
  78. if(e_flags == 0) { return; }
  79. //Find the point of intersection of the surface with each edge
  80. //Then find the normal to the surface at those points
  81. Eigen::Matrix<Index,12,1> edge_vertices;
  82. for(int e = 0; e < 12; e++)
  83. {
  84. #ifndef NDEBUG
  85. edge_vertices[e] = -1;
  86. #endif
  87. //if there is an intersection on this edge
  88. if(e_flags & (1<<e))
  89. {
  90. // find crossing point assuming linear interpolation along edges
  91. const Scalar & a = cS(a2eConnection[e][0]);
  92. const Scalar & b = cS(a2eConnection[e][1]);
  93. Scalar t;
  94. {
  95. const Scalar delta = b-a;
  96. if(delta == 0) { t = 0.5; }
  97. t = (isovalue - a)/delta;
  98. };
  99. // record global index into local table
  100. edge_vertices[e] =
  101. ij2vertex(cI(a2eConnection[e][0]),cI(a2eConnection[e][1]),t);
  102. assert(edge_vertices[e] >= 0);
  103. assert(edge_vertices[e] < n);
  104. }
  105. }
  106. // Insert the triangles that were found. There can be up to five per cube
  107. for(int f = 0; f < 5; f++)
  108. {
  109. if(a2fConnectionTable[c_flags][3*f] < 0) break;
  110. if(m==F.rows()){ F.conservativeResize(F.rows()*2+1,F.cols()); }
  111. assert(edge_vertices[a2fConnectionTable[c_flags][3*f+0]]>=0);
  112. assert(edge_vertices[a2fConnectionTable[c_flags][3*f+1]]>=0);
  113. assert(edge_vertices[a2fConnectionTable[c_flags][3*f+2]]>=0);
  114. F.row(m) <<
  115. edge_vertices[a2fConnectionTable[c_flags][3*f+0]],
  116. edge_vertices[a2fConnectionTable[c_flags][3*f+1]],
  117. edge_vertices[a2fConnectionTable[c_flags][3*f+2]];
  118. m++;
  119. }
  120. }
  121. #ifdef IGL_STATIC_LIBRARY
  122. // Explicit template instantiation
  123. template void igl::march_cube<Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >, double, long, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, 8, 1, 0, 8, 1> const&, Eigen::Matrix<long, 8, 1, 0, 8, 1> const&, double const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, long&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, long&, std::unordered_map<long long, int, std::hash<long long>, std::equal_to<long long>, std::allocator<std::pair<long long const, int> > >&);
  124. template void igl::march_cube<Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >, double, unsigned int, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, 8, 1, 0, 8, 1> const&, Eigen::Matrix<unsigned int, 8, 1, 0, 8, 1> const&, double const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, unsigned int&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, unsigned int&, std::unordered_map<long long, int, std::hash<long long>, std::equal_to<long long>, std::allocator<std::pair<long long const, int> > >&);
  125. #endif