Browse Source

changed templating for grad.h, grad.cpp

Former-commit-id: 8ec99a58aa2178ae13b15f5871d820c58b731cad
Olga Diamanti 10 years ago
parent
commit
e3e4fe9902
2 changed files with 37 additions and 36 deletions
  1. 26 26
      include/igl/grad.cpp
  2. 11 10
      include/igl/grad.h

+ 26 - 26
include/igl/grad.cpp

@@ -1,20 +1,20 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
-// 
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #include "grad.h"
 #include <Eigen/Geometry>
 #include <vector>
 
-template <typename T, typename S>
-IGL_INLINE void igl::grad(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &V,
-  const Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> &F,
-  Eigen::SparseMatrix<T> &G )
+template <typename DerivedV, typename DerivedF>
+IGL_INLINE void igl::grad(const Eigen::PlainObjectBase<DerivedV>&V,
+                     const Eigen::PlainObjectBase<DerivedF>&F,
+                    Eigen::SparseMatrix<typename DerivedV::Scalar> &G)
 {
-  Eigen::PlainObjectBase<Eigen::Matrix<T,Eigen::Dynamic,3> > eperp21, eperp13;
+  Eigen::PlainObjectBase<DerivedV > eperp21, eperp13;
   eperp21.resize(F.rows(),3);
   eperp13.resize(F.rows(),3);
 
@@ -24,23 +24,23 @@ IGL_INLINE void igl::grad(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
     int i1 = F(i,0);
     int i2 = F(i,1);
     int i3 = F(i,2);
-    
+
     // #F x 3 matrices of triangle edge vectors, named after opposite vertices
-    Eigen::Matrix<T, 1, 3> v32 = V.row(i3) - V.row(i2);
-    Eigen::Matrix<T, 1, 3> v13 = V.row(i1) - V.row(i3);
-    Eigen::Matrix<T, 1, 3> v21 = V.row(i2) - V.row(i1);
-    
+    Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v32 = V.row(i3) - V.row(i2);
+    Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v13 = V.row(i1) - V.row(i3);
+    Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v21 = V.row(i2) - V.row(i1);
+
     // area of parallelogram is twice area of triangle
-    // area of parallelogram is || v1 x v2 || 
-    Eigen::Matrix<T, 1, 3> n  = v32.cross(v13); 
-    
+    // area of parallelogram is || v1 x v2 ||
+    Eigen::Matrix<typename DerivedV::Scalar, 1, 3> n  = v32.cross(v13);
+
     // This does correct l2 norm of rows, so that it contains #F list of twice
     // triangle areas
     double dblA = std::sqrt(n.dot(n));
-    
+
     // now normalize normals to get unit normals
-    Eigen::Matrix<T, 1, 3> u = n / dblA;
-    
+    Eigen::Matrix<typename DerivedV::Scalar, 1, 3> u = n / dblA;
+
     // rotate each vector 90 degrees around normal
     double norm21 = std::sqrt(v21.dot(v21));
     double norm13 = std::sqrt(v13.dot(v13));
@@ -76,7 +76,7 @@ IGL_INLINE void igl::grad(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
     for(int i=0;i<F.rows();i++) cs.push_back(F(i,2));
     for(int i=0;i<F.rows();i++) cs.push_back(F(i,0));
   }
-  
+
   // values
   for(int i=0;i<F.rows();i++) vs.push_back(eperp13(i,0));
   for(int i=0;i<F.rows();i++) vs.push_back(-eperp13(i,0));
@@ -92,17 +92,17 @@ IGL_INLINE void igl::grad(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
   for(int i=0;i<F.rows();i++) vs.push_back(-eperp21(i,2));
 
   // create sparse gradient operator matrix
-  G.resize(3*F.rows(),V.rows()); 
-  std::vector<Eigen::Triplet<T> > triplets;
+  G.resize(3*F.rows(),V.rows());
+  std::vector<Eigen::Triplet<typename DerivedV::Scalar> > triplets;
   for (int i=0;i<(int)vs.size();++i)
   {
-    triplets.push_back(Eigen::Triplet<T>(rs[i],cs[i],vs[i]));
+    triplets.push_back(Eigen::Triplet<typename DerivedV::Scalar>(rs[i],cs[i],vs[i]));
   }
   G.setFromTriplets(triplets.begin(), triplets.end());
 }
 
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template specialization
-template void igl::grad<double, int>(Eigen::Matrix<double, -1, -1, 0, -1,-1> const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&,Eigen::SparseMatrix<double, 0, int>&);
+// template void igl::grad<double, int>(Eigen::Matrix<double, -1, -1, 0, -1,-1> const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&,Eigen::SparseMatrix<double, 0, int>&);
+template void igl::grad<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::SparseMatrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>::Scalar, 0, int>&);
 #endif
-

+ 11 - 10
include/igl/grad.h

@@ -1,9 +1,9 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
-// 
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #ifndef IGL_GRAD_MAT_H
 #define IGL_GRAD_MAT_H
@@ -24,18 +24,19 @@ namespace igl {
   // Outputs:
   //   G  #faces*dim by #V Gradient operator
   //
-  
+
   // Gradient of a scalar function defined on piecewise linear elements (mesh)
   // is constant on each triangle i,j,k:
   // grad(Xijk) = (Xj-Xi) * (Vi - Vk)^R90 / 2A + (Xk-Xi) * (Vj - Vi)^R90 / 2A
   // where Xi is the scalar value at vertex i, Vi is the 3D position of vertex
-  // i, and A is the area of triangle (i,j,k). ^R90 represent a rotation of 
+  // i, and A is the area of triangle (i,j,k). ^R90 represent a rotation of
   // 90 degrees
   //
-  template <typename T, typename S>
-  IGL_INLINE void grad(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &V,
-    const Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> &F,
-  Eigen::SparseMatrix<T> &G);
+template <typename DerivedV, typename DerivedF>
+IGL_INLINE void grad(const Eigen::PlainObjectBase<DerivedV>&V,
+                     const Eigen::PlainObjectBase<DerivedF>&F,
+                    Eigen::SparseMatrix<typename DerivedV::Scalar> &G);
+
 }
 
 #ifndef IGL_STATIC_LIBRARY