File: uv_utils.h

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,060 kB
  • ctags: 33,549
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (148 lines) | stat: -rw-r--r-- 5,342 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#ifndef VCG_UV_UTILS
#define VCG_UV_UTILS


namespace vcg {
	namespace tri{
		template <class MeshType>
		class UV_Utils
		{
		  typedef typename MeshType::CoordType CoordType;
			typedef typename MeshType::ScalarType ScalarType;
			typedef typename MeshType::FaceType FaceType;
			typedef typename MeshType::VertexType VertexType;
			typedef typename MeshType::VertexIterator VertexIterator;
			typedef typename MeshType::FaceIterator FaceIterator;
			typedef typename vcg::Point2<ScalarType> UVCoordType;

		public:
			///calculate the BBox in UV space
			static vcg::Box2<ScalarType> PerWedgeUVBox(MeshType &m)
			{
				vcg::Box2<ScalarType> UVBox;
				FaceIterator fi;
				for (fi=m.face.begin();fi!=m.face.end();fi++)
				{
					if ((*fi).IsD()) continue;
					for (int i=0;i<3;i++)
						UVBox.Add((*fi).WT(i).P());
				}
				return UVBox;
			}
			
			///calculate the BBox in UV space
			static vcg::Box2<ScalarType> PerVertUVBox(MeshType &m)
			{
				vcg::Box2<ScalarType> UVBox;
				VertexIterator vi;
				for (vi=m.vert.begin();vi!=m.vert.end();vi++)
				{
					if ((*vi).IsD()) continue;
					UVBox.Add((*vi).T().P());
				}
				return UVBox;
			}

		void PerWedgeMakeUnitaryUV(MeshType &m)
		{
		  vcg::Box2<typename MeshType::ScalarType> UVBox = PerWedgeUVBox(m);

			typename MeshType::FaceIterator fi;
			Point2f boxSize(UVBox.max-UVBox.min);
			for (fi=m.face.begin();fi!=m.face.end();fi++)
			{
				if ((*fi).IsD()) continue;
				for (int i=0;i<3;i++)
				{
				  (*fi).WT(i).U() = ((*fi).WT(i).U()-UVBox.min[0])/boxSize[0] ;
				  (*fi).WT(i).V() = ((*fi).WT(i).V()-UVBox.min[1])/boxSize[1] ;
				}
			}
		}
			///transform curvature to UV space
			static UVCoordType Coord3DtoUV(FaceType &f,const CoordType &dir)
			{
				///then transform to UV
				CoordType bary3d=(f.P(0)+f.P(1)+f.P(2))/3.0;
				UVCoordType baryUV=(f.WT(0).P()+f.WT(1).P()+f.WT(2).P())/3.0;
				CoordType dir3d=bary3d+dir;
				CoordType baryCoordsUV;
				vcg::InterpolationParameters<FaceType,ScalarType>(f,dir3d,baryCoordsUV);
				UVCoordType dirUV=baryCoordsUV.X()*f.WT(0).P()+
									baryCoordsUV.Y()*f.WT(1).P()+
									baryCoordsUV.Z()*f.WT(2).P()-baryUV;
				dirUV.Normalize();
				return dirUV;
			}

			static void GloballyMirrorX(MeshType &m)
			{
				vcg::Box2<ScalarType> BBuv=PerVertUVBox(m);
				ScalarType Xmin=BBuv.min.X();
				ScalarType Xmax=BBuv.max.X();
				ScalarType XAv=(Xmax+Xmin)/2;
				VertexIterator vi;
				for (vi=m.vert.begin();vi!=m.vert.end();vi++)
				{
					ScalarType distAV=(*vi).T().P().X()-XAv;
					(*vi).T().P().X()=XAv-distAV;
				}
			}
			
			static void LaplacianUVVert(MeshType &m,bool fix_borders=false,int steps=3)
			{
				FaceIterator fi;
				for (int s=0;s<steps;s++)
				{
					std::vector<int> num(m.vert.size(),0);
					std::vector<UVCoordType> UVpos(m.vert.size(),UVCoordType(0,0));
					for (fi=m.face.begin();fi!=m.face.end();fi++)
					{
						for (int j=0;j<3;j++)
						{
							VertexType *v0=(*fi).V(0);
							VertexType *v1=(*fi).V1(0);
							VertexType *v2=(*fi).V2(0);
							UVCoordType uv1=v1->T().P();
							UVCoordType uv2=v2->T().P();
							int index=v0-&(m.vert[0]);
							num[index]+=2;
							UVpos[index]+=uv1;
							UVpos[index]+=uv2;
						}
					}
					VertexIterator vi;
					for (int i=0;i<m.vert.size();i++)
					{
						if ((fix_borders)&&(m.vert[i].IsB()))continue;
						m.vert[i].T().P()=UVpos[i]/(ScalarType)num[i];
					}
				}
			}
		};
	} //End Namespace Tri
} // End Namespace vcg
#endif