UniSet  2.2.1
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 TypeOfValue ValueOutOfRange;
00073 
00075         static const long outOfRange;
00076 
00083         long getValue( long raw, bool crop_raw = false );
00084 
00086         inline long getMinValue()
00087         {
00088             return minVal;
00089         }
00091         inline long getMaxValue()
00092         {
00093             return maxVal;
00094         }
00095 
00097         inline long getLeftValue()
00098         {
00099             return leftVal;
00100         }
00102         inline long getRightValue()
00103         {
00104             return rightVal;
00105         }
00106 
00114         long getRawValue( long cal, bool range = false );
00115 
00117         inline long getMinRaw()
00118         {
00119             return minRaw;
00120         }
00122         inline long getMaxRaw()
00123         {
00124             return maxRaw;
00125         }
00126 
00128         inline long getLeftRaw()
00129         {
00130             return leftRaw;
00131         }
00133         inline long getRightRaw()
00134         {
00135             return rightRaw;
00136         }
00137 
00143         void build( const std::string& name, const std::string& confile, xmlNode* node = 0  );
00144 
00148         inline long tRound( const TypeOfValue& val ) const
00149         {
00150             return lround(val);
00151         }
00152 
00153         void setCacheSize( unsigned int sz );
00154         inline unsigned int getCacheSize()
00155         {
00156             return cache.size();
00157         }
00158 
00159         void setCacheResortCycle( unsigned int n );
00160         inline unsigned int getCacheResotrCycle()
00161         {
00162             return numCacheResort;
00163         }
00164         // ---------------------------------------------------------------
00165 
00166         friend std::ostream& operator<<(std::ostream& os, Calibration& c );
00167         friend std::ostream& operator<<(std::ostream& os, Calibration* c );
00168 
00169         // ---------------------------------------------------------------
00171         struct Point
00172         {
00173             Point(): x(outOfRange), y(outOfRange) {}
00174 
00175             Point( TypeOfValue _x, TypeOfValue _y ):
00176                 x(_x), y(_y) {}
00177 
00178             TypeOfValue x;
00179             TypeOfValue y;
00180 
00181             inline bool operator < ( const Point& p ) const
00182             {
00183                 return ( x < p.x );
00184             }
00185         };
00186 
00188         class Part
00189         {
00190             public:
00191                 Part();
00192                 Part( const Point& pleft, const Point& pright );
00193                 ~Part() {};
00194 
00196                 bool check( const Point& p ) const;
00197 
00199                 bool checkX( const TypeOfValue& x ) const;
00200 
00202                 bool checkY( const TypeOfValue& y ) const;
00203 
00204                 // функции могут вернуть OutOfRange
00205                 TypeOfValue getY( const TypeOfValue& x ) const;   
00206                 TypeOfValue getX( const TypeOfValue& y ) const;   
00208                 TypeOfValue calcY( const TypeOfValue& x ) const;  
00209                 TypeOfValue calcX( const TypeOfValue& y ) const;  
00211                 inline bool operator < ( const Part& p ) const
00212                 {
00213                     return (p_right < p.p_right);
00214                 }
00215 
00216                 inline Point leftPoint() const
00217                 {
00218                     return p_left;
00219                 }
00220                 inline Point rightPoint() const
00221                 {
00222                     return p_right;
00223                 }
00224                 inline TypeOfValue getK() const
00225                 {
00226                     return k;    
00227                 }
00228                 inline TypeOfValue left_x() const
00229                 {
00230                     return p_left.x;
00231                 }
00232                 inline TypeOfValue left_y() const
00233                 {
00234                     return p_left.y;
00235                 }
00236                 inline TypeOfValue right_x() const
00237                 {
00238                     return p_right.x;
00239                 }
00240                 inline TypeOfValue right_y() const
00241                 {
00242                     return p_right.y;
00243                 }
00244 
00245             protected:
00246                 Point p_left;     
00247                 Point p_right;     
00248                 TypeOfValue k;     
00249         };
00250 
00251         // список надо отсортировать по x!
00252         typedef std::vector<Part> PartsVec;
00253 
00254         inline std::string getName()
00255         {
00256             return myname;
00257         }
00258 
00259     protected:
00260 
00261         long minRaw, maxRaw, minVal, maxVal, rightVal, leftVal, rightRaw, leftRaw;
00262 
00263         void insertToCache( const long raw, const long val );
00264 
00265     private:
00266         PartsVec pvec;
00267         std::string myname;
00268 
00269         // Cache
00270         unsigned int szCache;
00271         struct CacheInfo
00272         {
00273             CacheInfo(): val(0), raw(outOfRange), cnt(0) {}
00274             CacheInfo( const long r, const long v ): val(v), raw(r), cnt(0) {}
00275 
00276             long val;
00277             long raw;
00278             unsigned long cnt; // счётчик обращений
00279 
00280             // сортируем в порядке убывания(!) обращений
00281             // т.е. наиболее часто используемые (впереди)
00282             inline bool operator<( const CacheInfo& r ) const
00283             {
00284                 if( r.raw == outOfRange )
00285                     return true;
00286 
00287                 // неинициализированные записи, сдвигаем в конец.
00288                 if( raw == outOfRange )
00289                     return false;
00290 
00291                 return cnt > r.cnt;
00292             }
00293         };
00294 
00295         typedef std::deque<CacheInfo> ValueCache;
00296         ValueCache cache;
00297         unsigned long numCacheResort; // количество обращений, при которых происходит перестроение (сортировка) кэша..
00298         unsigned long numCallToCache; // текущий счётчик обращений к кэшу
00299 };
00300 // -----------------------------------------------------------------------------
00301 #endif // Calibration_H_
00302 // -----------------------------------------------------------------------------