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 = 'd', // Main data, of named quantity
86 QUALITY = 256, // Marker for non-data (why not 128?)
87 QUANTITY_SPECIFIC = 32, // Bit marking upper-case chars, see below
88 // Quality fields can be GLOBAL or QUANTITY_SPECIFIC
89 WEIGHT_GLOBAL = 'W'|QUALITY, // Quality
90 COUNT_GLOBAL = 'C'|QUALITY, // Number of samples
91 WEIGHT = 'w'|QUALITY, // Quality
92 COUNT = 'c'|QUALITY, // Number of samples
93 DEVIATION = 's'|QUALITY // Separation: std.dev or difference
94 // DEVIATION = 's'|QUALITY, // Separation: std.dev or difference
95 };
96
97 typedef drain::Enum<FieldType>::dict_t dict_t;
98
99 //static
100 // const dict_t dict;
101
102 typedef std::list<FieldType> FieldList;
103
104 // Deprecating?
105 inline static
106 char getFieldChar(FieldType field){
107 return static_cast<char>(field & 127);
108 }
109
110 static
111 FieldType getField(char field);
112
113 inline static
114 bool isQuality(FieldType field){
115 return (field & QUALITY) != 0;
116 }
117
119 inline static
121 return ((int)field & (int)QUANTITY_SPECIFIC) != 0;
122 }
123
124 static
125 void getFields(const std::string & fieldStr, FieldList & fieldList);
126
127 static
128 void createFieldList(const std::string & fieldChars, FieldList & fieldList);
129
130
133
134 inline
135 Accumulator() : methodPtr(&undefinedMethod) {
136
138 /*
139 addMethod(overwriteMethod);
140 addMethod(maximumMethod);
141 addMethod(minimumMethod);
142 addMethod(averageMethod);
143 addMethod(weightedAverageMethod);
144 addMethod(maximumWeightMethod);
145 */
146 // setMethod(_overwriteMethod);
147
148 };
149
150 inline
151 Accumulator(const Accumulator & acc) : methodPtr(&undefinedMethod) {
152 setMethod(acc.getMethod()); // if unset, sets unset... ie. default method.
153 accArray.setGeometry(acc.accArray.getGeometry());
154 }
155
156
157 virtual inline
158 ~Accumulator(){};
159
161 void setMethod(const std::string & method);
162
164 void setMethod(const std::string & name, const std::string & params);
165
167
170 inline
171 void setMethod(const AccumulationMethod & method){
172 setMethod(method.getName(), method.getParameters().getValues());
173 }
174
175 inline
176 bool isMethodSet() const {
177 return (methodPtr != & undefinedMethod);
178 }
179
180 inline
181 const AccumulationMethod & getMethod() const {
182 return *methodPtr;
183 }
184
185 inline
186 AccumulationMethod & getMethod() {
187 return *methodPtr;
188 }
189
190 /*
191 void addMethod(AccumulationMethod & method){
192 // std::cerr << "addMethod:" << method.name << '\t' << method << '\n';
193 methods.insert(std::pair<std::string, AccumulationMethod &>(method.getName(), method));
194 }
195 */
196
198 inline
199 void add(const size_t i, double value, double weight) {
200 methodPtr->add(accArray, i, value, weight);
201 }
202
204 inline
205 void add(const size_t i, double value, double weight, unsigned int count) {
206 methodPtr->add(accArray, i, value, weight, count);
207 }
208
209
210
212
215 void addData(const Image & src, const AccumulationConverter & converter, double weight = 1.0, int iOffset=0, int jOffset=0);
216
218
222 void addData(const Image & src, const Image & srcQuality, const AccumulationConverter & converter, double weight = 1.0, int iOffset=0, int jOffset=0);
223
224
226
233 void addData(const Image & src, const Image & srcQuality, const Image & srcCount, const AccumulationConverter & converter);
234 // Could be easily added: ... double weight = 1.0, int iOffset=0, int jOffset=0
235
237
246 inline
247 void extractField(char field, const AccumulationConverter & converter, Image & dst, const drain::Rectangle<int> & crop) const {
248 extractField(getField(field), converter, dst, crop);
249 }
250
251 void extractField(FieldType field, const AccumulationConverter & converter, Image & dst, const drain::Rectangle<int> & crop = {0,0,0,0}) const;
252
253 virtual
254 std::ostream & toStream(std::ostream & ostr) const;
255
256public:
257
259 // std::map<std::string, AccumulationMethod &> methods;
260
261protected:
262
264
265 /*
266 AccumulationMethod undefinedMethod;
267 OverwriteMethod overwriteMethod;
268 MaximumMethod maximumMethod;
269 MinimumMethod minimumMethod;
270 AverageMethod averageMethod;
271 WeightedAverageMethod weightedAverageMethod;
272 MaximumWeightMethod maximumWeightMethod;
273 */
274
275 AccumulationMethod * methodPtr;
276
277 // Initialize destination image to match the accumulation array - cropped if requested.
278 /*
279 * \param dst – destination image to be cropped if cropArea supplied
280 * \param cropArea – sub-area of the accumulation array; empty if no cropping requested.
281 *
282 * If cropArea geometry equals that of the accumulation array, it will be cleared and discarded in processing.
283 *
284 */
285 void initDst(const AccumulationConverter & coder, Image & dst, drain::Rectangle<int> & cropArea) const;
286
287
288};
289
290
291inline
292std::ostream & operator<<(std::ostream &ostr, const Accumulator & accumulator){
293 return accumulator.Accumulator::toStream(ostr);
294}
295
296DRAIN_ENUM_DICT(Accumulator::FieldType);
297DRAIN_ENUM_OSTREAM(Accumulator::FieldType)
298
299} // image::
300
301} // drain::
302
303
304#endif // Accumulator
virtual const std::string & getName() const
Return the name of an instance.
Definition BeanLike.h:89
const map_t & getParameters() const
Returns the parameter map (const access).
Definition BeanLike.h:116
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:61
void getValues(std::ostream &ostr) const
Dumps the values.
Definition SmartMap.h:341
General-purpose image compositing.
Definition AccumulationArray.h:108
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:66
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:93
Definition Accumulator.h:76
void setMethod(const AccumulationMethod &method)
Copies the method and its parameters.
Definition Accumulator.h:171
static bool isSpecific(FieldType field)
Future option to mark scaled/normalized etc.
Definition Accumulator.h:120
static void createFieldList(const std::string &fieldChars, FieldList &fieldList)
Definition Accumulator.cpp:79
void initDst(const AccumulationConverter &coder, Image &dst, drain::Rectangle< int > &cropArea) const
Definition Accumulator.cpp:378
void add(const size_t i, double value, double weight, unsigned int count)
Adds decoded data that applies natural scaling.
Definition Accumulator.h:205
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:247
FieldType
Definition Accumulator.h:84
void setMethod(const std::string &method)
Set method to some of the predefined methods.
Definition Accumulator.cpp:194
Accumulator()
Definition Accumulator.h:135
void add(const size_t i, double value, double weight)
Adds decoded data that applies natural scaling.
Definition Accumulator.h:199
AccumulationArray accArray
Todo: export.
Definition Accumulator.h:132
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:212
AccumulationMethod undefinedMethod
A Some predefined methods, that can be set with setMethod(const std::string & key).
Definition Accumulator.h:263
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:183
Definition Data.h:1473
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