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 __HGRID_H
2
#define __HGRID_H
3
// Author: J. Andreas Bærentzen,
4
// Created: Wed Jan 24 18:29:0
5
 
6
#include <vector>
7
#include "AncestorGrid.h"
8
#include "Cell.h"
9
 
10
namespace Geometry 
11
{
12
	/** Hierarchical voxel grid. 
13
			In many cases we wish to save on the storage requirements of volumes.
14
			A hierarchical voxel grid is a volume representation where the volume 
15
			is divided into box shaped regions, and each region is represented only 
16
			if it contains voxels. This class template is for such a grid.
17
	*/
18
	template<class T, class CellT=DefaultCell<T,8> >
19
	class HGrid: public AncestorGrid<T,HGrid<T,CellT> > 
20
	{
21
	public:
22
		typedef T DataType;		
23
		typedef CellT CellType;
24
 
25
	private:
26
 
27
		/// Dimensions of top level grid.
28
		const CGLA::Vec3i top_dims;
29
 
30
		/// Top level grid. I.e. vector of sub grids.
31
		std::vector<CellT> top_grid;
32
 
33
		/// The default grid value, used to clear grid. 
34
		DataType default_val;
35
 
36
		/// Size of the top grid (number of cells x*y*z)
37
		int top_grid_size;
38
 
39
	public:
40
 
41
		const CGLA::Vec3i& get_top_dims() const {return top_dims;}
42
 
43
		int get_bottom_dim() const {return CellT::get_dim();}
44
 
45
 
46
	private:
47
 
48
		/// Get index into top level grid from int vector position.
49
		int get_top_index(const CGLA::Vec3i& idx) const
50
		{
51
			const CGLA::Vec3i top_idx = idx/get_bottom_dim();
52
			return (top_idx[2]*top_dims[1]+top_idx[1])*top_dims[0]+top_idx[0];
53
		}
54
 
55
	public:
56
 
57
		/// Construct grid of specified dimensions
58
		HGrid(const CGLA::Vec3i& dims, const T& val = T()):
59
			AncestorGrid<T,HGrid<T,CellT> >(dims),
60
			top_dims(dims/CellT::get_dim()+
61
							 CGLA::Vec3i(dims[0]%CellT::get_dim()?1:0,
62
													 dims[1]%CellT::get_dim()?1:0,
63
													 dims[2]%CellT::get_dim()?1:0)),
64
			top_grid(top_dims[0]*top_dims[1]*top_dims[2],CellT(val)),
65
			default_val(val), top_grid_size(top_grid.size())
66
		{}
67
 
68
		/** Store a voxel vox at position p in grid. The Cell will
69
				automatically subdivide if it is not already subdivided. */
70
 		void store(const CGLA::Vec3i& p, const T& vox)
71
		{
72
			assert(in_domain(p));
73
			top_grid[get_top_index(p)].store(p, vox);
74
		}
75
 
76
 
77
		/** Read only access to a voxel in the grid. */
78
 		const T& operator[](const CGLA::Vec3i& p) const
79
		{
80
			assert(in_domain(p));
81
			return top_grid[get_top_index(p)][p];
82
		}
83
 
84
 
85
//  		bool get(const CGLA::Vec3i& p, T* voxel)
86
// 		{
87
// 			assert(in_domain(p));
88
// 			CellT* cell = top_grid[get_top_index(p)];
89
// 			if(cell->is_coalesced()) return false;
90
// 			voxel = cell[p];
91
// 		}
92
 
93
		CellT& get_cell(const CGLA::Vec3i& p)
94
		{
95
			return top_grid[(p[2]*top_dims[1]+p[1])*top_dims[0]+p[0]];
96
		}
97
 
98
		const CellT& get_cell(const CGLA::Vec3i& p) const
99
		{
100
			return top_grid[(p[2]*top_dims[1]+p[1])*top_dims[0]+p[0]];
101
		}
102
 
103
		CellT& get_cell(int i) 
104
		{
105
			return top_grid[i];
106
		}
107
 
108
		const CellT& get_cell(int i) const
109
		{
110
			return top_grid[i];
111
		}
112
 
113
		void clear()
114
		{
115
			int N = top_grid.size();
116
			for(int i=0;i<N;++i)
117
				top_grid[i].coalesce(default_val);
118
		}
119
 
120
	};
121
 
122
}
123
#endif