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 
52 static void WedgeTexFromPlane(ComputeMeshType &m, const Point3<ScalarType> &uVec, const Point3<ScalarType> &vVec, bool aspectRatio, ScalarType sideGutter=0.0)
53 {
54  Box2f bb;
55 
56  FaceIterator fi;
57  for(fi=m.face.begin();fi!=m.face.end();++fi)
58  if(!(*fi).IsD())
59  {
60  for(int i=0;i<3;++i)
61  {
62  (*fi).WT(i).U()= (*fi).V(i)->cP() * uVec;
63  (*fi).WT(i).V()= (*fi).V(i)->cP() * vVec;
64  bb.Add((*fi).WT(i).P());
65  }
66  }
67 
68  ScalarType wideU = bb.max[0]- bb.min[0];
69  ScalarType wideV = bb.max[1]- bb.min[1];
70 
71  if (sideGutter>0.0)
72  {
73  ScalarType deltaGutter = std::min(wideU, wideV) * min(sideGutter, (ScalarType)0.5);
74 
75  bb.max[0] += deltaGutter;
76  bb.min[0] -= deltaGutter;
77  bb.max[1] += deltaGutter;
78  bb.min[1] -= deltaGutter;
79 
80  wideU = bb.max[0] - bb.min[0];
81  wideV = bb.max[1] - bb.min[1];
82  }
83 
84  if (aspectRatio) {
85  wideU = std::max(wideU, wideV);
86  wideV = wideU;
87  }
88 
89  for (fi = m.face.begin(); fi != m.face.end(); ++fi)
90  if (!(*fi).IsD())
91  {
92  for (int i = 0; i<3; ++i)
93  {
94  (*fi).WT(i).U() = ((*fi).WT(i).U() - bb.min[0]) / wideU;
95  (*fi).WT(i).V() = ((*fi).WT(i).V() - bb.min[1]) / wideV;
96  }
97  }
98 }
99 
100 static void WedgeTexFromVertexTex(ComputeMeshType &m)
101 {
102  for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
103  if(!(*fi).IsD())
104  {
105  for(int i=0;i<fi->VN();++i)
106  {
107  (*fi).WT(i).U() = (*fi).V(i)->T().U();
108  (*fi).WT(i).V() = (*fi).V(i)->T().V();
109  (*fi).WT(i).N() = 0;
110  }
111  }
112 }
113 
114 
118 static void WedgeTexRemoveNull(ComputeMeshType &m, const std::string &texturename)
119 {
120  bool found=false;
121 
122  FaceIterator fi;
123  // first loop lets check that there are -1 indexed textured face
124  for(fi=m.face.begin();fi!=m.face.end();++fi)
125  if(!(*fi).IsD()) if((*fi).WT(0).N()==-1) found = true;
126 
127  if(!found) return;
128  m.textures.push_back(texturename);
129 
130  int nullId=m.textures.size()-1;
131 
132  for(fi=m.face.begin();fi!=m.face.end();++fi)
133  if(!(*fi).IsD()) if((*fi).WT(0).N()==-1)
134  {
135  (*fi).WT(0).N() = nullId;
136  (*fi).WT(1).N() = nullId;
137  (*fi).WT(2).N() = nullId;
138  }
139 
140 }
141 
142 }; // end class
143 
144 } // End namespace
145 } // End namespace
146 
147 
148 #endif