lbm_reference
Grid_def.h
Go to the documentation of this file.
1 //! \file Grid_def.h
2 //! Brief Implementation of the Grid class
3 
4 //! \date Jan 16, 2009
5 //! \author Florian Rathgeber
6 
7 #ifndef GRID_DEF_H_
8 #define GRID_DEF_H_
9 
10 // Implementation of general Grid class
11 
12 // ============================ //
13 // Constructors and destructors //
14 // ============================ //
15 
16 template<typename T, int Cellsize>
18  sizeX_(0), sizeY_(0), sizeZ_(0), data_(0) {
19 }
20 
21 template<typename T, int Cellsize>
22 Grid<T,Cellsize>::Grid(int sizeX, int sizeY, int sizeZ) :
23  sizeX_(sizeX),
24  sizeY_(sizeY),
25  sizeZ_(sizeZ),
26  data_(Cellsize * sizeX * sizeY * sizeZ) {
27 }
28 
29 template<typename T, int Cellsize>
30 Grid<T,Cellsize>::Grid(int sizeX, int sizeY, int sizeZ, T val) :
31  sizeX_(sizeX),
32  sizeY_(sizeY),
33  sizeZ_(sizeZ),
34  data_(Cellsize * sizeX * sizeY * sizeZ, val) {
35 }
36 
37 template<typename T, int Cellsize>
39  sizeX_ = 0;
40  sizeY_ = 0;
41  sizeZ_ = 0;
42  data_.clear();
43 }
44 
45 // ============= //
46 // Set operators //
47 // ============= //
48 
49 template<typename T, int Cellsize>
50 void Grid<T,Cellsize>::init( int sizeX, int sizeY, int sizeZ, T val ) {
51  sizeX_ = sizeX;
52  sizeY_ = sizeY;
53  sizeZ_ = sizeZ;
54  data_.assign( Cellsize * sizeX * sizeY * sizeZ, val );
55 }
56 
57 template<typename T, int Cellsize>
59  data_.assign( Cellsize * sizeX_ * sizeY_ * sizeZ_, val );
60 }
61 
62 // ================ //
63 // Access operators //
64 // ================ //
65 
66 template<typename T, int Cellsize>
67 inline T& Grid<T,Cellsize>::operator()(int x, int y, int z, int f) {
68  assert( x >= 0 && x < sizeX_ );
69  assert( y >= 0 && y < sizeY_ );
70  assert( z >= 0 && z < sizeZ_ );
71  assert( f >= 0 && f < Cellsize );
72 
73  return data_[((z * sizeY_ + y) * sizeX_ + x) * Cellsize + f];
74 }
75 
76 template<typename T, int Cellsize>
77 inline const T& Grid<T,Cellsize>::operator()(int x, int y, int z, int f) const {
78  assert( x >= 0 && x < sizeX_ );
79  assert( y >= 0 && y < sizeY_ );
80  assert( z >= 0 && z < sizeZ_ );
81  assert( f >= 0 && f < Cellsize );
82 
83  return data_[((z * sizeY_ + y) * sizeX_ + x) * Cellsize + f];
84 }
85 
86 // Implementation of Grid class specialized to a single cell variable
87 
88 // ============================ //
89 // Constructors and destructors //
90 // ============================ //
91 
92 template<typename T>
94  sizeX_(0), sizeY_(0), sizeZ_(0), data_(0) {
95 }
96 
97 template<typename T>
98 Grid<T,1>::Grid(int sizeX, int sizeY, int sizeZ) :
99  sizeX_(sizeX),
100  sizeY_(sizeY),
101  sizeZ_(sizeZ),
102  data_(sizeX * sizeY * sizeZ) {
103 }
104 
105 template<typename T>
106 Grid<T,1>::Grid(int sizeX, int sizeY, int sizeZ, T val) :
107  sizeX_(sizeX),
108  sizeY_(sizeY),
109  sizeZ_(sizeZ),
110  data_(sizeX * sizeY * sizeZ, val) {
111 }
112 
113 template<typename T>
115  sizeX_ = 0;
116  sizeY_ = 0;
117  sizeZ_ = 0;
118  data_.clear();
119 }
120 
121 // ============= //
122 // Set operators //
123 // ============= //
124 
125 template<typename T>
126 void Grid<T,1>::init( int sizeX, int sizeY, int sizeZ, T val ) {
127  sizeX_ = sizeX;
128  sizeY_ = sizeY;
129  sizeZ_ = sizeZ;
130  data_.assign( sizeX * sizeY * sizeZ, val );
131 }
132 
133 template<typename T>
134 void Grid<T,1>::init( T val ) {
135  data_.assign( sizeX_ * sizeY_ * sizeZ_, val );
136 }
137 
138 // ================ //
139 // Access operators //
140 // ================ //
141 
142 template<typename T>
143 inline T& Grid<T,1>::operator()(int x, int y, int z) {
144  assert( x >= 0 && x < sizeX_ );
145  assert( y >= 0 && y < sizeY_ );
146  assert( z >= 0 && z < sizeZ_ );
147 
148  return data_[(z * sizeY_ + y) * sizeX_ + x];
149 }
150 
151 template<typename T>
152 inline const T& Grid<T,1>::operator()(int x, int y, int z) const {
153  assert( x >= 0 && x < sizeX_ );
154  assert( y >= 0 && y < sizeY_ );
155  assert( z >= 0 && z < sizeZ_ );
156 
157  return data_[(z * sizeY_ + y) * sizeX_ + x];
158 }
159 
160 #endif /* GRID_DEF_H_ */