UniSet  2.6.0
Calibration.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // -----------------------------------------------------------------------------
17 #ifndef Calibration_H_
18 #define Calibration_H_
19 // -----------------------------------------------------------------------------
20 #include <cmath>
21 #include <string>
22 #include <vector>
23 #include <deque>
24 #include <ostream>
25 //--------------------------------------------------------------------------
26 namespace uniset
27 {
28  // -----------------------------------------------------------------------------
80  {
81  public:
82  Calibration();
83  Calibration( const std::string& name, const std::string& confile = "calibration.xml", size_t reserv = 50 );
84  Calibration( xmlNode* node, size_t reserv = 50 );
85  ~Calibration();
86 
88  typedef float TypeOfValue;
89 
92 
94  static const long outOfRange;
95 
102  long getValue( const long raw, bool crop_raw = false );
103 
105  inline long getMinValue() const noexcept
106  {
107  return minVal;
108  }
110  inline long getMaxValue() const noexcept
111  {
112  return maxVal;
113  }
114 
116  inline long getLeftValue() const noexcept
117  {
118  return leftVal;
119  }
121  inline long getRightValue() const noexcept
122  {
123  return rightVal;
124  }
125 
133  long getRawValue( const long cal, bool range = false );
134 
136  inline long getMinRaw() const noexcept
137  {
138  return minRaw;
139  }
141  inline long getMaxRaw() const noexcept
142  {
143  return maxRaw;
144  }
145 
147  inline long getLeftRaw() const noexcept
148  {
149  return leftRaw;
150  }
152  inline long getRightRaw() const noexcept
153  {
154  return rightRaw;
155  }
156 
162  void build( const std::string& name, const std::string& confile, xmlNode* node = 0 );
163 
167  inline long tRound( const TypeOfValue& val ) const
168  {
169  return lround(val);
170  }
171 
172  void setCacheSize( size_t sz );
173 
174  inline size_t getCacheSize() const
175  {
176  return cache.size();
177  }
178 
179  void setCacheResortCycle( size_t n );
180  inline size_t getCacheResotrCycle() const noexcept
181  {
182  return numCacheResort;
183  }
184  // ---------------------------------------------------------------
185 
186  friend std::ostream& operator<<(std::ostream& os, Calibration& c );
187  friend std::ostream& operator<<(std::ostream& os, Calibration* c );
188 
189  // ---------------------------------------------------------------
191  struct Point
192  {
193  Point(): x(outOfRange), y(outOfRange) {}
194 
195  Point( TypeOfValue _x, TypeOfValue _y ):
196  x(_x), y(_y) {}
197 
198  TypeOfValue x;
199  TypeOfValue y;
200 
201  inline bool operator < ( const Point& p ) const
202  {
203  return ( x < p.x );
204  }
205  };
206 
208  class Part
209  {
210  public:
211  Part() noexcept;
212  Part( const Point& pleft, const Point& pright ) noexcept;
213  ~Part() {};
214 
216  bool check( const Point& p ) const noexcept;
217 
219  bool checkX( const TypeOfValue& x ) const noexcept;
220 
222  bool checkY( const TypeOfValue& y ) const noexcept;
223 
224  // функции могут вернуть OutOfRange
225  TypeOfValue getY( const TypeOfValue& x ) const noexcept;
226  TypeOfValue getX( const TypeOfValue& y ) const noexcept;
228  TypeOfValue calcY( const TypeOfValue& x ) const noexcept;
229  TypeOfValue calcX( const TypeOfValue& y ) const noexcept;
231  inline bool operator < ( const Part& p ) const noexcept
232  {
233  return (p_right < p.p_right);
234  }
235 
236  inline Point leftPoint() const noexcept
237  {
238  return p_left;
239  }
240  inline Point rightPoint() const noexcept
241  {
242  return p_right;
243  }
244  inline TypeOfValue getK() const noexcept
245  {
246  return k;
247  }
248  inline TypeOfValue left_x() const noexcept
249  {
250  return p_left.x;
251  }
252  inline TypeOfValue left_y() const noexcept
253  {
254  return p_left.y;
255  }
256  inline TypeOfValue right_x() const noexcept
257  {
258  return p_right.x;
259  }
260  inline TypeOfValue right_y() const noexcept
261  {
262  return p_right.y;
263  }
264 
265  protected:
269  };
270 
271  // список надо отсортировать по x!
272  typedef std::vector<Part> PartsVec;
273 
274  inline std::string getName()
275  {
276  return myname;
277  }
278 
279  protected:
280 
281  long minRaw, maxRaw, minVal, maxVal, rightVal, leftVal, rightRaw, leftRaw;
282 
283  void insertToCache( const long raw, const long val );
284 
285  private:
286  PartsVec pvec;
287  std::string myname;
288 
289  // Cache
290  size_t szCache;
291  struct CacheInfo
292  {
293  CacheInfo() noexcept: val(0), raw(outOfRange), cnt(0) {}
294  CacheInfo( const long r, const long v ) noexcept: val(v), raw(r), cnt(0) {}
295 
296  long val;
297  long raw;
298  size_t cnt; // счётчик обращений
299 
300  // сортируем в порядке убывания(!) обращений
301  // т.е. наиболее часто используемые (впереди)
302  inline bool operator<( const CacheInfo& r ) const noexcept
303  {
304  if( r.raw == outOfRange )
305  return true;
306 
307  // неинициализированные записи, сдвигаем в конец.
308  if( raw == outOfRange )
309  return false;
310 
311  return cnt > r.cnt;
312  }
313  };
314 
315  typedef std::deque<CacheInfo> ValueCache;
316  ValueCache cache;
317  size_t numCacheResort; // количество обращений, при которых происходит перестроение (сортировка) кэша..
318  size_t numCallToCache; // текущий счётчик обращений к кэшу
319  };
320  // --------------------------------------------------------------------------
321 } // end of namespace uniset
322 // -----------------------------------------------------------------------------
323 #endif // Calibration_H_
324 // -----------------------------------------------------------------------------