Loading...
Searching...
No Matches
Accumulator.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef DRAIN_ACCUMULATOR_H_
32#define DRAIN_ACCUMULATOR_H_
33
34#include <drain/image/CoordinatePolicy.h>
35#include <limits>
36#include <math.h>
37
38#include <stdexcept>
39
40#include "drain/util/Point.h"
41#include "drain/util/Rectangle.h"
42
43#include "ImageT.h"
44#include "AccumulationArray.h"
45#include "AccumulationMethods.h"
46#include "AccumulationConverter.h"
47
48
49
50// TODO: image/
54namespace drain
55{
56
57namespace image
58{
59
60
61
62
63// New
75//class Accumulator : public AccumulationArray {
77public:
78
84 enum FieldType {
85 DATA_SPECIFIC = 32, // Ascii bit for lower-case chars, see below
86 QUALITY = 256, // Marker for non-data
87 DATA = 'd', // Main data, of named quantity
88 WEIGHT = 'w'|QUALITY, // Quality
89 COUNT = 'c'|QUALITY, // Number of samples
90 DEVIATION = 's'|QUALITY, // Separation: std.dev or difference
91 WEIGHT_DS = 'W'|QUALITY, // Quality
92 COUNT_DS = 'C'|QUALITY, // Number of samples
93 DEVIATION_DS = 'S'|QUALITY // Separation
94 };
95
96 typedef drain::Enum<FieldType>::dict_t dict_t;
97
98 //static
99 // const dict_t dict;
100
101 typedef std::list<FieldType> FieldList;
102
103 // Deprecating?
104 inline static
105 char getFieldChar(FieldType field){
106 return static_cast<char>(field & 127);
107 }
108
109 static
110 FieldType getField(char field);
111
112 inline static
113 bool isQuality(FieldType field){
114 return (field & QUALITY) != 0;
115 }
116
118 inline static
120 return (field & DATA_SPECIFIC) != 0;
121 }
122
123 static
124 void getFields(const std::string & fieldStr, FieldList & fieldList);
125
126 static
127 void createFieldList(const std::string & fieldChars, FieldList & fieldList);
128
129
132
133 inline
134 Accumulator() : methodPtr(&undefinedMethod) {
135
137 /*
138 addMethod(overwriteMethod);
139 addMethod(maximumMethod);
140 addMethod(minimumMethod);
141 addMethod(averageMethod);
142 addMethod(weightedAverageMethod);
143 addMethod(maximumWeightMethod);
144 */
145 // setMethod(_overwriteMethod);
146
147 };
148
149 inline
150 Accumulator(const Accumulator & acc) : methodPtr(&undefinedMethod) {
151 setMethod(acc.getMethod()); // if unset, sets unset... ie. default method.
152 accArray.setGeometry(acc.accArray.getGeometry());
153 }
154
155
156 virtual inline
157 ~Accumulator(){};
158
160 void setMethod(const std::string & method);
161
163 void setMethod(const std::string & name, const std::string & params);
164
166
169 inline
170 void setMethod(const AccumulationMethod & method){
171 setMethod(method.getName(), method.getParameters().getValues());
172 }
173
174 inline
175 bool isMethodSet() const {
176 return (methodPtr != & undefinedMethod);
177 }
178
179 inline
180 const AccumulationMethod & getMethod() const {
181 return *methodPtr;
182 }
183
184 inline
185 AccumulationMethod & getMethod() {
186 return *methodPtr;
187 }
188
189 /*
190 void addMethod(AccumulationMethod & method){
191 // std::cerr << "addMethod:" << method.name << '\t' << method << '\n';
192 methods.insert(std::pair<std::string, AccumulationMethod &>(method.getName(), method));
193 }
194 */
195
197 inline
198 void add(const size_t i, double value, double weight) {
199 methodPtr->add(accArray, i, value, weight);
200 }
201
203 inline
204 void add(const size_t i, double value, double weight, unsigned int count) {
205 methodPtr->add(accArray, i, value, weight, count);
206 }
207
208
209
211
214 void addData(const Image & src, const AccumulationConverter & converter, double weight = 1.0, int iOffset=0, int jOffset=0);
215
217
221 void addData(const Image & src, const Image & srcQuality, const AccumulationConverter & converter, double weight = 1.0, int iOffset=0, int jOffset=0);
222
223
225
232 void addData(const Image & src, const Image & srcQuality, const Image & srcCount, const AccumulationConverter & converter);
233 // Could be easily added: ... double weight = 1.0, int iOffset=0, int jOffset=0
234
236
245 inline
246 void extractField(char field, const AccumulationConverter & converter, Image & dst, const drain::Rectangle<int> & crop) const {
247 extractField(getField(field), converter, dst, crop);
248 }
249
250 void extractField(FieldType field, const AccumulationConverter & converter, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
251
252 virtual
253 std::ostream & toStream(std::ostream & ostr) const;
254
255public:
256
258 // std::map<std::string, AccumulationMethod &> methods;
259
260protected:
261
263
264 /*
265 AccumulationMethod undefinedMethod;
266 OverwriteMethod overwriteMethod;
267 MaximumMethod maximumMethod;
268 MinimumMethod minimumMethod;
269 AverageMethod averageMethod;
270 WeightedAverageMethod weightedAverageMethod;
271 MaximumWeightMethod maximumWeightMethod;
272 */
273
274 AccumulationMethod * methodPtr;
275
276 // Initialize destination image to match the accumulation array - cropped if requested.
277 /*
278 * \param dst – destination image to be cropped if cropArea supplied
279 * \param cropArea – sub-area of the accumulation array; empty if no cropping requested.
280 *
281 * If cropArea geometry equals that of the accumulation array, it will be cleared and discarded in processing.
282 *
283 */
284 void initDst(const AccumulationConverter & coder, Image & dst, drain::Rectangle<int> & cropArea) const;
285
286
287};
288
289
290inline
291std::ostream & operator<<(std::ostream &ostr, const Accumulator & accumulator){
292 return accumulator.Accumulator::toStream(ostr);
293}
294
295DRAIN_ENUM_DICT(Accumulator::FieldType);
296DRAIN_ENUM_OSTREAM(Accumulator::FieldType);
297
298} // image::
299
300} // drain::
301
302
303#endif // Accumulator
virtual const std::string & getName() const
Return the name of an instance.
Definition BeanLike.h:82
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:63
void getValues(std::ostream &ostr) const
Dumps the values.
Definition SmartMap.h:345
General-purpose image compositing.
Definition AccumulationArray.h:112
virtual void setGeometry(size_t width, size_t height)
Changes the geometry of all the layers.
Definition AccumulationArray.cpp:47
Function for accumulating data: maximum, average, weighted average etc.
Definition AccumulationMethods.h:65
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:92
Definition Accumulator.h:76
void setMethod(const AccumulationMethod &method)
Copies the method and its parameters.
Definition Accumulator.h:170
static bool isSpecific(FieldType field)
Future option to mark scaled/normalized etc.
Definition Accumulator.h:119
static void createFieldList(const std::string &fieldChars, FieldList &fieldList)
Definition Accumulator.cpp:83
void initDst(const AccumulationConverter &coder, Image &dst, drain::Rectangle< int > &cropArea) const
Definition Accumulator.cpp:382
void add(const size_t i, double value, double weight, unsigned int count)
Adds decoded data that applies natural scaling.
Definition Accumulator.h:204
void extractField(char field, const AccumulationConverter &converter, Image &dst, const drain::Rectangle< int > &crop) const
Extracts the accumulated quantity or secondary quantities like weight and standard deviation.
Definition Accumulator.h:246
FieldType
Definition Accumulator.h:84
void setMethod(const std::string &method)
Set method to some of the predefined methods.
Definition Accumulator.cpp:198
Accumulator()
Definition Accumulator.h:134
void add(const size_t i, double value, double weight)
Adds decoded data that applies natural scaling.
Definition Accumulator.h:198
AccumulationArray accArray
Todo: export.
Definition Accumulator.h:131
void addData(const Image &src, const AccumulationConverter &converter, double weight=1.0, int iOffset=0, int jOffset=0)
Add (accumulate) data with given prior weight.
Definition Accumulator.cpp:216
AccumulationMethod undefinedMethod
A Some predefined methods, that can be set with setMethod(const std::string & key).
Definition Accumulator.h:262
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:193
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