lipschitz_octree_prune.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2025 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 "lipschitz_octree_prune.h"
  9. #include "unique_sparse_voxel_corners.h"
  10. #include "find.h"
  11. #include "matlab_format.h"
  12. #include "parallel_for.h"
  13. #include <cassert>
  14. #include <iostream>
  15. template <
  16. bool batched,
  17. typename Derivedorigin,
  18. typename Func,
  19. typename Derivedijk,
  20. typename Derivedijk_maybe
  21. >
  22. IGL_INLINE void igl::lipschitz_octree_prune(
  23. const Eigen::MatrixBase<Derivedorigin> & origin,
  24. const typename Derivedorigin::Scalar h0,
  25. const int depth,
  26. const Func & udf,
  27. const Eigen::MatrixBase<Derivedijk> & ijk,
  28. Eigen::PlainObjectBase<Derivedijk_maybe> & ijk_maybe)
  29. {
  30. // static assert to ensure that Derivedorigin is a vector and the
  31. // non-singleton dimension is 3 or Eigen::Dynamic
  32. static_assert(
  33. (Derivedorigin::RowsAtCompileTime == 1 && (
  34. Derivedorigin::ColsAtCompileTime == 3 ||
  35. Derivedorigin::ColsAtCompileTime == Eigen::Dynamic)) ||
  36. (Derivedorigin::ColsAtCompileTime == 1 && (
  37. Derivedorigin::RowsAtCompileTime == 3 ||
  38. Derivedorigin::RowsAtCompileTime == Eigen::Dynamic)),
  39. "Derivedorigin must be a vector with 3 or Eigen::Dynamic dimensions");
  40. // dynamic assert that the origin is a 3D vector
  41. assert((origin.rows() == 3 || origin.cols() == 3) && origin.size() == 3 &&
  42. "origin must be a 3D vector");
  43. using Scalar = typename Derivedorigin::Scalar;
  44. using RowVectorS3 = Eigen::Matrix<Scalar,1,3>;
  45. using MatrixSX8R = Eigen::Matrix<typename Derivedorigin::Scalar,Eigen::Dynamic,8,Eigen::RowMajor>;
  46. using MatrixSX3R = Eigen::Matrix<typename Derivedorigin::Scalar,Eigen::Dynamic,3,Eigen::RowMajor>;
  47. using MatrixiX3R = Eigen::Matrix<int,Eigen::Dynamic,3,Eigen::RowMajor>;
  48. // h0 is already the h at this depth.
  49. const Scalar h = h0 / (1 << depth);
  50. Eigen::Matrix<int,Eigen::Dynamic,8,Eigen::RowMajor> J;
  51. MatrixiX3R unique_ijk;
  52. MatrixSX3R unique_corner_positions;
  53. igl::unique_sparse_voxel_corners(origin,h0,depth,ijk,unique_ijk,J,unique_corner_positions);
  54. // Effectively a batched call to udf
  55. Eigen::Array<bool,Eigen::Dynamic,1> big(unique_corner_positions.rows());
  56. // Requires C++17
  57. if constexpr (batched)
  58. {
  59. Eigen::Matrix<Scalar,Eigen::Dynamic,1> u = udf(unique_corner_positions);
  60. for(int i = 0;i<unique_corner_positions.rows();i++)
  61. {
  62. assert(u(i) >= 0 && "udf must be non-negative for lipschitz_octree_prune");
  63. big(i) = (u(i) > h * std::sqrt(3));
  64. }
  65. }else
  66. {
  67. //for(int u = 0;u<unique_corner_positions.rows();u++)
  68. igl::parallel_for(
  69. unique_corner_positions.rows(),
  70. [&](const int i)
  71. {
  72. // evaluate the function at the corner
  73. const RowVectorS3 corner = unique_corner_positions.row(i);
  74. const Scalar u = udf(corner);
  75. assert(u >= 0 && "udf must be non-negative for lipschitz_octree_prune");
  76. big(i) = (u > h * std::sqrt(3));
  77. },
  78. 1000);
  79. }
  80. ijk_maybe.resize(ijk.rows(),3);
  81. int k = 0;
  82. for(int c = 0;c<ijk.rows();c++)
  83. {
  84. bool empty = false;
  85. for(int i = 0;i<8;i++)
  86. {
  87. empty = empty || big(J(c,i));
  88. }
  89. bool maybe = !empty;
  90. if(maybe)
  91. {
  92. ijk_maybe.row(k++) = ijk.row(c);
  93. }
  94. }
  95. ijk_maybe.conservativeResize(k,3);
  96. }
  97. #ifdef IGL_STATIC_LIBRARY
  98. // Explicit template instantiation
  99. template void igl::lipschitz_octree_prune<false,Eigen::Matrix<double, 1, 3, 1, 1, 3>, std::function<double (Eigen::Matrix<double, 1, 3, 1, 1, 3> const&)>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>>(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3>> const&, Eigen::Matrix<double, 1, 3, 1, 1, 3>::Scalar, int, std::function<double (Eigen::Matrix<double, 1, 3, 1, 1, 3> const&)> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3>> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3>>&);
  100. template void igl::lipschitz_octree_prune<true, Eigen::Matrix<double, 1, 3, 1, 1, 3>, std::function<Eigen::Matrix<double, -1, 1, 0, -1, 1> (Eigen::Matrix<double, -1, 3, 1, -1, 3> const&)>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>>(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3>> const&, Eigen::Matrix<double, 1, 3, 1, 1, 3>::Scalar, int, std::function<Eigen::Matrix<double, -1, 1, 0, -1, 1> (Eigen::Matrix<double, -1, 3, 1, -1, 3> const&)> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3>> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3>>&);
  101. #endif