grid.hpp
Go to the documentation of this file.
1// $Id: grid.hpp 882 2011-09-23 13:10:16Z perroe $
2
3// Copyright (c) 2011, Norwegian Computing Center
4// All rights reserved.
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7// • Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9// • Redistributions in binary form must reproduce the above copyright notice, this list of
10// conditions and the following disclaimer in the documentation and/or other materials
11// provided with the distribution.
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
13// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
15// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
17// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
20// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21
22#ifndef NRLIB_GRID_HPP
23#define NRLIB_GRID_HPP
24
25#include <cassert>
26#include <sstream>
27#include <vector>
28#include <limits>
29
30//#include "../../../src/definitions.h"
31
32namespace NRLib {
33
34template<class A>
35class Grid {
36public:
37 typedef typename std::vector<A>::iterator iterator;
38 typedef typename std::vector<A>::const_iterator const_iterator;
39 typedef typename std::vector<A>::reference reference;
40 typedef typename std::vector<A>::const_reference const_reference;
41
44 Grid(size_t ni, size_t nj, size_t nk, const A& val = A());
45 virtual ~Grid();
46
50 void Resize(size_t ni, size_t nj, size_t nk, const A& val = A());
51 void GetAvgMinMax(A& avg, A& min, A& max);
52 void GetAvgMinMaxWithMissing(A& avg, A& min, A& max, A missing);
53 void LogTransform(A missing);
54
55 inline reference operator()(size_t i, size_t j, size_t k);
56 inline reference operator()(size_t index);
57 inline reference GetValue(size_t i, size_t j, size_t k);
58
59 inline const_reference operator()(size_t i, size_t j, size_t k) const;
60 inline const_reference operator()(size_t index) const;
61 inline const_reference GetValue(size_t i, size_t j, size_t k) const;
62
63 iterator begin() {return( data_.begin()); }
64 iterator end() {return( data_.end()); }
65
66 const_iterator begin() const { return(data_.begin()); }
67 const_iterator end() const { return(data_.end()); }
68
69 size_t GetNI() const { return(ni_); }
70 size_t GetNJ() const { return(nj_); }
71 size_t GetNK() const { return(nk_); }
72 size_t GetN() const { return data_.size(); }
73
74 inline size_t GetIndex(size_t i, size_t j, size_t k) const;
75 void GetIJK(size_t index, size_t &i, size_t &j, size_t &k) const;
76 void SetValue(size_t i, size_t j, size_t k, const A& value);
77
78 void Swap(Grid<A>& other);
79
80private:
81 size_t ni_;
82 size_t nj_;
83 size_t nk_;
84
86 std::vector<A> data_;
87};
88
89template<class A>
91 : ni_(0),
92 nj_(0),
93 nk_(0),
94 data_()
95{}
96
97template<class A>
98Grid<A>::Grid(size_t ni, size_t nj, size_t nk, const A& val)
99 : ni_(ni),
100 nj_(nj),
101 nk_(nk),
102 data_(ni*nj*nk, val)
103{}
104
105template<class A>
107{}
108
109template<class A>
110void Grid<A>::Resize(size_t ni, size_t nj, size_t nk, const A& val)
111{
112 ni_ = ni;
113 nj_ = nj;
114 nk_ = nk;
115
116 data_.resize(0); //To avoid copying of elements
117 data_.resize(ni_ * nj_ * nk_, val);
118}
119
120template<class A>
121void Grid<A>::GetAvgMinMax(A& avg, A& min, A& max)
122{
123 A sum = 0.0;
124 A value = 0.0;
125 max = -std::numeric_limits<A>::infinity();
126 min = +std::numeric_limits<A>::infinity();
127
128 for(unsigned int i = 0; i < ni_; i++) {
129 for(unsigned int j = 0; j < nj_; j++) {
130 for(unsigned int k = 0; k < nk_; k++) {
131 value = data_[GetIndex(i, j, k)];
132 sum += value;
133
134 if(value > max)
135 max = value;
136
137 if(value < min)
138 min = value;
139
140 }
141 }
142 }
143 avg = sum /= GetN();
144}
145
146template<class A>
147void Grid<A>::GetAvgMinMaxWithMissing(A& avg, A& min, A& max, A missing)
148{
149 A sum = 0.0;
150 A value = 0.0;
151 max = -std::numeric_limits<A>::infinity();
152 min = +std::numeric_limits<A>::infinity();
153
154 unsigned int n = 0;
155 for(unsigned int i = 0; i < ni_; i++) {
156 for(unsigned int j = 0; j < nj_; j++) {
157 for(unsigned int k = 0; k < nk_; k++) {
158 value = data_[GetIndex(i, j, k)];
159 if(value != missing) {
160 sum += value;
161 n++;
162 if(value > max)
163 max = value;
164
165 if(value < min)
166 min = value;
167 }
168 }
169 }
170 }
171 avg = sum/static_cast<A>(n);
172}
173
174template<class A>
176{
177 for(size_t i = 0; i < ni_; i++) {
178 for(size_t j = 0; j < nj_; j++) {
179 for(size_t k = 0; k < nk_; k++) {
180 A value = data_[GetIndex(i, j, k)];
181 if(value == missing || value <= 0.0) //First RMISSING
182 data_[GetIndex(i, j, k)] = 0.0;
183 else
184 data_[GetIndex(i, j, k)] = log(value);
185 }
186 }
187 }
188}
189
190template<class A>
191typename Grid<A>::reference Grid<A>::operator()(size_t i, size_t j, size_t k)
192{
193 return data_[GetIndex(i, j, k)];
194}
195
196template<class A>
198{
199 assert(index < GetN());
200
201 return data_[index];
202}
203
204template<class A>
205typename Grid<A>::reference Grid<A>::GetValue(size_t i, size_t j, size_t k)
206{
207 return data_[GetIndex(i, j, k)];
208}
209
210template<class A>
211typename Grid<A>::const_reference Grid<A>::operator()(size_t i, size_t j, size_t k) const
212{
213 return data_[GetIndex(i, j, k)];
214}
215
216template<class A>
218{
219 assert(index < GetN());
220
221 return data_[index];
222}
223
224template<class A>
225typename Grid<A>::const_reference Grid<A>::GetValue(size_t i, size_t j, size_t k) const
226{
227 return data_[GetIndex(i, j, k)];
228}
229
230template<class A>
231size_t Grid<A>::GetIndex(size_t i, size_t j, size_t k) const
232{
233 assert(i < GetNI() && j < GetNJ() && k < GetNK());
234
235 return i + j*ni_ + k*ni_*nj_;
236}
237
238template<class A>
239void Grid<A>::GetIJK(size_t index, size_t &i, size_t &j, size_t &k) const
240{
241 assert(index < GetN());
242
243 i = index % ni_;
244 j = (index-i)/ni_ % nj_;
245 k = (index - j*ni_ - i)/ni_/nj_;
246}
247
248template<class A>
249void Grid<A>::SetValue(size_t i, size_t j, size_t k, const A& value)
250{
251 data_[GetIndex(i, j, k)] = value;
252}
253
254template<class A>
256{
257 std::swap(ni_, other.ni_);
258 std::swap(nj_, other.nj_);
259 std::swap(nk_, other.nk_);
260 data_.swap(other.data_);
261}
262
263}
264#endif
265
266
267
268
269
270
271
int index
Definition: cJSON.h:168
Definition: grid.hpp:35
std::vector< A >::const_reference const_reference
Definition: grid.hpp:40
const_reference GetValue(size_t i, size_t j, size_t k) const
Definition: grid.hpp:225
const_iterator begin() const
Definition: grid.hpp:66
virtual ~Grid()
Definition: grid.hpp:106
void Swap(Grid< A > &other)
Definition: grid.hpp:255
size_t GetNK() const
Definition: grid.hpp:71
void GetIJK(size_t index, size_t &i, size_t &j, size_t &k) const
Definition: grid.hpp:239
reference operator()(size_t index)
Definition: grid.hpp:197
size_t GetNJ() const
Definition: grid.hpp:70
std::vector< A >::reference reference
Definition: grid.hpp:39
const_reference operator()(size_t index) const
Definition: grid.hpp:217
size_t GetIndex(size_t i, size_t j, size_t k) const
Definition: grid.hpp:231
const_iterator end() const
Definition: grid.hpp:67
const_reference operator()(size_t i, size_t j, size_t k) const
Definition: grid.hpp:211
void GetAvgMinMax(A &avg, A &min, A &max)
Definition: grid.hpp:121
std::vector< A >::iterator iterator
Definition: grid.hpp:37
std::vector< A >::const_iterator const_iterator
Definition: grid.hpp:38
Grid()
Definition: grid.hpp:90
reference GetValue(size_t i, size_t j, size_t k)
Definition: grid.hpp:205
void SetValue(size_t i, size_t j, size_t k, const A &value)
Definition: grid.hpp:249
void Resize(size_t ni, size_t nj, size_t nk, const A &val=A())
Definition: grid.hpp:110
Grid(size_t ni, size_t nj, size_t nk, const A &val=A())
Definition: grid.hpp:98
iterator end()
Definition: grid.hpp:64
void LogTransform(A missing)
Definition: grid.hpp:175
void GetAvgMinMaxWithMissing(A &avg, A &min, A &max, A missing)
Definition: grid.hpp:147
reference operator()(size_t i, size_t j, size_t k)
Definition: grid.hpp:191
size_t GetNI() const
Definition: grid.hpp:69
iterator begin()
Definition: grid.hpp:63
size_t GetN() const
Definition: grid.hpp:72
Definition: exception.hpp:31
T min(const T v0, const T v1)
Definition: exprtk.hpp:1400
T max(const T v0, const T v1)
Definition: exprtk.hpp:1407
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12955