project.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.h"
  9. template <typename Scalar>
  10. Eigen::Matrix<Scalar,3,1> igl::project(
  11. const Eigen::Matrix<Scalar,3,1>& obj,
  12. const Eigen::Matrix<Scalar,4,4>& model,
  13. const Eigen::Matrix<Scalar,4,4>& proj,
  14. const Eigen::Matrix<Scalar,4,1>& viewport)
  15. {
  16. Eigen::Matrix<Scalar,4,1> tmp;
  17. tmp << obj,1;
  18. tmp = model * tmp;
  19. tmp = proj * tmp;
  20. tmp = tmp.array() / tmp(3);
  21. tmp = tmp.array() * 0.5f + 0.5f;
  22. tmp(0) = tmp(0) * viewport(2) + viewport(0);
  23. tmp(1) = tmp(1) * viewport(3) + viewport(1);
  24. return tmp.head(3);
  25. }
  26. template <typename DerivedV, typename Scalar, typename DerivedP>
  27. IGL_INLINE void igl::project(
  28. const Eigen::PlainObjectBase<DerivedV>& V,
  29. const Eigen::Matrix<Scalar,4,4>& model,
  30. const Eigen::Matrix<Scalar,4,4>& proj,
  31. const Eigen::Matrix<Scalar,4,1>& viewport,
  32. Eigen::PlainObjectBase<DerivedP> & P)
  33. {
  34. typedef typename DerivedP::Scalar PScalar;
  35. Eigen::Matrix<PScalar,DerivedV::RowsAtCompileTime,4> HV(V.rows(),4);
  36. HV.leftCols(3) = V.template cast<PScalar>();
  37. HV.col(3).setConstant(1);
  38. HV = (HV*model.template cast<PScalar>().transpose()*
  39. proj.template cast<PScalar>().transpose()).eval();
  40. HV = (HV.array().colwise()/HV.col(3).array()).eval();
  41. HV = (HV.array() * 0.5 + 0.5).eval();
  42. HV.col(0) = (HV.array().col(0) * viewport(2) + viewport(0)).eval();
  43. HV.col(1) = (HV.array().col(1) * viewport(3) + viewport(1)).eval();
  44. P = HV.leftCols(3);
  45. }
  46. #ifdef IGL_STATIC_LIBRARY
  47. // Explicit template specialization
  48. template Eigen::Matrix<double, 3, 1, 0, 3, 1> igl::project<double>(Eigen::Matrix<double, 3, 1, 0, 3, 1> const&, Eigen::Matrix<double, 4, 4, 0, 4, 4> const&, Eigen::Matrix<double, 4, 4, 0, 4, 4> const&, Eigen::Matrix<double, 4, 1, 0, 4, 1> const&);
  49. template Eigen::Matrix<float, 3, 1, 0, 3, 1> igl::project<float>(Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 1, 0, 4, 1> const&);
  50. template void igl::project<Eigen::Matrix<double, -1, -1, 0, -1, -1>, float, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 1, 0, 4, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  51. #endif