ColorModel.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #ifndef APP_COLORMODEL_H
00025 #define APP_COLORMODEL_H
00026 
00027 #include "Material.h"
00028 
00029 #include <deque>
00030 #include <string>
00031 #include <vector>
00032 
00033 #define CCR_EPS  1.0e-5f
00034 
00035 namespace App
00036 {
00037 
00041 class AppExport ValueFloatToRGB
00042 {
00043 public:
00044   virtual Color getColor(float fVal) const = 0;
00045 
00046 protected:
00047   ValueFloatToRGB (void) {}
00048   virtual ~ValueFloatToRGB () {}
00049 };
00050 
00051 
00052 class AppExport ColorModel
00053 {
00054 public:
00055   ColorModel (unsigned short usCt);
00056   ColorModel (const ColorModel &rclM);
00057   virtual ~ColorModel ();
00058   ColorModel& operator = (const ColorModel &rclM);
00059   unsigned short  _usColors;
00060   Color  *_pclColors;
00061 };
00062 
00063 class AppExport ColorModelTria : public ColorModel
00064 {
00065 public:
00066   ColorModelTria (void) : ColorModel(5)
00067   {
00068     _pclColors[0] = Color( 0, 0, 1);
00069     _pclColors[1] = Color( 0, 1, 1);
00070     _pclColors[2] = Color( 0, 1, 0);
00071     _pclColors[3] = Color( 1, 1, 0);
00072     _pclColors[4] = Color( 1, 0, 0);
00073   }
00074 };
00075 
00076 class AppExport ColorModelTriaBottom : public ColorModel
00077 {
00078 public:
00079   ColorModelTriaBottom (void) : ColorModel(3)
00080   {
00081     _pclColors[0] = Color( 0, 0, 1);
00082     _pclColors[1] = Color( 0, 1, 1);
00083     _pclColors[2] = Color( 0, 1, 0);
00084   }
00085 };
00086 
00087 class AppExport ColorModelTriaTop : public ColorModel
00088 {
00089 public:
00090   ColorModelTriaTop (void) : ColorModel(3)
00091   {
00092     _pclColors[0] = Color( 0, 1, 0);
00093     _pclColors[1] = Color( 1, 1, 0);
00094     _pclColors[2] = Color( 1, 0, 0);
00095   }
00096 };
00097 
00098 class AppExport ColorModelInverseTria : public ColorModel
00099 {
00100 public:
00101   ColorModelInverseTria (void) : ColorModel(5)
00102   {
00103     _pclColors[0] = Color( 1, 0, 0);
00104     _pclColors[1] = Color( 1, 1, 0);
00105     _pclColors[2] = Color( 0, 1, 0);
00106     _pclColors[3] = Color( 0, 1, 1);
00107     _pclColors[4] = Color( 0, 0, 1);
00108   }
00109 };
00110 
00111 class AppExport ColorModelInverseTriaTop : public ColorModel
00112 {
00113 public:
00114   ColorModelInverseTriaTop (void) : ColorModel(3)
00115   {
00116     _pclColors[2] = Color( 0, 0, 1);
00117     _pclColors[1] = Color( 0, 1, 1);
00118     _pclColors[0] = Color( 0, 1, 0);
00119   }
00120 };
00121 
00122 class AppExport ColorModelInverseTriaBottom : public ColorModel
00123 {
00124 public:
00125   ColorModelInverseTriaBottom (void) : ColorModel(3)
00126   {
00127     _pclColors[2] = Color( 0, 1, 0);
00128     _pclColors[1] = Color( 1, 1, 0);
00129     _pclColors[0] = Color( 1, 0, 0);
00130   }
00131 };
00132 
00133 class AppExport ColorModelGray : public ColorModel
00134 {
00135 public:
00136   ColorModelGray (void) : ColorModel(2)
00137   {
00138     _pclColors[0] = Color( 0, 0, 0);
00139     _pclColors[1] = Color( 1, 1, 1);
00140   }
00141 };
00142 
00143 class AppExport ColorModelGrayBottom : public ColorModel
00144 {
00145 public:
00146   ColorModelGrayBottom (void) : ColorModel(2)
00147   {
00148     _pclColors[0] = Color( 0.0f, 0.0f, 0.0f);
00149     _pclColors[1] = Color( 0.5f, 0.5f, 0.5f);
00150   }
00151 };
00152 
00153 class AppExport ColorModelGrayTop : public ColorModel
00154 {
00155 public:
00156   ColorModelGrayTop (void) : ColorModel(2)
00157   {
00158     _pclColors[0] = Color( 0.5f, 0.5f, 0.5f);
00159     _pclColors[1] = Color( 1.0f, 1.0f, 1.0f);
00160   }
00161 };
00162 
00163 class AppExport ColorModelInverseGray : public ColorModel
00164 {
00165 public:
00166   ColorModelInverseGray (void) : ColorModel(2)
00167   {
00168     _pclColors[0] = Color( 1, 1, 1);
00169     _pclColors[1] = Color( 0, 0, 0);
00170   }
00171 };
00172 
00173 class AppExport ColorModelInverseGrayBottom : public ColorModel
00174 {
00175 public:
00176   ColorModelInverseGrayBottom (void) : ColorModel(2)
00177   {
00178     _pclColors[0] = Color( 1.0f, 1.0f, 1.0f);
00179     _pclColors[1] = Color( 0.5f, 0.5f, 0.5f);
00180   }
00181 };
00182 
00183 class AppExport ColorModelInverseGrayTop : public ColorModel
00184 {
00185 public:
00186   ColorModelInverseGrayTop (void) : ColorModel(2)
00187   {
00188     _pclColors[0] = Color( 0.5f, 0.5f, 0.5f);
00189     _pclColors[1] = Color( 0.0f, 0.0f, 0.0f);
00190   }
00191 };
00192 
00193 class AppExport ColorField
00194 {
00195 public:
00196   ColorField (void);
00197   ColorField (const ColorField &rclCF);
00198   ColorField (const ColorModel &rclModel, float fMin, float fMax, unsigned short usCt);
00199   virtual ~ColorField ();    
00200 
00201   ColorField& operator = (const ColorField &rclCF);
00202 
00203   unsigned short getCountColors (void) const { return _usCtColors; }
00204   void set (const ColorModel &rclModel, float fMin, float fMax, unsigned short usCt);
00205   void setCountColors (unsigned short usCt) { set(_clModel, _fMin, _fMax, usCt); }
00206   void setRange (float fMin, float fMax) { set(_clModel, fMin, fMax, _usCtColors); }
00207   void getRange (float &rfMin, float &rfMax) { rfMin = _fMin; rfMax = _fMax; }
00208   unsigned short getMinColors (void) const { return _clModel._usColors; }
00209   void setColorModel (const ColorModel &rclModel);
00210   const ColorModel& getColorModel (void) const { return _clModel; }
00211   void setDirect (unsigned short usInd, Color clCol) { _aclField[usInd] = clCol; }
00212   float getMinValue (void) const { return _fMin; }
00213   float getMaxValue (void) const { return _fMax; }
00214   
00215   Color getColor (unsigned short usIndex) const { return _aclField[usIndex]; }
00216   inline Color  getColor (float fVal) const;
00217   inline unsigned short getColorIndex (float fVal) const;
00218 
00219 protected:
00220   ColorModel          _clModel;
00221   float               _fMin, _fMax;
00222   float               _fAscent, _fConstant;  // Index := _fConstant + _fAscent * WERT
00223   unsigned short      _usCtColors;
00224   std::vector<Color>  _aclField;
00225 
00226   void rebuild (void);
00227   void interpolate (Color clCol1, unsigned short usPos1, Color clCol2, unsigned short usPos2);
00228 };
00229 
00230 inline Color ColorField::getColor (float fVal) const
00231 {
00232 #if 1
00233   // if the value is outside or at the border of the range
00234   unsigned short ct = _clModel._usColors-1;
00235   if ( fVal <= _fMin )
00236     return _clModel._pclColors[0];
00237   else if ( fVal >= _fMax )
00238     return _clModel._pclColors[ct];
00239 
00240   // get the color field position (with 0 < t < 1)
00241   float t = (fVal-_fMin) / (_fMax-_fMin);
00242   Color col(1.0f, 1.0f, 1.0f); // white as default
00243   for ( unsigned short i=0; i<ct; i++ )
00244   {
00245     float r = (float)(i+1)/(float)ct;
00246     if ( t < r )
00247     {
00248       // calculate the exact position in the subrange
00249       float s = t*(float)ct-(float)i;
00250       Color c1 = _clModel._pclColors[i];
00251       Color c2 = _clModel._pclColors[i+1];
00252       col.r = (1.0f-s) * c1.r + s * c2.r;
00253       col.g = (1.0f-s) * c1.g + s * c2.g;
00254       col.b = (1.0f-s) * c1.b + s * c2.b;
00255       break;
00256     }
00257   }
00258 
00259   return col;
00260 #else
00261   return _aclField[getColorIndex(fVal)];
00262 #endif
00263 }
00264 
00265 inline unsigned short ColorField::getColorIndex (float fVal) const
00266 {
00267   return (unsigned short)(std::min<int>(std::max<int>(int(_fConstant + _fAscent * fVal), 0), int(_usCtColors-1)));
00268 }
00269 
00270 
00271 class AppExport ColorGradient
00272 {
00273 public:
00274   enum TStyle { FLOW, ZERO_BASED };
00275   enum TColorModel { TRIA, INVERSE_TRIA, GRAY, INVERSE_GRAY };
00276 
00277   ColorGradient (void);
00278   ColorGradient (float fMin, float fMax, unsigned short usCtColors, TStyle tS, bool bOG = false);
00279   ColorGradient (const ColorGradient &rclCR);
00280 
00281   ColorGradient& operator = (const ColorGradient &rclCR);
00282 
00283   void set (float fMin, float fMax, unsigned short usCt, TStyle tS, bool bOG); 
00284   void setRange (float fMin, float fMax) { set(fMin, fMax, _usCtColors, _tStyle, _bOutsideGrayed); }
00285   void getRange (float &rfMin, float &rfMax) const { rfMin = _fMin; rfMax = _fMax; }
00286   unsigned short getCountColors (void) const { return _usCtColors; }
00287   void setCountColors (unsigned short usCt) { set(_fMin, _fMax, usCt, _tStyle, _bOutsideGrayed); }
00288   void setStyle (TStyle tS) { set(_fMin, _fMax, _usCtColors, tS, _bOutsideGrayed); }
00289   unsigned short getMinColors (void) const;
00290   TStyle getStyle (void) const { return _tStyle; }
00291   void setOutsideGrayed (bool bGrayed) { _bOutsideGrayed = bGrayed; }
00292   bool isOutsideGrayed (void) const { return _bOutsideGrayed; }
00293   void setColorModel (TColorModel tModel);
00294   TColorModel getColorModelType (void) const { return _tColorModel; }
00295   inline const ColorModel& getColorModel (void) const;
00296   float getMinValue (void) const { return _fMin; }
00297   float getMaxValue (void) const { return _fMax; }
00298 
00299   inline Color  getColor (float fVal) const;
00300   inline unsigned short getColorIndex (float fVal) const;
00301 
00302 protected:
00303   ColorField  _clColFld1, _clColFld2;
00304   TColorModel    _tColorModel;
00305   TStyle         _tStyle;
00306   float          _fMin, _fMax;
00307   unsigned short        _usCtColors; 
00308   bool           _bOutsideGrayed;
00309   ColorModel       _clTotal, _clTop, _clBottom; 
00310 
00311   void  rebuild (void);
00312   void  setColorModel (void);
00313 };
00314 
00315 
00316 class AppExport ColorLegend
00317 {
00318 public:
00319   ColorLegend (void);
00320   ColorLegend (const ColorLegend &rclCL);
00321   virtual ~ColorLegend () {}
00322 
00323   ColorLegend& operator = (const ColorLegend &rclCL);
00324   bool operator == (const ColorLegend &rclCL) const;
00325   bool operator != (const ColorLegend &rclCL) const { return !(*this == rclCL); }
00326 
00327   void resize (unsigned long ulN);
00328   bool addMin (const std::string &rclName);
00329   bool addMax (const std::string &rclName);
00330   bool remove (unsigned long ulPos);
00331   void removeFirst (void);
00332   void removeLast (void);    
00333 
00334   Color getColor (unsigned long ulPos) const;
00335   uint32_t  getPackedColor (unsigned long ulPos) const;
00336   bool setColor (unsigned long ulPos, float ucRed, float ucGreen, float ucBlue);
00337   bool setColor (unsigned long ulPos, unsigned long ulColor);
00338   float getValue (unsigned long ulPos) const;
00339   bool setValue (unsigned long ulPos, float fVal);
00340   std::string getText (unsigned long ulPos) const;
00341   bool setText (unsigned long ulPos, const std::string &rclName);
00342   unsigned long hasNumberOfFields (void) const { return (unsigned long)_aclColorFields.size(); }
00343   void setOutsideGrayed (bool bOS) { _bOutsideGrayed = bOS; }
00344   bool isOutsideGrayed (void) const { return _bOutsideGrayed; }
00345   inline float  getMinValue (void) const;
00346   inline float  getMaxValue (void) const;
00347 
00348   inline Color getColor (float fVal) const;
00349   inline unsigned short getColorIndex (float fVal) const;
00350 
00351 protected:
00352   std::deque<Color>  _aclColorFields;
00353   std::deque<std::string> _aclNames;
00354   std::deque<float> _aclValues;
00355   bool _bOutsideGrayed;
00356 };
00357 
00358 inline Color ColorLegend::getColor (float fVal) const
00359 {
00360   std::deque<float>::const_iterator pI;
00361   for (pI = _aclValues.begin(); pI != _aclValues.end(); pI++)
00362   {
00363     if (fVal < *pI)
00364       break;
00365   }
00366 
00367   if (_bOutsideGrayed == true)
00368   {
00369     if ((pI == _aclValues.begin()) || (pI == _aclValues.end()))
00370       return Color(0.5f, 0.5f, 0.5f);
00371     else
00372       return _aclColorFields[pI - _aclValues.begin() - 1];
00373   }
00374 
00375   if (pI == _aclValues.begin())
00376     return *_aclColorFields.begin();
00377   else if (pI == _aclValues.end())
00378     return *(_aclColorFields.end()-1);
00379   else
00380     return _aclColorFields[pI - _aclValues.begin() - 1];
00381 }
00382 
00383 inline unsigned short ColorLegend::getColorIndex (float fVal) const
00384 {
00385   std::deque<float>::const_iterator pI;
00386   for (pI = _aclValues.begin(); pI != _aclValues.end(); pI++)
00387   {
00388     if (fVal < *pI)
00389       break;
00390   }
00391 
00392   if (pI == _aclValues.begin())
00393     return 0;
00394   else if (pI == _aclValues.end())
00395     return (unsigned short)(_aclColorFields.size() - 1);
00396   else
00397     return pI - _aclValues.begin() - 1;
00398 }
00399 
00400 inline float ColorLegend::getMinValue (void) const
00401 {
00402   return *_aclValues.begin();
00403 }
00404 
00405 inline float ColorLegend::getMaxValue (void) const
00406 {
00407   return *(_aclValues.end()-1);
00408 }
00409 
00410 inline Color ColorGradient::getColor (float fVal) const
00411 {
00412   if (_bOutsideGrayed == true)
00413   {
00414     if ((fVal < _fMin) || (fVal > _fMax))
00415       return Color(0.5f, 0.5f, 0.5f);
00416   }
00417   switch (_tStyle)
00418   {
00419     case ZERO_BASED:
00420     {
00421       if ((_fMin < 0.0f) && (_fMax > 0.0f))
00422       {
00423         if (fVal < 0.0f)
00424           return _clColFld1.getColor(fVal);
00425         else
00426           return _clColFld2.getColor(fVal);
00427       }
00428       else 
00429         return _clColFld1.getColor(fVal);
00430     }
00431     
00432     default:
00433     case FLOW:
00434     {
00435       return _clColFld1.getColor(fVal);
00436     }
00437   }
00438 }
00439 
00440 inline unsigned short ColorGradient::getColorIndex (float fVal) const
00441 {
00442   switch (_tStyle)
00443   {
00444     case ZERO_BASED:
00445     {
00446       if ((_fMin < 0.0f) && (_fMax > 0.0f))
00447       {
00448         if (fVal < 0.0f)
00449           return _clColFld1.getColorIndex(fVal);
00450         else
00451           return (unsigned short)(_clColFld1.getCountColors() + _clColFld2.getColorIndex(fVal));
00452       }
00453       else 
00454         return _clColFld1.getColorIndex(fVal);
00455     }
00456     
00457     default:
00458     case FLOW:
00459     {
00460       return _clColFld1.getColorIndex(fVal);
00461     }
00462   }
00463 }
00464 
00465 inline const ColorModel& ColorGradient::getColorModel (void) const
00466 { 
00467   if ( _tStyle == ZERO_BASED )
00468   {
00469     if ( _fMax <= 0.0f )
00470       return _clBottom;
00471     else if ( _fMin >= 0.0f )
00472       return _clTop;
00473     else
00474       return _clTotal;
00475   }
00476   else
00477   {
00478     return _clTotal;
00479   }
00480 }
00481 
00482 } // namespace App
00483 
00484 #endif // APP_COLORMODEL_H 

Generated on Wed Nov 23 19:00:01 2011 for FreeCAD by  doxygen 1.6.1