Subversion Repositories gelsvn

Rev

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