Subversion Repositories gelsvn

Rev

Rev 595 | Details | Compare with Previous | Last modification | View Log | RSS feed

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