Subversion Repositories gelsvn

Rev

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

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