Subversion Repositories gelsvn

Rev

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

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