-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.h
141 lines (117 loc) · 3.44 KB
/
grid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
#include <cassert>
#include <algorithm>
namespace lbm
{
typedef unsigned int uint;
template< typename Type, uint Cellsize >
class Grid
{
public:
inline Grid();
inline Grid( uint xsize, uint ysize );
inline ~Grid();
inline Type& operator()( uint x, uint y, uint f );
inline Type operator()( uint x, uint y, uint f ) const;
void swap(Grid<Type,Cellsize>& grid) /* throw() */
{
std::swap( xsize_, grid.xsize_ );
std::swap( ysize_, grid.ysize_ );
std::swap( data_, grid.data_ );
}
private:
uint xsize_; // Number of nodes in x-dimension
uint ysize_; // Number of nodes in y-dimension
Type* data_; // Linearized, 1-dimensional representation of the 2D data grid
};
// Implementation of the default constructor
template< typename Type, uint Cellsize >
Grid<Type,Cellsize>::Grid():
xsize_(0),
ysize_(0),
data_(0)
{}
// Implementation of the initialization constructor
template< typename Type, uint Cellsize >
Grid<Type,Cellsize>::Grid( uint xsize, uint ysize ):
xsize_(xsize),
ysize_(ysize),
data_(new Type[Cellsize*xsize*ysize])
{}
// Implementation of the destructor
template< typename Type, uint Cellsize >
Grid<Type,Cellsize>::~Grid(){
delete [] data_;
}
// Implementation of the function call operator
template< typename Type, uint Cellsize >
inline Type&
Grid<Type,Cellsize>::operator()( uint x, uint y, uint f )
{
assert( x < xsize_ && y < ysize_ && f < Cellsize );
return data_[y*xsize_*Cellsize+x*Cellsize+f];
}
// Implementation of the const function call operator//
template< typename Type, uint Cellsize >
inline Type
Grid<Type,Cellsize>::operator()( uint x, uint y, uint f ) const
{
assert( x < xsize_ && y < ysize_ && f < Cellsize );
return data_[y*xsize_*Cellsize+x*Cellsize+f];
}
template< typename Type, size_t N >
inline void swap(Grid<Type,N>& a,Grid<Type,N>& b ) /* throw() */
{
a.swap(b);
}
// Partial template specialization for Cellsize = 1
template< typename Type >
class Grid<Type,1>
{
public:
inline Grid( uint xsize, uint ysize );
inline ~Grid();
inline Type& operator()( uint x, uint y);
inline Type operator()( uint x, uint y) const;
private:
uint xsize_; // Number of nodes in x-dimension
uint ysize_; // Number of nodes in y-dimension
Type* data_; // Linearized, 1-dimensional representation of the 2D data grid
};
// Implementation of the initialization constructor
template< typename Type>
Grid<Type,1>::Grid( uint xsize, uint ysize ):
xsize_(xsize),
ysize_(ysize),
data_(new Type[xsize*ysize])
{}
// Implementation of the destructor
template< typename Type >
Grid<Type,1>::~Grid(){
delete [] data_;
}
// Implementation of the function call operator
template< typename Type>
inline Type&
Grid<Type,1>::operator()( uint x, uint y)
{
assert( x < xsize_ && y < ysize_ );
return data_[y*xsize_+x];
}
// Implementation of the const function call operator//
template< typename Type>
inline Type
Grid<Type,1>::operator()( uint x, uint y ) const
{
assert( x < xsize_ && y < ysize_ );
return data_[y*xsize_+x];
}
// Partial template specialization for Cellsize = 0
// // No class definition => compile time error
template< typename Type >
class Grid<Type,0>;
typedef Grid<double,9> PDF_Field;
typedef Grid<double,2> V_Field;
typedef Grid<double,1> D_Field;
typedef Grid<uint,1> Flags;
}