Subversion Repositories gelsvn

Rev

Rev 64 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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