VCG Library
texture.h
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 
24 
25 #ifndef __VCG_TRI_UPDATE_TEXTURE
26 #define __VCG_TRI_UPDATE_TEXTURE
27 
28 //#include <vcg/space/plane.h>
29 
30 namespace vcg {
31 namespace tri {
32 
34 
36 
38 template <class ComputeMeshType>
40 {
41 
42 public:
43 typedef ComputeMeshType MeshType;
44 typedef typename MeshType::ScalarType ScalarType;
45 typedef typename MeshType::VertexType VertexType;
46 typedef typename MeshType::VertexPointer VertexPointer;
47 typedef typename MeshType::VertexIterator VertexIterator;
48 typedef typename MeshType::FaceType FaceType;
49 typedef typename MeshType::FacePointer FacePointer;
50 typedef typename MeshType::FaceIterator FaceIterator;
51 typedef typename vcg::Point2<ScalarType> UVCoordType;
52 
53 static void WedgeTexFromPlane(ComputeMeshType &m, const Point3<ScalarType> &uVec, const Point3<ScalarType> &vVec, bool aspectRatio, ScalarType sideGutter=0.0)
54 {
55  Box2f bb;
56 
57  FaceIterator fi;
58  for(fi=m.face.begin();fi!=m.face.end();++fi)
59  if(!(*fi).IsD())
60  {
61  for(int i=0;i<3;++i)
62  {
63  (*fi).WT(i).U()= (*fi).V(i)->cP() * uVec;
64  (*fi).WT(i).V()= (*fi).V(i)->cP() * vVec;
65  bb.Add((*fi).WT(i).P());
66  }
67  }
68 
69  ScalarType wideU = bb.max[0]- bb.min[0];
70  ScalarType wideV = bb.max[1]- bb.min[1];
71 
72  if (sideGutter>0.0)
73  {
74  ScalarType deltaGutter = std::min(wideU, wideV) * std::min(sideGutter, (ScalarType)0.5);
75 
76  bb.max[0] += deltaGutter;
77  bb.min[0] -= deltaGutter;
78  bb.max[1] += deltaGutter;
79  bb.min[1] -= deltaGutter;
80 
81  wideU = bb.max[0] - bb.min[0];
82  wideV = bb.max[1] - bb.min[1];
83  }
84 
85  if (aspectRatio) {
86  wideU = std::max(wideU, wideV);
87  wideV = wideU;
88  }
89 
90  for (fi = m.face.begin(); fi != m.face.end(); ++fi)
91  if (!(*fi).IsD())
92  {
93  for (int i = 0; i<3; ++i)
94  {
95  (*fi).WT(i).U() = ((*fi).WT(i).U() - bb.min[0]) / wideU;
96  (*fi).WT(i).V() = ((*fi).WT(i).V() - bb.min[1]) / wideV;
97  }
98  }
99 }
100 
101 static void WedgeTexFromVertexTex(ComputeMeshType &m)
102 {
103  for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
104  if(!(*fi).IsD())
105  {
106  for(int i=0;i<fi->VN();++i)
107  {
108  (*fi).WT(i).U() = (*fi).V(i)->T().U();
109  (*fi).WT(i).V() = (*fi).V(i)->T().V();
110  (*fi).WT(i).N() = 0;
111  }
112  }
113 }
114 
115 
119 static void WedgeTexRemoveNull(ComputeMeshType &m, const std::string &texturename)
120 {
121  bool found=false;
122 
123  FaceIterator fi;
124  // first loop lets check that there are -1 indexed textured face
125  for(fi=m.face.begin();fi!=m.face.end();++fi)
126  if(!(*fi).IsD()) if((*fi).WT(0).N()==-1) found = true;
127 
128  if(!found) return;
129  m.textures.push_back(texturename);
130 
131  int nullId=m.textures.size()-1;
132 
133  for(fi=m.face.begin();fi!=m.face.end();++fi)
134  if(!(*fi).IsD()) if((*fi).WT(0).N()==-1)
135  {
136  (*fi).WT(0).N() = nullId;
137  (*fi).WT(1).N() = nullId;
138  (*fi).WT(2).N() = nullId;
139  }
140 
141 }
148 static int WedgeTexMergeClose(ComputeMeshType &m, ScalarType mergeThr = ScalarType(1.0/65536.0) )
149 {
150  tri::RequireVFAdjacency(m);
151  int mergedCnt=0;
152  ForEachVertex(m, [&](VertexType &v){
153  face::VFIterator<FaceType> vfi(&v);
154  std::vector<UVCoordType> clusterVec;
155  clusterVec.push_back(vfi.F()->WT(vfi.I()).P());
156  ++vfi;
157  while(!vfi.End())
158  {
159  UVCoordType cur= vfi.F()->WT(vfi.I()).P();
160  bool merged=false;
161  for(auto p:clusterVec) {
162  if(p!=cur && Distance(p,cur) < mergeThr){
163  vfi.F()->WT(vfi.I()).P()=p;
164  ++mergedCnt;
165  merged=true;
166  }
167  }
168  if(!merged)
169  clusterVec.push_back(cur);
170 
171  ++vfi;
172  }
173  });
174  return mergedCnt;
175 }
176 
177 }; // end class
178 
179 } // End namespace
180 } // End namespace
181 
182 
183 #endif