VCG Library
Loading...
Searching...
No Matches
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
30namespace vcg {
31namespace tri {
32
34
36
38template <class ComputeMeshType>
40{
41
42public:
43typedef ComputeMeshType MeshType;
44typedef typename MeshType::ScalarType ScalarType;
45typedef typename MeshType::VertexType VertexType;
46typedef typename MeshType::VertexPointer VertexPointer;
47typedef typename MeshType::VertexIterator VertexIterator;
48typedef typename MeshType::FaceType FaceType;
49typedef typename MeshType::FacePointer FacePointer;
50typedef typename MeshType::FaceIterator FaceIterator;
51typedef typename vcg::Point2<ScalarType> UVCoordType;
52
53static 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 (*fi).WT(i).N()= 0; // the whole mesh is mapped onto one texture (see TexCoord2)
66 bb.Add((*fi).WT(i).P());
67 }
68 }
69
70 ScalarType wideU = bb.max[0]- bb.min[0];
71 ScalarType wideV = bb.max[1]- bb.min[1];
72
73 if (sideGutter>0.0)
74 {
75 ScalarType deltaGutter = std::min(wideU, wideV) * std::min(sideGutter, (ScalarType)0.5);
76
77 bb.max[0] += deltaGutter;
78 bb.min[0] -= deltaGutter;
79 bb.max[1] += deltaGutter;
80 bb.min[1] -= deltaGutter;
81
82 wideU = bb.max[0] - bb.min[0];
83 wideV = bb.max[1] - bb.min[1];
84 }
85
86 if (aspectRatio) {
87 wideU = std::max(wideU, wideV);
88 wideV = wideU;
89 }
90
91 for (fi = m.face.begin(); fi != m.face.end(); ++fi)
92 if (!(*fi).IsD())
93 {
94 for (int i = 0; i<3; ++i)
95 {
96 (*fi).WT(i).U() = ((*fi).WT(i).U() - bb.min[0]) / wideU;
97 (*fi).WT(i).V() = ((*fi).WT(i).V() - bb.min[1]) / wideV;
98 }
99 }
100}
101
102static void WedgeTexFromVertexTex(ComputeMeshType &m)
103{
104 for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
105 if(!(*fi).IsD())
106 {
107 for(int i=0;i<fi->VN();++i)
108 {
109 (*fi).WT(i).U() = (*fi).V(i)->T().U();
110 (*fi).WT(i).V() = (*fi).V(i)->T().V();
111 (*fi).WT(i).N() = 0;
112 }
113 }
114}
115
116
120static void WedgeTexRemoveNull(ComputeMeshType &m, const std::string &texturename)
121{
122 bool found=false;
123
124 FaceIterator fi;
125 // first loop lets check that there are -1 indexed textured face
126 for(fi=m.face.begin();fi!=m.face.end();++fi)
127 if(!(*fi).IsD()) if((*fi).WT(0).N()==-1) found = true;
128
129 if(!found) return;
130 m.textures.push_back(texturename);
131
132 int nullId=m.textures.size()-1;
133
134 for(fi=m.face.begin();fi!=m.face.end();++fi)
135 if(!(*fi).IsD()) if((*fi).WT(0).N()==-1)
136 {
137 (*fi).WT(0).N() = nullId;
138 (*fi).WT(1).N() = nullId;
139 (*fi).WT(2).N() = nullId;
140 }
141
142}
149static int WedgeTexMergeClose(ComputeMeshType &m, ScalarType mergeThr = ScalarType(1.0/65536.0) )
150{
151 tri::RequireVFAdjacency(m);
152 int mergedCnt=0;
153 ForEachVertex(m, [&](VertexType &v){
154 face::VFIterator<FaceType> vfi(&v);
155 std::vector<UVCoordType> clusterVec;
156 clusterVec.push_back(vfi.F()->WT(vfi.I()).P());
157 ++vfi;
158 while(!vfi.End())
159 {
160 UVCoordType cur= vfi.F()->WT(vfi.I()).P();
161 bool merged=false;
162 for(auto p:clusterVec) {
163 if(p!=cur && Distance(p,cur) < mergeThr){
164 vfi.F()->WT(vfi.I()).P()=p;
165 ++mergedCnt;
166 merged=true;
167 }
168 }
169 if(!merged)
170 clusterVec.push_back(cur);
171
172 ++vfi;
173 }
174 });
175 return mergedCnt;
176}
177
178}; // end class
179
180} // End namespace
181} // End namespace
182
183
184#endif
Definition: point3.h:43
This class is used to update/generate texcoord position according to various critera.
Definition: texture.h:40
static int WedgeTexMergeClose(ComputeMeshType &m, ScalarType mergeThr=ScalarType(1.0/65536.0))
Merge supposedly wrong texcoords It can happens that for rounding errors texcoords on different wedge...
Definition: texture.h:149
static void WedgeTexRemoveNull(ComputeMeshType &m, const std::string &texturename)
Definition: texture.h:120
void ForEachVertex(const MeshType &m, Callable action)
Definition: foreach.h:126
Definition: color4.h:30