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_missmatch.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 MissMatchCalculator
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
MissMatchByCross(const int f0,const int f1)47     inline int MissMatchByCross(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       // TODO: this should be equivalent to the other code below, to check!
59       // Compute the angle between the two vectors
60       //    double a0 = atan2(dir0.dot(B2.row(f0)),dir0.dot(B1.row(f0)));
61       //    double a1 = atan2(dir1Rot.dot(B2.row(f0)),dir1Rot.dot(B1.row(f0)));
62       //
63       //    double angle_diff = a1-a0;   //VectToAngle(f0,dir1Rot);
64 
65       double angle_diff = atan2(dir1Rot.dot(PD2.row(f0)),dir1Rot.dot(PD1.row(f0)));
66 
67       //    std::cerr << "Dani: " << dir0(0) << " " << dir0(1) << " " << dir0(2) << " " << dir1Rot(0) << " " << dir1Rot(1) << " " << dir1Rot(2) << " " << angle_diff << std::endl;
68 
69       double step=igl::PI/2.0;
70       int i=(int)std::floor((angle_diff/step)+0.5);
71       int k=0;
72       if (i>=0)
73         k=i%4;
74       else
75         k=(-(3*i))%4;
76       return k;
77     }
78 
79 
80 public:
MissMatchCalculator(const Eigen::PlainObjectBase<DerivedV> & _V,const Eigen::PlainObjectBase<DerivedF> & _F,const Eigen::PlainObjectBase<DerivedV> & _PD1,const Eigen::PlainObjectBase<DerivedV> & _PD2)81   inline MissMatchCalculator(const Eigen::PlainObjectBase<DerivedV> &_V,
82                       const Eigen::PlainObjectBase<DerivedF> &_F,
83                       const Eigen::PlainObjectBase<DerivedV> &_PD1,
84                       const Eigen::PlainObjectBase<DerivedV> &_PD2
85                       ):
86   V(_V),
87   F(_F),
88   PD1(_PD1),
89   PD2(_PD2)
90   {
91     igl::per_face_normals(V,F,N);
92     V_border = igl::is_border_vertex(V,F);
93     igl::vertex_triangle_adjacency(V,F,VF,VFi);
94     igl::triangle_triangle_adjacency(F,TT,TTi);
95   }
96 
calculateMissmatch(Eigen::PlainObjectBase<DerivedM> & Handle_MMatch)97   inline void calculateMissmatch(Eigen::PlainObjectBase<DerivedM> &Handle_MMatch)
98   {
99     Handle_MMatch.setConstant(F.rows(),3,-1);
100     for (size_t i=0;i<F.rows();i++)
101     {
102       for (int j=0;j<3;j++)
103       {
104         if (((int)i)==TT(i,j) || TT(i,j) == -1)
105           Handle_MMatch(i,j)=0;
106         else
107           Handle_MMatch(i,j) = MissMatchByCross(i,TT(i,j));
108       }
109     }
110   }
111 
112 };
113 }
114 template <typename DerivedV, typename DerivedF, typename DerivedM>
cross_field_missmatch(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> & missmatch)115 IGL_INLINE void igl::cross_field_missmatch(const Eigen::PlainObjectBase<DerivedV> &V,
116                                            const Eigen::PlainObjectBase<DerivedF> &F,
117                                            const Eigen::PlainObjectBase<DerivedV> &PD1,
118                                            const Eigen::PlainObjectBase<DerivedV> &PD2,
119                                            const bool isCombed,
120                                            Eigen::PlainObjectBase<DerivedM> &missmatch)
121 {
122   DerivedV PD1_combed;
123   DerivedV PD2_combed;
124 
125   if (!isCombed)
126     igl::comb_cross_field(V,F,PD1,PD2,PD1_combed,PD2_combed);
127   else
128   {
129     PD1_combed = PD1;
130     PD2_combed = PD2;
131   }
132   igl::MissMatchCalculator<DerivedV, DerivedF, DerivedM> sf(V, F, PD1_combed, PD2_combed);
133   sf.calculateMissmatch(missmatch);
134 }
135 
136 #ifdef IGL_STATIC_LIBRARY
137 // Explicit template instantiation
138 template void igl::cross_field_missmatch<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&, bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
139 template void igl::cross_field_missmatch<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&, bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
140 template void igl::cross_field_missmatch<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&, bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
141 
142 #endif
143