Subversion Repositories gelsvn

Rev

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

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