1 // This file is part of libigl, a simple c++ geometry processing library.
2 //
3 // Copyright (C) 2014 Nico Pietroni <nico.pietroni@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 
9 #include "line_field_mismatch.h"
10 
11 #include <vector>
12 #include <deque>
13 #include <igl/comb_line_field.h>
14 #include <igl/rotate_vectors.h>
15 #include <igl/comb_cross_field.h>
16 #include <igl/comb_line_field.h>
17 #include <igl/per_face_normals.h>
18 #include <igl/is_border_vertex.h>
19 #include <igl/vertex_triangle_adjacency.h>
20 #include <igl/triangle_triangle_adjacency.h>
21 #include <igl/rotation_matrix_from_directions.h>
22 #include <igl/local_basis.h>
23 #include <igl/PI.h>
24 
25 namespace igl {
26 template <typename DerivedV, typename DerivedF, typename DerivedO>
27 class MismatchCalculatorLine
28 {
29 public:
30 
31     const Eigen::PlainObjectBase<DerivedV> &V;
32     const Eigen::PlainObjectBase<DerivedF> &F;
33     const Eigen::PlainObjectBase<DerivedV> &PD1;
34     const Eigen::PlainObjectBase<DerivedV> &PD2;
35     DerivedV N;
36 
37 private:
38     // internal
39     std::vector<bool> V_border; // bool
40     std::vector<std::vector<int> > VF;
41     std::vector<std::vector<int> > VFi;
42     DerivedF TT;
43     DerivedF TTi;
44 
45 
46 private:
47 
48     //compute the mismatch between 2 faces
mismatchByLine(const int f0,const int f1)49     inline int mismatchByLine(const int f0,
50                               const int f1)
51     {
52         Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir0 = PD1.row(f0);
53         Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1 = PD1.row(f1);
54         Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n0 = N.row(f0);
55         Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n1 = N.row(f1);
56 
57         Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1Rot = igl::rotation_matrix_from_directions(n1,n0)*dir1;
58         dir1Rot.normalize();
59 
60         // TODO: this should be equivalent to the other code below, to check!
61         // Compute the angle between the two vectors
62         //    double a0 = atan2(dir0.dot(B2.row(f0)),dir0.dot(B1.row(f0)));
63         //    double a1 = atan2(dir1Rot.dot(B2.row(f0)),dir1Rot.dot(B1.row(f0)));
64         //
65         //    double angle_diff = a1-a0;   //VectToAngle(f0,dir1Rot);
66 
67         double angle_diff = atan2(dir1Rot.dot(PD2.row(f0)),dir1Rot.dot(PD1.row(f0)));
68 
69         double step=igl::PI;
70         int i=(int)std::floor((angle_diff/step)+0.5);
71         assert((i>=-2)&&(i<=2));
72         int k=0;
73         if (i>=0)
74             k=i%2;
75         else
76             k=(2+i)%2;
77 
78         assert((k==0)||(k==1));
79         return (k*2);
80     }
81 
82 public:
83 
MismatchCalculatorLine(const Eigen::PlainObjectBase<DerivedV> & _V,const Eigen::PlainObjectBase<DerivedF> & _F,const Eigen::PlainObjectBase<DerivedV> & _PD1,const Eigen::PlainObjectBase<DerivedV> & _PD2)84     inline MismatchCalculatorLine(const Eigen::PlainObjectBase<DerivedV> &_V,
85                                const Eigen::PlainObjectBase<DerivedF> &_F,
86                                const Eigen::PlainObjectBase<DerivedV> &_PD1,
87                                const Eigen::PlainObjectBase<DerivedV> &_PD2
88                                ):
89         V(_V),
90         F(_F),
91         PD1(_PD1),
92         PD2(_PD2)
93     {
94         igl::per_face_normals(V,F,N);
95         V_border = igl::is_border_vertex(V,F);
96         igl::vertex_triangle_adjacency(V,F,VF,VFi);
97         igl::triangle_triangle_adjacency(F,TT,TTi);
98     }
99 
calculateMismatchLine(Eigen::PlainObjectBase<DerivedO> & Handle_MMatch)100     inline void calculateMismatchLine(Eigen::PlainObjectBase<DerivedO> &Handle_MMatch)
101     {
102         Handle_MMatch.setConstant(F.rows(),3,-1);
103         for (unsigned int i=0;i<F.rows();i++)
104         {
105             for (int j=0;j<3;j++)
106             {
107                 if (i==TT(i,j) || TT(i,j) == -1)
108                     Handle_MMatch(i,j)=0;
109                 else
110                     Handle_MMatch(i,j) = mismatchByLine(i, TT(i, j));
111             }
112         }
113     }
114 
115 };
116 }
117 
118 
119 template <typename DerivedV, typename DerivedF, typename DerivedO>
line_field_mismatch(const Eigen::PlainObjectBase<DerivedV> & V,const Eigen::PlainObjectBase<DerivedF> & F,const Eigen::PlainObjectBase<DerivedV> & PD1,const bool isCombed,Eigen::PlainObjectBase<DerivedO> & mismatch)120 IGL_INLINE void igl::line_field_mismatch(const Eigen::PlainObjectBase<DerivedV> &V,
121                                          const Eigen::PlainObjectBase<DerivedF> &F,
122                                          const Eigen::PlainObjectBase<DerivedV> &PD1,
123                                          const bool isCombed,
124                                          Eigen::PlainObjectBase<DerivedO> &mismatch)
125 {
126     DerivedV PD1_combed;
127     DerivedV PD2_combed;
128 
129     if (!isCombed)
130         igl::comb_line_field(V,F,PD1,PD1_combed);
131     else
132     {
133         PD1_combed = PD1;
134     }
135     Eigen::MatrixXd B1,B2,B3;
136     igl::local_basis(V,F,B1,B2,B3);
137     PD2_combed = igl::rotate_vectors(PD1_combed, Eigen::VectorXd::Constant(1,igl::PI/2), B1, B2);
138     igl::MismatchCalculatorLine<DerivedV, DerivedF, DerivedO> sf(V, F, PD1_combed, PD2_combed);
139     sf.calculateMismatchLine(mismatch);
140 }
141 
142 #ifdef IGL_STATIC_LIBRARY
143 // Explicit template instantiation
144 #endif
145