point_simplex_squared_distance.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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. #include "point_simplex_squared_distance.h"
  9. #include "project_to_line_segment.h"
  10. #include "barycentric_coordinates.h"
  11. #include <Eigen/Geometry>
  12. #include <limits>
  13. #include <cassert>
  14. template <
  15. int DIM,
  16. typename Derivedp,
  17. typename DerivedV,
  18. typename DerivedEle,
  19. typename Derivedsqr_d,
  20. typename Derivedc>
  21. IGL_INLINE void igl::point_simplex_squared_distance(
  22. const Eigen::MatrixBase<Derivedp> & p,
  23. const Eigen::MatrixBase<DerivedV> & V,
  24. const Eigen::MatrixBase<DerivedEle> & Ele,
  25. const typename DerivedEle::Index primitive,
  26. Derivedsqr_d & sqr_d,
  27. Eigen::PlainObjectBase<Derivedc> & c)
  28. {
  29. typedef typename Derivedp::Scalar Scalar;
  30. typedef typename Eigen::Matrix<Scalar,1,DIM> Vector;
  31. typedef Vector Point;
  32. const auto & Dot = [](const Point & a, const Point & b)->Scalar
  33. {
  34. return a.dot(b);
  35. };
  36. // Real-time collision detection, Ericson, Chapter 5
  37. const auto & ClosestPtPointTriangle =
  38. [&Dot](Point p, Point a, Point b, Point c)->Point
  39. {
  40. // Check if P in vertex region outside A
  41. Vector ab = b - a;
  42. Vector ac = c - a;
  43. Vector ap = p - a;
  44. Scalar d1 = Dot(ab, ap);
  45. Scalar d2 = Dot(ac, ap);
  46. if (d1 <= 0.0 && d2 <= 0.0) return a; // barycentric coordinates (1,0,0)
  47. // Check if P in vertex region outside B
  48. Vector bp = p - b;
  49. Scalar d3 = Dot(ab, bp);
  50. Scalar d4 = Dot(ac, bp);
  51. if (d3 >= 0.0 && d4 <= d3) return b; // barycentric coordinates (0,1,0)
  52. // Check if P in edge region of AB, if so return projection of P onto AB
  53. Scalar vc = d1*d4 - d3*d2;
  54. if( a != b)
  55. {
  56. if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0) {
  57. Scalar v = d1 / (d1 - d3);
  58. return a + v * ab; // barycentric coordinates (1-v,v,0)
  59. }
  60. }
  61. // Check if P in vertex region outside C
  62. Vector cp = p - c;
  63. Scalar d5 = Dot(ab, cp);
  64. Scalar d6 = Dot(ac, cp);
  65. if (d6 >= 0.0 && d5 <= d6) return c; // barycentric coordinates (0,0,1)
  66. // Check if P in edge region of AC, if so return projection of P onto AC
  67. Scalar vb = d5*d2 - d1*d6;
  68. if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) {
  69. Scalar w = d2 / (d2 - d6);
  70. return a + w * ac; // barycentric coordinates (1-w,0,w)
  71. }
  72. // Check if P in edge region of BC, if so return projection of P onto BC
  73. Scalar va = d3*d6 - d5*d4;
  74. if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0) {
  75. Scalar w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
  76. return b + w * (c - b); // barycentric coordinates (0,1-w,w)
  77. }
  78. // P inside face region. Compute Q through its barycentric coordinates (u,v,w)
  79. Scalar denom = 1.0 / (va + vb + vc);
  80. Scalar v = vb * denom;
  81. Scalar w = vc * denom;
  82. return a + ab * v + ac * w; // = u*a + v*b + w*c, u = va * denom = 1.0-v-w
  83. };
  84. assert(p.size() == DIM);
  85. assert(V.cols() == DIM);
  86. assert(Ele.cols() <= DIM+1);
  87. assert(Ele.cols() <= 3 && "Only simplices up to triangles are considered");
  88. c = ClosestPtPointTriangle(
  89. p,
  90. V.row(Ele(primitive,0)),
  91. V.row(Ele(primitive,1%Ele.cols())),
  92. V.row(Ele(primitive,2%Ele.cols())));
  93. sqr_d = (p-c).squaredNorm();
  94. }
  95. #ifdef IGL_STATIC_LIBRARY
  96. // Explicit template instanciation
  97. template void igl::point_simplex_squared_distance<3, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double, Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1>::Index, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  98. template void igl::point_simplex_squared_distance<2, Eigen::Matrix<double, 1, 2, 1, 1, 2>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double, Eigen::Matrix<double, 1, 2, 1, 1, 2> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1>::Index, double&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> >&);
  99. #endif