Loading...
Searching...
No Matches
LookUp.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/*
32 * LookUp.h
33 *
34 * Created on: Oct 23, 2012
35 * Author: mpeura
36 */
37
38#ifndef LOOKUP_H_
39#define LOOKUP_H_
40
41#include <drain/Type.h>
42#include <drain/TypeUtils.h>
43#include <cmath>
44#include <vector>
45#include <exception>
46
47
48// // using namespace std;
49
50namespace drain {
51
52
54// Works well...
60template <class T>
61class LookUp : public std::vector<T> {
62
63public:
64
65
66 int bitShift;
67 int byteSize;
68
69};
70
71
73template <class T>
74class LookUp2 : public std::vector<T> {
75public:
76
77 LookUp2(size_t size, double scale=1.0) : std::vector<T>(size), max(getScale()), scale(scale*static_cast<double>(max)), scaleInv(1.0/this->scale) {
78 if (scale == 0.0)
79 throw std::runtime_error("LookUp: scale=0.0 not allowed.");
80 //init();
81 } ; // if (size == 0) throw std::runtime_error("LookUp: undetectValue sized lookup not allowed.");
82
83 virtual ~LookUp2(){};
84
85 const T max;
86
87 const double scale;
88 const double scaleInv;
89
90 double getScaled(size_t i) const {
91 return scaleInv * (*this)[i];
92 }
93
94 void putScaled(size_t i, double value){
95 (*this)[i] = scale * value;
96 }
97
98
99protected:
100
101 static
102 inline
103 double getScale(){
104 return Type::call<drain::typeIsInteger>(typeid(T)) ? Type::call<drain::typeMax,double>(typeid(T)) : 1.0;
105 }
106
107 /*
108 virtual
109 void init(){
110 std::cerr << "Dammit, called base class" << std::endl;
111 }
112 */
113
114};
115
117
120template <class T, class F> //=1.0, typename OFFSET=0.0>
121class FunctorLookUp : public LookUp2<T> {
122public:
123
124 FunctorLookUp(size_t size, double scale, double scaleIndex = 1.0, double offsetIndex=0.0) : LookUp2<T>(size, scale), scaleIndex(scaleIndex), offsetIndex(offsetIndex) {
125 init();
126 };
127
128 const double scaleIndex;
129
130 const double offsetIndex;
131
132protected:
133
134 //virtual
135 inline
136 void init(){
137 F functor;
138 const size_t n = std::vector<T>::size();
139 const double coeff = scaleIndex/static_cast<double>(n);
140 for (size_t i=0; i<n; ++i)
141 this->putScaled(i, functor(offsetIndex + coeff*static_cast<double>(i)));
142 }
143
144
145};
146
147
148/*
149template <class T>
150class LookUpSin : public LookUp<T> {
151public:
152
153 LookUpSin(size_t size, double scale) : LookUp<T>(size, scale) {};
154
155protected:
156
157
158 virtual inline
159 void init(){
160 const size_t n = std::vector<T>::size();
161 const double coeff = 2.0*M_PI / static_cast<double>(n);
162 for (size_t i=0; i<n; ++i)
163 this->putScaled(i, sin(coeff * i));
164 }
165
166};
167
168template <class T>
169class LookUpCos : public LookUp<T> {
170public:
171
172 LookUpCos(size_t size) : LookUp<T>(size) {};
173
174protected:
175
176 virtual inline
177 void init(){
178 const size_t n = std::vector<T>::size();
179 const double coeff = 2.0 * M_PI / n;
180 for (size_t i=0; i<n; ++i)
181 (*this)[i] = cos(coeff * i);
182 }
183
184
185};
186*/
187
188/*
189template <class T>
190class LookUpATan : public LookUp<T> {
191public:
192 LookUpATan(size_t size) : LookUp<T>(size), _max(size-1) {
193 const double coeff = 1.0 / _max;
194 for (size_t i=0; i<=_max; ++i)
195 (*this)[i] = std::atan2(coeff * i, 1.0); // tan enough?
196 };
197
198 //template <class T2>
199 double atan2(const double & y, const double & x) const {
200
201 if (y < 0){
202 return - this->atan2(-y, x);
203 }
204 // now y>=0
205 else if (x < 0){
206 return M_PI - this->atan2(y, -x);
207 }
209 else if (x == 0){
210 //std::cout << "zerox\n";
211 if (y == 0)
212 return 0; // TODO nan<T>;
213 else
214 return M_PI_2;
215 }
217 else if (y > x){
218 //std::cout << "y>x\n";
219 return M_PI_2 - this->atan2(x,y);
220 }
222 else {
223 //std::cout << " final\n";
224 return (*this)[ (_max*y)/x ];
225 }
226
227 }
228
229protected:
230 const size_t _max;
231};
232*/
233
234
235}
236
237#endif /* LOOKUP_H_ */
238
239// Rack
Values are available from [0,2PI].
Definition LookUp.h:121
Look-up tables.
Definition LookUp.h:74
Look-up tables.
Definition LookUp.h:61
Definition DataSelector.cpp:1277