~ubuntu-branches/ubuntu/lucid/meshlab/lucid

« back to all changes in this revision

Viewing changes to meshlab/src/meshlabplugins/filter_isoparametrization/param_flip.h

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-10-08 16:40:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20091008164041-0c2ealqv8b8uc20c
Tags: 1.2.2-1
* New upstream version
* Do not build filter_isoparametrization because liblevmar dependency
  is not (yet) in Debian
* Fix compilation with gcc-4.4, thanks to Jonathan Liu for the patch
  (closes: #539544)
* rules: Add compiler variables to the qmake call (for testing with new
  GCC versions)
* io_3ds.pro: Make LIBS and INCLUDEPATH point to Debian version of lib3ds
* io_epoch.pro: Make LIBS point to Debian version of libbz2
* control:
  - Move Homepage URL to the source package section
  - Update to standards-version 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef PARAM_FLIP
 
2
#define PARAM_FLIP
 
3
 
 
4
#include <vcg/complex/local_optimization/tri_edge_flip.h>
 
5
 
 
6
///Flip function
 
7
template <class BaseMesh>
 
8
class ParamEdgeFlip : public vcg::tri::PlanarEdgeFlip<BaseMesh, ParamEdgeFlip<BaseMesh> >
 
9
{
 
10
        typedef typename BaseMesh::VertexType::EdgeType EdgeType;
 
11
        typedef typename BaseMesh::VertexType BaseVertex;
 
12
        typedef typename BaseMesh::VertexType VertexType;
 
13
        typedef typename BaseMesh::FaceType   BaseFace;
 
14
        typedef typename BaseMesh::FaceType   FaceType;
 
15
        typedef typename BaseMesh::CoordType   CoordType;
 
16
        typedef typename BaseMesh::ScalarType   ScalarType;
 
17
        typedef vcg::tri::PlanarEdgeFlip<BaseMesh, ParamEdgeFlip<BaseMesh> > Super;
 
18
        ScalarType diff;
 
19
        
 
20
        public:
 
21
 
 
22
        bool savedomain;
 
23
 
 
24
        bool IsFeasible()
 
25
        {
 
26
                if(!vcg::face::CheckFlipEdge(*this->_pos.F(), this->_pos.E()))
 
27
                        return false;
 
28
                ///ScalarType diff=EdgeDiff();
 
29
                /*return true;*/
 
30
                return (this->_priority>0);
 
31
        }
 
32
 
 
33
        inline ParamEdgeFlip() {}
 
34
 
 
35
        /*!
 
36
         *      Constructor with <I>pos</I> type
 
37
         */
 
38
        inline ParamEdgeFlip(const typename Super::PosType pos, int mark)
 
39
        {
 
40
                this->_pos = pos;
 
41
                this->_localMark = mark;
 
42
                this->_priority = this->ComputePriority();
 
43
                savedomain=false;
 
44
        }
 
45
 
 
46
        ///do the effective flip 
 
47
        void ExecuteFlip(FaceType &f, const int &edge)
 
48
        {
 
49
                std::vector<FaceType*> faces;
 
50
                faces.push_back(&f);
 
51
                faces.push_back(f.FFp(edge));
 
52
                std::vector<VertexType*> HresVert;
 
53
                getHresVertex<FaceType>(faces,HresVert);
 
54
                ///parametrize H_res mesh respect to diamond
 
55
                for (unsigned int i=0;i<HresVert.size();i++)
 
56
                {
 
57
                        VertexType* v=HresVert[i];
 
58
                        ///get father & bary
 
59
                        FaceType* father=v->father;
 
60
                        CoordType bary=v->Bary;
 
61
                        assert((father==faces[0])||(father==faces[1]));
 
62
                        vcg::Point2<ScalarType> t0=father->V(0)->T().P();
 
63
                        vcg::Point2<ScalarType> t1=father->V(1)->T().P();
 
64
                        vcg::Point2<ScalarType> t2=father->V(2)->T().P();
 
65
 
 
66
                        //assert(testBaryCoords(bary));
 
67
                        if(!testBaryCoords(bary))
 
68
                {
 
69
                        printf("BAry0 :%lf,%lf,%lf",bary.X(),bary.Y(),bary.Z());
 
70
                        system("pause");
 
71
                }
 
72
 
 
73
                        GetUV<BaseMesh>(father,bary,v->T().U(),v->T().V());
 
74
                }
 
75
                ///then do the effective flip
 
76
                vcg::face::FlipEdge(f,edge);
 
77
 
 
78
                ///set son->father new link
 
79
                for (unsigned int i=0;i<HresVert.size();i++)
 
80
                {
 
81
                        VertexType* v=HresVert[i];
 
82
                        ScalarType U=v->T().U();
 
83
                        ScalarType V=v->T().V();
 
84
                        CoordType bary;
 
85
                        int index;
 
86
                        bool found=GetBaryFaceFromUV(faces,U,V,bary,index);
 
87
                        if (!found)
 
88
                        {
 
89
                                printf("\n U : %lf; V : %lf \n",U,V);
 
90
                                system("pause");
 
91
                        }
 
92
                        //assert(found);
 
93
                        assert(testBaryCoords(bary));
 
94
                        v->father=faces[index];
 
95
                        v->Bary=bary;
 
96
                }
 
97
                
 
98
                
 
99
 
 
100
                ///set father->son new link
 
101
                for (unsigned int i=0;i<faces.size();i++)
 
102
                        faces[i]->vertices_bary.clear();
 
103
 
 
104
                for (unsigned int i=0;i<HresVert.size();i++)
 
105
                {
 
106
                        VertexType *son=HresVert[i];
 
107
                        FaceType *father=son->father;
 
108
                        CoordType bary=son->Bary;
 
109
                        father->vertices_bary.push_back(std::pair<BaseVertex*,vcg::Point3f>(son,bary));
 
110
                }
 
111
                
 
112
        }
 
113
 
 
114
        ScalarType EdgeDiff()
 
115
        {
 
116
                /*
 
117
                     1  
 
118
                    /|\
 
119
                   / | \
 
120
                 2 f0|f1 3 
 
121
                   \ | /
 
122
                    \|/
 
123
                     0
 
124
                 */
 
125
                
 
126
                VertexType *v0, *v1, *v2, *v3;
 
127
                int edge0 = this->_pos.E();
 
128
                v0 = this->_pos.F()->V0(edge0);
 
129
                v1 = this->_pos.F()->V1(edge0);
 
130
                v2 = this->_pos.F()->V2(edge0);
 
131
                v3 = this->_pos.F()->FFp(edge0)->V2(this->_pos.F()->FFi(edge0));
 
132
                int edge1=this->_pos.F()->FFi(edge0);
 
133
                FaceType* f0=this->_pos.F();
 
134
                FaceType* f1=this->_pos.F()->FFp(edge0);
 
135
 
 
136
                ///parametrize all possible diamonds
 
137
                ///diam0 & diam1
 
138
                ///make a copy of the mesh
 
139
                std::vector<FaceType*> OrdFace;
 
140
                OrdFace.push_back(f0);
 
141
                OrdFace.push_back(f1);
 
142
                
 
143
                BaseMesh Diam;
 
144
                BaseMesh DiamHres;
 
145
 
 
146
                ///create a copy of the domain and of the H resolution
 
147
                CopySubMeshLevels(OrdFace,Diam,DiamHres);
 
148
                
 
149
                ///parametrize domains
 
150
                ParametrizeDiamondEquilateral(Diam,edge0,edge1);
 
151
 
 
152
                ///copy parametrization on original mesh
 
153
 
 
154
                FaceType* on_edge[2];
 
155
                on_edge[0]=&Diam.face[0];
 
156
                on_edge[1]=&Diam.face[1];
 
157
                assert(Diam.face[0].FFp(edge0)==&Diam.face[1]);///test
 
158
                assert(Diam.face[1].FFp(edge1)==&Diam.face[0]);///test
 
159
 
 
160
                ///Evaluate lenght of shared edge
 
161
                ScalarType L0=EstimateLenghtByParam<BaseMesh>(Diam.face[0].V(edge0),Diam.face[0].V((edge0+1)%3),on_edge);
 
162
 
 
163
                ///do the flip on the copied mesh do not affect the original mesh
 
164
                ExecuteFlip(Diam.face[0],edge0);
 
165
                
 
166
                UpdateTopologies(&Diam);
 
167
                
 
168
                ///get the non border edge of face0
 
169
                int NB_edge=-1;
 
170
                if (!Diam.face[0].IsB(0))
 
171
                        NB_edge=0;
 
172
                else
 
173
                if (!Diam.face[0].IsB(1))
 
174
                        NB_edge=1;
 
175
                else
 
176
                if (!Diam.face[0].IsB(2))
 
177
                        NB_edge=2;
 
178
                assert(NB_edge!=-1);
 
179
 
 
180
                ScalarType L1=EstimateLenghtByParam<BaseMesh>(Diam.face[0].V(NB_edge),Diam.face[0].V((NB_edge+1)%3),on_edge);
 
181
                
 
182
                ScalarType value=L0-L1;
 
183
                diff=value;
 
184
                this->_priority = 1.0/value;
 
185
                return (this->_priority);
 
186
        }
 
187
        
 
188
        
 
189
        void Execute(BaseMesh &m)
 
190
        {
 
191
                
 
192
                assert(this->_priority>0);
 
193
                /*
 
194
                     1  
 
195
                    /|\
 
196
                   / | \
 
197
                 2 f0|f1 3 
 
198
                   \ | /
 
199
                    \|/
 
200
                     0
 
201
                 */
 
202
                VertexType *v0, *v1, *v2, *v3;
 
203
                int edge0 = this->_pos.E();
 
204
                v0 = this->_pos.F()->V0(edge0);
 
205
                v1 = this->_pos.F()->V1(edge0);
 
206
                v2 = this->_pos.F()->V2(edge0);
 
207
                v3 = this->_pos.F()->FFp(edge0)->V2(this->_pos.F()->FFi(edge0));
 
208
                ///assing texcoords
 
209
                ScalarType h=(sqrt((ScalarType)3.0)/(ScalarType)2.0);
 
210
                v0->T().P()=vcg::Point2<ScalarType>(0,(ScalarType)-0.5);
 
211
                v1->T().P()=vcg::Point2<ScalarType>(0,(ScalarType)0.5);
 
212
                v2->T().P()=vcg::Point2<ScalarType>(-h,0);
 
213
                v3->T().P()=vcg::Point2<ScalarType>(h,0);
 
214
 
 
215
#ifndef _MESHLAB
 
216
                ///save domain if need for demos
 
217
                if (savedomain)
 
218
                {
 
219
                        BaseMesh hlev_mesh;
 
220
                        std::vector<FaceType*> faces;
 
221
                        FaceType* f=this->_pos.F();
 
222
                        int edge=this->_pos.E();
 
223
                        faces.push_back(f);
 
224
                        faces.push_back(f->FFp(edge));
 
225
                        std::vector<VertexType*> HresVert;
 
226
                        getHresVertex<FaceType>(faces,HresVert);
 
227
 
 
228
                        ///parametrize H_res mesh respect to diamond
 
229
                        std::vector<VertexType*> OrderedVertices;
 
230
                        std::vector<FaceType*> OrderedFaces;
 
231
                        CopyMeshFromVertices(HresVert,OrderedVertices,OrderedFaces,hlev_mesh);
 
232
                        //for (int i=0;i<hlev_mesh.vert.size();i++)
 
233
                                //hlev_mesh.vert[i].C()=hlev_mesh.vert[i].OriginalCol;
 
234
 
 
235
                        vcg::tri::io::ExporterPLY<BaseMesh>::Save(hlev_mesh,"c:/export_submeshes/FLIPHlev3D.ply",vcg::tri::io::Mask::IOM_VERTCOLOR);
 
236
                        for (unsigned int i=0;i<hlev_mesh.vert.size();i++)
 
237
                        {
 
238
                                hlev_mesh.vert[i].P().X()=hlev_mesh.vert[i].T().U();
 
239
                                hlev_mesh.vert[i].P().Y()=hlev_mesh.vert[i].T().V();
 
240
                                hlev_mesh.vert[i].P().Z()=0;
 
241
                        }
 
242
                        vcg::tri::io::ExporterPLY<BaseMesh>::Save(hlev_mesh,"c:/export_submeshes/FLIPHlevUV.ply",vcg::tri::io::Mask::IOM_VERTCOLOR);
 
243
                }
 
244
#endif
 
245
 
 
246
                ExecuteFlip(*this->_pos.F(),this->_pos.E());
 
247
                
 
248
                UpdateTopologies(&m);
 
249
                
 
250
                ///stars optimization
 
251
                /*int t0=clock();*/
 
252
                /*OptimizeStar<BaseMesh>(v0);
 
253
                OptimizeStar<BaseMesh>(v1);
 
254
                OptimizeStar<BaseMesh>(v2);
 
255
                OptimizeStar<BaseMesh>(v3);*/
 
256
                
 
257
                SmartOptimizeStar<BaseMesh>(v0);
 
258
                SmartOptimizeStar<BaseMesh>(v1);
 
259
                SmartOptimizeStar<BaseMesh>(v2);
 
260
                SmartOptimizeStar<BaseMesh>(v3);        
 
261
                /*int t1=clock();
 
262
                time_opt+=(t1-t0);*/
 
263
        }
 
264
 
 
265
        ScalarType ComputePriority()
 
266
        {
 
267
                this->_priority=EdgeDiff();
 
268
                return this->_priority;
 
269
        }
 
270
 
 
271
        BaseFace *getF()
 
272
        {return this->_pos.F();}
 
273
 
 
274
        int getE()
 
275
        {return this->_pos.E();}
 
276
};
 
277
 
 
278
#endif