signed_distance.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. // lower_bound lower bound of distances needed {std::numeric_limits::min}
  35. // upper_bound lower bound of distances needed {std::numeric_limits::max}
  36. // Outputs:
  37. // S #P list of smallest signed distances
  38. // I #P list of facet indices corresponding to smallest distances
  39. // C #P by 3 list of closest points
  40. // N #P by 3 list of closest normals (only set if
  41. // sign_type=SIGNED_DISTANCE_TYPE_PSEUDONORMAL)
  42. //
  43. // Known bugs: This only computes distances to triangles. So unreferenced
  44. // vertices and degenerate triangles are ignored.
  45. template <
  46. typename DerivedP,
  47. typename DerivedV,
  48. typename DerivedF,
  49. typename DerivedS,
  50. typename DerivedI,
  51. typename DerivedC,
  52. typename DerivedN>
  53. IGL_INLINE void signed_distance(
  54. const Eigen::MatrixBase<DerivedP> & P,
  55. const Eigen::MatrixBase<DerivedV> & V,
  56. const Eigen::MatrixBase<DerivedF> & F,
  57. const SignedDistanceType sign_type,
  58. const typename DerivedV::Scalar lower_bound,
  59. const typename DerivedV::Scalar upper_bound,
  60. Eigen::PlainObjectBase<DerivedS> & S,
  61. Eigen::PlainObjectBase<DerivedI> & I,
  62. Eigen::PlainObjectBase<DerivedC> & C,
  63. Eigen::PlainObjectBase<DerivedN> & N);
  64. // Default bounds
  65. template <
  66. typename DerivedP,
  67. typename DerivedV,
  68. typename DerivedF,
  69. typename DerivedS,
  70. typename DerivedI,
  71. typename DerivedC,
  72. typename DerivedN>
  73. IGL_INLINE void signed_distance(
  74. const Eigen::MatrixBase<DerivedP> & P,
  75. const Eigen::MatrixBase<DerivedV> & V,
  76. const Eigen::MatrixBase<DerivedF> & F,
  77. const SignedDistanceType sign_type,
  78. Eigen::PlainObjectBase<DerivedS> & S,
  79. Eigen::PlainObjectBase<DerivedI> & I,
  80. Eigen::PlainObjectBase<DerivedC> & C,
  81. Eigen::PlainObjectBase<DerivedN> & N);
  82. // Computes signed distance to mesh
  83. //
  84. // Inputs:
  85. // tree AABB acceleration tree (see AABB.h)
  86. // F #F by 3 list of triangle indices
  87. // FN #F by 3 list of triangle normals
  88. // VN #V by 3 list of vertex normals (ANGLE WEIGHTING)
  89. // EN #E by 3 list of edge normals (UNIFORM WEIGHTING)
  90. // EMAP #F*3 mapping edges in F to E
  91. // q Query point
  92. // Returns signed distance to mesh
  93. //
  94. template <
  95. typename DerivedV,
  96. typename DerivedF,
  97. typename DerivedFN,
  98. typename DerivedVN,
  99. typename DerivedEN,
  100. typename DerivedEMAP,
  101. typename Derivedq>
  102. IGL_INLINE typename DerivedV::Scalar signed_distance_pseudonormal(
  103. const AABB<DerivedV,3> & tree,
  104. const Eigen::MatrixBase<DerivedV> & V,
  105. const Eigen::MatrixBase<DerivedF> & F,
  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. const Eigen::MatrixBase<Derivedq> & q);
  111. template <
  112. typename DerivedP,
  113. typename DerivedV,
  114. typename DerivedF,
  115. typename DerivedFN,
  116. typename DerivedVN,
  117. typename DerivedEN,
  118. typename DerivedEMAP,
  119. typename DerivedS,
  120. typename DerivedI,
  121. typename DerivedC,
  122. typename DerivedN>
  123. IGL_INLINE void signed_distance_pseudonormal(
  124. const Eigen::MatrixBase<DerivedP> & P,
  125. const Eigen::MatrixBase<DerivedV> & V,
  126. const Eigen::MatrixBase<DerivedF> & F,
  127. const AABB<DerivedV,3> & tree,
  128. const Eigen::MatrixBase<DerivedFN> & FN,
  129. const Eigen::MatrixBase<DerivedVN> & VN,
  130. const Eigen::MatrixBase<DerivedEN> & EN,
  131. const Eigen::MatrixBase<DerivedEMAP> & EMAP,
  132. Eigen::PlainObjectBase<DerivedS> & S,
  133. Eigen::PlainObjectBase<DerivedI> & I,
  134. Eigen::PlainObjectBase<DerivedC> & C,
  135. Eigen::PlainObjectBase<DerivedN> & N);
  136. // Outputs:
  137. // s sign
  138. // sqrd squared distance
  139. // i closest primitive
  140. // c closest point
  141. // n normal
  142. template <
  143. typename DerivedV,
  144. typename DerivedF,
  145. typename DerivedFN,
  146. typename DerivedVN,
  147. typename DerivedEN,
  148. typename DerivedEMAP,
  149. typename Derivedq,
  150. typename Scalar,
  151. typename Derivedc,
  152. typename Derivedn>
  153. IGL_INLINE void signed_distance_pseudonormal(
  154. const AABB<DerivedV,3> & tree,
  155. const Eigen::MatrixBase<DerivedV> & V,
  156. const Eigen::MatrixBase<DerivedF> & F,
  157. const Eigen::MatrixBase<DerivedFN> & FN,
  158. const Eigen::MatrixBase<DerivedVN> & VN,
  159. const Eigen::MatrixBase<DerivedEN> & EN,
  160. const Eigen::MatrixBase<DerivedEMAP> & EMAP,
  161. const Eigen::MatrixBase<Derivedq> & q,
  162. Scalar & s,
  163. Scalar & sqrd,
  164. int & i,
  165. Eigen::PlainObjectBase<Derivedc> & c,
  166. Eigen::PlainObjectBase<Derivedn> & n);
  167. template <
  168. typename DerivedV,
  169. typename DerivedE,
  170. typename DerivedEN,
  171. typename DerivedVN,
  172. typename Derivedq,
  173. typename Scalar,
  174. typename Derivedc,
  175. typename Derivedn>
  176. IGL_INLINE void signed_distance_pseudonormal(
  177. const AABB<DerivedV,2> & tree,
  178. const Eigen::MatrixBase<DerivedV> & V,
  179. const Eigen::MatrixBase<DerivedE> & E,
  180. const Eigen::MatrixBase<DerivedEN> & EN,
  181. const Eigen::MatrixBase<DerivedVN> & VN,
  182. const Eigen::MatrixBase<Derivedq> & q,
  183. Scalar & s,
  184. Scalar & sqrd,
  185. int & i,
  186. Eigen::PlainObjectBase<Derivedc> & c,
  187. Eigen::PlainObjectBase<Derivedn> & n);
  188. // Inputs:
  189. // tree AABB acceleration tree (see cgal/point_mesh_squared_distance.h)
  190. // hier Winding number evaluation hierarchy
  191. // q Query point
  192. // Returns signed distance to mesh
  193. template <
  194. typename DerivedV,
  195. typename DerivedF,
  196. typename Derivedq>
  197. IGL_INLINE typename DerivedV::Scalar signed_distance_winding_number(
  198. const AABB<DerivedV,3> & tree,
  199. const Eigen::MatrixBase<DerivedV> & V,
  200. const Eigen::MatrixBase<DerivedF> & F,
  201. const igl::WindingNumberAABB<Derivedq,DerivedV,DerivedF> & hier,
  202. const Eigen::MatrixBase<Derivedq> & q);
  203. // Outputs:
  204. // s sign
  205. // sqrd squared distance
  206. // pp closest point and primitve
  207. template <
  208. typename DerivedV,
  209. typename DerivedF,
  210. typename Derivedq,
  211. typename Scalar,
  212. typename Derivedc>
  213. IGL_INLINE void signed_distance_winding_number(
  214. const AABB<DerivedV,3> & tree,
  215. const Eigen::MatrixBase<DerivedV> & V,
  216. const Eigen::MatrixBase<DerivedF> & F,
  217. const igl::WindingNumberAABB<Derivedq,DerivedV,DerivedF> & hier,
  218. const Eigen::MatrixBase<Derivedq> & q,
  219. Scalar & s,
  220. Scalar & sqrd,
  221. int & i,
  222. Eigen::PlainObjectBase<Derivedc> & c);
  223. template <
  224. typename DerivedV,
  225. typename DerivedF,
  226. typename Derivedq,
  227. typename Scalar,
  228. typename Derivedc>
  229. IGL_INLINE void signed_distance_winding_number(
  230. const AABB<DerivedV,2> & tree,
  231. const Eigen::MatrixBase<DerivedV> & V,
  232. const Eigen::MatrixBase<DerivedF> & F,
  233. const Eigen::MatrixBase<Derivedq> & q,
  234. Scalar & s,
  235. Scalar & sqrd,
  236. int & i,
  237. Eigen::PlainObjectBase<Derivedc> & c);
  238. }
  239. #ifndef IGL_STATIC_LIBRARY
  240. # include "signed_distance.cpp"
  241. #endif
  242. #endif