FunctorPack.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 FUNCTOR_PACK_H_
32 #define FUNCTOR_PACK_H_
33 
34 #include <cmath>
35 
36 #include "Functor.h"
37 
38 
39 namespace drain
40 {
41 
42 
43 
44 
46 
56 // Inversely: f = (f'-offset)/scale = a*f+b, where a=1/scale and b=-offset/scale.
57 class ScalingFunctor : public UnaryFunctor {
58 
59 public:
60 
61  ScalingFunctor(double scale = 1.0, double bias = 0.0) : UnaryFunctor(__FUNCTION__, "Rescales values linerarly: y = scale*x + bias", scale, bias){
62  this->getParameters().link("scale", this->scale);
63  this->getParameters().link("bias", this->bias);
64  this->setScale(scale, bias);
65  };
66 
67  ScalingFunctor(const ScalingFunctor & ftor) : UnaryFunctor(ftor) {
68  this->parameters.copyStruct(ftor.getParameters(), ftor, *this);
69  updateBean();
70  }
71 
72  //virtual
73  inline
74  double operator()(double s) const {
75  return this->scaleFinal*s + this->biasFinal;
76  };
77 
78 protected:
79 
80  ScalingFunctor(const std::string & name, const std::string & description, double scale = 1.0, double bias = 0.0) :
81  UnaryFunctor(name, description, scale, bias){
82  this->setScale(scale, bias);
83  };
84 
85 };
86 
87 
88 
90 
97 class NegateFunctor : public ScalingFunctor {
98 
99 public:
100 
101  NegateFunctor() : ScalingFunctor(__FUNCTION__, "Inverts values.", -1.0, 1.0) {
102  //updateScale(); // needed?
103  }
104 
105 
106 
107 };
108 
110 
118 
119 public:
120 
121  RemappingFunctor(double fromValue = 0.0, double toValue = 0.0) : UnaryFunctor(__FUNCTION__, "Rescales intensities linerarly") , fromValue(fromValue), toValue(toValue) {
122  this->getParameters().link("fromValue", this->fromValue = fromValue);
123  this->getParameters().link("toValue", this->toValue = toValue);
124  };
125 
126  RemappingFunctor(const RemappingFunctor & ftor) : UnaryFunctor(ftor){
127  parameters.copyStruct(ftor.parameters, ftor, *this);
128  }
129 
130 
131  inline
132  double operator()(double s) const {
133  // this->scale*s + this->bias;
134  if (s == fromValue)
135  return toValue;
136  else
137  return s;
138  };
139 
140  double fromValue;
141  double toValue;
142 
143 };
144 
145 
147 
155 
156 public:
157 
158  ThresholdFunctor(double threshold = 0.5, double replace = 0.0) : UnaryFunctor(__FUNCTION__, "Resets values lower than a threshold") , threshold(threshold), replace(replace) {
159  this->getParameters().link("threshold", this->threshold = threshold);
160  this->getParameters().link("replace", this->replace = replace);
161  };
162 
163  ThresholdFunctor(const ThresholdFunctor & ftor) : UnaryFunctor(ftor){
164  parameters.copyStruct(ftor.parameters, ftor, *this);
165  }
166 
167 
168  inline
169  double operator()(double s) const {
170  if (s < threshold)
171  return replace;
172  else
173  return s;
174  };
175 
176  double threshold;
177  double replace;
178 
179 };
180 
182 
189 class BinaryThresholdFunctor : public UnaryFunctor { // : public ThresholdFunctor {
190 
191 public:
192 
193  /*
194  BinaryThresholdFunctor(double threshold = 0.5, double replace = 0.0, double replaceHigh = 1.0) : ThresholdFunctor(threshold, replace), replaceHigh(replaceHigh) {
195  this->getParameters().link("replaceHigh", this->replaceHigh = replaceHigh);
196  };
197  */
198 
199  BinaryThresholdFunctor(double threshold = 0.5, double replace = 0.0, double replaceHigh = 1.0) : UnaryFunctor(__FUNCTION__, "Resets values lower and higher than a threshold") {
200  //, threshold(threshold), replace(replace)
201  this->getParameters().link("threshold", this->threshold = threshold);
202  this->getParameters().link("replace", this->replace = replace);
203  this->getParameters().link("replaceHigh", this->replaceHigh = replaceHigh);
204  };
205 
207  parameters.copyStruct(ftor.parameters, ftor, *this);
208  }
209 
210  inline
211  double operator()(double s) const {
212  if (s < threshold)
213  return replace;
214  else
215  return replaceHigh;
216  };
217 
218  double threshold;
219  double replace;
220  double replaceHigh;
221 
222 };
223 
224 
225 
227 
239 
240 public:
241 
242  AdditionFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Adds values", scale, bias){
243  this->getParameters().link("scale", this->scale);
244  this->getParameters().link("bias", this->bias);
245  };
246 
247  inline
248  double operator()(double s1, double s2) const {
249  return this->scaleFinal*(s1 + s2) + this->biasFinal;
250  };
251 };
252 
253 
255 
264 
265 public:
266 
267  SubtractionFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Subtracts values", scale, bias){ // , bool LIMIT=false
268  this->getParameters().link("scale", this->scale);
269  this->getParameters().link("bias", this->bias);
270  };
271 
272  inline
273  double operator()(double s1, double s2) const {
274  return this->scaleFinal*(s1 - s2) + this->biasFinal;
275  };
276 };
277 
278 
279 
281 
302 
303 public:
304 
305  MultiplicationFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Rescales intensities linerarly", scale, bias){
306  this->getParameters().link("scale", this->scale);
307  this->getParameters().link("bias", this->bias);
308  // updateBean();
309  };
310 
311  inline
312  double operator()(double s1, double s2) const {
313  return this->scaleFinal*(s1*s2) + this->biasFinal;
314  };
315 };
316 
318 
326 
327 public:
328 
329  DivisionFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Rescales intensities linerarly", scale, bias){
330  this->getParameters().link("scale", this->scale);
331  this->getParameters().link("bias", this->bias);
332  //updateBean();
333  };
334 
335  inline
336  double operator()(double s1, double s2) const {
337  if (s2 != 0.0)
338  return this->scale*(s1/s2) + this->bias;
339  else
340  return NAN;
341  };
342 };
343 
344 
346 
370 class MixerFunctor : public BinaryFunctor {
371 
372 public:
373 
374  MixerFunctor(double coeff=0.5, double scale=1.0, double bias=0.0) : BinaryFunctor(__FUNCTION__, "Rescales intensities linerarly", scale, bias), coeff(coeff), scaleFinal2(1.0){
375  this->getParameters().link("coeff", this->coeff);
376  this->getParameters().link("scale", this->scale);
377  this->getParameters().link("bias", this->bias);
378  updateBean();
379  //updateScale();
380  };
381 
382  MixerFunctor(const MixerFunctor & ftor) : BinaryFunctor(ftor){
383  parameters.copyStruct(ftor.parameters, ftor, *this); // ~coeff
384  //std::cerr << "copy const" << __FUNCTION__ << std::endl;
385  }
386 
387 
388  inline
389  double operator()(double s1, double s2) const {
390  return this->scaleFinal*s1 + this->scaleFinal2*s2 + this->biasFinal;
391  };
392 
393  inline
394  virtual
395  void updateScale() const {
396  this->scaleFinal = this->scale*this->coeff;
397  this->scaleFinal2 = this->scale*(1.0-this->coeff);
398  this->biasFinal = this->bias;
399  }
400 
401  double coeff;
402 
403 protected:
404  mutable
405  double scaleFinal2;
406 };
407 
408 
409 
410 
412 
421 
422 public:
423 
424  MaximumFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Maximum of two values.", scale, bias){
425  updateBean();
426  };
427 
428  inline
429  double operator()(double s1, double s2) const {
430  return this->scale * std::max(static_cast<double>(s1), static_cast<double>(s2)) + this->bias;
431  };
432 };
433 
434 
435 
437 
446 
447 public:
448 
449  MinimumFunctor(double scale = 1.0, double bias = 0.0) : BinaryFunctor(__FUNCTION__, "Minimum of two values.", scale, bias){
450  updateBean();
451  };
452 
453  inline
454  double operator()(double s1, double s2) const {
455  return this->scale * std::min(static_cast<double>(s1), static_cast<double>(s2)) + this->bias;
456  };
457 };
458 
459 
460 
461 }
462 
463 #endif /* MATH_OP_PACK */
464 
465 // Drain
Adds a intensity values .
Definition: FunctorPack.h:238
Definition: Functor.h:135
Thresholds intensity values.
Definition: FunctorPack.h:189
Divides image by another image.
Definition: FunctorPack.h:325
double scale
Relative scale, typically 1. Optional.
Definition: Functor.h:99
double scaleFinal
Scaling factor after encodings of src and dst images are known.
Definition: Functor.h:106
double bias
"Relative" bias, typically 0. Optional.
Definition: Functor.h:102
virtual void updateBean() const override
Called after setParameters()
Definition: Functor.h:65
double biasFinal
Scaling factor after encodings of src and dst images are known.
Definition: Functor.h:110
Definition: FunctorPack.h:420
Minimum intensity. Prescaled input.
Definition: FunctorPack.h:445
Blends an image to another with given proportion.
Definition: FunctorPack.h:370
Multiplies two images, with optional post scaling and offset.
Definition: FunctorPack.h:301
Inverts intensities: f' = f_max - f.
Definition: FunctorPack.h:97
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
Maps a single intensity value to another value.
Definition: FunctorPack.h:117
Rescales intensities linearly: f' = scale*f + offset.
Definition: FunctorPack.h:57
Subtracts image from another image.
Definition: FunctorPack.h:263
Thresholds intensity values.
Definition: FunctorPack.h:154
Definition: DataSelector.cpp:1277
Definition: Functor.h:116