UniSet  2.6.0
Calibration.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // -----------------------------------------------------------------------------
00017 #ifndef Calibration_H_
00018 #define Calibration_H_
00019 // -----------------------------------------------------------------------------
00020 #include <cmath>
00021 #include <string>
00022 #include <vector>
00023 #include <deque>
00024 #include <ostream>
00025 //--------------------------------------------------------------------------
00026 namespace uniset
00027 {
00028 // -----------------------------------------------------------------------------
00079 class Calibration
00080 {
00081     public:
00082         Calibration();
00083         Calibration( const std::string& name, const std::string& confile = "calibration.xml", size_t reserv = 50 );
00084         Calibration( xmlNode* node, size_t reserv = 50 );
00085         ~Calibration();
00086 
00088         typedef float TypeOfValue;
00089 
00091         static const TypeOfValue ValueOutOfRange;
00092 
00094         static const long outOfRange;
00095 
00102         long getValue( const long raw, bool crop_raw = false );
00103 
00105         inline long getMinValue() const noexcept
00106         {
00107             return minVal;
00108         }
00110         inline long getMaxValue() const noexcept
00111         {
00112             return maxVal;
00113         }
00114 
00116         inline long getLeftValue() const noexcept
00117         {
00118             return leftVal;
00119         }
00121         inline long getRightValue() const noexcept
00122         {
00123             return rightVal;
00124         }
00125 
00133         long getRawValue( const long cal, bool range = false );
00134 
00136         inline long getMinRaw() const noexcept
00137         {
00138             return minRaw;
00139         }
00141         inline long getMaxRaw() const noexcept
00142         {
00143             return maxRaw;
00144         }
00145 
00147         inline long getLeftRaw() const noexcept
00148         {
00149             return leftRaw;
00150         }
00152         inline long getRightRaw() const noexcept
00153         {
00154             return rightRaw;
00155         }
00156 
00162         void build( const std::string& name, const std::string& confile, xmlNode* node = 0  );
00163 
00167         inline long tRound( const TypeOfValue& val ) const
00168         {
00169             return lround(val);
00170         }
00171 
00172         void setCacheSize( size_t sz );
00173 
00174         inline size_t getCacheSize() const
00175         {
00176             return cache.size();
00177         }
00178 
00179         void setCacheResortCycle( size_t n );
00180         inline size_t getCacheResotrCycle() const noexcept
00181         {
00182             return numCacheResort;
00183         }
00184         // ---------------------------------------------------------------
00185 
00186         friend std::ostream& operator<<(std::ostream& os, Calibration& c );
00187         friend std::ostream& operator<<(std::ostream& os, Calibration* c );
00188 
00189         // ---------------------------------------------------------------
00191         struct Point
00192         {
00193             Point(): x(outOfRange), y(outOfRange) {}
00194 
00195             Point( TypeOfValue _x, TypeOfValue _y ):
00196                 x(_x), y(_y) {}
00197 
00198             TypeOfValue x;
00199             TypeOfValue y;
00200 
00201             inline bool operator < ( const Point& p ) const
00202             {
00203                 return ( x < p.x );
00204             }
00205         };
00206 
00208         class Part
00209         {
00210             public:
00211                 Part() noexcept;
00212                 Part( const Point& pleft, const Point& pright ) noexcept;
00213                 ~Part() {};
00214 
00216                 bool check( const Point& p ) const noexcept;
00217 
00219                 bool checkX( const TypeOfValue& x ) const noexcept;
00220 
00222                 bool checkY( const TypeOfValue& y ) const noexcept;
00223 
00224                 // функции могут вернуть OutOfRange
00225                 TypeOfValue getY( const TypeOfValue& x ) const noexcept;      
00226                 TypeOfValue getX( const TypeOfValue& y ) const noexcept;   
00228                 TypeOfValue calcY( const TypeOfValue& x ) const noexcept;  
00229                 TypeOfValue calcX( const TypeOfValue& y ) const noexcept;  
00231                 inline bool operator < ( const Part& p ) const noexcept
00232                 {
00233                     return (p_right < p.p_right);
00234                 }
00235 
00236                 inline Point leftPoint() const noexcept
00237                 {
00238                     return p_left;
00239                 }
00240                 inline Point rightPoint() const noexcept
00241                 {
00242                     return p_right;
00243                 }
00244                 inline TypeOfValue getK() const noexcept
00245                 {
00246                     return k;    
00247                 }
00248                 inline TypeOfValue left_x() const noexcept
00249                 {
00250                     return p_left.x;
00251                 }
00252                 inline TypeOfValue left_y() const noexcept
00253                 {
00254                     return p_left.y;
00255                 }
00256                 inline TypeOfValue right_x() const noexcept
00257                 {
00258                     return p_right.x;
00259                 }
00260                 inline TypeOfValue right_y() const noexcept
00261                 {
00262                     return p_right.y;
00263                 }
00264 
00265             protected:
00266                 Point p_left;  
00267                 Point p_right; 
00268                 TypeOfValue k; 
00269         };
00270 
00271         // список надо отсортировать по x!
00272         typedef std::vector<Part> PartsVec;
00273 
00274         inline std::string getName()
00275         {
00276             return myname;
00277         }
00278 
00279     protected:
00280 
00281         long minRaw, maxRaw, minVal, maxVal, rightVal, leftVal, rightRaw, leftRaw;
00282 
00283         void insertToCache( const long raw, const long val );
00284 
00285     private:
00286         PartsVec pvec;
00287         std::string myname;
00288 
00289         // Cache
00290         size_t szCache;
00291         struct CacheInfo
00292         {
00293             CacheInfo() noexcept: val(0), raw(outOfRange), cnt(0) {}
00294             CacheInfo( const long r, const long v ) noexcept: val(v), raw(r), cnt(0) {}
00295 
00296             long val;
00297             long raw;
00298             size_t cnt; // счётчик обращений
00299 
00300             // сортируем в порядке убывания(!) обращений
00301             // т.е. наиболее часто используемые (впереди)
00302             inline bool operator<( const CacheInfo& r ) const noexcept
00303             {
00304                 if( r.raw == outOfRange )
00305                     return true;
00306 
00307                 // неинициализированные записи, сдвигаем в конец.
00308                 if( raw == outOfRange )
00309                     return false;
00310 
00311                 return cnt > r.cnt;
00312             }
00313         };
00314 
00315         typedef std::deque<CacheInfo> ValueCache;
00316         ValueCache cache;
00317         size_t numCacheResort; // количество обращений, при которых происходит перестроение (сортировка) кэша..
00318         size_t numCallToCache; // текущий счётчик обращений к кэшу
00319 };
00320 // --------------------------------------------------------------------------
00321 } // end of namespace uniset
00322 // -----------------------------------------------------------------------------
00323 #endif // Calibration_H_
00324 // -----------------------------------------------------------------------------