unproject_on_line.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "unproject_on_line.h"
  2. #include "projection_constraint.h"
  3. template <
  4. typename DerivedUV,
  5. typename DerivedM,
  6. typename DerivedVP,
  7. typename Derivedorigin,
  8. typename Deriveddir>
  9. void igl::unproject_on_line(
  10. const Eigen::MatrixBase<DerivedUV> & UV,
  11. const Eigen::MatrixBase<DerivedM> & M,
  12. const Eigen::MatrixBase<DerivedVP> & VP,
  13. const Eigen::MatrixBase<Derivedorigin> & origin,
  14. const Eigen::MatrixBase<Deriveddir> & dir,
  15. typename DerivedUV::Scalar & t)
  16. {
  17. typedef typename DerivedUV::Scalar Scalar;
  18. Eigen::Matrix<Scalar,2,3> A;
  19. Eigen::Matrix<Scalar,2,1> B;
  20. projection_constraint(UV,M,VP,A,B);
  21. // min_z,t ‖Az - B‖² subject to z = origin + t*dir
  22. // min_t ‖A(origin + t*dir) - B‖²
  23. // min_t ‖A*t*dir + A*origin - B‖²
  24. // min_t ‖D*t + C‖²
  25. // t = -(D'D)\(D'*C)
  26. Eigen::Matrix<Scalar,2,1> C = A*origin.template cast<Scalar>() - B;
  27. Eigen::Matrix<Scalar,2,1> D = A*dir.template cast<Scalar>();
  28. // Solve least squares system directly
  29. const Eigen::Matrix<Scalar,1,1> t_mat = D.jacobiSvd(Eigen::ComputeFullU | Eigen::ComputeFullV).solve(-C);
  30. t = t_mat(0,0);
  31. }
  32. template <
  33. typename DerivedUV,
  34. typename DerivedM,
  35. typename DerivedVP,
  36. typename Derivedorigin,
  37. typename Deriveddir,
  38. typename DerivedZ>
  39. void igl::unproject_on_line(
  40. const Eigen::MatrixBase<DerivedUV> & UV,
  41. const Eigen::MatrixBase<DerivedM> & M,
  42. const Eigen::MatrixBase<DerivedVP> & VP,
  43. const Eigen::MatrixBase<Derivedorigin> & origin,
  44. const Eigen::MatrixBase<Deriveddir> & dir,
  45. Eigen::PlainObjectBase<DerivedZ> & Z)
  46. {
  47. typedef typename DerivedZ::Scalar Scalar;
  48. typename DerivedUV::Scalar t;
  49. unproject_on_line(UV,M,VP,origin,dir,t);
  50. Z = origin + dir*Scalar(t);
  51. }
  52. #ifdef IGL_STATIC_LIBRARY
  53. // Explicit template instantiation
  54. // generated by autoexplicit.sh
  55. template void igl::unproject_on_line<Eigen::Matrix<double, -1, 1, 0, 3, 1>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 1, 0, 4, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, 3, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 1, 0, 4, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
  56. // generated by autoexplicit.sh
  57. template void igl::unproject_on_line<Eigen::Matrix<double, 2, 1, 0, 2, 1>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 1, 0, 4, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, 2, 1, 0, 2, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 1, 0, 4, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
  58. template void igl::unproject_on_line<Eigen::Matrix<double, 2, 1, 0, 2, 1>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 1, 0, 4, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, 2, 1, 0, 2, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 1, 0, 4, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::Matrix<double, 2, 1, 0, 2, 1>::Scalar&);
  59. #endif