Subversion Repositories gelsvn

Rev

Rev 443 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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