Subversion Repositories gelsvn

Rev

Rev 89 | Rev 443 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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