UniSet  2.0.0
Calibration.h
00001 #ifndef Calibration_H_
00002 #define Calibration_H_
00003 // -----------------------------------------------------------------------------
00004 #include <cmath>
00005 #include <string>
00006 #include <vector>
00007 #include <deque>
00008 #include <ostream>
00009 // -----------------------------------------------------------------------------
00060 class Calibration
00061 {
00062     public:
00063         Calibration();
00064         Calibration( const std::string& name, const std::string& confile="calibration.xml" );
00065         Calibration( xmlNode* node );
00066         ~Calibration();
00067 
00069         typedef float TypeOfValue;
00070 
00072         static const long outOfRange;
00073 
00080         long getValue( long raw, bool crop_raw=false );
00081 
00083         inline long getMinValue(){ return minVal; }
00085         inline long getMaxValue(){ return maxVal; }
00086 
00088         inline long getLeftValue(){ return leftVal; }
00090         inline long getRightValue(){ return rightVal; }
00091 
00099         long getRawValue( long cal, bool range=false );
00100 
00102         inline long getMinRaw(){ return minRaw; }
00104         inline long getMaxRaw(){ return maxRaw; }
00105 
00107         inline long getLeftRaw(){ return leftRaw; }
00109         inline long getRightRaw(){ return rightRaw; }
00110 
00116         void build( const std::string& name, const std::string& confile, xmlNode* node=0  );
00117 
00121         inline long tRound( const TypeOfValue& val ) const
00122         {
00123             return lround(val);
00124         }
00125 
00126         void setCacheSize( unsigned int sz );
00127         inline unsigned int getCacheSize(){ return cache.size(); }
00128 
00129         void setCacheResortCycle( unsigned int n );
00130         inline unsigned int getCacheResotrCycle(){ return numCacheResort; }
00131         // ---------------------------------------------------------------
00132 
00133         friend std::ostream& operator<<(std::ostream& os, Calibration& c );
00134         friend std::ostream& operator<<(std::ostream& os, Calibration* c );
00135 
00136         // ---------------------------------------------------------------
00138         struct Point
00139         {
00140             Point():x(outOfRange),y(outOfRange){}
00141 
00142             Point( TypeOfValue _x, TypeOfValue _y ):
00143                 x(_x),y(_y){}
00144 
00145             TypeOfValue x;
00146             TypeOfValue y;
00147 
00148             inline bool operator < ( const Point& p ) const
00149             {
00150                 return ( x < p.x );
00151             }
00152         };
00153 
00155         class Part
00156         {
00157             public:
00158                 Part();
00159                 Part( const Point& pleft, const Point& pright );
00160                 ~Part(){};
00161 
00163                 bool check( const Point& p ) const;
00164 
00166                 bool checkX( const TypeOfValue& x ) const;
00167 
00169                 bool checkY( const TypeOfValue& y ) const;
00170 
00171                 // функции могут вернуть OutOfRange
00172                 TypeOfValue getY( const TypeOfValue& x ) const;         
00173                 TypeOfValue getX( const TypeOfValue& y ) const;        
00175                 TypeOfValue calcY( const TypeOfValue& x ) const;     
00176                 TypeOfValue calcX( const TypeOfValue& y ) const;     
00178                 inline bool operator < ( const Part& p ) const
00179                 {
00180                     return (p_right < p.p_right);
00181                 }
00182 
00183                 inline Point leftPoint() const { return p_left; }
00184                 inline Point rightPoint() const { return p_right; }
00185                 inline TypeOfValue getK() const { return k; }     
00186                 inline TypeOfValue left_x() const { return p_left.x; }
00187                 inline TypeOfValue left_y() const { return p_left.y; }
00188                 inline TypeOfValue right_x() const { return p_right.x; }
00189                 inline TypeOfValue right_y() const { return p_right.y; }
00190 
00191             protected:
00192                 Point p_left;     
00193                 Point p_right;     
00194                 TypeOfValue k;     
00195         };
00196 
00197         // список надо отсортировать по x!
00198         typedef std::vector<Part> PartsVec;
00199 
00200     protected:
00201 
00202         long minRaw, maxRaw, minVal, maxVal, rightVal, leftVal, rightRaw, leftRaw;
00203 
00204         void insertToCache( const long raw, const long val );
00205 
00206     private:
00207         PartsVec pvec;
00208         std::string myname;
00209 
00210         // Cache
00211         unsigned int szCache;
00212         struct CacheInfo
00213         {
00214              CacheInfo():val(0),raw(outOfRange),cnt(0){}
00215              CacheInfo( const long r, const long v ):val(v),raw(r),cnt(0){}
00216 
00217              long val;
00218              long raw;
00219              unsigned long cnt; // счётчик обращений
00220 
00221              // сортируем в порядке убывания(!) обращений
00222              // т.е. наиболее часто используемые (впереди)
00223              inline bool operator<( const CacheInfo& r ) const
00224              {
00225                  if( r.raw == outOfRange )
00226                     return true;
00227 
00228                  // неинициализированные записи, сдвигаем в конец.
00229                  if( raw == outOfRange )
00230                     return false;
00231 
00232                  return cnt > r.cnt;
00233              }
00234         };
00235 
00236         typedef std::deque<CacheInfo> ValueCache;
00237         ValueCache cache;
00238         unsigned long numCacheResort; // количество обращений, при которых происходит перестроение (сортировка) кэша..
00239         unsigned long numCallToCache; // текущий счётчик обращений к кэшу
00240 };
00241 // -----------------------------------------------------------------------------
00242 #endif // Calibration_H_
00243 // -----------------------------------------------------------------------------