added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / 3dengfx / scfield.hpp
1 /*
2 This file is part of the 3dengfx, realtime visualization system.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 3dengfx is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 3dengfx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with 3dengfx; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* Scalar fields and polygonization
22  *
23  * Author: Mihalis Georgoulopoulos 2005
24  */
25
26 #include <vector>
27 #include "3dengfx/3denginefx_types.hpp"
28 #include "n3dmath2/n3dmath2.hpp"
29 #include "gfx/3dgeom.hpp"
30
31 #ifndef _SCALAR_FIELD_HEADER_
32 #define _SCALAR_FIELD_HEADER_
33
34 class ScalarField
35 {
36 protected:
37         scalar_t *values;                                       // array that holds all values of the field
38         
39         unsigned int *edges_x, *edges_y, *edges_z;      // x- y- and z-aligned edges.
40                                                                                                 // these arrays hold indices to the
41                                                                                                 // vertex list, associated with the specific edge
42
43         unsigned int dimensions;                        // dimensions of the field
44         Vector3 from, to, cell_size;            // limits in space of the field
45
46         // Mesh storage
47         std::vector <Vertex> verts;
48         std::vector <Triangle> tris;
49
50         // Evaluators
51         scalar_t (*evaluate)(const Vector3 &vec, scalar_t t);
52         Vector3 (*get_normal)(const Vector3 &vec, scalar_t t);
53
54         // private methods
55         unsigned int add_vertex(const Vertex &vert);    // adds a vertex and returns its index
56         void clear();                           // clears the std::vectors that hold mesh data
57         void evaluate_all(scalar_t t);
58         void process_cell(int x, int y, int z, scalar_t isolevel);
59
60         unsigned int get_value_index(int x, int y, int z);
61         Vector3 def_eval_normals(const Vector3 &vec, scalar_t t);
62
63 public:
64
65         // constructor
66         ScalarField();
67         ScalarField(unsigned int dimensions, const Vector3 &from, const Vector3 &to);
68         ~ScalarField();
69
70         void set_dimensions(unsigned int dimensions);
71         
72         // draw the 3d grid.
73         // if full, draws everything. If not, draws the bounding volume
74         void draw_field(bool full);
75
76         // Get / Set
77         void set_value(int x, int y, int z, scalar_t value);
78         scalar_t get_value(int x, int y, int z);
79
80         // get / set relative to cell
81         void set_value(int cx, int cy, int cz, int vert_index, scalar_t value);
82         scalar_t get_value(int cx, int cy, int cz, int vert_index);
83         
84         // edges are only addressed relative to a cell
85         void set_edge(int cx, int cy, int cz, int edge, unsigned int index);
86         unsigned int get_edge(int cx, int cy, int cz, int edge);
87
88         // Position in space
89         Vector3 get_position(int x, int y, int z);
90         Vector3 get_position(int cx, int cy, int cz, int vert_index);
91         void set_from_to(const Vector3 &from, const Vector3 &to);
92         Vector3 get_from();
93         Vector3 get_to();
94
95         // Evaluators
96         void set_evaluator(scalar_t (*evaluate)(const Vector3 &vec, scalar_t t));
97         void set_normal_evaluator(Vector3 (*get_normal)(const Vector3 &vec, scalar_t t));
98         
99         // last but not least 
100         void triangulate(TriMesh *mesh, scalar_t isolevel, scalar_t t, bool calc_normals);
101 };
102
103 #endif // ndef _SCALAR_FIELD_HEADER_