Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/AIS_ColorScale.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Created on: 2015-02-03
0002 // Copyright (c) 2015 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _AIS_ColorScale_HeaderFile
0016 #define _AIS_ColorScale_HeaderFile
0017 
0018 #include <AIS_InteractiveObject.hxx>
0019 #include <Aspect_TypeOfColorScaleData.hxx>
0020 #include <Aspect_TypeOfColorScalePosition.hxx>
0021 #include <Quantity_Color.hxx>
0022 #include <NCollection_Sequence.hxx>
0023 #include <Standard.hxx>
0024 #include <Standard_DefineHandle.hxx>
0025 #include <TCollection_ExtendedString.hxx>
0026 
0027 //! Class for drawing a custom color scale.
0028 //!
0029 //! The color scale consists of rectangular color bar (composed of fixed
0030 //! number of color intervals), optional labels, and title.
0031 //! The labels can be positioned either at the boundaries of the intervals,
0032 //! or at the middle of each interval.
0033 //! Colors and labels can be either defined automatically or set by the user.
0034 //! Automatic labels are calculated from numerical limits of the scale,
0035 //! its type (logarithmic or plain), and formatted by specified format string.
0036 class AIS_ColorScale : public AIS_InteractiveObject
0037 {
0038   DEFINE_STANDARD_RTTIEXT(AIS_ColorScale, AIS_InteractiveObject)
0039 public:
0040   //! Calculate color according passed value; returns true if value is in range or false, if isn't
0041   Standard_EXPORT static bool FindColor(const double                    theValue,
0042                                         const double                    theMin,
0043                                         const double                    theMax,
0044                                         const int                       theColorsCount,
0045                                         const NCollection_Vec3<double>& theColorHlsMin,
0046                                         const NCollection_Vec3<double>& theColorHlsMax,
0047                                         Quantity_Color&                 theColor);
0048 
0049   //! Calculate color according passed value; returns true if value is in range or false, if isn't
0050   static bool FindColor(const double    theValue,
0051                         const double    theMin,
0052                         const double    theMax,
0053                         const int       theColorsCount,
0054                         Quantity_Color& theColor)
0055   {
0056     return FindColor(theValue,
0057                      theMin,
0058                      theMax,
0059                      theColorsCount,
0060                      NCollection_Vec3<double>(230.0, 1.0, 1.0),
0061                      NCollection_Vec3<double>(0.0, 1.0, 1.0),
0062                      theColor);
0063   }
0064 
0065   //! Shift hue into valid range.
0066   //! Lightness and Saturation should be specified in valid range [0.0, 1.0],
0067   //! however Hue might be given out of Quantity_Color range to specify desired range for
0068   //! interpolation.
0069   static double hueToValidRange(const double theHue)
0070   {
0071     double aHue = theHue;
0072     while (aHue < 0.0)
0073     {
0074       aHue += 360.0;
0075     }
0076     while (aHue > 360.0)
0077     {
0078       aHue -= 360.0;
0079     }
0080     return aHue;
0081   }
0082 
0083 public:
0084   //! Default constructor.
0085   Standard_EXPORT AIS_ColorScale();
0086 
0087   //! Calculate color according passed value; returns true if value is in range or false, if isn't
0088   Standard_EXPORT bool FindColor(const double theValue, Quantity_Color& theColor) const;
0089 
0090   //! Returns minimal value of color scale, 0.0 by default.
0091   double GetMin() const { return myMin; }
0092 
0093   //! Sets the minimal value of color scale.
0094   void SetMin(const double theMin) { SetRange(theMin, GetMax()); }
0095 
0096   //! Returns maximal value of color scale, 1.0 by default.
0097   double GetMax() const { return myMax; }
0098 
0099   //! Sets the maximal value of color scale.
0100   void SetMax(const double theMax) { SetRange(GetMin(), theMax); }
0101 
0102   //! Returns minimal and maximal values of color scale, 0.0 to 1.0 by default.
0103   void GetRange(double& theMin, double& theMax) const
0104   {
0105     theMin = myMin;
0106     theMax = myMax;
0107   }
0108 
0109   //! Sets the minimal and maximal value of color scale.
0110   //! Note that values order will be ignored - the minimum and maximum values will be swapped if
0111   //! needed.
0112   //! ::SetReversed() should be called to swap displaying order.
0113   Standard_EXPORT void SetRange(const double theMin, const double theMax);
0114 
0115   //! Returns the hue angle corresponding to minimum value, 230 by default (blue).
0116   double HueMin() const { return myColorHlsMin[0]; }
0117 
0118   //! Returns the hue angle corresponding to maximum value, 0 by default (red).
0119   double HueMax() const { return myColorHlsMax[0]; }
0120 
0121   //! Returns the hue angle range corresponding to minimum and maximum values, 230 to 0 by default
0122   //! (blue to red).
0123   void HueRange(double& theMinAngle, double& theMaxAngle) const
0124   {
0125     theMinAngle = myColorHlsMin[0];
0126     theMaxAngle = myColorHlsMax[0];
0127   }
0128 
0129   //! Sets hue angle range corresponding to minimum and maximum values.
0130   //! The valid angle range is [0, 360], see Quantity_Color and Quantity_TOC_HLS for more details.
0131   void SetHueRange(const double theMinAngle, const double theMaxAngle)
0132   {
0133     myColorHlsMin[0] = theMinAngle;
0134     myColorHlsMax[0] = theMaxAngle;
0135   }
0136 
0137   //! Returns color range corresponding to minimum and maximum values, blue to red by default.
0138   void ColorRange(Quantity_Color& theMinColor, Quantity_Color& theMaxColor) const
0139   {
0140     theMinColor.SetValues(hueToValidRange(myColorHlsMin[0]),
0141                           myColorHlsMin[1],
0142                           myColorHlsMin[2],
0143                           Quantity_TOC_HLS);
0144     theMaxColor.SetValues(hueToValidRange(myColorHlsMax[0]),
0145                           myColorHlsMax[1],
0146                           myColorHlsMax[2],
0147                           Quantity_TOC_HLS);
0148   }
0149 
0150   //! Sets color range corresponding to minimum and maximum values.
0151   void SetColorRange(const Quantity_Color& theMinColor, const Quantity_Color& theMaxColor)
0152   {
0153     theMinColor.Values(myColorHlsMin[0], myColorHlsMin[1], myColorHlsMin[2], Quantity_TOC_HLS);
0154     theMaxColor.Values(myColorHlsMax[0], myColorHlsMax[1], myColorHlsMax[2], Quantity_TOC_HLS);
0155   }
0156 
0157   //! Returns the type of labels, Aspect_TOCSD_AUTO by default.
0158   //! Aspect_TOCSD_AUTO - labels as boundary values for intervals
0159   //! Aspect_TOCSD_USER - user specified label is used
0160   Aspect_TypeOfColorScaleData GetLabelType() const { return myLabelType; }
0161 
0162   //! Sets the type of labels.
0163   //! Aspect_TOCSD_AUTO - labels as boundary values for intervals
0164   //! Aspect_TOCSD_USER - user specified label is used
0165   void SetLabelType(const Aspect_TypeOfColorScaleData theType) { myLabelType = theType; }
0166 
0167   //! Returns the type of colors, Aspect_TOCSD_AUTO by default.
0168   //! Aspect_TOCSD_AUTO - value between Red and Blue
0169   //! Aspect_TOCSD_USER - user specified color from color map
0170   Aspect_TypeOfColorScaleData GetColorType() const { return myColorType; }
0171 
0172   //! Sets the type of colors.
0173   //! Aspect_TOCSD_AUTO - value between Red and Blue
0174   //! Aspect_TOCSD_USER - user specified color from color map
0175   void SetColorType(const Aspect_TypeOfColorScaleData theType) { myColorType = theType; }
0176 
0177   //! Returns the number of color scale intervals, 10 by default.
0178   int GetNumberOfIntervals() const { return myNbIntervals; }
0179 
0180   //! Sets the number of color scale intervals.
0181   Standard_EXPORT void SetNumberOfIntervals(const int theNum);
0182 
0183   //! Returns the color scale title string, empty string by default.
0184   const TCollection_ExtendedString& GetTitle() const { return myTitle; }
0185 
0186   //! Sets the color scale title string.
0187   void SetTitle(const TCollection_ExtendedString& theTitle) { myTitle = theTitle; }
0188 
0189   //! Returns the format for numbers, "%.4g" by default.
0190   //! The same like format for function printf().
0191   //! Used if GetLabelType() is TOCSD_AUTO;
0192   const TCollection_AsciiString& GetFormat() const { return myFormat; }
0193 
0194   //! Returns the format of text.
0195   const TCollection_AsciiString& Format() const { return myFormat; }
0196 
0197   //! Sets the color scale auto label format specification.
0198   void SetFormat(const TCollection_AsciiString& theFormat) { myFormat = theFormat; }
0199 
0200   //! Returns the user specified label with index theIndex.
0201   //! Index is in range from 1 to GetNumberOfIntervals() or to
0202   //! GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true.
0203   //! Returns empty string if label not defined.
0204   Standard_EXPORT TCollection_ExtendedString GetLabel(const int theIndex) const;
0205 
0206   //! Returns the user specified color from color map with index (starts at 1).
0207   //! Returns default color if index is out of range in color map.
0208   Standard_EXPORT Quantity_Color GetIntervalColor(const int theIndex) const;
0209 
0210   //! Sets the color of the specified interval.
0211   //! Note that list is automatically resized to include specified index.
0212   //! @param theColor color value to set
0213   //! @param theIndex index in range [1, GetNumberOfIntervals()];
0214   //!                 appended to the end of list if -1 is specified
0215   Standard_EXPORT void SetIntervalColor(const Quantity_Color& theColor, const int theIndex);
0216 
0217   //! Returns the user specified labels.
0218   Standard_EXPORT void GetLabels(NCollection_Sequence<TCollection_ExtendedString>& theLabels) const;
0219 
0220   //! Returns the user specified labels.
0221   const NCollection_Sequence<TCollection_ExtendedString>& Labels() const { return myLabels; }
0222 
0223   //! Sets the color scale labels.
0224   //! The length of the sequence should be equal to GetNumberOfIntervals() or to
0225   //! GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true. If length of the sequence does not
0226   //! much the number of intervals, then these labels will be considered as "free" and will be
0227   //! located at the virtual intervals corresponding to the number of labels (with flag
0228   //! IsLabelAtBorder() having the same effect as in normal case).
0229   Standard_EXPORT void SetLabels(const NCollection_Sequence<TCollection_ExtendedString>& theSeq);
0230 
0231   //! Returns the user specified colors.
0232   Standard_EXPORT void GetColors(NCollection_Sequence<Quantity_Color>& theColors) const;
0233 
0234   //! Returns the user specified colors.
0235   const NCollection_Sequence<Quantity_Color>& GetColors() const { return myColors; }
0236 
0237   //! Sets the color scale colors.
0238   //! The length of the sequence should be equal to GetNumberOfIntervals().
0239   Standard_EXPORT void SetColors(const NCollection_Sequence<Quantity_Color>& theSeq);
0240 
0241   //! Populates colors scale by colors of the same lightness value in CIE Lch
0242   //! color space, distributed by hue, with perceptually uniform differences
0243   //! between consequent colors.
0244   //! See MakeUniformColors() for description of parameters.
0245   void SetUniformColors(double theLightness, double theHueFrom, double theHueTo)
0246   {
0247     SetColors(MakeUniformColors(myNbIntervals, theLightness, theHueFrom, theHueTo));
0248     SetColorType(Aspect_TOCSD_USER);
0249   }
0250 
0251   //! Generates sequence of colors of the same lightness value in CIE Lch
0252   //! color space (see #Quantity_TOC_CIELch), with hue values in the specified range.
0253   //! The colors are distributed across the range such as to have perceptually
0254   //! same difference between neighbour colors.
0255   //! For each color, maximal chroma value fitting in sRGB gamut is used.
0256   //!
0257   //! @param theNbColors - number of colors to generate
0258   //! @param theLightness - lightness to be used (0 is black, 100 is white, 32 is
0259   //!        lightness of pure blue)
0260   //! @param theHueFrom - hue value at the start of the scale
0261   //! @param theHueTo - hue value defining the end of the scale
0262   //!
0263   //! Hue value can be out of the range [0, 360], interpreted as modulo 360.
0264   //! The colors of the scale will be in the order of increasing hue if
0265   //! theHueTo > theHueFrom, and decreasing otherwise.
0266   Standard_EXPORT static NCollection_Sequence<Quantity_Color> MakeUniformColors(int    theNbColors,
0267                                                                                 double theLightness,
0268                                                                                 double theHueFrom,
0269                                                                                 double theHueTo);
0270 
0271   //! Returns the position of labels concerning color filled rectangles, Aspect_TOCSP_RIGHT by
0272   //! default.
0273   Aspect_TypeOfColorScalePosition GetLabelPosition() const { return myLabelPos; }
0274 
0275   //! Sets the color scale labels position relative to color bar.
0276   void SetLabelPosition(const Aspect_TypeOfColorScalePosition thePos) { myLabelPos = thePos; }
0277 
0278   //! Returns the position of color scale title, Aspect_TOCSP_LEFT by default.
0279   Aspect_TypeOfColorScalePosition GetTitlePosition() const { return myTitlePos; }
0280 
0281   //! Sets the color scale title position.
0282   Standard_DEPRECATED("AIS_ColorScale::SetTitlePosition() has no effect!")
0283   void SetTitlePosition(const Aspect_TypeOfColorScalePosition thePos) { myTitlePos = thePos; }
0284 
0285   //! Returns TRUE if the labels and colors used in reversed order, FALSE by default.
0286   //!  - Normal,   bottom-up order with Minimal value on the Bottom and Maximum value on Top.
0287   //!  - Reversed, top-down  order with Maximum value on the Bottom and Minimum value on Top.
0288   bool IsReversed() const { return myIsReversed; }
0289 
0290   //! Sets true if the labels and colors used in reversed order.
0291   void SetReversed(const bool theReverse) { myIsReversed = theReverse; }
0292 
0293   //! Return TRUE if color transition between neighbor intervals
0294   //! should be linearly interpolated, FALSE by default.
0295   bool IsSmoothTransition() const { return myIsSmooth; }
0296 
0297   //! Setup smooth color transition.
0298   void SetSmoothTransition(const bool theIsSmooth) { myIsSmooth = theIsSmooth; }
0299 
0300   //! Returns TRUE if the labels are placed at border of color intervals, TRUE by default.
0301   //! The automatically generated label will show value exactly on the current position:
0302   //!  - value connecting two neighbor intervals (TRUE)
0303   //!  - value in the middle of interval (FALSE)
0304   bool IsLabelAtBorder() const { return myIsLabelAtBorder; }
0305 
0306   //! Sets true if the labels are placed at border of color intervals (TRUE by default).
0307   //! If set to False, labels will be drawn at color intervals rather than at borders.
0308   void SetLabelAtBorder(const bool theOn) { myIsLabelAtBorder = theOn; }
0309 
0310   //! Returns TRUE if the color scale has logarithmic intervals, FALSE by default.
0311   bool IsLogarithmic() const { return myIsLogarithmic; }
0312 
0313   //! Sets true if the color scale has logarithmic intervals.
0314   void SetLogarithmic(const bool isLogarithmic) { myIsLogarithmic = isLogarithmic; }
0315 
0316   //! Sets the color scale label at index.
0317   //! Note that list is automatically resized to include specified index.
0318   //! @param theLabel new label text
0319   //! @param theIndex index in range [1, GetNumberOfIntervals()] or [1, GetNumberOfIntervals() + 1]
0320   //! if IsLabelAtBorder() is true;
0321   //!                 label is appended to the end of list if negative index is specified
0322   Standard_EXPORT void SetLabel(const TCollection_ExtendedString& theLabel, const int theIndex);
0323 
0324   //! Returns the size of color bar, 0 and 0 by default
0325   //! (e.g. should be set by user explicitly before displaying).
0326   void GetSize(int& theBreadth, int& theHeight) const
0327   {
0328     theBreadth = myBreadth;
0329     theHeight  = myHeight;
0330   }
0331 
0332   //! Sets the size of color bar.
0333   void SetSize(const int theBreadth, const int theHeight)
0334   {
0335     myBreadth = theBreadth;
0336     myHeight  = theHeight;
0337   }
0338 
0339   //! Returns the breadth of color bar, 0 by default
0340   //! (e.g. should be set by user explicitly before displaying).
0341   int GetBreadth() const { return myBreadth; }
0342 
0343   //! Sets the width of color bar.
0344   void SetBreadth(const int theBreadth) { myBreadth = theBreadth; }
0345 
0346   //! Returns the height of color bar, 0 by default
0347   //! (e.g. should be set by user explicitly before displaying).
0348   int GetHeight() const { return myHeight; }
0349 
0350   //! Sets the height of color bar.
0351   void SetHeight(const int theHeight) { myHeight = theHeight; }
0352 
0353   //! Returns the bottom-left position of color scale, 0x0 by default.
0354   void GetPosition(double& theX, double& theY) const
0355   {
0356     theX = myXPos;
0357     theY = myYPos;
0358   }
0359 
0360   //! Sets the position of color scale.
0361   void SetPosition(const int theX, const int theY)
0362   {
0363     myXPos = theX;
0364     myYPos = theY;
0365   }
0366 
0367   //! Returns the left position of color scale, 0 by default.
0368   int GetXPosition() const { return myXPos; }
0369 
0370   //! Sets the left position of color scale.
0371   void SetXPosition(const int theX) { myXPos = theX; }
0372 
0373   //! Returns the bottom position of color scale, 0 by default.
0374   int GetYPosition() const { return myYPos; }
0375 
0376   //! Sets the bottom position of color scale.
0377   void SetYPosition(const int theY) { myYPos = theY; }
0378 
0379   //! Returns the font height of text labels, 20 by default.
0380   int GetTextHeight() const { return myTextHeight; }
0381 
0382   //! Sets the height of text of color scale.
0383   void SetTextHeight(const int theHeight) { myTextHeight = theHeight; }
0384 
0385 public:
0386   //! Returns the width of text.
0387   //! @param[in] theText  the text of which to calculate width.
0388   Standard_EXPORT int TextWidth(const TCollection_ExtendedString& theText) const;
0389 
0390   //! Returns the height of text.
0391   //! @param[in] theText  the text of which to calculate height.
0392   Standard_EXPORT int TextHeight(const TCollection_ExtendedString& theText) const;
0393 
0394   Standard_EXPORT void TextSize(const TCollection_ExtendedString& theText,
0395                                 const int                         theHeight,
0396                                 int&                              theWidth,
0397                                 int&                              theAscent,
0398                                 int&                              theDescent) const;
0399 
0400 public:
0401   //! Return true if specified display mode is supported.
0402   bool AcceptDisplayMode(const int theMode) const override { return theMode == 0; }
0403 
0404   //! Compute presentation.
0405   Standard_EXPORT void Compute(const occ::handle<PrsMgr_PresentationManager>& thePrsMgr,
0406                                const occ::handle<Prs3d_Presentation>&         thePresentation,
0407                                const int                                      theMode) override;
0408 
0409   //! Compute selection - not implemented for color scale.
0410   void ComputeSelection(const occ::handle<SelectMgr_Selection>& /*aSelection*/,
0411                         const int /*aMode*/) override
0412   {
0413   }
0414 
0415 private:
0416   //! Returns the size of color scale.
0417   //! @param[out] theWidth  the width of color scale.
0418   //! @param[out] theHeight  the height of color scale.
0419   void SizeHint(int& theWidth, int& theHeight) const;
0420 
0421   //! Returns the upper value of given interval, or minimum for theIndex = 0.
0422   double GetIntervalValue(const int theIndex) const;
0423 
0424   //! Returns the color for the given value in the given interval.
0425   //! @param[in] theValue  the current value of interval
0426   //! @param[in] theMin    the min value of interval
0427   //! @param[in] theMax    the max value of interval
0428   Quantity_Color colorFromValue(const double theValue,
0429                                 const double theMin,
0430                                 const double theMax) const;
0431 
0432   //! Initialize text aspect for drawing the labels.
0433   void updateTextAspect();
0434 
0435   //! Simple alias for Prs3d_Text::Draw().
0436   //! @param[in] theGroup  presentation group
0437   //! @param[in] theText   text to draw
0438   //! @param[in] theX      X coordinate of text position
0439   //! @param[in] theY      Y coordinate of text position
0440   //! @param[in] theVertAlignment  text vertical alignment
0441   void drawText(const occ::handle<Graphic3d_Group>&   theGroup,
0442                 const TCollection_ExtendedString&     theText,
0443                 const int                             theX,
0444                 const int                             theY,
0445                 const Graphic3d_VerticalTextAlignment theVertAlignment);
0446 
0447   //! Determine the maximum text label width in pixels.
0448   int computeMaxLabelWidth(const NCollection_Sequence<TCollection_ExtendedString>& theLabels) const;
0449 
0450   //! Draw labels.
0451   void drawLabels(const occ::handle<Graphic3d_Group>&                     theGroup,
0452                   const NCollection_Sequence<TCollection_ExtendedString>& theLabels,
0453                   const int                                               theBarBottom,
0454                   const int                                               theBarHeight,
0455                   const int                                               theMaxLabelWidth,
0456                   const int                                               theColorBreadth);
0457 
0458   //! Draw a color bar.
0459   void drawColorBar(const occ::handle<Prs3d_Presentation>& thePrs,
0460                     const int                              theBarBottom,
0461                     const int                              theBarHeight,
0462                     const int                              theMaxLabelWidth,
0463                     const int                              theColorBreadth);
0464 
0465   //! Draw a frame.
0466   //! @param[in] theX  the X coordinate of frame position.
0467   //! @param[in] theY  the Y coordinate of frame position.
0468   //! @param[in] theWidth  the width of frame.
0469   //! @param[in] theHeight  the height of frame.
0470   //! @param[in] theColor  the color of frame.
0471   void drawFrame(const occ::handle<Prs3d_Presentation>& thePrs,
0472                  const int                              theX,
0473                  const int                              theY,
0474                  const int                              theWidth,
0475                  const int                              theHeight,
0476                  const Quantity_Color&                  theColor);
0477 
0478 private:
0479   double myMin;                                 //!< values range - minimal value
0480   double myMax;                                 //!< values range - maximal value
0481                                                 // clang-format off
0482   NCollection_Vec3<double>                  myColorHlsMin;     //!< HLS color corresponding to minimum value
0483   NCollection_Vec3<double>                  myColorHlsMax;     //!< HLS color corresponding to maximum value
0484   TCollection_ExtendedString       myTitle;           //!< optional title string     
0485   TCollection_AsciiString          myFormat;          //!< Sprintf() format for generating label from value
0486   int                 myNbIntervals;     //!< number of intervals
0487   Aspect_TypeOfColorScaleData      myColorType;       //!< color type
0488   Aspect_TypeOfColorScaleData      myLabelType;       //!< label type
0489   bool                 myIsLabelAtBorder; //!< at border
0490   bool                 myIsReversed;      //!< flag indicating reversed order
0491   bool                 myIsLogarithmic;   //!< flag indicating logarithmic scale
0492   bool                 myIsSmooth;        //!< flag indicating smooth transition between the colors
0493   NCollection_Sequence<Quantity_Color>           myColors;          //!< sequence of custom colors
0494   NCollection_Sequence<TCollection_ExtendedString> myLabels;          //!< sequence of custom text labels
0495   Aspect_TypeOfColorScalePosition  myLabelPos;        //!< label position relative to the color scale
0496                                                 // clang-format on
0497   Aspect_TypeOfColorScalePosition myTitlePos;   //!< title position
0498   int                             myXPos;       //!< left   position
0499   int                             myYPos;       //!< bottom position
0500   int                             myBreadth;    //!< color scale breadth
0501   int                             myHeight;     //!< height of the color scale
0502   int                             mySpacing;    //!< extra spacing between element
0503   int                             myTextHeight; //!< label font height
0504 };
0505 
0506 #endif