File: poly_triangulator.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (139 lines) | stat: -rw-r--r-- 3,747 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
#ifndef POLY_TRIANGULATOR_H
#define POLY_TRIANGULATOR_H

#include "util_dae.h"
#include <wrap/gl/glu_tesselator.h>

namespace vcg {
namespace tri {
namespace io {	
		// These two classes is used for temporary storing of the
		// collected data of the polgons during the reading of files. 
		
	template<typename VERTEX_TYPE>
	class MyPolygon 
	{
	public:
		typedef VERTEX_TYPE BaseVertexType;

		int _nvert;
		std::vector<VERTEX_TYPE*> _pv;
		std::vector< vcg::TexCoord2<float> > _txc;


		MyPolygon(int n)
		:_nvert(n),_pv(_nvert),_txc(_nvert)
		{
		}
	};

	template<typename POLYGONAL_TYPE>
	class PolygonalMesh
	{
	public:
		typedef POLYGONAL_TYPE FaceType;

		enum PERWEDGEATTRIBUTETYPE {NONE = 0,NORMAL = 1,MULTITEXTURECOORD = 2,MULTICOLOR = 4};

		typedef typename FaceType::BaseVertexType VertexType;
		typedef VertexType* VertexPointer;
		typedef typename std::vector<VertexType>::iterator VertexIterator; 
		typedef typename std::vector<FaceType>::iterator PolygonIterator; 

		vcg::Box3<float> bbox;

		std::vector<VertexType> vert;
		std::vector<FaceType> _pols;

		void generatePointsVector(std::vector<std::vector<vcg::Point3f> >& v)
		{
			for(typename PolygonalMesh::PolygonIterator itp = _pols.begin();itp != _pols.end();++itp)
			{
				v.push_back(std::vector<vcg::Point3f>());
				for(typename std::vector<VertexPointer>::iterator itv = itp->_pv.begin();itv != itp->_pv.end();++itv)
				{
					v[v.size() - 1].push_back((*itv)->P());
				}	
			}
		}

		void usePerWedgeAttributes(PERWEDGEATTRIBUTETYPE att,const unsigned int multitexture = 1,const unsigned int multicolor = 1)
		{
			if (att != NONE)
			{
				for(PolygonIterator itp = _pols.begin();itp != _pols.end();++itp)
				{
					if (att & MULTICOLOR) itp->usePerWedgeColor(multicolor);
					if (att & MULTITEXTURECOORD) itp->usePerWedgeMultiTexture(multitexture);
					if (att & NORMAL) itp->usePerWedgeNormal();
				}
			}
		}

		template<class TRIMESH>
		void triangulate(TRIMESH& mesh)
		{
			std::vector<std::vector<vcg::Point3f> > pl;
			mesh.vert.resize(vert.size());
			int multicoor = 0;
			//PolygonalMesh's points has been copied in TriangularMesh
			for(size_t jj = 0;jj < mesh.vert.size();++jj)
				mesh.vert[jj].P() = vert[jj].P();

			bool texen = mesh.face.IsWedgeTexEnabled();
			unsigned int totaltri = 0;
			for(size_t ii = 0;ii < _pols.size();++ii)
					totaltri += _pols[ii]._nvert - 2;
				
			mesh.face.resize(totaltri);

			//transform the polygonal mesh in a vector<vector<Point>>
			generatePointsVector(pl);


			int trioff = 0;
			//foreach Polygon
			for(size_t ii = 0;ii < pl.size();++ii)
			{
				std::vector<int> tx;
				std::vector<std::vector<vcg::Point3f> > pl2(1);
				pl2[0] = pl[ii];

				vcg::glu_tesselator::tesselate(pl2,tx);
				size_t ntri = tx.size() / 3;
				assert(tx.size() % 3 == 0);
					

				int polvert = 0;
				//foreach triangle
				for(size_t tr = 0;tr < ntri;++tr)
				{
						
					//typename TRIMESH::FaceType& f = mesh.face[tr];

					//typename TRIMESH::FaceType& f = mesh.face[tr];
					for(unsigned int tt = 0;tt < 3; ++tt)
					{
						mesh.face[trioff + tr].V(tt) = &(mesh.vert[_pols[ii]._pv[tx[3 * tr + tt]] - &(vert[0])]);
						//vcg::Point3f ppp = mesh.face[tr].V(tt)->P();
						if (texen)
						{
						/*	f.WT(multicoor).U() = _pols[ii]._txc[polvert].U();
							f.WT(multicoor).V() = _pols[ii]._txc[polvert].V();
							f.WT(multicoor).N() = _pols[ii]._txc[polvert].N();*/
								
						}
						polvert = (polvert + 1) % _pols[ii]._nvert;
					}
					//mesh.face.push_back(f);
				}
				trioff += ntri;
			}
			assert(trioff == totaltri);
		}
	};
}
}
}

#endif