signed_distance.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
  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. #ifndef IGL_SIGNED_DISTANCE_H
  9. #define IGL_SIGNED_DISTANCE_H
  10. #include <igl/igl_inline.h>
  11. #include <igl/WindingNumberAABB.h>
  12. #include <Eigen/Core>
  13. #include <vector>
  14. #include "CGAL_includes.hpp"
  15. namespace igl
  16. {
  17. enum SignedDistanceType
  18. {
  19. SIGNED_DISTANCE_TYPE_PSEUDONORMAL = 0,
  20. SIGNED_DISTANCE_TYPE_WINDING_NUMBER = 1,
  21. SIGNED_DISTANCE_TYPE_DEFAULT = 2,
  22. SIGNED_DISTANCE_TYPE_IGNORE = 3,
  23. NUM_SIGNED_DISTANCE_TYPE = 4
  24. };
  25. // Computes signed distance to a mesh
  26. //
  27. // Templates:
  28. // Kernal CGAL computation and construction kernel (e.g.
  29. // CGAL::Simple_cartesian<double>)
  30. // Inputs:
  31. // P #P by 3 list of query point positions
  32. // V #V by 3 list of vertex positions
  33. // F #F by 3 list of triangle indices
  34. // sign_type method for computing distance _sign_ (see signed_distance.h)
  35. // Outputs:
  36. // S #P list of smallest signed distances
  37. // I #P list of facet indices corresponding to smallest distances
  38. // C #P by 3 list of closest points
  39. // N #P by 3 list of closest normals (only set if
  40. // sign_type=SIGNED_DISTANCE_TYPE_PSEUDONORMAL)
  41. //
  42. // Known bugs: This only computes distances to triangles. So unreferenced
  43. // vertices are ignored.
  44. template <typename Kernel>
  45. IGL_INLINE void signed_distance(
  46. const Eigen::MatrixXd & P,
  47. const Eigen::MatrixXd & V,
  48. const Eigen::MatrixXi & F,
  49. const SignedDistanceType sign_type,
  50. Eigen::VectorXd & S,
  51. Eigen::VectorXi & I,
  52. Eigen::MatrixXd & C,
  53. Eigen::MatrixXd & N);
  54. // Computes signed distance to mesh
  55. //
  56. // Templates:
  57. // Kernal CGAL computation and construction kernel (e.g.
  58. // CGAL::Simple_cartesian<double>)
  59. // Inputs:
  60. // tree AABB acceleration tree (see point_mesh_squared_distance.h)
  61. // T #F list of CGAL triangles (see point_mesh_squared_distance.h)
  62. // F #F by 3 list of triangle indices
  63. // FN #F by 3 list of triangle normals
  64. // VN #V by 3 list of vertex normals (ANGLE WEIGHTING)
  65. // EN #E by 3 list of edge normals (UNIFORM WEIGHTING)
  66. // EMAP #F*3 mapping edges in F to E
  67. // q Query point
  68. // Returns signed distance to mesh
  69. //
  70. template <typename Kernel>
  71. IGL_INLINE typename Kernel::FT signed_distance_pseudonormal(
  72. const CGAL::AABB_tree<
  73. CGAL::AABB_traits<Kernel,
  74. CGAL::AABB_triangle_primitive<Kernel,
  75. typename std::vector<CGAL::Triangle_3<Kernel> >::iterator
  76. >
  77. >
  78. > & tree,
  79. const std::vector<CGAL::Triangle_3<Kernel> > & T,
  80. const Eigen::MatrixXi & F,
  81. const Eigen::MatrixXd & FN,
  82. const Eigen::MatrixXd & VN,
  83. const Eigen::MatrixXd & EN,
  84. const Eigen::VectorXi & EMAP,
  85. const typename Kernel::Point_3 & q);
  86. // Outputs:
  87. // s sign
  88. // sqrd squared distance
  89. // pp closest point and primitve
  90. // n normal
  91. template <typename Kernel>
  92. IGL_INLINE void signed_distance_pseudonormal(
  93. const CGAL::AABB_tree<
  94. CGAL::AABB_traits<Kernel,
  95. CGAL::AABB_triangle_primitive<Kernel,
  96. typename std::vector<CGAL::Triangle_3<Kernel> >::iterator
  97. >
  98. >
  99. > & tree,
  100. const std::vector<CGAL::Triangle_3<Kernel> > & T,
  101. const Eigen::MatrixXi & F,
  102. const Eigen::MatrixXd & FN,
  103. const Eigen::MatrixXd & VN,
  104. const Eigen::MatrixXd & EN,
  105. const Eigen::VectorXi & EMAP,
  106. const typename Kernel::Point_3 & q,
  107. typename Kernel::FT & s,
  108. typename Kernel::FT & sqrd,
  109. typename CGAL::AABB_tree<
  110. CGAL::AABB_traits<Kernel,
  111. CGAL::AABB_triangle_primitive<Kernel,
  112. typename std::vector<CGAL::Triangle_3<Kernel> >::iterator>
  113. >
  114. >::Point_and_primitive_id & pp,
  115. Eigen::Vector3d & n);
  116. // Inputs:
  117. // tree AABB acceleration tree (see point_mesh_squared_distance.h)
  118. // hier Winding number evaluation hierarchy
  119. // q Query point
  120. // Returns signed distance to mesh
  121. template <typename Kernel>
  122. IGL_INLINE typename Kernel::FT signed_distance_winding_number(
  123. const CGAL::AABB_tree<
  124. CGAL::AABB_traits<Kernel,
  125. CGAL::AABB_triangle_primitive<Kernel,
  126. typename std::vector<CGAL::Triangle_3<Kernel> >::iterator
  127. >
  128. >
  129. > & tree,
  130. const igl::WindingNumberAABB<Eigen::Vector3d> & hier,
  131. const typename Kernel::Point_3 & q);
  132. // Outputs:
  133. // s sign
  134. // sqrd squared distance
  135. // pp closest point and primitve
  136. template <typename Kernel>
  137. IGL_INLINE void signed_distance_winding_number(
  138. const CGAL::AABB_tree<
  139. CGAL::AABB_traits<Kernel,
  140. CGAL::AABB_triangle_primitive<Kernel,
  141. typename std::vector<CGAL::Triangle_3<Kernel> >::iterator
  142. >
  143. >
  144. > & tree,
  145. const igl::WindingNumberAABB<Eigen::Vector3d> & hier,
  146. const typename Kernel::Point_3 & q,
  147. typename Kernel::FT & s,
  148. typename Kernel::FT & sqrd,
  149. typename CGAL::AABB_tree<
  150. CGAL::AABB_traits<Kernel,
  151. CGAL::AABB_triangle_primitive<Kernel,
  152. typename std::vector<CGAL::Triangle_3<Kernel> >::iterator>
  153. >
  154. >::Point_and_primitive_id & pp);
  155. }
  156. #ifndef IGL_STATIC_LIBRARY
  157. # include "signed_distance.cpp"
  158. #endif
  159. #endif