1 /*
2  * Copyright (C) 2001-2005  Terence M. Welsh
3  *
4  * This file is part of Implicit.
5  *
6  * Implicit is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License version 2.1 as published by the Free Software Foundation.
9  *
10  * Implicit is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 
21 #ifndef IMPCUBEVOLUME_H
22 #define IMPCUBEVOLUME_H
23 
24 
25 
26 #include <Implicit/impSurface.h>
27 #include <Implicit/impCubeTables.h>
28 #include <Implicit/impCrawlPoint.h>
29 #include <vector>
30 #include <list>
31 
32 #include <math.h>
33 
34 
35 
36 struct cubedata{
37 	// corner mask which describes how cube is polygonized
38 	unsigned int mask;
39 	// position left-lower-far corner (-x, -y, -z)
40 	float x;
41 	float y;
42 	float z;
43 	// field value at this corner
44 	float value;
45 	// edge vertex indices for surfaces
46 	unsigned int x_vertex_index;
47 	unsigned int y_vertex_index;
48 	unsigned int z_vertex_index;
49 	// done flags
50 	bool cube_done;
51 	bool corner_done;
52 };
53 
54 
55 // For making a list of cubes to be polygonized.
56 // The list can be sorted by depth before polygonization in
57 // the case of transparent surfaces.
58 class sortableCube{
59 public:
60 	unsigned int index;  // index of cube in volume
61 	float depth;  // distance squared from eyepoint
62 
sortableCube()63 	sortableCube(){}
sortableCube(unsigned int ind)64 	sortableCube(unsigned int ind){index = ind;}
~sortableCube()65 	~sortableCube(){}
66 
67 	friend bool operator < (const sortableCube& a, const sortableCube& b){return((a.depth)<(b.depth));}
68 	friend bool operator > (const sortableCube& a, const sortableCube& b){return((a.depth)>(b.depth));}
69 	friend bool operator == (const sortableCube& a, const sortableCube& b){return((a.depth)==(b.depth));}
70 	friend bool operator != (const sortableCube& a, const sortableCube& b){return((a.depth)!=(b.depth));}
71 };
72 
73 
74 class impCubeVolume{
75 public:
76 	float (*function)(float* position);
77 private:
78 	float lbf[3];  // left-bottom-far corner of volume
79 	float cubewidth;
80 	unsigned int w, h, l, w_1, h_1, l_1;
81 	unsigned int triStripPatterns[256][17];
82 	bool crawlDirections[256][6];
83 	unsigned int currentVertexIndex;
84 	std::vector<cubedata> cubes;
85 	std::vector<unsigned int> cubeIndices;
86 	std::list<sortableCube> sortableCubes;
87 	unsigned int currentCubeIndex;
88 	bool fastnormals;
89 	float surfacevalue;  // surface's position on gradient
90 	impSurface* surface;
91 
92 public:
93 	impCubeVolume();
94 	~impCubeVolume();
95 	// pass dimensions of volume in cubes plus "cubewidth"
96 	void init(unsigned int width, unsigned int height, unsigned int length, float cw);
useFastNormals(bool val)97 	void useFastNormals(bool val){fastnormals = val;}
setSurfaceValue(float sv)98 	void setSurfaceValue(float sv){surfacevalue = sv;}
getSurfaceValue()99 	float getSurfaceValue(){return surfacevalue;}
setSurface(impSurface * s)100 	void setSurface(impSurface* s){surface = s;}
getSurface()101 	impSurface* getSurface(){return surface;}
102 	// These routines compute geometry and store it in "surface"
103 	// Providing an eyepoint indicates that you want to sort the surface
104 	// so that transparent surfaces will be drawn back-to-front.
105 	// Providing a list of crawlpoints indicates that you want to use a
106 	// surface crawling algorithm.
107 	// If no crawlpoint list is provided, then every cube is checked, which
108 	// is slow but thorough (there's no chance of missing a piece of the
109 	// surface).
110 	void makeSurface();
111 	void makeSurface(float eyex, float eyey, float eyez);
112 	void makeSurface(impCrawlPointVector &cpv);
113 	void makeSurface(float eyex, float eyey, float eyez, impCrawlPointVector &cpv);
114 
115 private:
116 	// x, y, and z define position of cube in this volume
117 	inline unsigned int calculateCornerMask(unsigned int x, unsigned int y, unsigned int z);
118 	inline void crawl_nosort(unsigned int x, unsigned int y, unsigned int z);
119 	inline void crawl_sort(unsigned int x, unsigned int y, unsigned int z);
120 	inline void uncrawl(unsigned int x, unsigned int y, unsigned int z);
121 	inline void polygonize(unsigned int index);
122 	inline void findcornervalues(unsigned int x, unsigned int y, unsigned int z);
123 	inline void addVertexToSurface(unsigned int axis, unsigned int index);
cubeindex(unsigned int i,unsigned int j,unsigned int k)124 	inline unsigned int cubeindex(unsigned int i, unsigned int j, unsigned int k)
125 		{ return (((k * h_1) + j) * w_1) + i; }
126 };
127 
128 
129 
130 #endif
131