Functor.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 DRAIN_FUNCTOR_H_
32 #define DRAIN_FUNCTOR_H_
33 
34 //
35 //#include <cmath>
36 
37 //#include <vector>
38 //#include <iostream>
39 #include <drain/Log.h>
40 #include <stdexcept>
41 //#include <map>
42 
43 #include "BeanLike.h"
44 
45 namespace drain
46 {
47 
48 // consider: template<N,T=double> : UniTuple<T,N>()
49 // or add variadic set() to SmartMap
50 
52 class Functor : public BeanLike {
53 
54 public:
55 
56 
57  inline
58  void setScale(double scale, double bias=0.0) {
59  this->scale = scale;
60  this->bias = bias;
61  updateBean();
62  }
63 
64  virtual inline
65  void updateBean() const override {
66  updateScale();
67  //drain::Logger mout(__FILE__, __FUNCTION__);
68  //mout.debug2("final scale,bias: " , this->scaleFinal , ',' , this->biasFinal );
69  }
70 
71 
72 protected:
73 
74 
75  Functor(const std::string & name, const std::string & description="", double scale=1.0, double bias=0.0) : BeanLike(name, description),
76  scale(scale), bias(bias), scaleFinal(scale), biasFinal(bias) {} // dstMax(0.0),
77 
78  Functor(const Functor & fct) : BeanLike(fct) {
79  setScale(fct.scale, fct.bias);
80  };
81 
82  /*
83  Functor(const Functor & fct) : BeanLike(fct){
84  parameters.copyStruct(fct.parameters, fct, *this); //
85  }
86  */
87 
88  virtual
89  ~Functor(){};
90 
91  virtual inline
92 
93  void updateScale() const {
94  this->scaleFinal = scale; // * SCALE;
95  this->biasFinal = bias; // * SCALE;
96  }
97 
99  double scale;
100 
102  double bias;
103 
105  mutable
106  double scaleFinal;
107 
109  mutable
110  double biasFinal;
111 
112 
113 };
114 
115 
116 struct UnaryFunctor : public Functor {
117 
118  UnaryFunctor(const std::string & name, const std::string & description="", double scale=1.0, double bias=0.0) : Functor(name, description, scale, bias) {}
119 
120 
121  virtual
122  double operator()(double s) const = 0;
123 
124 };
125 
126 struct IdentityFunctor : public UnaryFunctor {
127 
128  IdentityFunctor() : UnaryFunctor(__FUNCTION__, "Returns the input value without changes."){};
129 
130  inline
131  double operator()(double s) const {return s;};
132 
133 };
134 
135 class BinaryFunctor : public Functor {
136 
137 public:
138 
139  BinaryFunctor(const std::string & name, const std::string & description="", double scale=1.0, double bias=0.0) : Functor(name, description, scale, bias) {}
140 
141  virtual
142  double operator()(double s1, double s2) const = 0;
143 
144 };
145 
146 }
147 
148 
149 #endif
150 
151 // Drain
Something which has a name, a description and possibly some parameters of varying type.
Definition: BeanLike.h:60
Definition: Functor.h:135
Base class for sequential computations. Optional scaling utilities.
Definition: Functor.h:52
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: DataSelector.cpp:1277
Definition: Functor.h:126
Definition: Functor.h:116