Browse Source

many point project

Former-commit-id: 13414c6d21b81de4142b8bb837504776b843d88c
Alec Jacobson 9 years ago
parent
commit
1f05d754eb
2 changed files with 35 additions and 0 deletions
  1. 21 0
      include/igl/project.cpp
  2. 14 0
      include/igl/project.h

+ 21 - 0
include/igl/project.cpp

@@ -29,9 +29,30 @@ Eigen::Matrix<Scalar,3,1> igl::project(
   return tmp.head(3);
 }
 
+template <typename DerivedV, typename Scalar, typename DerivedP>
+IGL_INLINE void igl::project(
+  const    Eigen::PlainObjectBase<DerivedV>&  V,
+  const    Eigen::Matrix<Scalar,4,4>& model,
+  const    Eigen::Matrix<Scalar,4,4>& proj,
+  const    Eigen::Matrix<Scalar,4,1>&  viewport,
+  Eigen::PlainObjectBase<DerivedP> & P)
+{
+  typedef typename DerivedP::Scalar PScalar;
+  Eigen::Matrix<PScalar,DerivedV::RowsAtCompileTime,4> HV(V.rows(),4);
+  HV.leftCols(3) = V.template cast<PScalar>();
+  HV.col(3).setConstant(1);
+  HV = (HV*model.template cast<PScalar>().transpose()*
+      proj.template cast<PScalar>().transpose()).eval();
+  HV = (HV.array().colwise()/HV.col(3).array()).eval();
+  HV = (HV.array() * 0.5 + 0.5).eval();
+  HV.col(0) = (HV.array().col(0) * viewport(2) + viewport(0)).eval();
+  HV.col(1) = (HV.array().col(1) * viewport(3) + viewport(1)).eval();
+  P = HV.leftCols(3);
+}
 
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template specialization
 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&);
 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&);
+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> >&);
 #endif

+ 14 - 0
include/igl/project.h

@@ -25,6 +25,20 @@ namespace igl
     const    Eigen::Matrix<Scalar,4,4>& model,
     const    Eigen::Matrix<Scalar,4,4>& proj,
     const    Eigen::Matrix<Scalar,4,1>&  viewport);
+  // Inputs:
+  //   V  #V by 3 list of object points
+  //   model  model matrix
+  //   proj  projection matrix
+  //   viewport  viewport vector
+  // Outputs:
+  //   P  #V by 3 list of screen space points
+  template <typename DerivedV, typename Scalar, typename DerivedP>
+  IGL_INLINE void project(
+    const    Eigen::PlainObjectBase<DerivedV>&  V,
+    const    Eigen::Matrix<Scalar,4,4>& model,
+    const    Eigen::Matrix<Scalar,4,4>& proj,
+    const    Eigen::Matrix<Scalar,4,1>&  viewport,
+    Eigen::PlainObjectBase<DerivedP> & P);
 }
 
 #ifndef IGL_STATIC_LIBRARY