Subversion Repositories gelsvn

Rev

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

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