point_triangle_squared_distance.cpp 883 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "point_triangle_squared_distance.h"
  2. template < typename Kernel>
  3. IGL_INLINE void point_triangle_squared_distance(
  4. const CGAL::Point_3<Kernel> & P1,
  5. const CGAL::Triangle_3<Kernel> & T2,
  6. CGAL::Point_3<Kernel> & P2,
  7. typename Kernel::FT & d)
  8. {
  9. assert(!T2.is_degenerate());
  10. if(T2.has_on(P1))
  11. {
  12. P2 = P1;
  13. d = 0;
  14. return;
  15. }
  16. const auto proj_1 = T2.supporting_plane().projection(P2);
  17. if(T2.has_on(proj_1))
  18. {
  19. P2 = proj_1;
  20. d = (proj_1-P1).squared_length();
  21. return;
  22. }
  23. // closest point must be on the boundary
  24. bool first = true;
  25. // loop over edges
  26. for(int i=0;i<3;i++)
  27. {
  28. Point_3<Kernel> P2i;
  29. typename Kernel::FT di;
  30. const Segment_3<Kernel> si( T2.vertex(i+1), T2.vertex(i+2));
  31. point_segment_squared_distance(P1,si,P2i,di);
  32. if(first || di < d)
  33. {
  34. first = false;
  35. d = di;
  36. P2 = P2i;
  37. }
  38. }
  39. }