Browse Source

use correct deducd types and rename function

hanxiao 6 years ago
parent
commit
ce07eb79f4

+ 32 - 19
include/igl/predicates/ear_clipping.cpp

@@ -1,7 +1,14 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2019 Hanxiao Shen <[email protected]>
+//
+// 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 <igl/slice.h>
 #include <igl/slice.h>
 #include "ear_clipping.h"
 #include "ear_clipping.h"
-#include "point_inside_polygon.h"
+#include "point_inside_convex_polygon.h"
 #include "predicates.h"
 #include "predicates.h"
 
 
 template <typename DerivedP, typename DerivedRT,
 template <typename DerivedP, typename DerivedRT,
@@ -13,20 +20,26 @@ IGL_INLINE void igl::predicates::ear_clipping(
   Eigen::PlainObjectBase<DerivedF>& eF,
   Eigen::PlainObjectBase<DerivedF>& eF,
   Eigen::PlainObjectBase<DerivedP>& nP
   Eigen::PlainObjectBase<DerivedP>& nP
 ){
 ){
+  typedef typename DerivedF::Scalar Index;
+  typedef typename DerivedP::Scalar Scalar;
+  static_assert(std::is_same<typename DerivedI::Scalar,
+                             typename DerivedF::Scalar>::value,
+                "index type should be consistent");
   
   
   // check whether vertex i is an ear
   // check whether vertex i is an ear
   auto is_ear = [](
   auto is_ear = [](
     const Eigen::MatrixBase<DerivedP>& P,
     const Eigen::MatrixBase<DerivedP>& P,
     const Eigen::MatrixBase<DerivedRT>& RT,
     const Eigen::MatrixBase<DerivedRT>& RT,
-    const Eigen::VectorXi& L,
-    const Eigen::VectorXi& R,
-    const int i
+    const Eigen::Matrix<Index,-1,1>& L,
+    const Eigen::Matrix<Index,-1,1>& R,
+    const Index i
   ){
   ){
-    int a = L(i), b = R(i);
+    
+    Index a = L(i), b = R(i);
     if(RT(i) != 0 || RT(a) != 0 || RT(b) != 0) return false;
     if(RT(i) != 0 || RT(a) != 0 || RT(b) != 0) return false;
-    Eigen::RowVector2d pa = P.row(a);
-    Eigen::RowVector2d pb = P.row(b);
-    Eigen::RowVector2d pi = P.row(i);
+    Eigen::Matrix<Scalar,1,2> pa = P.row(a);
+    Eigen::Matrix<Scalar,1,2> pb = P.row(b);
+    Eigen::Matrix<Scalar,1,2> pi = P.row(i);
     auto r = igl::predicates::orient2d(pa,pi,pb);
     auto r = igl::predicates::orient2d(pa,pi,pb);
     if(r == igl::predicates::Orientation::NEGATIVE || 
     if(r == igl::predicates::Orientation::NEGATIVE || 
        r == igl::predicates::Orientation::COLLINEAR) return false;
        r == igl::predicates::Orientation::COLLINEAR) return false;
@@ -34,21 +47,21 @@ IGL_INLINE void igl::predicates::ear_clipping(
     // check if any vertex is lying inside triangle (a,b,i);
     // check if any vertex is lying inside triangle (a,b,i);
     int k=R(b);
     int k=R(b);
     while(k!=a){
     while(k!=a){
-      Eigen::MatrixX2d T(3,2);
+      Eigen::Matrix<Scalar,-1,2> T(3,2);
       T<<P.row(a),P.row(i),P.row(b);
       T<<P.row(a),P.row(i),P.row(b);
-      Eigen::RowVector2d q=P.row(k);
-      if(igl::predicates::point_inside_polygon(T,q)) 
+      Eigen::Matrix<Scalar,1,2> q=P.row(k);
+      if(igl::predicates::point_inside_convex_polygon(T,q))
         return false;
         return false;
       k=R(k);
       k=R(k);
     }
     }
     return true;
     return true;
   };
   };
 
 
-  Eigen::VectorXi L(P.rows());
-  Eigen::VectorXi R(P.rows());
+  Eigen::Matrix<Index,-1,1> L(P.rows());
+  Eigen::Matrix<Index,-1,1> R(P.rows());
   for(int i=0;i<P.rows();i++){
   for(int i=0;i<P.rows();i++){
-    L(i) = (i-1+P.rows())%P.rows();
-    R(i) = (i+1)%P.rows();
+    L(i) = Index((i-1+P.rows())%P.rows());
+    R(i) = Index((i+1)%P.rows());
   }
   }
 
 
   Eigen::VectorXi ears; // mark ears
   Eigen::VectorXi ears; // mark ears
@@ -65,11 +78,11 @@ IGL_INLINE void igl::predicates::ear_clipping(
   while(ears.maxCoeff()==1){
   while(ears.maxCoeff()==1){
     
     
     // find the first ear
     // find the first ear
-    int e = 0;
+    Index e = 0;
     while(e<ears.rows()&&ears(e)!=1) e++;
     while(e<ears.rows()&&ears(e)!=1) e++;
     
     
     // find valid neighbors
     // find valid neighbors
-    int a = L(e), b = R(e);
+    Index a = L(e), b = R(e);
     if(a == b) break;
     if(a == b) break;
 
 
     // clip ear and store face
     // clip ear and store face
@@ -98,8 +111,8 @@ IGL_INLINE void igl::predicates::ear_clipping(
   for(int i=0;i<X.rows();i++)
   for(int i=0;i<X.rows();i++)
     X(i) = 1-X(i);
     X(i) = 1-X(i);
   I.resize(X.sum());
   I.resize(X.sum());
-  int j=0;
-  for(int i=0;i<X.rows();i++)
+  Index j=0;
+  for(Index i=0;i<X.rows();i++)
     if(X(i)==1){
     if(X(i)==1){
       I(j++) = i;
       I(j++) = i;
     }
     }

+ 1 - 1
include/igl/predicates/ear_clipping.h

@@ -48,4 +48,4 @@ namespace igl
 #endif
 #endif
 
 
 
 
-#endif
+#endif

+ 8 - 5
include/igl/predicates/point_inside_polygon.cpp → include/igl/predicates/point_inside_convex_polygon.cpp

@@ -6,17 +6,20 @@
 // v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // 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/.
 // obtain one at http://mozilla.org/MPL/2.0/.
 
 
-#include "point_inside_polygon.h"
+#include "point_inside_convex_polygon.h"
 
 
 template <typename DerivedP, typename DerivedQ>
 template <typename DerivedP, typename DerivedQ>
-IGL_INLINE bool igl::predicates::point_inside_polygon(
+IGL_INLINE bool igl::predicates::point_inside_convex_polygon(
   const Eigen::MatrixBase<DerivedP>& P,
   const Eigen::MatrixBase<DerivedP>& P,
   const Eigen::MatrixBase<DerivedQ>& q
   const Eigen::MatrixBase<DerivedQ>& q
 ){
 ){
+  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(DerivedP, -1, 2);
+  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(DerivedQ, 1, 2);
+  typedef typename DerivedP::Scalar Scalar;
   for(int i=0;i<P.rows();i++){
   for(int i=0;i<P.rows();i++){
     int i_1 = (i+1) % P.rows();
     int i_1 = (i+1) % P.rows();
-    Eigen::RowVector2d a = P.row(i);
-    Eigen::RowVector2d b = P.row(i_1);
+    Eigen::Matrix<Scalar,1,2> a = P.row(i);
+    Eigen::Matrix<Scalar,1,2> b = P.row(i_1);
     auto r = igl::predicates::orient2d(a,b,q);
     auto r = igl::predicates::orient2d(a,b,q);
     if(r == igl::predicates::Orientation::COLLINEAR || 
     if(r == igl::predicates::Orientation::COLLINEAR || 
        r == igl::predicates::Orientation::NEGATIVE)
        r == igl::predicates::Orientation::NEGATIVE)
@@ -26,5 +29,5 @@ IGL_INLINE bool igl::predicates::point_inside_polygon(
 }
 }
 
 
 #ifdef IGL_STATIC_LIBRARY
 #ifdef IGL_STATIC_LIBRARY
-template bool igl::predicates::point_inside_polygon<Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<double, 1, 2, 1, 1, 2> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&);
+template bool igl::predicates::point_inside_convex_polygon<Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<double, 1, 2, 1, 1, 2> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&);
 #endif
 #endif

+ 4 - 4
include/igl/predicates/point_inside_polygon.h → include/igl/predicates/point_inside_convex_polygon.h

@@ -6,8 +6,8 @@
 // v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // 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/.
 // obtain one at http://mozilla.org/MPL/2.0/.
 
 
-#ifndef PREDICATE_POINT_INSIDE_POLYGON_H
-#define PREDICATE_POINT_INSIDE_POLYGON_H
+#ifndef PREDICATE_POINT_INSIDE_CONVEX_POLYGON_H
+#define PREDICATE_POINT_INSIDE_CONVEX_POLYGON_H
 
 
 
 
 
 
@@ -25,7 +25,7 @@ namespace igl
     //   q: 2d query point
     //   q: 2d query point
     // Returns true if point is inside polygon
     // Returns true if point is inside polygon
     template <typename DerivedP, typename DerivedQ>
     template <typename DerivedP, typename DerivedQ>
-    IGL_INLINE bool point_inside_polygon(
+    IGL_INLINE bool point_inside_convex_polygon(
         const Eigen::MatrixBase<DerivedP>& P,
         const Eigen::MatrixBase<DerivedP>& P,
         const Eigen::MatrixBase<DerivedQ>& q
         const Eigen::MatrixBase<DerivedQ>& q
     );
     );
@@ -33,7 +33,7 @@ namespace igl
 }
 }
 
 
 #ifndef IGL_STATIC_LIBRARY
 #ifndef IGL_STATIC_LIBRARY
-#  include "point_inside_polygon.cpp"
+#  include "point_inside_convex_polygon.cpp"
 #endif
 #endif
 
 
 #endif
 #endif