projection_constraint.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "projection_constraint.h"
  2. template <
  3. typename DerivedUV,
  4. typename DerivedM,
  5. typename DerivedVP,
  6. typename DerivedA,
  7. typename DerivedB>
  8. void igl::projection_constraint(
  9. const Eigen::MatrixBase<DerivedUV> & UV,
  10. const Eigen::MatrixBase<DerivedM> & _M,
  11. const Eigen::MatrixBase<DerivedVP> & VP,
  12. Eigen::PlainObjectBase<DerivedA> & A,
  13. Eigen::PlainObjectBase<DerivedB> & B)
  14. {
  15. typedef typename DerivedA::Scalar Scalar;
  16. const Scalar u = UV(0);
  17. const Scalar v = UV(1);
  18. const Scalar cu = VP(0);
  19. const Scalar cv = VP(1);
  20. const Scalar w = VP(2);
  21. const Scalar h = VP(3);
  22. // u = cu + w*(0.5 + 0.5*((M.row(0)*X) / (M.row(3)*X) ))
  23. // u-cu = w*(0.5 + 0.5*((M.row(0)*X) / (M.row(3)*X) ))
  24. // (u-cu)/w = 0.5 + 0.5*((M.row(0)*X) / (M.row(3)*X) )
  25. // (u-cu)/w - 0.5 = 0.5*((M.row(0)*X) / (M.row(3)*X) )
  26. // 2.*(u-cu)/w - 1 = ((M.row(0)*X) / (M.row(3)*X) )
  27. // (2.*(u - cu)/w - 1) * M.row(3)*X = M.row(0)*X
  28. // (2.*(u - cu)/w - 1) * M.row(3)*X - M.row(0)*X = 0
  29. // (2.*(u - cu)/w - 1) * (M.block(3,0,1,3)*x + M(3,3)) - M.block(0,0,1,3)*x - M(0,3) = 0
  30. // (2.*(u - cu)/w - 1) * (M.block(3,0,1,3)*x + M(3,3)) - M.block(0,0,1,3)*x = M(0,3)
  31. // ((2.*(u - cu)/w - 1) * M.block(3,0,1,3) - M.block(0,0,1,3))*x = M(0,3) - (2.*(u - cu)/w - 1)*M(3,3)
  32. Eigen::Matrix<Scalar,4,4> M = _M.template cast<Scalar>();
  33. A.resize(2,3);
  34. A<<
  35. ((2.*(u - cu)/w - 1.) * M.block(3,0,1,3) - M.block(0,0,1,3)),
  36. ((2.*(v - cv)/h - 1.) * M.block(3,0,1,3) - M.block(1,0,1,3));
  37. B.resize(2,1);
  38. B<<
  39. M(0,3) - (2.*(u - cu)/w - 1.)*M(3,3),
  40. M(1,3) - (2.*(v - cv)/h - 1.)*M(3,3);
  41. }
  42. #ifdef IGL_STATIC_LIBRARY
  43. // Explicit template instantiation
  44. // generated by autoexplicit.sh
  45. template void igl::projection_constraint<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, 2, 3, 0, 2, 3>, Eigen::Matrix<double, 2, 1, 0, 2, 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::PlainObjectBase<Eigen::Matrix<double, 2, 3, 0, 2, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 1, 0, 2, 1> >&);
  46. // generated by autoexplicit.sh
  47. template void igl::projection_constraint<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, 2, 3, 0, 2, 3>, Eigen::Matrix<double, 2, 1, 0, 2, 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::PlainObjectBase<Eigen::Matrix<double, 2, 3, 0, 2, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, 2, 1, 0, 2, 1> >&);
  48. #endif