point_simplex_squared_distance.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. typename Derivedb>
  22. IGL_INLINE void igl::point_simplex_squared_distance(
  23. const Eigen::MatrixBase<Derivedp> & p,
  24. const Eigen::MatrixBase<DerivedV> & V,
  25. const Eigen::MatrixBase<DerivedEle> & Ele,
  26. const typename DerivedEle::Index primitive,
  27. Derivedsqr_d & sqr_d,
  28. Eigen::MatrixBase<Derivedc> & c,
  29. Eigen::PlainObjectBase<Derivedb> & bary)
  30. {
  31. typedef typename Derivedp::Scalar Scalar;
  32. typedef typename Eigen::Matrix<Scalar,1,DIM> Vector;
  33. typedef Vector Point;
  34. //typedef Derivedb BaryPoint;
  35. typedef Eigen::Matrix<typename Derivedb::Scalar,1,3> BaryPoint;
  36. const auto & Dot = [](const Point & a, const Point & b)->Scalar
  37. {
  38. return a.dot(b);
  39. };
  40. // Real-time collision detection, Ericson, Chapter 5
  41. const auto & ClosestBaryPtPointTriangle =
  42. [&Dot](Point p, Point a, Point b, Point c, BaryPoint& bary_out )->Point
  43. {
  44. // Check if P in vertex region outside A
  45. Vector ab = b - a;
  46. Vector ac = c - a;
  47. Vector ap = p - a;
  48. Scalar d1 = Dot(ab, ap);
  49. Scalar d2 = Dot(ac, ap);
  50. if (d1 <= 0.0 && d2 <= 0.0) {
  51. // barycentric coordinates (1,0,0)
  52. bary_out << 1, 0, 0;
  53. return a;
  54. }
  55. // Check if P in vertex region outside B
  56. Vector bp = p - b;
  57. Scalar d3 = Dot(ab, bp);
  58. Scalar d4 = Dot(ac, bp);
  59. if (d3 >= 0.0 && d4 <= d3) {
  60. // barycentric coordinates (0,1,0)
  61. bary_out << 0, 1, 0;
  62. return b;
  63. }
  64. // Check if P in edge region of AB, if so return projection of P onto AB
  65. Scalar vc = d1*d4 - d3*d2;
  66. if( a != b)
  67. {
  68. if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0) {
  69. Scalar v = d1 / (d1 - d3);
  70. // barycentric coordinates (1-v,v,0)
  71. bary_out << 1-v, v, 0;
  72. return a + v * ab;
  73. }
  74. }
  75. // Check if P in vertex region outside C
  76. Vector cp = p - c;
  77. Scalar d5 = Dot(ab, cp);
  78. Scalar d6 = Dot(ac, cp);
  79. if (d6 >= 0.0 && d5 <= d6) {
  80. // barycentric coordinates (0,0,1)
  81. bary_out << 0, 0, 1;
  82. return c;
  83. }
  84. // Check if P in edge region of AC, if so return projection of P onto AC
  85. Scalar vb = d5*d2 - d1*d6;
  86. if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) {
  87. Scalar w = d2 / (d2 - d6);
  88. // barycentric coordinates (1-w,0,w)
  89. bary_out << 1-w, 0, w;
  90. return a + w * ac;
  91. }
  92. // Check if P in edge region of BC, if so return projection of P onto BC
  93. Scalar va = d3*d6 - d5*d4;
  94. if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0) {
  95. Scalar w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
  96. // barycentric coordinates (0,1-w,w)
  97. bary_out << 0, 1-w, w;
  98. return b + w * (c - b);
  99. }
  100. // P inside face region. Compute Q through its barycentric coordinates (u,v,w)
  101. Scalar denom = 1.0 / (va + vb + vc);
  102. Scalar v = vb * denom;
  103. Scalar w = vc * denom;
  104. bary_out << 1.0-v-w, v, w;
  105. return a + ab * v + ac * w; // = u*a + v*b + w*c, u = va * denom = 1.0-v-w
  106. };
  107. assert(p.size() == DIM);
  108. assert(V.cols() == DIM);
  109. assert(Ele.cols() <= DIM+1);
  110. assert(Ele.cols() <= 3 && "Only simplices up to triangles are considered");
  111. assert((Derivedb::RowsAtCompileTime == 1 || Derivedb::ColsAtCompileTime == 1) && "bary must be Eigen Vector or Eigen RowVector");
  112. assert(
  113. ((Derivedb::RowsAtCompileTime == -1 || Derivedb::ColsAtCompileTime == -1) ||
  114. (Derivedb::RowsAtCompileTime == Ele.cols() || Derivedb::ColsAtCompileTime == Ele.cols())
  115. ) && "bary must be Dynamic or size of Ele.cols()");
  116. BaryPoint tmp_bary;
  117. c = (const Derivedc &)ClosestBaryPtPointTriangle(
  118. p,
  119. V.row(Ele(primitive,0)),
  120. // modulo is a HACK to handle points, segments and triangles. Because of
  121. // this, we need 3d buffer for bary
  122. V.row(Ele(primitive,1%Ele.cols())),
  123. V.row(Ele(primitive,2%Ele.cols())),
  124. tmp_bary);
  125. bary.resize( Derivedb::RowsAtCompileTime == 1 ? 1 : Ele.cols(), Derivedb::ColsAtCompileTime == 1 ? 1 : Ele.cols());
  126. bary.head(Ele.cols()) = tmp_bary.head(Ele.cols());
  127. sqr_d = (p-c).squaredNorm();
  128. }
  129. template <
  130. int DIM,
  131. typename Derivedp,
  132. typename DerivedV,
  133. typename DerivedEle,
  134. typename Derivedsqr_d,
  135. typename Derivedc>
  136. IGL_INLINE void igl::point_simplex_squared_distance(
  137. const Eigen::MatrixBase<Derivedp> & p,
  138. const Eigen::MatrixBase<DerivedV> & V,
  139. const Eigen::MatrixBase<DerivedEle> & Ele,
  140. const typename DerivedEle::Index primitive,
  141. Derivedsqr_d & sqr_d,
  142. Eigen::MatrixBase<Derivedc> & c)
  143. {
  144. // Use Dynamic because we don't know Ele.cols() at compile time.
  145. Eigen::Matrix<typename Derivedc::Scalar,1,Eigen::Dynamic> b;
  146. point_simplex_squared_distance<DIM>( p, V, Ele, primitive, sqr_d, c, b );
  147. }
  148. namespace igl
  149. {
  150. template <> IGL_INLINE void point_simplex_squared_distance<2>(Eigen::MatrixBase<Eigen::Matrix<float, 1, 2, 1, 1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::Matrix<int, -1, 3, 0, -1, 3>::Index, float&, Eigen::MatrixBase<Eigen::Matrix<float, 1, 2, 1, 1, 2> >&) {assert(false);};
  151. template <> IGL_INLINE void point_simplex_squared_distance<2>(Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::Matrix<int, -1, 3, 0, -1, 3>::Index, double&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> >&) {assert(false);};
  152. template <> IGL_INLINE void point_simplex_squared_distance<2>(Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::Matrix<int, -1, 3, 1, -1, 3>::Index, double&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> >&) {assert(false);};
  153. }
  154. #ifdef IGL_STATIC_LIBRARY
  155. // Explicit template instantiation
  156. // generated by autoexplicit.sh
  157. template void igl::point_simplex_squared_distance<3, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, 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, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::Matrix<int, -1, 3, 1, -1, 3>::Index, double&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  158. // generated by autoexplicit.sh
  159. template void igl::point_simplex_squared_distance<3, Eigen::Matrix<float, 1, 3, 1, 1, 3>, Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, float, Eigen::Matrix<float, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<float, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::Matrix<int, -1, 3, 0, -1, 3>::Index, float&, Eigen::MatrixBase<Eigen::Matrix<float, 1, 3, 1, 1, 3> >&);
  160. // generated by autoexplicit.sh
  161. // generated by autoexplicit.sh
  162. template void igl::point_simplex_squared_distance<3, Eigen::Matrix<float, 1, 3, 1, 1, 3>, Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, float, Eigen::Matrix<float, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<float, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::Matrix<int, -1, 3, 1, -1, 3>::Index, float&, Eigen::MatrixBase<Eigen::Matrix<float, 1, 3, 1, 1, 3> >&);
  163. // generated by autoexplicit.sh
  164. template void igl::point_simplex_squared_distance<3, Eigen::Matrix<float, 1, 3, 1, 1, 3>, Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, float, Eigen::Matrix<float, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<float, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1>::Index, float&, Eigen::MatrixBase<Eigen::Matrix<float, 1, 3, 1, 1, 3> >&);
  165. 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::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  166. 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::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> >&);
  167. 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::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  168. 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::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 1, 1, 3> >&);
  169. 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::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> >&);
  170. 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::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 1, 1, 1, 2> >&);
  171. #endif