signed_distance.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 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. #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 "fast_winding_number.h"
  14. #include <Eigen/Core>
  15. #include <vector>
  16. namespace igl
  17. {
  18. /// Types of signing a distance field
  19. enum SignedDistanceType
  20. {
  21. /// Use fast pseudo-normal test [Bærentzen & Aanæs 2005]
  22. SIGNED_DISTANCE_TYPE_PSEUDONORMAL = 0,
  23. /// Use winding number [Jacobson, Kavan Sorking-Hornug 2013]
  24. SIGNED_DISTANCE_TYPE_WINDING_NUMBER = 1,
  25. /// Default
  26. SIGNED_DISTANCE_TYPE_DEFAULT = 2,
  27. /// Unsigned (absolute value)
  28. SIGNED_DISTANCE_TYPE_UNSIGNED = 3,
  29. /// Use Fast winding number [Barill, Dickson, Schmidt, Levin, Jacobson 2018]
  30. SIGNED_DISTANCE_TYPE_FAST_WINDING_NUMBER = 4,
  31. /// Total number of signed distance types
  32. NUM_SIGNED_DISTANCE_TYPE = 5
  33. };
  34. /// Computes signed distance to a mesh
  35. ///
  36. /// @param[in] P #P by 3 list of query point positions
  37. /// @param[in] V #V by 3 list of vertex positions
  38. /// @param[in] F #F by ss list of triangle indices, ss should be 3 unless
  39. /// sign_type == SIGNED_DISTANCE_TYPE_UNSIGNED
  40. /// @param[in] sign_type method for computing distance _sign_ S
  41. /// @param[in] lower_bound lower bound of distances needed {std::numeric_limits::min}
  42. /// @param[in] upper_bound lower bound of distances needed {std::numeric_limits::max}
  43. /// @param[out] S #P list of smallest signed distances
  44. /// @param[out] I #P list of facet indices corresponding to smallest distances
  45. /// @param[out] C #P by 3 list of closest points
  46. /// @param[out] N #P by 3 list of closest normals (only set if
  47. /// sign_type=SIGNED_DISTANCE_TYPE_PSEUDONORMAL)
  48. ///
  49. /// \bug This only computes distances to triangles. So unreferenced
  50. /// vertices and degenerate triangles are ignored.
  51. template <
  52. typename DerivedP,
  53. typename DerivedV,
  54. typename DerivedF,
  55. typename DerivedS,
  56. typename DerivedI,
  57. typename DerivedC,
  58. typename DerivedN>
  59. IGL_INLINE void signed_distance(
  60. const Eigen::MatrixBase<DerivedP> & P,
  61. const Eigen::MatrixBase<DerivedV> & V,
  62. const Eigen::MatrixBase<DerivedF> & F,
  63. const SignedDistanceType sign_type,
  64. const typename DerivedV::Scalar lower_bound,
  65. const typename DerivedV::Scalar upper_bound,
  66. Eigen::PlainObjectBase<DerivedS> & S,
  67. Eigen::PlainObjectBase<DerivedI> & I,
  68. Eigen::PlainObjectBase<DerivedC> & C,
  69. Eigen::PlainObjectBase<DerivedN> & N);
  70. /// \overload
  71. template <
  72. typename DerivedP,
  73. typename DerivedV,
  74. typename DerivedF,
  75. typename DerivedS,
  76. typename DerivedI,
  77. typename DerivedC,
  78. typename DerivedN>
  79. IGL_INLINE void signed_distance(
  80. const Eigen::MatrixBase<DerivedP> & P,
  81. const Eigen::MatrixBase<DerivedV> & V,
  82. const Eigen::MatrixBase<DerivedF> & F,
  83. const SignedDistanceType sign_type,
  84. Eigen::PlainObjectBase<DerivedS> & S,
  85. Eigen::PlainObjectBase<DerivedI> & I,
  86. Eigen::PlainObjectBase<DerivedC> & C,
  87. Eigen::PlainObjectBase<DerivedN> & N);
  88. /// Computes signed distance to mesh using pseudonormal with precomputed AABB
  89. /// tree and edge/vertice normals
  90. ///
  91. /// @param[in] tree AABB acceleration tree (see AABB.h)
  92. /// @param[in] F #F by 3 list of triangle indices
  93. /// @param[in] FN #F by 3 list of triangle normals
  94. /// @param[in] VN #V by 3 list of vertex normals (ANGLE WEIGHTING)
  95. /// @param[in] EN #E by 3 list of edge normals (UNIFORM WEIGHTING)
  96. /// @param[in] EMAP #F*3 mapping edges in F to E
  97. /// @param[in] q Query point
  98. /// @return signed distance to mesh
  99. ///
  100. /// \fileinfo
  101. template <
  102. typename DerivedV,
  103. typename DerivedF,
  104. typename DerivedFN,
  105. typename DerivedVN,
  106. typename DerivedEN,
  107. typename DerivedEMAP,
  108. typename Derivedq>
  109. IGL_INLINE typename DerivedV::Scalar signed_distance_pseudonormal(
  110. const AABB<DerivedV,3> & tree,
  111. const Eigen::MatrixBase<DerivedV> & V,
  112. const Eigen::MatrixBase<DerivedF> & F,
  113. const Eigen::MatrixBase<DerivedFN> & FN,
  114. const Eigen::MatrixBase<DerivedVN> & VN,
  115. const Eigen::MatrixBase<DerivedEN> & EN,
  116. const Eigen::MatrixBase<DerivedEMAP> & EMAP,
  117. const Eigen::MatrixBase<Derivedq> & q);
  118. /// \overload
  119. ///
  120. /// \fileinfo
  121. template <
  122. typename DerivedP,
  123. typename DerivedV,
  124. typename DerivedF,
  125. typename DerivedFN,
  126. typename DerivedVN,
  127. typename DerivedEN,
  128. typename DerivedEMAP,
  129. typename DerivedS,
  130. typename DerivedI,
  131. typename DerivedC,
  132. typename DerivedN>
  133. IGL_INLINE void signed_distance_pseudonormal(
  134. const Eigen::MatrixBase<DerivedP> & P,
  135. const Eigen::MatrixBase<DerivedV> & V,
  136. const Eigen::MatrixBase<DerivedF> & F,
  137. const AABB<DerivedV,3> & tree,
  138. const Eigen::MatrixBase<DerivedFN> & FN,
  139. const Eigen::MatrixBase<DerivedVN> & VN,
  140. const Eigen::MatrixBase<DerivedEN> & EN,
  141. const Eigen::MatrixBase<DerivedEMAP> & EMAP,
  142. Eigen::PlainObjectBase<DerivedS> & S,
  143. Eigen::PlainObjectBase<DerivedI> & I,
  144. Eigen::PlainObjectBase<DerivedC> & C,
  145. Eigen::PlainObjectBase<DerivedN> & N);
  146. /// \overload
  147. ///
  148. /// @param[out] s sign
  149. /// @param[out] sqrd squared distance
  150. /// @param[out] i closest primitive
  151. /// @param[out] c closest point
  152. /// @param[out] n normal
  153. ///
  154. /// \fileinfo
  155. template <
  156. typename DerivedV,
  157. typename DerivedF,
  158. typename DerivedFN,
  159. typename DerivedVN,
  160. typename DerivedEN,
  161. typename DerivedEMAP,
  162. typename Derivedq,
  163. typename Scalar,
  164. typename Derivedc,
  165. typename Derivedn>
  166. IGL_INLINE void signed_distance_pseudonormal(
  167. const AABB<DerivedV,3> & tree,
  168. const Eigen::MatrixBase<DerivedV> & V,
  169. const Eigen::MatrixBase<DerivedF> & F,
  170. const Eigen::MatrixBase<DerivedFN> & FN,
  171. const Eigen::MatrixBase<DerivedVN> & VN,
  172. const Eigen::MatrixBase<DerivedEN> & EN,
  173. const Eigen::MatrixBase<DerivedEMAP> & EMAP,
  174. const Eigen::MatrixBase<Derivedq> & q,
  175. Scalar & s,
  176. Scalar & sqrd,
  177. int & i,
  178. Eigen::PlainObjectBase<Derivedc> & c,
  179. Eigen::PlainObjectBase<Derivedn> & n);
  180. /// \overload
  181. template <
  182. typename DerivedV,
  183. typename DerivedE,
  184. typename DerivedEN,
  185. typename DerivedVN,
  186. typename Derivedq,
  187. typename Scalar,
  188. typename Derivedc,
  189. typename Derivedn>
  190. IGL_INLINE void signed_distance_pseudonormal(
  191. const AABB<DerivedV,2> & tree,
  192. const Eigen::MatrixBase<DerivedV> & V,
  193. const Eigen::MatrixBase<DerivedE> & E,
  194. const Eigen::MatrixBase<DerivedEN> & EN,
  195. const Eigen::MatrixBase<DerivedVN> & VN,
  196. const Eigen::MatrixBase<Derivedq> & q,
  197. Scalar & s,
  198. Scalar & sqrd,
  199. int & i,
  200. Eigen::PlainObjectBase<Derivedc> & c,
  201. Eigen::PlainObjectBase<Derivedn> & n);
  202. /// Computes signed distance to mesh using winding number with precomputed AABB
  203. ///
  204. /// @param[in] tree AABB acceleration tree (see cgal/point_mesh_squared_distance.h)
  205. /// @param[in] hier Winding number evaluation hierarchy
  206. /// @param[in] q Query point
  207. /// @return signed distance to mesh
  208. ///
  209. /// \fileinfo
  210. template <
  211. typename DerivedV,
  212. typename DerivedF,
  213. typename Derivedq>
  214. IGL_INLINE typename DerivedV::Scalar signed_distance_winding_number(
  215. const AABB<DerivedV,3> & tree,
  216. const Eigen::MatrixBase<DerivedV> & V,
  217. const Eigen::MatrixBase<DerivedF> & F,
  218. const igl::WindingNumberAABB<Derivedq,DerivedV,DerivedF> & hier,
  219. const Eigen::MatrixBase<Derivedq> & q);
  220. /// \overload
  221. /// @param[out] s sign
  222. /// @param[out] sqrd squared distance
  223. /// @param[out] pp closest point and primitve
  224. /// \fileinfo
  225. template <
  226. typename DerivedV,
  227. typename DerivedF,
  228. typename Derivedq,
  229. typename Scalar,
  230. typename Derivedc>
  231. IGL_INLINE void signed_distance_winding_number(
  232. const AABB<DerivedV,3> & tree,
  233. const Eigen::MatrixBase<DerivedV> & V,
  234. const Eigen::MatrixBase<DerivedF> & F,
  235. const igl::WindingNumberAABB<Derivedq,DerivedV,DerivedF> & hier,
  236. const Eigen::MatrixBase<Derivedq> & q,
  237. Scalar & s,
  238. Scalar & sqrd,
  239. int & i,
  240. Eigen::PlainObjectBase<Derivedc> & c);
  241. /// \overload
  242. template <
  243. typename DerivedV,
  244. typename DerivedF,
  245. typename Derivedq,
  246. typename Scalar,
  247. typename Derivedc>
  248. IGL_INLINE void signed_distance_winding_number(
  249. const AABB<DerivedV,2> & tree,
  250. const Eigen::MatrixBase<DerivedV> & V,
  251. const Eigen::MatrixBase<DerivedF> & F,
  252. const Eigen::MatrixBase<Derivedq> & q,
  253. Scalar & s,
  254. Scalar & sqrd,
  255. int & i,
  256. Eigen::PlainObjectBase<Derivedc> & c);
  257. /// Calculates signed distance at query points P, using fast winding number
  258. /// for sign.
  259. ///
  260. /// #### Usage:
  261. /// VectorXd S;
  262. /// VectorXd V, P; //where V is mesh vertices, P are query points
  263. /// VectorXi F;
  264. /// igl::FastWindingNumberBVH fwn_bvh;
  265. /// igl::fast_winding_number(V.cast<float>(), F, 2, fwn_bvh);
  266. /// igl::signed_distance_fast_winding_number(P,V,F,tree,fwn_bvh,S);
  267. ///
  268. /// @param[in] P #P by 3 list of query point positions
  269. /// @param[in] V #V by 3 list of triangle indices
  270. /// @param[in] F #F by 3 list of triangle normals
  271. /// @param[in] tree AABB acceleration tree (see AABB.h)
  272. /// @param[in] bvh fast winding precomputation (see Fast_Winding_Number.h)
  273. /// @param[out] S #P list of signed distances of each point in P
  274. ///
  275. /// \fileinfo
  276. template <
  277. typename DerivedP,
  278. typename DerivedV,
  279. typename DerivedF,
  280. typename DerivedS>
  281. IGL_INLINE void signed_distance_fast_winding_number(
  282. const Eigen::MatrixBase<DerivedP> & P,
  283. const Eigen::MatrixBase<DerivedV> & V,
  284. const Eigen::MatrixBase<DerivedF> & F,
  285. const AABB<DerivedV,3> & tree,
  286. const igl::FastWindingNumberBVH & fwn_bvh,
  287. Eigen::PlainObjectBase<DerivedS> & S
  288. );
  289. /// Calculates signed distance at query point q, using fast winding number for
  290. /// sign.
  291. ///
  292. /// @param[in] tree AABB acceleration tree (see AABB.h)
  293. /// @param[in] V #V by 3 list of triangle indices
  294. /// @param[in] F #F by 3 list of triangle normals
  295. /// @param[in] bvh fast winding precomputation (see Fast_Winding_Number.h)
  296. /// @param[in] q 1 by 3 list of query point positions
  297. /// @param[out] S #P list of signed distances of each point in P
  298. ///
  299. /// \fileinfo
  300. template <
  301. typename Derivedq,
  302. typename DerivedV,
  303. typename DerivedF>
  304. IGL_INLINE typename DerivedV::Scalar signed_distance_fast_winding_number(
  305. const Eigen::MatrixBase<Derivedq> & q,
  306. const Eigen::MatrixBase<DerivedV> & V,
  307. const Eigen::MatrixBase<DerivedF> & F,
  308. const AABB<DerivedV,3> & tree,
  309. const igl::FastWindingNumberBVH & fwn_bvh
  310. );
  311. }
  312. #ifndef IGL_STATIC_LIBRARY
  313. # include "signed_distance.cpp"
  314. #endif
  315. #endif