Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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