Subversion Repositories gelsvn

Rev

Rev 89 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 89 Rev 145
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
	/** \brief Implicit function.
27
	/** \brief Implicit function.
28
 
28
 
29
	The implicit function class represents the implicit function we wish 
29
	The implicit function class represents the implicit function we wish 
30
			to polygonize. Derive a class from this one and implement your 
30
			to polygonize. Derive a class from this one and implement your 
31
			implicit primitive in the eval function. Eval takes x,y,z coordinates and
31
			implicit primitive in the eval function. Eval takes x,y,z coordinates and
32
			returns a value. We assume that the surface is the zero level set 
32
			returns a value. We assume that the surface is the zero level set 
33
			and that the negative values are outside. This an arbitrary choice
33
			and that the negative values are outside. This an arbitrary choice
34
			which does not make the code less general. */
34
			which does not make the code less general. */
35
	class ImplicitFunction
35
	class ImplicitFunction
36
		{
36
		{
37
		public:
37
		public:
38
			virtual float eval(float,float,float) = 0;
38
			virtual float eval(float,float,float) = 0;
39
		};
39
		};
40
 
40
 
41
	struct POINT { float x, y, z;	};
41
	struct POINT { float x, y, z;	};
42
 
42
 
43
	typedef POINT VERTEX;
43
	typedef POINT VERTEX;
44
	typedef POINT NORMAL;
44
	typedef POINT NORMAL;
45
 
45
 
46
	/** TRIANGLE struct contains the indices of the vertices comprising 
46
	/** TRIANGLE struct contains the indices of the vertices comprising 
47
			the triangle */
47
			the triangle */
48
	struct TRIANGLE
48
	struct TRIANGLE
49
	{
49
	{
50
		int v0,v1,v2;
50
		int v0,v1,v2;
51
	};
51
	};
52
 
52
 
53
	/** \brief Polygonizer is the class used to perform polygonization.*/
53
	/** \brief Polygonizer is the class used to perform polygonization.*/
54
	class Polygonizer
54
	class Polygonizer
55
		{
55
		{
56
			std::vector<NORMAL> gnormals;  
56
			std::vector<NORMAL> gnormals;  
57
			std::vector<VERTEX> gvertices;  
57
			std::vector<VERTEX> gvertices;  
58
			std::vector<TRIANGLE> gtriangles;
58
			std::vector<TRIANGLE> gtriangles;
59
  
59
  
60
			ImplicitFunction* func;
60
			ImplicitFunction* func;
61
			float size;
61
			float size;
62
			int bounds;
62
			int bounds;
63
			
63
			
64
			bool use_tetra;
64
			bool use_tetra;
65
			bool use_normals;
65
			bool use_normals;
66
 
66
 
67
		public:	
67
		public:	
68
	
68
	
69
			/** Constructor of Polygonizer. The first argument is the 
69
			/** Constructor of Polygonizer. The first argument is the 
70
					ImplicitFunction that we wish to polygonize. The second
70
					ImplicitFunction that we wish to polygonize. The second
71
					argument is the size of the polygonizing cell. 
71
					argument is the size of the polygonizing cell. 
72
					The third arg. is the limit to how far away we will
72
					The third arg. is the limit to how far away we will
73
					look for components of the implicit surface. 
73
					look for components of the implicit surface. 
74
					the fourth argument indicates whether the polygonizing cell
74
					the fourth argument indicates whether the polygonizing cell
75
					is a tetrahedron (true) or cube (false). The final argument
75
					is a tetrahedron (true) or cube (false). The final argument
76
					indicates whether normals should be computed.
76
					indicates whether normals should be computed.
77
			*/
77
			*/
78
			Polygonizer(ImplicitFunction* _func, float _size, int _bounds,
78
			Polygonizer(ImplicitFunction* _func, float _size, int _bounds,
79
									bool _use_tetra=false,
79
									bool _use_tetra=false,
80
									bool _use_normals=false):
80
									bool _use_normals=false):
81
				func(_func), size(_size), bounds(_bounds),
81
				func(_func), size(_size), bounds(_bounds),
82
				use_tetra(_use_tetra),
82
				use_tetra(_use_tetra),
83
				use_normals(_use_normals) {}
83
				use_normals(_use_normals) {}
84
 
84
 
85
			/** March erases the triangles gathered so far and builds a new 
85
			/** March erases the triangles gathered so far and builds a new 
86
					polygonization.  The  x,y,z 
86
					polygonization.  The  x,y,z 
87
					arguments indicate a point near the surface. */
87
					arguments indicate a point near the surface. */
88
			void march(float x, float y, float z);
88
			void march(float x, float y, float z);
89
 
89
 
90
			/** Return number of triangles generated after the polygonization.
90
			/** Return number of triangles generated after the polygonization.
91
					Call this function only when march has been called. */
91
					Call this function only when march has been called. */
92
			int no_triangles() const
92
			int no_triangles() const
93
				{
93
				{
94
					return gtriangles.size();
94
					return gtriangles.size();
95
				}
95
				}
96
 
96
 
97
			/** Return number of vertices generated after the polygonization.
97
			/** Return number of vertices generated after the polygonization.
98
					Call this function only when march has been called. */
98
					Call this function only when march has been called. */
99
			int no_vertices() const
99
			int no_vertices() const
100
				{
100
				{
101
					return gvertices.size();
101
					return gvertices.size();
102
				}
102
				}
103
	
103
	
104
			/** Return number of normals generated after the polygonization.
104
			/** Return number of normals generated after the polygonization.
105
					Of course the result of calling this function is the same as
105
					Of course the result of calling this function is the same as
106
					no_vertices.
106
					no_vertices.
107
					Call this function only when march has been called. */
107
					Call this function only when march has been called. */
108
			int no_normals() const
108
			int no_normals() const
109
				{
109
				{
110
					return gnormals.size();
110
					return gnormals.size();
111
				}
111
				}
112
	
112
	
113
			/// Return triangle with index i. 
113
			/// Return triangle with index i. 
114
				TRIANGLE& get_triangle(int i) 
114
				TRIANGLE& get_triangle(int i) 
115
					{
115
					{
116
						return gtriangles[i];
116
						return gtriangles[i];
117
					}
117
					}
118
 
118
 
119
				/// Return vertex with index i. 
119
				/// Return vertex with index i. 
120
					VERTEX& get_vertex(int i) 
120
					VERTEX& get_vertex(int i) 
121
						{
121
						{
122
							return gvertices[i];
122
							return gvertices[i];
123
						}
123
						}
124
	
124
	
125
	
125
	
126
					/// Return normal with index i. 
126
					/// Return normal with index i. 
127
						NORMAL& get_normal(int i) 
127
						NORMAL& get_normal(int i) 
128
							{
128
							{
129
								return gnormals[i];
129
								return gnormals[i];
130
							}
130
							}
131
 
131
 
132
 
132
 
133
		};
133
		};
134
}
134
}
135
 
135
 
136
#endif
136
#endif
137
 
137