1 #ifndef _ISO_TRANSFER
2 #define _ISO_TRANSFER
3 
4 #include <iso_parametrization.h>
5 
6 //#include<vcg/simplex/edge/base.h>
7 //#include<vcg/simplex/vertex/base.h>
8 //#include<vcg/simplex/face/base.h>
9 #include <vcg/complex/complex.h>
10 //#include <vcg/complex/algorithms/update/topology.h>
11 //#include <vcg/complex/algorithms/update/edges.h>
12 //#include <vcg/complex/algorithms/update/bounding.h>
13 //#include <vcg/complex/algorithms/update/flag.h>
14 //#include <vcg/space/index/grid_static_ptr.h>
15 //#include <vcg/complex/algorithms/closest.h>
16 
17 class IsoTransfer
18 {
19     typedef vcg::GridStaticPtr<ParamMesh::FaceType, ParamMesh::ScalarType> TriMeshGrid;
20     typedef ParamMesh::CoordType CoordType;
21     typedef ParamMesh::ScalarType ScalarType;
22     TriMeshGrid TRGrid;
23 
Clamp(CoordType & bary)24     void Clamp(CoordType &bary)
25     {
26     /*	float eps=0.01;*/
27         float sum=0;
28         int bigger=0;
29         int lower=0;
30     /*	for (int i=0;i<3;i++)
31         {
32             if ((bary.V(i)<eps)&&(bary.V(i)>-eps))
33                 bary.V(i)=0;
34             if ((bary.V(i)<1+eps)&&(bary.V(i)>1-eps))
35                 bary.V(i)=1;
36 
37             sum+=bary.V(i);
38 
39             if (bary.V(i)>bary.V(bigger))
40                 bigger=i;
41             if (bary.V(i)<bary.V(lower))
42                 lower=i;
43         }
44         assert(bigger!=lower);
45         if (sum>(1.0+eps))
46         {
47             float diff=sum-1.0;
48             bary.V(bigger)-=diff;
49         }
50         else
51         if (sum<(1.0-eps))
52         {
53             float diff=1.0-sum;
54             bary.V(lower)+=diff;
55         }*/
56             for (int i=0;i<3;i++)
57         {
58             if (bary.V(i)<0)
59                 bary.V(i)=0;
60             if (bary.V(i)>1)
61                 bary.V(i)=1;
62 
63             sum+=bary.V(i);
64 
65             if (bary.V(i)>bary.V(bigger))
66                 bigger=i;
67             if (bary.V(i)<bary.V(lower))
68                 lower=i;
69         }
70         //assert(bigger!=lower);
71         if (sum>(1.0))
72         {
73             float diff=sum-1.0;
74             bary.V(bigger)-=diff;
75         }
76         else
77         if (sum<(1.0))
78         {
79             float diff=1.0-sum;
80             bary.V(lower)+=diff;
81         }
82     }
83 
84     public:
85     template <class MeshType>
Transfer(IsoParametrization & IsoParam,MeshType & to_assing)86     void Transfer(IsoParametrization &IsoParam,
87                                 MeshType &to_assing)
88     {
89         ///put the mesh in the grid
90         typedef typename MeshType::ScalarType ScalarType;
91         vcg::tri::UpdateBounding<ParamMesh>::Box(*IsoParam.ParaMesh());
92         vcg::tri::UpdateNormal<ParamMesh>::PerFaceNormalized(*IsoParam.ParaMesh());
93         vcg::tri::UpdateNormal<ParamMesh>::PerVertexAngleWeighted(*IsoParam.ParaMesh());
94         vcg::tri::UpdateNormal<ParamMesh>::NormalizePerVertex(*IsoParam.ParaMesh());
95 
96         TRGrid.Set(IsoParam.ParaMesh()->face.begin(),IsoParam.ParaMesh()->face.end());
97         ScalarType maxDist=IsoParam.ParaMesh()->bbox.Diag();
98         ///then for each vertex find the closest
99     for (size_t i=0;i<to_assing.vert.size();i++)
100         {
101       typename MeshType::VertexType *vert=&to_assing.vert[i];
102             if (!vert->IsD())
103             {
104                 typename ParamMesh::ScalarType dist;
105                 typename ParamMesh::CoordType queryPoint,closest,bary;
106                 ParamMesh::FaceType * f=NULL;
107                 queryPoint.Import(vert->P());
108                 f=GetClosestFaceBase(*IsoParam.ParaMesh(),TRGrid,queryPoint, maxDist,dist,closest);
109                 vcg::InterpolationParameters<typename ParamMesh::FaceType,typename ParamMesh::ScalarType>(*f,f->N(),closest, bary);
110                 assert(f!=NULL);
111 
112                 ///then find back the coordinates
113                 if (!((bary.X()>=0)&&(bary.X()<=1)&&
114                       (bary.Y()>=0)&&(bary.Y()<=1)&&
115                         (bary.Z()>=0)&&(bary.Z()<=1)))
116                 {
117             printf("%i,%3.3f,%3.3f,%3.3f",int(i),bary.X(),bary.Y(),bary.Z());
118                         system("pause");
119                 }
120                 Clamp(bary);
121                 int I;
122                 vcg::Point2<typename ParamMesh::ScalarType> UV;
123                 IsoParam.Phi(f,bary,I,UV);
124                 ///and finally set to the vertex
125                 assert(I>=0);
126                 vert->T().P().Import(UV);
127                 vert->T().N()=I;
128                 vert->Q()=(typename MeshType::ScalarType)I;
129             }
130         }
131     }
132 
133 };
134 
135 #endif
136