Subversion Repositories gelsvn

Rev

Rev 125 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
61 jab 1
#ifndef __RGRID_H
2
#define __RGRID_H
3
 
4
#include <vector>
5
#include "CGLA/Vec3i.h"
6
#include "AncestorGrid.h"
7
 
8
namespace Geometry 
9
{
10
 
11
	/** Regular voxel grid. 
12
			This class template can be instantiated and used directly.
13
			This is just a regular voxel grid stored as a linear array
14
			with functions to access its contents. */			
15
	template<class T>
16
	class RGrid: public AncestorGrid<T,RGrid<T> >
17
	{
18
	public:
19
		typedef T DataType;
20
 
21
	private:
22
		/// x size of grid.
23
		int x_dim;
24
 
25
		/// x size times y size of grid. Stored for efficiency reasons.
26
		int xy_dim;
27
 
28
		/// Vector containing the actual data.
29
		std::vector<T> data;
30
 
31
		/// Convert xyz index into an index in a linear array.
32
		int grid_idx(const CGLA::Vec3i& idx) const
33
		{
34
			return  idx[2] * xy_dim + idx[1] * x_dim + idx[0];
35
		}
36
 
37
		/// The default grid value, used to clear grid. 
38
		DataType default_val;
39
 
40
	public:
41
 
42
		/** Construct a regular voxel grid. This function
43
				is passed a Vec3i _dims and an optional 
44
				initialization value, val. It creates a grid
45
				of specified dimensions, and initializes the 
46
				value of all voxels to val. */
47
		RGrid(CGLA::Vec3i _dims, const T& val = T()):
48
			AncestorGrid<T,RGrid<T> >(_dims), 
49
			x_dim(_dims[0]), xy_dim(_dims[0]*_dims[1]),
50
			data(_dims[0]*_dims[1]*_dims[2],val),
51
			default_val(val)
52
		{}	
53
 
54
		/** Construct a grid of dimensions 0,0,0 */
55
		RGrid(): AncestorGrid<T,RGrid<T> >(CGLA::Vec3i(0)), 
56
			x_dim(0), xy_dim(0),
57
			data(0,0), default_val(0)
58
		{}	
59
 
60
		/** Store a voxel in a regular grid. */
61
		void store(const CGLA::Vec3i& p, const T& t) 
62
		{
63
			assert(in_domain(p));
64
			data[grid_idx(p)] = t;
65
		}
66
 
67
		/** Read/write access to voxel grid. This is 
68
				a non-const overloaded operator[]. In a regular 
69
				grid, we have reserved room for all voxels in 
70
				advance. Hence, it is possible to create a non-const
71
				operator[]. See AncestorGrid::operator[]. */
72
		T& operator[](const CGLA::Vec3i& p) 
73
		{
74
			assert(in_domain(p));
75
			return data[grid_idx(p)];
76
		}
77
 
78
		/// Read only access to voxel grid through const operator[]
79
		const T& operator[](const CGLA::Vec3i& p) const 
80
		{
81
			assert(in_domain(p));
82
			return data[grid_idx(p)];
83
		}
84
 
85
		/// Const function to get a pointer to the first voxel in grid.
86
		const T* get() const {return &data[0];}
87
 
88
		/// Non-const function to get a pointer to the first voxel in grid.
89
		T* get() {return &data[0];}
90
 
91
		/// Get x dimensions of grid.
92
		int get_x_dim() const { return x_dim;}
93
 
94
		/// Get x size times y size of grid.
95
		int get_xy_dim() const { return xy_dim;}
96
 
97
		/// Get length of linear array actually containing voxels.
98
		int get_size() const { return data.size();}
99
 
100
		void clear()
101
		{
102
			int N = data.size();
103
			for(int i=0;i<N;++i)
104
				data[i] = default_val;
105
		}
106
 
107
 
108
	};
109
 
110
}
111
 
112
#endif