Subversion Repositories gelsvn

Rev

Rev 64 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
61 jab 1
/**********************************************************************
2
 
3
polygonizer.h
4
 
5
This is Jules Bloomenthal's implicit surface polygonizer from GRAPHICS 
6
GEMS IV. Bloomenthal's polygonizer is still used and the present code
7
is simply the original code morphed into C++.
8
 
9
J. Andreas Bærentzen 2003.
10
 
11
**********************************************************************/
12
 
13
#ifndef POLYGONIZER_H
14
#define POLYGONIZER_H
15
 
16
#include <vector>
17
 
64 jab 18
namespace Geometry
61 jab 19
{
20
 
21
	enum ToTetraHedralize
22
		{
23
			TET = 0,  // use tetrahedral decomposition 
24
			NOTET = 1  // no tetrahedral decomposition  */
25
		};
26
 
27
	/** The implicit function class represents the implicit function we wish 
28
			to polygonize. Derive a class from this one and implement your 
29
			implicit primitive in the eval function. Eval takes x,y,z coordinates and
30
			returns a value. We assume that the surface is the zero level set 
31
			and that the negative values are outside. This an arbitrary choice
32
			which does not make the code less general. */
33
	class ImplicitFunction
34
		{
35
		public:
36
			virtual float eval(float,float,float) = 0;
37
		};
38
 
39
	struct POINT { float x, y, z;	};
40
 
41
	typedef POINT VERTEX;
42
	typedef POINT NORMAL;
43
 
44
	/** TRIANGLE struct contains the indices of the vertices comprising 
45
			the triangle */
46
	struct TRIANGLE
47
	{
48
		int v0,v1,v2;
49
	};
50
 
51
	/** Polygonizer is the class used to perform polygonization.*/
52
	class Polygonizer
53
		{
54
			std::vector<NORMAL> gnormals;  
55
			std::vector<VERTEX> gvertices;  
56
			std::vector<TRIANGLE> gtriangles;
57
 
58
			ImplicitFunction* func;
59
			float size;
60
			int bounds;
61
 
62
			bool use_tetra;
63
			bool use_normals;
64
 
65
		public:	
66
 
67
			/** Constructor of Polygonizer. The first argument is the 
68
					ImplicitFunction that we wish to polygonize. The second
69
					argument is the size of the polygonizing cell. 
70
					The third arg. is the limit to how far away we will
71
					look for components of the implicit surface. 
72
					the fourth argument indicates whether the polygonizing cell
73
					is a tetrahedron (true) or cube (false). The final argument
74
					indicates whether normals should be computed.
75
			*/
76
			Polygonizer(ImplicitFunction* _func, float _size, int _bounds,
77
									bool _use_tetra=false,
78
									bool _use_normals=false):
79
				func(_func), size(_size), bounds(_bounds),
80
				use_tetra(_use_tetra),
81
				use_normals(_use_normals) {}
82
 
83
			/** March erases the triangles gathered so far and builds a new 
84
					polygonization.  The  x,y,z 
85
					arguments indicate a point near the surface. */
86
			void march(float x, float y, float z);
87
 
88
			/** Return number of triangles generated after the polygonization.
89
					Call this function only when march has been called. */
90
			int no_triangles() const
91
				{
92
					return gtriangles.size();
93
				}
94
 
95
			/** Return number of vertices generated after the polygonization.
96
					Call this function only when march has been called. */
97
			int no_vertices() const
98
				{
99
					return gvertices.size();
100
				}
101
 
102
			/** Return number of normals generated after the polygonization.
103
					Of course the result of calling this function is the same as
104
					no_vertices.
105
					Call this function only when march has been called. */
106
			int no_normals() const
107
				{
108
					return gnormals.size();
109
				}
110
 
111
			/// Return triangle with index i. 
112
				TRIANGLE& get_triangle(int i) 
113
					{
114
						return gtriangles[i];
115
					}
116
 
117
				/// Return vertex with index i. 
118
					VERTEX& get_vertex(int i) 
119
						{
120
							return gvertices[i];
121
						}
122
 
123
 
124
					/// Return normal with index i. 
125
						NORMAL& get_normal(int i) 
126
							{
127
								return gnormals[i];
128
							}
129
 
130
 
131
		};
132
}
133
 
134
#endif