signed_distance.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_inline.h"
  11. #include "AABB.h"
  12. #include "WindingNumberAABB.h"
  13. #include <Eigen/Core>
  14. #include <vector>
  15. namespace igl
  16. {
  17. enum SignedDistanceType
  18. {
  19. // Use fast pseudo-normal test [Bærentzen & Aanæs 2005]
  20. SIGNED_DISTANCE_TYPE_PSEUDONORMAL = 0,
  21. SIGNED_DISTANCE_TYPE_WINDING_NUMBER = 1,
  22. SIGNED_DISTANCE_TYPE_DEFAULT = 2,
  23. SIGNED_DISTANCE_TYPE_UNSIGNED = 3,
  24. NUM_SIGNED_DISTANCE_TYPE = 4
  25. };
  26. // Computes signed distance to a mesh
  27. //
  28. // Inputs:
  29. // P #P by 3 list of query point positions
  30. // V #V by 3 list of vertex positions
  31. // F #F by ss list of triangle indices, ss should be 3 unless sign_type ==
  32. // SIGNED_DISTANCE_TYPE_UNSIGNED
  33. // sign_type method for computing distance _sign_ S
  34. // Outputs:
  35. // S #P list of smallest signed distances
  36. // I #P list of facet indices corresponding to smallest distances
  37. // C #P by 3 list of closest points
  38. // N #P by 3 list of closest normals (only set if
  39. // sign_type=SIGNED_DISTANCE_TYPE_PSEUDONORMAL)
  40. //
  41. // Known bugs: This only computes distances to triangles. So unreferenced
  42. // vertices and degenerate triangles are ignored.
  43. template <
  44. typename DerivedP,
  45. typename DerivedV,
  46. typename DerivedF,
  47. typename DerivedS,
  48. typename DerivedI,
  49. typename DerivedC,
  50. typename DerivedN>
  51. IGL_INLINE void signed_distance(
  52. const Eigen::MatrixBase<DerivedP> & P,
  53. const Eigen::MatrixBase<DerivedV> & V,
  54. const Eigen::MatrixBase<DerivedF> & F,
  55. const SignedDistanceType sign_type,
  56. Eigen::PlainObjectBase<DerivedS> & S,
  57. Eigen::PlainObjectBase<DerivedI> & I,
  58. Eigen::PlainObjectBase<DerivedC> & C,
  59. Eigen::PlainObjectBase<DerivedN> & N);
  60. // Computes signed distance to mesh
  61. //
  62. // Inputs:
  63. // tree AABB acceleration tree (see AABB.h)
  64. // F #F by 3 list of triangle indices
  65. // FN #F by 3 list of triangle normals
  66. // VN #V by 3 list of vertex normals (ANGLE WEIGHTING)
  67. // EN #E by 3 list of edge normals (UNIFORM WEIGHTING)
  68. // EMAP #F*3 mapping edges in F to E
  69. // q Query point
  70. // Returns signed distance to mesh
  71. //
  72. template <
  73. typename DerivedV,
  74. typename DerivedF,
  75. typename DerivedFN,
  76. typename DerivedVN,
  77. typename DerivedEN,
  78. typename DerivedEMAP,
  79. typename Derivedq>
  80. IGL_INLINE typename DerivedV::Scalar signed_distance_pseudonormal(
  81. const AABB<DerivedV,3> & tree,
  82. const Eigen::MatrixBase<DerivedV> & V,
  83. const Eigen::MatrixBase<DerivedF> & F,
  84. const Eigen::MatrixBase<DerivedFN> & FN,
  85. const Eigen::MatrixBase<DerivedVN> & VN,
  86. const Eigen::MatrixBase<DerivedEN> & EN,
  87. const Eigen::MatrixBase<DerivedEMAP> & EMAP,
  88. const Eigen::MatrixBase<Derivedq> & q);
  89. template <
  90. typename DerivedP,
  91. typename DerivedV,
  92. typename DerivedF,
  93. typename DerivedFN,
  94. typename DerivedVN,
  95. typename DerivedEN,
  96. typename DerivedEMAP,
  97. typename DerivedS,
  98. typename DerivedI,
  99. typename DerivedC,
  100. typename DerivedN>
  101. IGL_INLINE void signed_distance_pseudonormal(
  102. const Eigen::MatrixBase<DerivedP> & P,
  103. const Eigen::MatrixBase<DerivedV> & V,
  104. const Eigen::MatrixBase<DerivedF> & F,
  105. const AABB<DerivedV,3> & tree,
  106. const Eigen::MatrixBase<DerivedFN> & FN,
  107. const Eigen::MatrixBase<DerivedVN> & VN,
  108. const Eigen::MatrixBase<DerivedEN> & EN,
  109. const Eigen::MatrixBase<DerivedEMAP> & EMAP,
  110. Eigen::PlainObjectBase<DerivedS> & S,
  111. Eigen::PlainObjectBase<DerivedI> & I,
  112. Eigen::PlainObjectBase<DerivedC> & C,
  113. Eigen::PlainObjectBase<DerivedN> & N);
  114. // Outputs:
  115. // s sign
  116. // sqrd squared distance
  117. // i closest primitive
  118. // c closest point
  119. // n normal
  120. template <
  121. typename DerivedV,
  122. typename DerivedF,
  123. typename DerivedFN,
  124. typename DerivedVN,
  125. typename DerivedEN,
  126. typename DerivedEMAP,
  127. typename Derivedq,
  128. typename Scalar,
  129. typename Derivedc,
  130. typename Derivedn>
  131. IGL_INLINE void signed_distance_pseudonormal(
  132. const AABB<DerivedV,3> & tree,
  133. const Eigen::MatrixBase<DerivedV> & V,
  134. const Eigen::MatrixBase<DerivedF> & F,
  135. const Eigen::MatrixBase<DerivedFN> & FN,
  136. const Eigen::MatrixBase<DerivedVN> & VN,
  137. const Eigen::MatrixBase<DerivedEN> & EN,
  138. const Eigen::MatrixBase<DerivedEMAP> & EMAP,
  139. const Eigen::MatrixBase<Derivedq> & q,
  140. Scalar & s,
  141. Scalar & sqrd,
  142. int & i,
  143. Eigen::PlainObjectBase<Derivedc> & c,
  144. Eigen::PlainObjectBase<Derivedn> & n);
  145. template <
  146. typename DerivedV,
  147. typename DerivedE,
  148. typename DerivedEN,
  149. typename DerivedVN,
  150. typename Derivedq,
  151. typename Scalar,
  152. typename Derivedc,
  153. typename Derivedn>
  154. IGL_INLINE void signed_distance_pseudonormal(
  155. const AABB<DerivedV,2> & tree,
  156. const Eigen::MatrixBase<DerivedV> & V,
  157. const Eigen::MatrixBase<DerivedE> & E,
  158. const Eigen::MatrixBase<DerivedEN> & EN,
  159. const Eigen::MatrixBase<DerivedVN> & VN,
  160. const Eigen::MatrixBase<Derivedq> & q,
  161. Scalar & s,
  162. Scalar & sqrd,
  163. int & i,
  164. Eigen::PlainObjectBase<Derivedc> & c,
  165. Eigen::PlainObjectBase<Derivedn> & n);
  166. // Inputs:
  167. // tree AABB acceleration tree (see cgal/point_mesh_squared_distance.h)
  168. // hier Winding number evaluation hierarchy
  169. // q Query point
  170. // Returns signed distance to mesh
  171. template <
  172. typename DerivedV,
  173. typename DerivedF,
  174. typename Derivedq>
  175. IGL_INLINE typename DerivedV::Scalar signed_distance_winding_number(
  176. const AABB<DerivedV,3> & tree,
  177. const Eigen::MatrixBase<DerivedV> & V,
  178. const Eigen::MatrixBase<DerivedF> & F,
  179. const igl::WindingNumberAABB<Derivedq,DerivedV,DerivedF> & hier,
  180. const Eigen::MatrixBase<Derivedq> & q);
  181. // Outputs:
  182. // s sign
  183. // sqrd squared distance
  184. // pp closest point and primitve
  185. template <
  186. typename DerivedV,
  187. typename DerivedF,
  188. typename Derivedq,
  189. typename Scalar,
  190. typename Derivedc>
  191. IGL_INLINE void signed_distance_winding_number(
  192. const AABB<DerivedV,3> & tree,
  193. const Eigen::MatrixBase<DerivedV> & V,
  194. const Eigen::MatrixBase<DerivedF> & F,
  195. const igl::WindingNumberAABB<Derivedq,DerivedV,DerivedF> & hier,
  196. const Eigen::MatrixBase<Derivedq> & q,
  197. Scalar & s,
  198. Scalar & sqrd,
  199. int & i,
  200. Eigen::PlainObjectBase<Derivedc> & c);
  201. template <
  202. typename DerivedV,
  203. typename DerivedF,
  204. typename Derivedq,
  205. typename Scalar,
  206. typename Derivedc>
  207. IGL_INLINE void signed_distance_winding_number(
  208. const AABB<DerivedV,2> & tree,
  209. const Eigen::MatrixBase<DerivedV> & V,
  210. const Eigen::MatrixBase<DerivedF> & F,
  211. const Eigen::MatrixBase<Derivedq> & q,
  212. Scalar & s,
  213. Scalar & sqrd,
  214. int & i,
  215. Eigen::PlainObjectBase<Derivedc> & c);
  216. }
  217. #ifndef IGL_STATIC_LIBRARY
  218. # include "signed_distance.cpp"
  219. #endif
  220. #endif