Subversion Repositories gelsvn

Rev

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

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