signed_distance.h 11 KB

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