project_to_line_segment.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "project_to_line_segment.h"
  9. #include "project_to_line.h"
  10. #include <Eigen/Core>
  11. template <
  12. typename DerivedP,
  13. typename DerivedS,
  14. typename DerivedD,
  15. typename Derivedt,
  16. typename DerivedsqrD>
  17. IGL_INLINE void igl::project_to_line_segment(
  18. const Eigen::PlainObjectBase<DerivedP> & P,
  19. const Eigen::PlainObjectBase<DerivedS> & S,
  20. const Eigen::PlainObjectBase<DerivedD> & D,
  21. Eigen::PlainObjectBase<Derivedt> & t,
  22. Eigen::PlainObjectBase<DerivedsqrD> & sqrD)
  23. {
  24. project_to_line(P,S,D,t,sqrD);
  25. const int np = P.rows();
  26. // loop over points and fix those that projected beyond endpoints
  27. #pragma omp parallel for if (np>10000)
  28. for(int p = 0;p<np;p++)
  29. {
  30. if(t(p)<0)
  31. {
  32. sqrD(p) = (P.row(p)-S).squaredNorm();
  33. t(p) = 0;
  34. }else if(t(p)>1)
  35. {
  36. sqrD(p) = (P.row(p)-D).squaredNorm();
  37. t(p) = 1;
  38. }
  39. }
  40. }
  41. #ifndef IGL_HEADER_ONLY
  42. // Explicit template specialization
  43. template void igl::project_to_line_segment<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 1, 0, 1, 1>, Eigen::Matrix<double, 1, 1, 0, 1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 1, 0, 1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 1, 0, 1, 1> >&);
  44. #endif