Subversion Repositories gelsvn

Rev

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

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