1 // Created on: 2015-02-03
2 // Copyright (c) 2015 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <AIS_ColorScale.hxx>
17 #include <AIS_InteractiveContext.hxx>
18 #include <Aspect_TypeOfColorScaleData.hxx>
19 #include <Aspect_TypeOfColorScalePosition.hxx>
20 #include <Geom_Line.hxx>
21 #include <GeomAdaptor_Curve.hxx>
22 #include <Graphic3d_ArrayOfPolylines.hxx>
23 #include <Graphic3d_AspectFillArea3d.hxx>
24 #include <Graphic3d_AspectText3d.hxx>
25 #include <Graphic3d_GraphicDriver.hxx>
26 #include <Graphic3d_ArrayOfTriangles.hxx>
27 #include <Graphic3d_Text.hxx>
28 #include <Prs3d_Text.hxx>
29 #include <Prs3d_TextAspect.hxx>
30 #include <Select3D_SensitiveBox.hxx>
31 #include <StdPrs_Curve.hxx>
32 #include <V3d_View.hxx>
34 IMPLEMENT_STANDARD_RTTIEXT(AIS_ColorScale, AIS_InteractiveObject)
38 //! Method to add colored quad into array of triangles.
39 static void addColoredQuad (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
40 const Standard_Integer theXLeft, const Standard_Integer theYBottom,
41 const Standard_Integer theSizeX, const Standard_Integer theSizeY,
42 const Quantity_Color& theColorBottom,
43 const Quantity_Color& theColorTop)
45 const Standard_Integer aVertIndex = theTris->VertexNumber() + 1;
46 theTris->AddVertex (gp_Pnt (theXLeft, theYBottom, 0.0), theColorBottom);
47 theTris->AddVertex (gp_Pnt (theXLeft + theSizeX, theYBottom, 0.0), theColorBottom);
48 theTris->AddVertex (gp_Pnt (theXLeft, theYBottom + theSizeY, 0.0), theColorTop);
49 theTris->AddVertex (gp_Pnt (theXLeft + theSizeX, theYBottom + theSizeY, 0.0), theColorTop);
50 theTris->AddEdges (aVertIndex, aVertIndex + 1, aVertIndex + 2);
51 theTris->AddEdges (aVertIndex + 1, aVertIndex + 2, aVertIndex + 3);
54 //! Compute hue angle from specified value.
55 static Quantity_Color colorFromValueEx (const Standard_Real theValue,
56 const Standard_Real theMin,
57 const Standard_Real theMax,
58 const Graphic3d_Vec3d& theHlsMin,
59 const Graphic3d_Vec3d& theHlsMax)
61 const Standard_Real aValueDelta = theMax - theMin;
62 Standard_Real aValue = 0.0;
63 if (aValueDelta != 0.0)
65 aValue = (theValue - theMin) / aValueDelta;
68 Standard_Real aHue = NCollection_Lerp<Standard_Real>::Interpolate (theHlsMin[0], theHlsMax[0], aValue);
69 Standard_Real aLightness = NCollection_Lerp<Standard_Real>::Interpolate (theHlsMin[1], theHlsMax[1], aValue);
70 Standard_Real aSaturation = NCollection_Lerp<Standard_Real>::Interpolate (theHlsMin[2], theHlsMax[2], aValue);
71 return Quantity_Color (AIS_ColorScale::hueToValidRange (aHue), aLightness, aSaturation, Quantity_TOC_HLS);
74 //! Return the index of discrete interval for specified value.
75 //! Note that when value lies exactly on the border between two intervals,
76 //! determining which interval to return is undefined operation;
77 //! Current implementation returns the following interval in this case.
78 //! @param theValue [in] value to map
79 //! @param theMin [in] values range, lower value
80 //! @param theMax [in] values range, upper value
81 //! @param theNbIntervals [in] number of discrete intervals
82 //! @return index of interval within [1, theNbIntervals] range
83 static Standard_Integer colorDiscreteInterval (Standard_Real theValue,
86 Standard_Integer theNbIntervals)
88 if (Abs (theMax - theMin) <= Precision::Approximation())
93 Standard_Integer anInterval = 1 + (Standard_Integer )Floor (Standard_Real (theNbIntervals) * (theValue - theMin) / (theMax - theMin));
94 // map the very upper value (theValue==theMax) to the largest color interval
95 anInterval = Min (anInterval, theNbIntervals);
100 //=======================================================================
101 //function : AIS_ColorScale
103 //=======================================================================
104 AIS_ColorScale::AIS_ColorScale()
107 myColorHlsMin (230.0, 1.0, 1.0),
108 myColorHlsMax (0.0, 1.0, 1.0),
111 myColorType (Aspect_TOCSD_AUTO),
112 myLabelType (Aspect_TOCSD_AUTO),
113 myIsLabelAtBorder (Standard_True),
114 myIsReversed (Standard_False),
115 myIsLogarithmic (Standard_False),
116 myIsSmooth (Standard_False),
117 myLabelPos (Aspect_TOCSP_RIGHT),
118 myTitlePos (Aspect_TOCSP_LEFT),
127 myDrawer->SetupOwnShadingAspect();
128 myDrawer->ShadingAspect()->Aspect()->SetShadingModel (Graphic3d_TypeOfShadingModel_Unlit);
129 myDrawer->ShadingAspect()->Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Opaque);
130 myDrawer->ShadingAspect()->Aspect()->SetInteriorColor (Quantity_NOC_WHITE);
133 //=======================================================================
134 //function : GetLabel
136 //=======================================================================
137 TCollection_ExtendedString AIS_ColorScale::GetLabel (const Standard_Integer theIndex) const
139 if (myLabelType == Aspect_TOCSD_USER)
141 if (theIndex >= myLabels.Lower()
142 || theIndex <= myLabels.Upper())
144 return myLabels.Value(theIndex);
146 return TCollection_ExtendedString();
149 // value to be shown depends on label position
150 const Standard_Real aVal = myIsLabelAtBorder
151 ? GetIntervalValue (theIndex - 1)
152 : (0.5 * (GetIntervalValue (theIndex - 1) + GetIntervalValue (theIndex)));
155 sprintf (aBuf, myFormat.ToCString(), aVal);
156 return TCollection_ExtendedString (aBuf);
159 //=======================================================================
160 //function : GetIntervalColor
162 //=======================================================================
163 Quantity_Color AIS_ColorScale::GetIntervalColor (const Standard_Integer theIndex) const
165 if (myColorType== Aspect_TOCSD_USER)
167 if (theIndex <= 0 || theIndex > myColors.Length())
169 return Quantity_Color();
171 return myColors.Value (theIndex);
174 return colorFromValue (theIndex - 1, 0, myNbIntervals - 1);
177 //=======================================================================
178 //function : GetLabels
180 //=======================================================================
181 void AIS_ColorScale::GetLabels (TColStd_SequenceOfExtendedString& theLabels) const
184 for (TColStd_SequenceOfExtendedString::Iterator aLabIter (myLabels); aLabIter.More(); aLabIter.Next())
186 theLabels.Append (aLabIter.Value());
190 //=======================================================================
191 //function : GetColors
193 //=======================================================================
194 void AIS_ColorScale::GetColors (Aspect_SequenceOfColor& theColors) const
197 for (Aspect_SequenceOfColor::Iterator aColorIter (myColors); aColorIter.More(); aColorIter.Next())
199 theColors.Append (aColorIter.Value());
203 //=======================================================================
204 //function : SetRange
206 //=======================================================================
207 void AIS_ColorScale::SetRange (const Standard_Real theMin, const Standard_Real theMax)
209 myMin = Min (theMin, theMax);
210 myMax = Max (theMin, theMax);
213 //=======================================================================
214 //function : SetNumberOfIntervals
216 //=======================================================================
217 void AIS_ColorScale::SetNumberOfIntervals (const Standard_Integer theNum)
224 myNbIntervals = theNum;
227 //=======================================================================
228 //function : SetLabel
230 //=======================================================================
231 void AIS_ColorScale::SetLabel (const TCollection_ExtendedString& theLabel,
232 const Standard_Integer theIndex)
234 const Standard_Integer aLabIndex = (theIndex <= 0 ? myLabels.Length() + 1 : theIndex);
235 while (myLabels.Length() < aLabIndex)
237 myLabels.Append (TCollection_ExtendedString());
239 myLabels.SetValue (aLabIndex, theLabel);
242 //=======================================================================
243 //function : SetIntervalColor
245 //=======================================================================
246 void AIS_ColorScale::SetIntervalColor (const Quantity_Color& theColor,
247 const Standard_Integer theIndex)
249 const Standard_Integer aColorIndex = (theIndex <= 0 ? myColors.Length() + 1 : theIndex);
250 while (myColors.Length() < aColorIndex)
252 myColors.Append (Quantity_Color());
254 myColors.SetValue (aColorIndex, theColor);
257 //=======================================================================
258 //function : SetLabels
260 //=======================================================================
261 void AIS_ColorScale::SetLabels (const TColStd_SequenceOfExtendedString& theSeq)
264 for (TColStd_SequenceOfExtendedString::Iterator aLabIter (theSeq); aLabIter.More(); aLabIter.Next())
266 myLabels.Append (aLabIter.Value());
270 //=======================================================================
271 //function : SetColors
273 //=======================================================================
274 void AIS_ColorScale::SetColors (const Aspect_SequenceOfColor& theSeq)
277 for (Aspect_SequenceOfColor::Iterator aColorIter (theSeq); aColorIter.More(); aColorIter.Next())
279 myColors.Append (aColorIter.Value());
283 //=======================================================================
284 //function : MakeUniformColors
286 //=======================================================================
287 Aspect_SequenceOfColor AIS_ColorScale::MakeUniformColors (Standard_Integer theNbColors,
288 Standard_Real theLightness,
289 Standard_Real theHueFrom,
290 Standard_Real theHueTo)
292 Aspect_SequenceOfColor aResult;
294 // adjust range to be within (0, 360], with sign according to theHueFrom and theHueTo
295 Standard_Real aHueRange = std::fmod (theHueTo - theHueFrom, 360.);
296 const Standard_Real aHueEps = Precision::Angular() * 180. / M_PI;
297 if (Abs (aHueRange) <= aHueEps)
299 aHueRange = (aHueRange < 0 ? -360. : 360.);
307 if (theNbColors == 1)
309 Standard_Real aHue = std::fmod (theHueFrom, 360.);
314 Quantity_Color aColor (theLightness, 130., aHue, Quantity_TOC_CIELch);
315 aResult.Append (aColor);
319 // discretize the range with 1 degree step
320 const int NBCOLORS = 2 + (int)Abs (aHueRange / 1.);
321 Standard_Real aHueStep = aHueRange / (NBCOLORS - 1);
322 NCollection_Array1<Quantity_Color> aGrid (0, NBCOLORS - 1);
323 for (Standard_Integer i = 0; i < NBCOLORS; i++)
325 Standard_Real aHue = std::fmod (theHueFrom + i * aHueStep, 360.);
330 aGrid(i).SetValues (theLightness, 130., aHue, Quantity_TOC_CIELch);
333 // and compute distances between each two colors in a grid
334 TColStd_Array1OfReal aMetric (0, NBCOLORS - 1);
335 Standard_Real aLength = 0.;
336 for (Standard_Integer i = 0, j = NBCOLORS - 1; i < NBCOLORS; j = i++)
338 aLength += (aMetric(i) = aGrid(i).DeltaE2000 (aGrid(j)));
341 // determine desired step by distance;
342 // normally we aim to distribute colors from start to end
343 // of the range, but if distance between first and last points of the range
344 // is less than that step (e.g. range is full 360 deg),
345 // then distribute by the whole 360 deg scope to ensure that first
346 // and last colors are sufficiently distanced
347 Standard_Real aDStep = (aLength - aMetric.First()) / (theNbColors - 1);
348 if (aMetric.First() < aDStep)
350 aDStep = aLength / theNbColors;
354 aResult.Append(aGrid(0));
355 Standard_Real aParam = 0., aPrev = 0., aTarget = aDStep;
356 for (int i = 1; i < NBCOLORS; i++)
358 aParam = aPrev + aMetric(i);
359 while (aTarget <= aParam)
361 float aCoefPrev = float((aParam - aTarget) / (aParam - aPrev));
362 float aCoefCurr = float((aTarget - aPrev) / (aParam - aPrev));
363 Quantity_Color aColor (aGrid(i).Rgb() * aCoefCurr + aGrid(i-1).Rgb() * aCoefPrev);
364 aResult.Append (aColor);
369 if (aResult.Length() < theNbColors)
371 aResult.Append (aGrid.Last());
373 Standard_ASSERT_VOID (aResult.Length() == theNbColors, "Failed to generate requested nb of colors");
377 //=======================================================================
378 //function : SizeHint
380 //=======================================================================
381 void AIS_ColorScale::SizeHint (Standard_Integer& theWidth, Standard_Integer& theHeight) const
383 const Standard_Integer aTextHeight = TextHeight ("");
384 const Standard_Integer aColorWidth = 20;
385 Standard_Integer aTextWidth = 0;
386 if (myLabelPos != Aspect_TOCSP_NONE)
388 for (Standard_Integer aLabIter = (myIsLabelAtBorder ? 0 : 1); aLabIter <= myNbIntervals; ++aLabIter)
390 aTextWidth = Max (aTextWidth, TextWidth (GetLabel (aLabIter)));
394 const Standard_Integer aScaleWidth = aColorWidth + aTextWidth + (aTextWidth ? 3 : 2) * mySpacing;
395 const Standard_Integer aScaleHeight = (Standard_Integer)(1.5 * (myNbIntervals + (myIsLabelAtBorder ? 2 : 1)) * aTextHeight);
397 Standard_Integer aTitleWidth = 0;
398 Standard_Integer aTitleHeight = 0;
399 if (!myTitle.IsEmpty())
401 aTitleHeight = TextHeight (myTitle) + mySpacing;
402 aTitleWidth = TextWidth (myTitle) + mySpacing * 2;
405 theWidth = Max (aTitleWidth, aScaleWidth);
406 theHeight = aScaleHeight + aTitleHeight;
409 //=======================================================================
410 //function : GetIntervalValue
412 //=======================================================================
413 Standard_Real AIS_ColorScale::GetIntervalValue (const Standard_Integer theIndex) const
415 if (myNbIntervals <= 0)
422 Standard_Real aMin = myMin > 0 ? myMin : 1.0;
423 Standard_Real aDivisor = std::pow (myMax / aMin, 1.0 / myNbIntervals);
424 return aMin * std::pow (aDivisor,theIndex);
427 Standard_Real aNum = 0;
428 if (myNbIntervals > 0)
430 aNum = GetMin() + theIndex * (Abs (GetMax() - GetMin()) / myNbIntervals);
435 //=======================================================================
436 //function : colorFromValue
438 //=======================================================================
439 Quantity_Color AIS_ColorScale::colorFromValue (const Standard_Real theValue,
440 const Standard_Real theMin,
441 const Standard_Real theMax) const
443 return colorFromValueEx (theValue, theMin, theMax, myColorHlsMin, myColorHlsMax);
446 //=======================================================================
447 //function : FindColor
449 //=======================================================================
450 Standard_Boolean AIS_ColorScale::FindColor (const Standard_Real theValue,
451 Quantity_Color& theColor) const
453 if (theValue < myMin || theValue > myMax || myMax < myMin)
455 theColor = Quantity_Color();
456 return Standard_False;
459 if (myColorType == Aspect_TOCSD_USER)
461 const Standard_Integer anInterval = colorDiscreteInterval (theValue, myMin, myMax, myNbIntervals);
462 if (anInterval < myColors.Lower() || anInterval > myColors.Upper())
464 theColor = Quantity_Color();
465 return Standard_False;
468 theColor = myColors.Value (anInterval);
469 return Standard_True;
472 return FindColor (theValue, myMin, myMax, myNbIntervals, theColor);
475 //=======================================================================
476 //function : FindColor
478 //=======================================================================
479 Standard_Boolean AIS_ColorScale::FindColor (const Standard_Real theValue,
480 const Standard_Real theMin,
481 const Standard_Real theMax,
482 const Standard_Integer theColorsCount,
483 const Graphic3d_Vec3d& theColorHlsMin,
484 const Graphic3d_Vec3d& theColorHlsMax,
485 Quantity_Color& theColor)
487 if (theValue < theMin || theValue > theMax || theMax < theMin)
489 return Standard_False;
492 const Standard_Integer anInterval = colorDiscreteInterval (theValue, theMin, theMax, theColorsCount);
493 theColor = colorFromValueEx (anInterval - 1, 0, theColorsCount - 1, theColorHlsMin, theColorHlsMax);
494 return Standard_True;
497 //=======================================================================
498 //function : computeMaxLabelWidth
500 //=======================================================================
501 Standard_Integer AIS_ColorScale::computeMaxLabelWidth (const TColStd_SequenceOfExtendedString& theLabels) const
503 Standard_Integer aWidthMax = 0;
504 for (TColStd_SequenceOfExtendedString::Iterator aLabIter (theLabels); aLabIter.More(); aLabIter.Next())
506 if (!aLabIter.Value().IsEmpty())
508 aWidthMax = Max (aWidthMax, TextWidth (aLabIter.Value()));
514 //=======================================================================
515 //function : updateTextAspect
517 //=======================================================================
518 void AIS_ColorScale::updateTextAspect()
520 // update text aspect
521 const Quantity_Color aFgColor (hasOwnColor ? myDrawer->Color() : Quantity_NOC_WHITE);
522 if (!myDrawer->HasOwnTextAspect())
524 myDrawer->SetTextAspect (new Prs3d_TextAspect());
525 *myDrawer->TextAspect()->Aspect() = *myDrawer->Link()->TextAspect()->Aspect();
528 const Handle(Prs3d_TextAspect)& anAspect = myDrawer->TextAspect();
529 anAspect->SetColor (aFgColor);
530 anAspect->SetHeight (myTextHeight);
531 anAspect->SetHorizontalJustification (Graphic3d_HTA_LEFT);
532 anAspect->SetVerticalJustification (Graphic3d_VTA_BOTTOM);
533 anAspect->Aspect()->SetTextZoomable (Standard_True);
536 //=======================================================================
539 //=======================================================================
540 void AIS_ColorScale::Compute (const Handle(PrsMgr_PresentationManager)& ,
541 const Handle(Prs3d_Presentation)& thePrs,
542 const Standard_Integer theMode)
549 // update text aspect
552 const Standard_Integer aTitleOffset = !myTitle.IsEmpty() ? (myTextHeight + mySpacing) : 0;
554 const Standard_Integer aBarYOffset = myTextHeight / 2 + 2 * mySpacing; // a half-label offset
555 const Standard_Integer aBarBottom = myYPos + aBarYOffset;
556 const Standard_Integer aBarTop = myYPos + myHeight - aTitleOffset - aBarYOffset;
557 const Standard_Integer aBarHeight = aBarTop - aBarBottom;
559 TColStd_SequenceOfExtendedString aLabels;
560 if (myLabelType == Aspect_TOCSD_USER)
566 const Standard_Integer aNbLabels = myIsLabelAtBorder ? myNbIntervals + 1 : myNbIntervals;
567 for (Standard_Integer aLabIter = 1; aLabIter <= aNbLabels; ++aLabIter)
571 aLabels.Prepend (GetLabel (aLabIter));
575 aLabels.Append (GetLabel (aLabIter));
580 const Standard_Integer aTextWidth = myLabelPos != Aspect_TOCSP_NONE ? computeMaxLabelWidth (aLabels) : 0;
581 Standard_Integer aColorBreadth = Max (5, Min (20, myBreadth - aTextWidth - 3 * mySpacing));
582 if (myLabelPos == Aspect_TOCSP_CENTER
583 || myLabelPos == Aspect_TOCSP_NONE)
585 aColorBreadth += aTextWidth;
589 Handle(Graphic3d_Group) aLabelsGroup;
590 if (!myTitle.IsEmpty()
591 || !aLabels.IsEmpty())
593 aLabelsGroup = thePrs->NewGroup();
594 aLabelsGroup->SetGroupPrimitivesAspect (myDrawer->TextAspect()->Aspect());
596 if (!myTitle.IsEmpty())
598 drawText (aLabelsGroup, myTitle,
600 aBarTop + aBarYOffset,
601 Graphic3d_VTA_BOTTOM);
605 drawColorBar (thePrs, aBarBottom, aBarHeight, aTextWidth, aColorBreadth);
608 drawLabels (aLabelsGroup, aLabels, aBarBottom, aBarHeight, aTextWidth, aColorBreadth);
611 //=======================================================================
612 //function : drawColorBar
614 //=======================================================================
615 void AIS_ColorScale::drawColorBar (const Handle(Prs3d_Presentation)& thePrs,
616 const Standard_Integer theBarBottom,
617 const Standard_Integer theBarHeight,
618 const Standard_Integer theMaxLabelWidth,
619 const Standard_Integer theColorBreadth)
621 const Standard_Real aStepY = Standard_Real(theBarHeight) / Standard_Real(myNbIntervals);
628 const Standard_Integer anXLeft = myLabelPos == Aspect_TOCSP_LEFT
629 ? myXPos + mySpacing + theMaxLabelWidth + (theMaxLabelWidth != 0 ? 1 : 0) * mySpacing
630 : myXPos + mySpacing;
632 Aspect_SequenceOfColor aColors;
633 for (Standard_Integer anIntervalIter = 1; anIntervalIter <= myNbIntervals; ++anIntervalIter)
637 aColors.Prepend (GetIntervalColor (anIntervalIter));
641 aColors.Append (GetIntervalColor (anIntervalIter));
645 Handle(Graphic3d_ArrayOfTriangles) aTriangles;
647 && myColorType == Aspect_TOCSD_USER)
649 // Smooth custom intervals, so that the color in the center of interval is equal to specified one
650 // (thus the halves of first and last intervals have solid color)
651 aTriangles = new Graphic3d_ArrayOfTriangles ((aColors.Length() + 1) * 4, // quads
652 (aColors.Length() + 1) * 2 * 3, // quads as triangles
653 false, true); // per-vertex colors
654 Quantity_Color aColor1 (aColors.Value (1)), aColor2;
655 Standard_Integer aSizeY = Standard_Integer(aStepY / 2);
656 const Standard_Integer anYBottom = theBarBottom + aSizeY;
657 Standard_Integer anYBottomIter = anYBottom;
658 addColoredQuad (aTriangles,
659 anXLeft, theBarBottom,
660 theColorBreadth, aSizeY,
662 for (Standard_Integer aColorIter = 0; aColorIter < myNbIntervals - 1; ++aColorIter)
664 aColor1 = aColors.Value (aColorIter + 1);
665 aColor2 = aColors.Value (aColorIter + 2);
666 aSizeY = anYBottom + Standard_Integer((aColorIter + 1) * aStepY) - anYBottomIter;
667 addColoredQuad (aTriangles,
668 anXLeft, anYBottomIter,
669 theColorBreadth, aSizeY,
671 anYBottomIter += aSizeY;
673 aColor2 = aColors.Value (myNbIntervals);
674 aSizeY = theBarBottom + theBarHeight - anYBottomIter;
675 addColoredQuad (aTriangles,
676 anXLeft, anYBottomIter,
677 theColorBreadth, aSizeY,
682 // smooth transition between standard colors - without solid color regions at the beginning and end of full color range
683 const Quantity_Color aColorsFixed[5] =
685 colorFromValue (0, 0, 4),
686 colorFromValue (1, 0, 4),
687 colorFromValue (2, 0, 4),
688 colorFromValue (3, 0, 4),
689 colorFromValue (4, 0, 4)
691 aTriangles = new Graphic3d_ArrayOfTriangles (4 * 4, // quads
692 4 * 2 * 3, // quads as triangles
693 false, true); // per-vertex colors
694 Standard_Integer anYBottomIter = theBarBottom;
695 addColoredQuad (aTriangles,
696 anXLeft, theBarBottom,
697 theColorBreadth, theBarHeight / 4,
698 aColorsFixed[0], aColorsFixed[1]);
699 anYBottomIter += theBarHeight / 4;
700 addColoredQuad (aTriangles,
701 anXLeft, anYBottomIter,
702 theColorBreadth, theBarHeight / 4,
703 aColorsFixed[1], aColorsFixed[2]);
704 anYBottomIter += theBarHeight / 4;
705 addColoredQuad (aTriangles,
706 anXLeft, anYBottomIter,
707 theColorBreadth, theBarHeight / 4,
708 aColorsFixed[2], aColorsFixed[3]);
709 anYBottomIter += theBarHeight / 4;
710 const Standard_Integer aLastSizeY = theBarBottom + theBarHeight - anYBottomIter;
711 addColoredQuad (aTriangles,
712 anXLeft, anYBottomIter,
713 theColorBreadth, aLastSizeY,
714 aColorsFixed[3], aColorsFixed[4]);
718 // no color smoothing
719 aTriangles = new Graphic3d_ArrayOfTriangles (aColors.Length() * 4, // quads
720 aColors.Length() * 2 * 3, // quads as triangles
721 false, true); // per-vertex colors
722 Standard_Integer anYBottomIter = theBarBottom;
723 for (Standard_Integer aColorIter = 0; aColorIter < myNbIntervals; ++aColorIter)
725 const Quantity_Color& aColor = aColors.Value (aColorIter + 1);
726 const Standard_Integer aSizeY = theBarBottom + Standard_Integer((aColorIter + 1) * aStepY) - anYBottomIter;
727 addColoredQuad (aTriangles,
728 anXLeft, anYBottomIter,
729 theColorBreadth, aSizeY,
731 anYBottomIter += aSizeY;
735 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
736 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
737 aGroup->AddPrimitiveArray (aTriangles);
739 const Quantity_Color aFgColor (hasOwnColor ? myDrawer->Color() : Quantity_NOC_WHITE);
741 anXLeft - 1, theBarBottom - 1,
747 //=======================================================================
748 //function : drawLabels
750 //=======================================================================
751 void AIS_ColorScale::drawLabels (const Handle(Graphic3d_Group)& theGroup,
752 const TColStd_SequenceOfExtendedString& theLabels,
753 const Standard_Integer theBarBottom,
754 const Standard_Integer theBarHeight,
755 const Standard_Integer theMaxLabelWidth,
756 const Standard_Integer theColorBreadth)
758 if (myLabelPos == Aspect_TOCSP_NONE
759 || theLabels.IsEmpty())
764 const Standard_Integer aNbLabels = theLabels.Size();
765 const Standard_Integer aNbIntervals = myIsLabelAtBorder ? aNbLabels - 1 : aNbLabels;
766 const Standard_Real aStepY = Standard_Real(theBarHeight) / Standard_Real(aNbIntervals);
772 Standard_Integer aFilter = 0;
774 const Standard_Integer aTitleHeight = !myTitle.IsEmpty() ? (myTextHeight + 2 * mySpacing) : mySpacing;
775 const Standard_Integer aSpc = myHeight - aTitleHeight - ((Min (aNbLabels, 2) + Abs (aNbLabels - aNbIntervals - 1)) * myTextHeight);
781 const Standard_Real aVal = Standard_Real(aNbLabels) * myTextHeight / aSpc;
782 Standard_Real anIPart = 0.0;
783 Standard_Real anFPart = std::modf (aVal, &anIPart);
784 aFilter = (Standard_Integer )anIPart + (anFPart != 0 ? 1 : 0);
791 Standard_Integer anXLeft = myXPos + mySpacing;
792 const Standard_Integer anAscent = 0;
795 case Aspect_TOCSP_NONE:
796 case Aspect_TOCSP_LEFT:
800 case Aspect_TOCSP_CENTER:
802 anXLeft += (theColorBreadth - theMaxLabelWidth) / 2;
805 case Aspect_TOCSP_RIGHT:
807 anXLeft += theColorBreadth + mySpacing;
812 Standard_Integer i1 = 0;
813 Standard_Integer i2 = aNbLabels - 1;
814 Standard_Integer aLast1 = i1;
815 Standard_Integer aLast2 = i2;
816 const Standard_Integer anYBottom = myIsLabelAtBorder
818 : theBarBottom + Standard_Integer(aStepY / 2);
819 while (i2 - i1 >= aFilter || ( i2 == 0 && i1 == 0 ))
821 Standard_Integer aPos1 = i1;
822 Standard_Integer aPos2 = aNbLabels - 1 - i2;
823 if (aFilter && !(aPos1 % aFilter))
825 drawText (theGroup, theLabels.Value (i1 + 1),
826 anXLeft, anYBottom + Standard_Integer(i1 * aStepY + anAscent),
827 Graphic3d_VTA_CENTER);
830 if (aFilter && !(aPos2 % aFilter))
832 drawText (theGroup, theLabels.Value (i2 + 1),
833 anXLeft, anYBottom + Standard_Integer(i2 * aStepY + anAscent),
834 Graphic3d_VTA_CENTER);
840 Standard_Integer aPos = i1;
841 Standard_Integer i0 = -1;
842 while (aPos <= i2 && i0 == -1)
844 if (aFilter && !(aPos % aFilter)
845 && Abs (aPos - aLast1) >= aFilter
846 && Abs (aPos - aLast2) >= aFilter)
855 drawText (theGroup, theLabels.Value (i0 + 1),
856 anXLeft, anYBottom + Standard_Integer(i0 * aStepY + anAscent),
857 Graphic3d_VTA_CENTER);
861 //=======================================================================
862 //function : drawFrame
864 //=======================================================================
865 void AIS_ColorScale::drawFrame (const Handle(Prs3d_Presentation)& thePrs,
866 const Standard_Integer theX, const Standard_Integer theY,
867 const Standard_Integer theWidth, const Standard_Integer theHeight,
868 const Quantity_Color& theColor)
870 Handle(Graphic3d_ArrayOfPolylines) aPrim = new Graphic3d_ArrayOfPolylines(5);
871 aPrim->AddVertex (theX, theY, 0.0);
872 aPrim->AddVertex (theX + theWidth, theY, 0.0);
873 aPrim->AddVertex (theX + theWidth, theY + theHeight, 0.0);
874 aPrim->AddVertex (theX, theY + theHeight, 0.0);
875 aPrim->AddVertex (theX, theY, 0.0);
877 Handle(Graphic3d_AspectLine3d) anAspect = new Graphic3d_AspectLine3d (theColor, Aspect_TOL_SOLID, 1.0);
878 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
879 aGroup->SetGroupPrimitivesAspect (anAspect);
880 aGroup->AddPrimitiveArray (aPrim);
883 //=======================================================================
884 //function : drawText
886 //=======================================================================
887 void AIS_ColorScale::drawText (const Handle(Graphic3d_Group)& theGroup,
888 const TCollection_ExtendedString& theText,
889 const Standard_Integer theX, const Standard_Integer theY,
890 const Graphic3d_VerticalTextAlignment theVertAlignment)
892 const Handle(Prs3d_TextAspect)& anAspect = myDrawer->TextAspect();
894 Handle(Graphic3d_Text) aText = new Graphic3d_Text ((Standard_ShortReal)anAspect->Height());
895 aText->SetText (theText.ToExtString());
896 aText->SetOrientation (gp_Ax2 (gp_Pnt (theX, theY, 0.0), gp::DZ()));
897 aText->SetOwnAnchorPoint (Standard_False);
898 aText->SetVerticalAlignment (theVertAlignment);
900 theGroup->AddText (aText);
903 //=======================================================================
904 //function : TextWidth
906 //=======================================================================
907 Standard_Integer AIS_ColorScale::TextWidth (const TCollection_ExtendedString& theText) const
909 Standard_Integer aWidth = 0, anAscent = 0, aDescent = 0;
910 TextSize (theText, myTextHeight, aWidth, anAscent, aDescent);
914 //=======================================================================
915 //function : TextHeight
917 //=======================================================================
918 Standard_Integer AIS_ColorScale::TextHeight (const TCollection_ExtendedString& theText) const
920 Standard_Integer aWidth = 0, anAscent = 0, aDescent = 0;
921 TextSize (theText, myTextHeight, aWidth, anAscent, aDescent);
922 return anAscent + aDescent;
925 //=======================================================================
926 //function : TextSize
928 //=======================================================================
929 void AIS_ColorScale::TextSize (const TCollection_ExtendedString& theText,
930 const Standard_Integer theHeight,
931 Standard_Integer& theWidth,
932 Standard_Integer& theAscent,
933 Standard_Integer& theDescent) const
935 Standard_ShortReal aWidth = 10.0f, anAscent = 1.0f, aDescent = 1.0f;
936 if (HasInteractiveContext())
938 const TCollection_AsciiString aText (theText);
939 const Handle(V3d_Viewer)& aViewer = GetContext()->CurrentViewer();
940 const Handle(Graphic3d_CView)& aView = aViewer->ActiveViewIterator().Value()->View();
941 aViewer->Driver()->TextSize (aView, aText.ToCString(), (Standard_ShortReal)theHeight, aWidth, anAscent, aDescent);
943 theWidth = (Standard_Integer)aWidth;
944 theAscent = (Standard_Integer)anAscent;
945 theDescent = (Standard_Integer)aDescent;