Subversion Repositories gelsvn

Rev

Rev 125 | Rev 595 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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