AccumulationMethods.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef ACCUMULATION_H_
32 #define ACCUMULATION_H_
33 
34 #include <limits>
35 #include <math.h>
36 
37 #include <stdexcept>
38 
39 #include <drain/util/Bank.h>
40 #include <drain/util/BeanLike.h>
41 #include <drain/util/Point.h>
42 #include <drain/util/Rectangle.h>
43 #include "ImageT.h"
44 #include "CoordinatePolicy.h"
45 #include "AccumulationArray.h"
46 
50 namespace drain
51 {
52 
53 namespace image
54 {
55 
56 
58 
64 class AccumulationMethod : public BeanLike {
65 public:
66 
67  inline
68  AccumulationMethod() : BeanLike("UNDEFINED", __FUNCTION__) {
69  };
70 
71  inline
72  AccumulationMethod(const AccumulationMethod &method) : BeanLike(method.getName(), method.getDescription()){
73  };
74 
75  virtual
76  ~AccumulationMethod(){};
77 
78 
79 
80 
82 
90  virtual inline
91  void add(AccumulationArray & accArray, const size_t i, double value, double weight) const {
92  throw std::runtime_error("AccumulationMethod::add - method/rule UNDEFINED");
93  };
94 
95 
97  virtual inline
98  void add(AccumulationArray & accArray, const size_t i, double value, double weight, unsigned int count) const {
99  add(accArray, i, value, weight);
100  };
101 
102 
103 
105 
114  virtual
115  void extractValue(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
116 
117 
119 
124  virtual
125  void extractWeight(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
126 
128 
133  virtual
134  void extractCount(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
135 
137 
143  virtual
144  void extractDev(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
145 
146  //virtual
147  //void extractDevInv(const AccumulationConverter & coder, Image & dst) const;
148 
149  //const std::string name;
150 
151  /*
152  virtual
153  std::ostream & toStream(std::ostream & ostr) const {
154  ostr << name;
155  if (!parameters.empty())
156  ostr << " [" << parameters << "]";
157  return ostr;
158  };
159  */
160 
161 protected:
162 
164  //virtual
165  //void updateInternalParameters(){};
166 
167  void initDstOLD(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop) const ;
168 
169 
170  // AccumulationMethod(const std::string & name, AccumulationArray & c) : BeanLike(name, __FUNCTION__), accumulationArray(c) {
171  //};
172 
173  AccumulationMethod(const std::string & name) : BeanLike(name, __FUNCTION__) {
174  };
175 
176  //AccumulationArray & accumulationArray;
177 
178 };
179 
180 /*
181 inline
182 std::ostream & operator<<(std::ostream & ostr, const AccumulationMethod & accumulationMethod){
183  return accumulationMethod.toStream(ostr);
184 }
185 */
186 
187 
188 
189 
191 
192 public:
193 
194  //OverwriteMethod(AccumulationArray & c) : AccumulationMethod("LATEST", c) {};
195  OverwriteMethod() : AccumulationMethod("LATEST") {};
196 
197  virtual
198  void add(AccumulationArray & accArray, const size_t i, double value, double weight) const;
199 
201  virtual
202  void extractDev(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
203 
204 
205 };
206 
208 
209 public:
210 
211  //MaximumMethod(AccumulationArray & c) : AccumulationMethod("MAXIMUM", c) {};
212  inline
213  MaximumMethod() : AccumulationMethod("MAXIMUM") {};
214 
215  virtual
216  void add(AccumulationArray & accArray, const size_t i, double value, double weight) const;
217 
218 };
219 
221 
222 public:
223 
224  //MinimumMethod(AccumulationArray & c) : AccumulationMethod("MINIMUM", c) {};
225  inline
226  MinimumMethod() : AccumulationMethod("MINIMUM") {};
227 
228  virtual
229  void add(AccumulationArray & accArray, const size_t i, double value, double weight) const;
230 
231 };
232 
233 
235 
236 public:
237 
238  //AverageMethod(AccumulationArray & c) : AccumulationMethod("AVERAGE", c) {};
239  inline
240  AverageMethod() : AccumulationMethod("AVERAGE") {};
241 
242  virtual
243  void add(AccumulationArray & accArray, const size_t i, double value, double weight) const;
244 
245  virtual // inline
246  void add(AccumulationArray & accArray, const size_t i, double value, double weight, unsigned int count) const;
247 
248  virtual
249  void extractValue(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
250 
251  virtual
252  void extractWeight(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
253 
254  virtual
255  void extractDev(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
256 
257 };
258 
259 
260 
262 
263 
264 public:
265 
266  inline
267  WeightedAverageMethod() : AccumulationMethod("WAVG"), pInv(1.0), USE_P(false), rInv(1.0), USE_R(false) {
268  parameters.link("p", p = 1.0);
269  parameters.link("r", r = 1.0);
270  parameters.link("bias", bias = 0.0);
271  updateBean();
272  };
273 
274  inline
275  WeightedAverageMethod(const WeightedAverageMethod & method) : AccumulationMethod(method), pInv(1.0), USE_P(false), rInv(1.0), USE_R(false) {
276  parameters.copyStruct(method.parameters, method, *this);
277  /*
278  parameters.link("p", p = 1.0);
279  parameters.link("r", r = 1.0);
280  parameters.link("bias", bias = 0.0);
281  */
282  updateBean();
283  };
284 
285 
286  virtual
287  void updateBean() const;
288 
289  virtual
290  void add(AccumulationArray & accArray, const size_t i, double value, double weight) const;
291 
292  virtual inline
293  void add(AccumulationArray & accArray, const size_t i, double value, double weight, unsigned int count) const;
294 
295  virtual
296  void extractValue(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
297 
298  virtual
299  void extractWeight(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
300 
301  virtual
302  void extractDev(const AccumulationArray & accArray, const AccumulationConverter & coder, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
303 
304  /*
305  virtual
306  std::ostream & toStream(std::ostream & ostr) const {
307  //ostr << name << '(' << p << ',' << r << ',' << dataScaling << ')';
308  ostr << name << '(' << p << ',' << r << ',' << bias << ')';
309  return ostr;
310  };
311  */
312 
313 protected:
314 
315  double bias = 0.0;
316 
318  double p = 1.0;
319  mutable double pInv = 1.0;
320  mutable bool USE_P = false;
321 
323  double r = 1.0;
324  mutable double rInv = 1.0;
325  mutable bool USE_R = false;
326 
327 };
328 
329 
331 
332 public:
333 
334  //MaximumWeightMethod(AccumulationArray & c) : AccumulationMethod("MAXW", c) {};
335  inline
337 
338  virtual
339  void add(AccumulationArray & accArray, const size_t i, double value, double weight) const;
340 
341 };
342 
343 
345 
346 extern
347 AccMethodBank & getAccMethodBank();
348 
349 
350 }
351 
352 }
353 
354 #endif /* Cumulator_H_ */
355 
356 // Drain
Definition: Bank.h:64
Something which has a name, a description and possibly some parameters of varying type.
Definition: BeanLike.h:60
virtual const std::string & getName() const
Return the name of an instance.
Definition: BeanLike.h:69
virtual const std::string & getDescription() const
Return a brief description.
Definition: BeanLike.h:74
void copyStruct(const ReferenceMap &m, const T &src, T &dst, extLinkPolicy policy=RESERVE)
Experimental. Copies references and values of a structure to another.
Definition: ReferenceMap.h:399
General-purpose image compositing.
Definition: AccumulationArray.h:112
Function for accumulating data: maximum, average, weighted average etc.
Definition: AccumulationMethods.h:64
virtual void add(AccumulationArray &accArray, const size_t i, double value, double weight, unsigned int count) const
Adds 'count' copies of a weighted value to the accumulation array.
Definition: AccumulationMethods.h:98
void initDstOLD(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop) const
Sets variables that depend on public parameters. Called by setParameters().
Definition: AccumulationMethods.cpp:75
virtual void extractWeight(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieves the (average) weight of the accumulated values.
Definition: AccumulationMethods.cpp:186
virtual void extractValue(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieve the accumulated values from the accumulation matrix back to a data array.
Definition: AccumulationMethods.cpp:106
virtual void extractDev(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieves the standard deviation of the accumulated values.
Definition: AccumulationMethods.cpp:258
virtual void extractCount(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieves the count of values accumulated.
Definition: AccumulationMethods.cpp:229
virtual void add(AccumulationArray &accArray, const size_t i, double value, double weight) const
Adds a weighted value to the accumulation array.
Definition: AccumulationMethods.h:91
Definition: AccumulationMethods.h:234
virtual void extractWeight(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieves the (average) weight of the accumulated values.
Definition: AccumulationMethods.cpp:484
virtual void add(AccumulationArray &accArray, const size_t i, double value, double weight) const
Adds a weighted value to the accumulation array.
Definition: AccumulationMethods.cpp:388
virtual void extractValue(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieve the accumulated values from the accumulation matrix back to a data array.
Definition: AccumulationMethods.cpp:413
virtual void extractDev(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieves the standard deviation of the accumulated values.
Definition: AccumulationMethods.cpp:535
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition: Image.h:184
Definition: AccumulationMethods.h:207
virtual void add(AccumulationArray &accArray, const size_t i, double value, double weight) const
Adds a weighted value to the accumulation array.
Definition: AccumulationMethods.cpp:360
Definition: AccumulationMethods.h:330
virtual void add(AccumulationArray &accArray, const size_t i, double value, double weight) const
Adds a weighted value to the accumulation array.
Definition: AccumulationMethods.cpp:906
Definition: AccumulationMethods.h:220
virtual void add(AccumulationArray &accArray, const size_t i, double value, double weight) const
Adds a weighted value to the accumulation array.
Definition: AccumulationMethods.cpp:374
Definition: AccumulationMethods.h:190
virtual void add(AccumulationArray &accArray, const size_t i, double value, double weight) const
Adds a weighted value to the accumulation array.
Definition: AccumulationMethods.cpp:291
virtual void extractDev(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Special defimition: difference.
Definition: AccumulationMethods.cpp:306
Definition: AccumulationMethods.h:261
virtual void extractWeight(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieves the (average) weight of the accumulated values.
Definition: AccumulationMethods.cpp:771
virtual void updateBean() const
Called after setParameters()
Definition: AccumulationMethods.cpp:585
double r
Power for weights.
Definition: AccumulationMethods.h:323
virtual void add(AccumulationArray &accArray, const size_t i, double value, double weight) const
Adds a weighted value to the accumulation array.
Definition: AccumulationMethods.cpp:605
virtual void extractValue(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieve the accumulated values from the accumulation matrix back to a data array.
Definition: AccumulationMethods.cpp:672
virtual void extractDev(const AccumulationArray &accArray, const AccumulationConverter &coder, Image &dst, const drain::Rectangle< int > &crop={0, 0, 0, 0}) const
Retrieves the standard deviation of the accumulated values.
Definition: AccumulationMethods.cpp:829
double p
Power for data values.
Definition: AccumulationMethods.h:318
Definition: DataSelector.cpp:1277
Rectange defined through lower left and upper right coordinates.
Definition: Rectangle.h:65
Converts raw data to values appropriate for accumulation.
Definition: AccumulationConverter.h:50