point_mesh_squared_distance.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_POINT_MESH_SQUARED_DISTANCE_H
  9. #define IGL_POINT_MESH_SQUARED_DISTANCE_H
  10. #include "igl_inline.h"
  11. #include <Eigen/Core>
  12. #include <vector>
  13. namespace igl
  14. {
  15. /// Compute distances from a set of points P to a triangle mesh (V,F)
  16. ///
  17. /// @param[in] P #P by 3 list of query point positions
  18. /// @param[in] V #V by 3 list of vertex positions
  19. /// @param[in] Ele #Ele by (3|2|1) list of (triangle|edge|point) indices
  20. /// @param[out] sqrD #P list of smallest squared distances
  21. /// @param[out] I #P list of primitive indices corresponding to smallest distances
  22. /// @param[out] C #P by 3 list of closest points
  23. ///
  24. /// \bug This only computes distances to given primitives. So
  25. /// unreferenced vertices are ignored. However, degenerate primitives are
  26. /// handled correctly: triangle [1 2 2] is treated as a segment [1 2], and
  27. /// triangle [1 1 1] is treated as a point. So one _could_ add extra
  28. /// combinatorially degenerate rows to Ele for all unreferenced vertices to
  29. /// also get distances to points.
  30. ///
  31. /// ##### Example:
  32. ///
  33. /// ```cpp
  34. /// Eigen::MatrixXd V;
  35. /// Eigen::MatrixXi F;
  36. /// igl::read_triangle_mesh("bunny.obj",V,F);
  37. /// // 100 points in [-1,1]³ cube
  38. /// Eigen::MatrixXd P = Eigen::MatrixXd::Random(100,3);
  39. /// Eigen::VectorXd sqrD;
  40. /// Eigen::VectorXi I;
  41. /// Eigen::MatrixXd C;
  42. /// igl::point_mesh_squared_distance(P,V,F,sqrD,I,C);
  43. /// // Now sqrD(i) = squared distance from P.row(i) to mesh
  44. /// // I(i) = closest primitive index
  45. /// // C.row(i) = closest point on mesh to P.row(i)
  46. /// ```
  47. template <
  48. typename DerivedP,
  49. typename DerivedV,
  50. typename DerivedEle,
  51. typename DerivedsqrD,
  52. typename DerivedI,
  53. typename DerivedC>
  54. IGL_INLINE void point_mesh_squared_distance(
  55. const Eigen::MatrixBase<DerivedP> &P,
  56. const Eigen::MatrixBase<DerivedV> &V,
  57. const Eigen::MatrixBase<DerivedEle> &Ele,
  58. Eigen::PlainObjectBase<DerivedsqrD> &sqrD,
  59. Eigen::PlainObjectBase<DerivedI> &I,
  60. Eigen::PlainObjectBase<DerivedC> &C);
  61. }
  62. #ifndef IGL_STATIC_LIBRARY
  63. # include "point_mesh_squared_distance.cpp"
  64. #endif
  65. #endif