Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
39 bj 1
#pragma warning(disable: 4786) // disable warning C4786: symbol greater than 255 
2
 
3
#include <iostream>
4
#include "TriMeshBuilder.h"
5
 
6
#define my_max(x,y) ((x)>(y)?(x):(y))
7
 
8
using namespace std;
9
 
10
namespace IMesh
11
{
12
 
13
	TriMeshBuilder::TriMeshBuilder(): did_call_get_trimesh(false)
14
	{
15
		// Register the default face set
16
		register_face_set();
17
 
18
		// Register the vertex position attribute. There is really no sense
19
		// in a mesh without geometry, so this is default.
20
		register_vertex_attribute("VA_POS", VA_POS);
21
		assert(VA_POS.idx == 0);
22
	}
23
 
24
	void TriMeshBuilder::clear()
25
	{
26
		// If a trimesh was not emitted, we remove any allocated dynamic memory
27
		if(!did_call_get_trimesh)
28
			clean_up();
29
 
30
		// Clear all variables
31
		TriMeshData::clear();
32
		did_call_get_trimesh = false;
33
 
34
		// Register the default face set
35
		register_face_set();
36
 
37
		// Register the vertex position attribute. There is really no sense
38
		// in a mesh without geometry, so this is default.
39
		register_vertex_attribute("VA_POS", VA_POS);
40
		assert(VA_POS.idx == 0);
41
	}
42
 
43
	TriMeshBuilder::~TriMeshBuilder()
44
	{
45
		// If a trimesh was not emitted, we remove allocated dynamic memory.
46
		if(!did_call_get_trimesh)
47
			clean_up();
48
	}
49
 
50
	int TriMeshBuilder::register_face_set()
51
	{
52
		assert(!did_call_get_trimesh);
53
		int n = face_sets.size();
54
		if(n==0)
55
			face_sets.push_back(new FaceSet);
56
		else 
57
			face_sets.push_back(new FaceSet(*face_sets[0]));
58
		return n;
59
	}
60
 
61
	void TriMeshBuilder::register_face_colors()
62
	{
63
		assert(!did_call_get_trimesh);
64
		register_face_attribute("FA_COL", FA_COL);
65
	}
66
 
67
	void TriMeshBuilder::register_face_normals()
68
	{
69
		assert(!did_call_get_trimesh);
70
		register_face_attribute("FA_NORM", FA_NORM);
71
	}
72
 
73
 
74
	void TriMeshBuilder::register_vertex_normals(int face_set)
75
	{
76
		assert(!did_call_get_trimesh);
77
		register_vertex_attribute("VA_NORM", VA_NORM, face_set);
78
	}
79
 
80
 
81
	void TriMeshBuilder::register_vertex_colors(int face_set)
82
	{
83
		assert(!did_call_get_trimesh);
84
		register_vertex_attribute("VA_COL", VA_COL, face_set);
85
	}
86
 
87
 
88
	void TriMeshBuilder::register_vertex_texcoords(int face_set)
89
	{
90
		assert(!did_call_get_trimesh);
91
		register_vertex_attribute("VA_TEX", VA_TEX, face_set);
92
	}
93
 
94
	void TriMeshBuilder::add_face(const CGLA::Vec3i& face)
95
	{
96
		assert(!did_call_get_trimesh);
97
		++faces_size;
98
 
99
		int i; // To placate MSVC 6.0 ... which sucks
100
		for(i=0;i<face_sets.size();++i)
101
			face_sets[i]->push_back(face);
102
	}
103
 
104
 
105
 
106
	TriMesh* TriMeshBuilder::get_trimesh() 
107
	{
108
		assert(!did_call_get_trimesh);
109
 
110
#ifndef NDEBUG
111
		int face_set_no;  // To placate MSVC 6.0 ... which sucks
112
		for(face_set_no=0;face_set_no<face_sets.size();++face_set_no)
113
			{
114
				int n = face_sets[face_set_no]->size();
115
				if(n != faces_size)
116
					{
117
						cout << "Face set " << face_set_no << " has wrong size : "
118
								 << n << endl;
119
						exit(1);
120
					}
121
 
122
				int j;
123
				int max_vertex=-1;
124
				for(j=0;j<n;++j)
125
					{
126
						max_vertex = my_max(max_vertex, (*face_sets[face_set_no])[j][0]);
127
						max_vertex = my_max(max_vertex, (*face_sets[face_set_no])[j][1]);
128
						max_vertex = my_max(max_vertex, (*face_sets[face_set_no])[j][2]);
129
					}
130
 
131
				int vattr_no;
132
				for(vattr_no=0; vattr_no<face_set_mapping.size(); ++vattr_no)
133
				{
134
					if(face_set_mapping[vattr_no] == face_set_no)
135
						if(vert_attr[vattr_no]->size() > (max_vertex+1))
136
							{
137
								cout << "Excess vertex attributes in attribute vector "
138
										 << vattr_no  << " associated with face set "
139
										 << face_set_no << endl;
140
								exit(1);
141
							}
142
 
143
						else if(vert_attr[vattr_no]->size() < (max_vertex+1))
144
							{
145
								cout << "Face set " << face_set_no << " addresses beyond the "
146
										 << "end of attribute vector " << vattr_no << endl;
147
								exit(1);
148
							}
149
				}
150
 
151
			}
152
		int i;
153
		for(i=0;i<face_attr.size();++i)
154
			{
155
				int n = face_attr[i]->size();
156
				if(n != faces_size)
157
					{
158
						cout << "Face attribute vector " << i 
159
								 << " has wrong size : " << n << endl;
160
						exit(0);
161
					}
162
			}
163
#endif
164
 
165
		did_call_get_trimesh = true;
166
		return new TriMesh(*this);
167
	}
168
}