1 // This file is part of libigl, a simple c++ geometry processing library.
2 //
3 // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>, Olga Diamanti <olga.diam@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 "cross_field_mismatch.h"
10 
11 #include <cmath>
12 #include <vector>
13 #include <deque>
14 #include <igl/comb_cross_field.h>
15 #include <igl/per_face_normals.h>
16 #include <igl/is_border_vertex.h>
17 #include <igl/vertex_triangle_adjacency.h>
18 #include <igl/triangle_triangle_adjacency.h>
19 #include <igl/rotation_matrix_from_directions.h>
20 #include <igl/PI.h>
21 
22 namespace igl {
23   template <typename DerivedV, typename DerivedF, typename DerivedM>
24   class MismatchCalculator
25   {
26   public:
27 
28     const Eigen::PlainObjectBase<DerivedV> &V;
29     const Eigen::PlainObjectBase<DerivedF> &F;
30     const Eigen::PlainObjectBase<DerivedV> &PD1;
31     const Eigen::PlainObjectBase<DerivedV> &PD2;
32 
33     DerivedV N;
34 
35   private:
36     // internal
37     std::vector<bool> V_border; // bool
38     std::vector<std::vector<int> > VF;
39     std::vector<std::vector<int> > VFi;
40 
41     DerivedF TT;
42     DerivedF TTi;
43 
44 
45   private:
46     ///compute the mismatch between 2 faces
mismatchByCross(const int f0,const int f1)47     inline int mismatchByCross(const int f0,
48                                const int f1)
49     {
50       Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir0 = PD1.row(f0);
51       Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1 = PD1.row(f1);
52       Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n0 = N.row(f0);
53       Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n1 = N.row(f1);
54 
55       Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1Rot = igl::rotation_matrix_from_directions(n1,n0)*dir1;
56       dir1Rot.normalize();
57 
58       double angle_diff = atan2(dir1Rot.dot(PD2.row(f0)),dir1Rot.dot(PD1.row(f0)));
59 
60       double step=igl::PI/2.0;
61       int i=(int)std::floor((angle_diff/step)+0.5);
62       int k=0;
63       if (i>=0)
64         k=i%4;
65       else
66         k=(-(3*i))%4;
67       return k;
68     }
69 
70 
71 public:
MismatchCalculator(const Eigen::PlainObjectBase<DerivedV> & _V,const Eigen::PlainObjectBase<DerivedF> & _F,const Eigen::PlainObjectBase<DerivedV> & _PD1,const Eigen::PlainObjectBase<DerivedV> & _PD2)72   inline MismatchCalculator(const Eigen::PlainObjectBase<DerivedV> &_V,
73                             const Eigen::PlainObjectBase<DerivedF> &_F,
74                             const Eigen::PlainObjectBase<DerivedV> &_PD1,
75                             const Eigen::PlainObjectBase<DerivedV> &_PD2):
76   V(_V),
77   F(_F),
78   PD1(_PD1),
79   PD2(_PD2)
80   {
81     igl::per_face_normals(V,F,N);
82     V_border = igl::is_border_vertex(V,F);
83     igl::vertex_triangle_adjacency(V,F,VF,VFi);
84     igl::triangle_triangle_adjacency(F,TT,TTi);
85   }
86 
calculateMismatch(Eigen::PlainObjectBase<DerivedM> & Handle_MMatch)87   inline void calculateMismatch(Eigen::PlainObjectBase<DerivedM> &Handle_MMatch)
88   {
89     Handle_MMatch.setConstant(F.rows(),3,-1);
90     for (size_t i=0;i<F.rows();i++)
91     {
92       for (int j=0;j<3;j++)
93       {
94         if (((int)i)==TT(i,j) || TT(i,j) == -1)
95           Handle_MMatch(i,j)=0;
96         else
97           Handle_MMatch(i,j) = mismatchByCross(i, TT(i, j));
98       }
99     }
100   }
101 
102 };
103 }
104 template <typename DerivedV, typename DerivedF, typename DerivedM>
cross_field_mismatch(const Eigen::PlainObjectBase<DerivedV> & V,const Eigen::PlainObjectBase<DerivedF> & F,const Eigen::PlainObjectBase<DerivedV> & PD1,const Eigen::PlainObjectBase<DerivedV> & PD2,const bool isCombed,Eigen::PlainObjectBase<DerivedM> & mismatch)105 IGL_INLINE void igl::cross_field_mismatch(const Eigen::PlainObjectBase<DerivedV> &V,
106                                           const Eigen::PlainObjectBase<DerivedF> &F,
107                                           const Eigen::PlainObjectBase<DerivedV> &PD1,
108                                           const Eigen::PlainObjectBase<DerivedV> &PD2,
109                                           const bool isCombed,
110                                           Eigen::PlainObjectBase<DerivedM> &mismatch)
111 {
112   DerivedV PD1_combed;
113   DerivedV PD2_combed;
114 
115   if (!isCombed)
116     igl::comb_cross_field(V,F,PD1,PD2,PD1_combed,PD2_combed);
117   else
118   {
119     PD1_combed = PD1;
120     PD2_combed = PD2;
121   }
122   igl::MismatchCalculator<DerivedV, DerivedF, DerivedM> sf(V, F, PD1_combed, PD2_combed);
123   sf.calculateMismatch(mismatch);
124 }
125 
126 #ifdef IGL_STATIC_LIBRARY
127 // Explicit template instantiation
128 template void igl::cross_field_mismatch<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::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const &,  Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const &, const bool,  Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > &);
129 template void igl::cross_field_mismatch<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >( Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const &, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const &, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const &, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const &, const bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > &);
130 template void igl::cross_field_mismatch<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const &, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const &, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const &, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const &, const bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > &);
131 
132 #endif
133