Subversion Repositories gelsvn

Rev

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

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