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 <Aspect_Window.hxx>
21 #include <Geom_Line.hxx>
22 #include <GeomAdaptor_Curve.hxx>
23 #include <Graphic3d_ArrayOfPolygons.hxx>
24 #include <Graphic3d_ArrayOfPolylines.hxx>
25 #include <Graphic3d_AspectFillArea3d.hxx>
26 #include <Graphic3d_AspectText3d.hxx>
27 #include <Graphic3d_GraphicDriver.hxx>
28 #include <Graphic3d_ArrayOfTriangles.hxx>
29 #include <Graphic3d_Text.hxx>
30 #include <Prs3d_LineAspect.hxx>
31 #include <Prs3d_ShadingAspect.hxx>
32 #include <Prs3d_Text.hxx>
33 #include <Prs3d_TextAspect.hxx>
34 #include <SelectMgr_EntityOwner.hxx>
35 #include <SelectMgr_Selection.hxx>
36 #include <Select3D_SensitiveBox.hxx>
37 #include <Select3D_SensitiveSegment.hxx>
38 #include <StdPrs_Curve.hxx>
39 #include <V3d_Viewer.hxx>
40 #include <V3d_View.hxx>
42 IMPLEMENT_STANDARD_RTTIEXT(AIS_ColorScale, AIS_InteractiveObject)
46 //! Method to add colored quad into array of triangles.
47 static void addColoredQuad (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
48 const Standard_Integer theXLeft, const Standard_Integer theYBottom,
49 const Standard_Integer theSizeX, const Standard_Integer theSizeY,
50 const Quantity_Color& theColorBottom,
51 const Quantity_Color& theColorTop)
53 const Standard_Integer aVertIndex = theTris->VertexNumber() + 1;
54 theTris->AddVertex (gp_Pnt (theXLeft, theYBottom, 0.0), theColorBottom);
55 theTris->AddVertex (gp_Pnt (theXLeft + theSizeX, theYBottom, 0.0), theColorBottom);
56 theTris->AddVertex (gp_Pnt (theXLeft, theYBottom + theSizeY, 0.0), theColorTop);
57 theTris->AddVertex (gp_Pnt (theXLeft + theSizeX, theYBottom + theSizeY, 0.0), theColorTop);
58 theTris->AddEdges (aVertIndex, aVertIndex + 1, aVertIndex + 2);
59 theTris->AddEdges (aVertIndex + 1, aVertIndex + 2, aVertIndex + 3);
62 //! Compute hue angle from specified value.
63 static Quantity_Color colorFromValueEx (const Standard_Real theValue,
64 const Standard_Real theMin,
65 const Standard_Real theMax,
66 const Graphic3d_Vec3d& theHlsMin,
67 const Graphic3d_Vec3d& theHlsMax)
69 const Standard_Real aValueDelta = theMax - theMin;
70 Standard_Real aValue = 0.0;
71 if (aValueDelta != 0.0)
73 aValue = (theValue - theMin) / aValueDelta;
76 Standard_Real aHue = NCollection_Lerp<Standard_Real>::Interpolate (theHlsMin[0], theHlsMax[0], aValue);
77 Standard_Real aLightness = NCollection_Lerp<Standard_Real>::Interpolate (theHlsMin[1], theHlsMax[1], aValue);
78 Standard_Real aSaturation = NCollection_Lerp<Standard_Real>::Interpolate (theHlsMin[2], theHlsMax[2], aValue);
79 return Quantity_Color (AIS_ColorScale::hueToValidRange (aHue), aLightness, aSaturation, Quantity_TOC_HLS);
82 //! Return the index of discrete interval for specified value.
83 //! Note that when value lies exactly on the border between two intervals,
84 //! determining which interval to return is undefined operation;
85 //! Current implementation returns the following interval in this case.
86 //! @param theValue [in] value to map
87 //! @param theMin [in] values range, lower value
88 //! @param theMax [in] values range, upper value
89 //! @param theNbIntervals [in] number of discrete intervals
90 //! @return index of interval within [1, theNbIntervals] range
91 static Standard_Integer colorDiscreteInterval (Standard_Real theValue,
94 Standard_Integer theNbIntervals)
96 if (Abs (theMax - theMin) <= Precision::Approximation())
101 Standard_Integer anInterval = 1 + (Standard_Integer )Floor (Standard_Real (theNbIntervals) * (theValue - theMin) / (theMax - theMin));
102 // map the very upper value (theValue==theMax) to the largest color interval
103 anInterval = Min (anInterval, theNbIntervals);
108 //=======================================================================
109 //function : AIS_ColorScale
111 //=======================================================================
112 AIS_ColorScale::AIS_ColorScale()
115 myColorHlsMin (230.0, 1.0, 1.0),
116 myColorHlsMax (0.0, 1.0, 1.0),
119 myColorType (Aspect_TOCSD_AUTO),
120 myLabelType (Aspect_TOCSD_AUTO),
121 myIsLabelAtBorder (Standard_True),
122 myIsReversed (Standard_False),
123 myIsLogarithmic (Standard_False),
124 myIsSmooth (Standard_False),
125 myLabelPos (Aspect_TOCSP_RIGHT),
126 myTitlePos (Aspect_TOCSP_LEFT),
135 myDrawer->SetupOwnShadingAspect();
136 myDrawer->ShadingAspect()->Aspect()->SetShadingModel (Graphic3d_TOSM_UNLIT);
137 myDrawer->ShadingAspect()->Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Opaque);
138 myDrawer->ShadingAspect()->Aspect()->SetInteriorColor (Quantity_NOC_WHITE);
141 //=======================================================================
142 //function : GetLabel
144 //=======================================================================
145 TCollection_ExtendedString AIS_ColorScale::GetLabel (const Standard_Integer theIndex) const
147 if (myLabelType == Aspect_TOCSD_USER)
149 if (theIndex >= myLabels.Lower()
150 || theIndex <= myLabels.Upper())
152 return myLabels.Value(theIndex);
154 return TCollection_ExtendedString();
157 // value to be shown depends on label position
158 const Standard_Real aVal = myIsLabelAtBorder
159 ? GetIntervalValue (theIndex - 1)
160 : (0.5 * (GetIntervalValue (theIndex - 1) + GetIntervalValue (theIndex)));
163 sprintf (aBuf, myFormat.ToCString(), aVal);
164 return TCollection_ExtendedString (aBuf);
167 //=======================================================================
168 //function : GetIntervalColor
170 //=======================================================================
171 Quantity_Color AIS_ColorScale::GetIntervalColor (const Standard_Integer theIndex) const
173 if (myColorType== Aspect_TOCSD_USER)
175 if (theIndex <= 0 || theIndex > myColors.Length())
177 return Quantity_Color();
179 return myColors.Value (theIndex);
182 return colorFromValue (theIndex - 1, 0, myNbIntervals - 1);
185 //=======================================================================
186 //function : GetLabels
188 //=======================================================================
189 void AIS_ColorScale::GetLabels (TColStd_SequenceOfExtendedString& theLabels) const
192 for (TColStd_SequenceOfExtendedString::Iterator aLabIter (myLabels); aLabIter.More(); aLabIter.Next())
194 theLabels.Append (aLabIter.Value());
198 //=======================================================================
199 //function : GetColors
201 //=======================================================================
202 void AIS_ColorScale::GetColors (Aspect_SequenceOfColor& theColors) const
205 for (Aspect_SequenceOfColor::Iterator aColorIter (myColors); aColorIter.More(); aColorIter.Next())
207 theColors.Append (aColorIter.Value());
211 //=======================================================================
212 //function : SetRange
214 //=======================================================================
215 void AIS_ColorScale::SetRange (const Standard_Real theMin, const Standard_Real theMax)
217 myMin = Min (theMin, theMax);
218 myMax = Max (theMin, theMax);
221 //=======================================================================
222 //function : SetNumberOfIntervals
224 //=======================================================================
225 void AIS_ColorScale::SetNumberOfIntervals (const Standard_Integer theNum)
232 myNbIntervals = theNum;
235 //=======================================================================
236 //function : SetLabel
238 //=======================================================================
239 void AIS_ColorScale::SetLabel (const TCollection_ExtendedString& theLabel,
240 const Standard_Integer theIndex)
242 const Standard_Integer aLabIndex = (theIndex <= 0 ? myLabels.Length() + 1 : theIndex);
243 while (myLabels.Length() < aLabIndex)
245 myLabels.Append (TCollection_ExtendedString());
247 myLabels.SetValue (aLabIndex, theLabel);
250 //=======================================================================
251 //function : SetIntervalColor
253 //=======================================================================
254 void AIS_ColorScale::SetIntervalColor (const Quantity_Color& theColor,
255 const Standard_Integer theIndex)
257 const Standard_Integer aColorIndex = (theIndex <= 0 ? myColors.Length() + 1 : theIndex);
258 while (myColors.Length() < aColorIndex)
260 myColors.Append (Quantity_Color());
262 myColors.SetValue (aColorIndex, theColor);
265 //=======================================================================
266 //function : SetLabels
268 //=======================================================================
269 void AIS_ColorScale::SetLabels (const TColStd_SequenceOfExtendedString& theSeq)
272 for (TColStd_SequenceOfExtendedString::Iterator aLabIter (theSeq); aLabIter.More(); aLabIter.Next())
274 myLabels.Append (aLabIter.Value());
278 //=======================================================================
279 //function : SetColors
281 //=======================================================================
282 void AIS_ColorScale::SetColors (const Aspect_SequenceOfColor& theSeq)
285 for (Aspect_SequenceOfColor::Iterator aColorIter (theSeq); aColorIter.More(); aColorIter.Next())
287 myColors.Append (aColorIter.Value());
291 //=======================================================================
292 //function : MakeUniformColors
294 //=======================================================================
295 Aspect_SequenceOfColor AIS_ColorScale::MakeUniformColors (Standard_Integer theNbColors,
296 Standard_Real theLightness,
297 Standard_Real theHueFrom,
298 Standard_Real theHueTo)
300 Aspect_SequenceOfColor aResult;
302 // adjust range to be within (0, 360], with sign according to theHueFrom and theHueTo
303 Standard_Real aHueRange = std::fmod (theHueTo - theHueFrom, 360.);
304 const Standard_Real aHueEps = Precision::Angular() * 180. / M_PI;
305 if (Abs (aHueRange) <= aHueEps)
307 aHueRange = (aHueRange < 0 ? -360. : 360.);
315 if (theNbColors == 1)
317 Standard_Real aHue = std::fmod (theHueFrom, 360.);
322 Quantity_Color aColor (theLightness, 130., aHue, Quantity_TOC_CIELch);
323 aResult.Append (aColor);
327 // discretize the range with 1 degree step
328 const int NBCOLORS = 2 + (int)Abs (aHueRange / 1.);
329 Standard_Real aHueStep = aHueRange / (NBCOLORS - 1);
330 NCollection_Array1<Quantity_Color> aGrid (0, NBCOLORS - 1);
331 for (Standard_Integer i = 0; i < NBCOLORS; i++)
333 Standard_Real aHue = std::fmod (theHueFrom + i * aHueStep, 360.);
338 aGrid(i).SetValues (theLightness, 130., aHue, Quantity_TOC_CIELch);
341 // and compute distances between each two colors in a grid
342 TColStd_Array1OfReal aMetric (0, NBCOLORS - 1);
343 Standard_Real aLength = 0.;
344 for (Standard_Integer i = 0, j = NBCOLORS - 1; i < NBCOLORS; j = i++)
346 aLength += (aMetric(i) = aGrid(i).DeltaE2000 (aGrid(j)));
349 // determine desired step by distance;
350 // normally we aim to distribute colors from start to end
351 // of the range, but if distance between first and last points of the range
352 // is less than that step (e.g. range is full 360 deg),
353 // then distribute by the whole 360 deg scope to ensure that first
354 // and last colors are sufficiently distanced
355 Standard_Real aDStep = (aLength - aMetric.First()) / (theNbColors - 1);
356 if (aMetric.First() < aDStep)
358 aDStep = aLength / theNbColors;
362 aResult.Append(aGrid(0));
363 Standard_Real aParam = 0., aPrev = 0., aTarget = aDStep;
364 for (int i = 1; i < NBCOLORS; i++)
366 aParam = aPrev + aMetric(i);
367 while (aTarget <= aParam)
369 float aCoefPrev = float((aParam - aTarget) / (aParam - aPrev));
370 float aCoefCurr = float((aTarget - aPrev) / (aParam - aPrev));
371 Quantity_Color aColor (aGrid(i).Rgb() * aCoefCurr + aGrid(i-1).Rgb() * aCoefPrev);
372 aResult.Append (aColor);
377 if (aResult.Length() < theNbColors)
379 aResult.Append (aGrid.Last());
381 Standard_ASSERT_VOID (aResult.Length() == theNbColors, "Failed to generate requested nb of colors");
385 //=======================================================================
386 //function : SizeHint
388 //=======================================================================
389 void AIS_ColorScale::SizeHint (Standard_Integer& theWidth, Standard_Integer& theHeight) const
391 const Standard_Integer aTextHeight = TextHeight ("");
392 const Standard_Integer aColorWidth = 20;
393 Standard_Integer aTextWidth = 0;
394 if (myLabelPos != Aspect_TOCSP_NONE)
396 for (Standard_Integer aLabIter = (myIsLabelAtBorder ? 0 : 1); aLabIter <= myNbIntervals; ++aLabIter)
398 aTextWidth = Max (aTextWidth, TextWidth (GetLabel (aLabIter)));
402 const Standard_Integer aScaleWidth = aColorWidth + aTextWidth + (aTextWidth ? 3 : 2) * mySpacing;
403 const Standard_Integer aScaleHeight = (Standard_Integer)(1.5 * (myNbIntervals + (myIsLabelAtBorder ? 2 : 1)) * aTextHeight);
405 Standard_Integer aTitleWidth = 0;
406 Standard_Integer aTitleHeight = 0;
407 if (!myTitle.IsEmpty())
409 aTitleHeight = TextHeight (myTitle) + mySpacing;
410 aTitleWidth = TextWidth (myTitle) + mySpacing * 2;
413 theWidth = Max (aTitleWidth, aScaleWidth);
414 theHeight = aScaleHeight + aTitleHeight;
417 //=======================================================================
418 //function : GetIntervalValue
420 //=======================================================================
421 Standard_Real AIS_ColorScale::GetIntervalValue (const Standard_Integer theIndex) const
423 if (myNbIntervals <= 0)
430 Standard_Real aMin = myMin > 0 ? myMin : 1.0;
431 Standard_Real aDivisor = std::pow (myMax / aMin, 1.0 / myNbIntervals);
432 return aMin * std::pow (aDivisor,theIndex);
435 Standard_Real aNum = 0;
436 if (myNbIntervals > 0)
438 aNum = GetMin() + theIndex * (Abs (GetMax() - GetMin()) / myNbIntervals);
443 //=======================================================================
444 //function : colorFromValue
446 //=======================================================================
447 Quantity_Color AIS_ColorScale::colorFromValue (const Standard_Real theValue,
448 const Standard_Real theMin,
449 const Standard_Real theMax) const
451 return colorFromValueEx (theValue, theMin, theMax, myColorHlsMin, myColorHlsMax);
454 //=======================================================================
455 //function : FindColor
457 //=======================================================================
458 Standard_Boolean AIS_ColorScale::FindColor (const Standard_Real theValue,
459 Quantity_Color& theColor) const
461 if (theValue < myMin || theValue > myMax || myMax < myMin)
463 theColor = Quantity_Color();
464 return Standard_False;
467 if (myColorType == Aspect_TOCSD_USER)
469 const Standard_Integer anInterval = colorDiscreteInterval (theValue, myMin, myMax, myNbIntervals);
470 if (anInterval < myColors.Lower() || anInterval > myColors.Upper())
472 theColor = Quantity_Color();
473 return Standard_False;
476 theColor = myColors.Value (anInterval);
477 return Standard_True;
480 return FindColor (theValue, myMin, myMax, myNbIntervals, theColor);
483 //=======================================================================
484 //function : FindColor
486 //=======================================================================
487 Standard_Boolean AIS_ColorScale::FindColor (const Standard_Real theValue,
488 const Standard_Real theMin,
489 const Standard_Real theMax,
490 const Standard_Integer theColorsCount,
491 const Graphic3d_Vec3d& theColorHlsMin,
492 const Graphic3d_Vec3d& theColorHlsMax,
493 Quantity_Color& theColor)
495 if (theValue < theMin || theValue > theMax || theMax < theMin)
497 return Standard_False;
500 const Standard_Integer anInterval = colorDiscreteInterval (theValue, theMin, theMax, theColorsCount);
501 theColor = colorFromValueEx (anInterval - 1, 0, theColorsCount - 1, theColorHlsMin, theColorHlsMax);
502 return Standard_True;
505 //=======================================================================
506 //function : computeMaxLabelWidth
508 //=======================================================================
509 Standard_Integer AIS_ColorScale::computeMaxLabelWidth (const TColStd_SequenceOfExtendedString& theLabels) const
511 Standard_Integer aWidthMax = 0;
512 for (TColStd_SequenceOfExtendedString::Iterator aLabIter (theLabels); aLabIter.More(); aLabIter.Next())
514 if (!aLabIter.Value().IsEmpty())
516 aWidthMax = Max (aWidthMax, TextWidth (aLabIter.Value()));
522 //=======================================================================
523 //function : updateTextAspect
525 //=======================================================================
526 void AIS_ColorScale::updateTextAspect()
528 // update text aspect
529 const Quantity_Color aFgColor (hasOwnColor ? myDrawer->Color() : Quantity_NOC_WHITE);
530 if (!myDrawer->HasOwnTextAspect())
532 myDrawer->SetTextAspect (new Prs3d_TextAspect());
533 *myDrawer->TextAspect()->Aspect() = *myDrawer->Link()->TextAspect()->Aspect();
536 const Handle(Prs3d_TextAspect)& anAspect = myDrawer->TextAspect();
537 anAspect->SetColor (aFgColor);
538 anAspect->SetHeight (myTextHeight);
539 anAspect->SetHorizontalJustification (Graphic3d_HTA_LEFT);
540 anAspect->SetVerticalJustification (Graphic3d_VTA_BOTTOM);
541 anAspect->Aspect()->SetTextZoomable (Standard_True);
544 //=======================================================================
547 //=======================================================================
548 void AIS_ColorScale::Compute (const Handle(PrsMgr_PresentationManager)& ,
549 const Handle(Prs3d_Presentation)& thePrs,
550 const Standard_Integer theMode)
557 // update text aspect
560 const Standard_Integer aTitleOffset = !myTitle.IsEmpty() ? (myTextHeight + mySpacing) : 0;
562 const Standard_Integer aBarYOffset = myTextHeight / 2 + 2 * mySpacing; // a half-label offset
563 const Standard_Integer aBarBottom = myYPos + aBarYOffset;
564 const Standard_Integer aBarTop = myYPos + myHeight - aTitleOffset - aBarYOffset;
565 const Standard_Integer aBarHeight = aBarTop - aBarBottom;
567 TColStd_SequenceOfExtendedString aLabels;
568 if (myLabelType == Aspect_TOCSD_USER)
574 const Standard_Integer aNbLabels = myIsLabelAtBorder ? myNbIntervals + 1 : myNbIntervals;
575 for (Standard_Integer aLabIter = 1; aLabIter <= aNbLabels; ++aLabIter)
579 aLabels.Prepend (GetLabel (aLabIter));
583 aLabels.Append (GetLabel (aLabIter));
588 const Standard_Integer aTextWidth = myLabelPos != Aspect_TOCSP_NONE ? computeMaxLabelWidth (aLabels) : 0;
589 Standard_Integer aColorBreadth = Max (5, Min (20, myBreadth - aTextWidth - 3 * mySpacing));
590 if (myLabelPos == Aspect_TOCSP_CENTER
591 || myLabelPos == Aspect_TOCSP_NONE)
593 aColorBreadth += aTextWidth;
597 Handle(Graphic3d_Group) aLabelsGroup;
598 if (!myTitle.IsEmpty()
599 || !aLabels.IsEmpty())
601 aLabelsGroup = thePrs->NewGroup();
602 aLabelsGroup->SetGroupPrimitivesAspect (myDrawer->TextAspect()->Aspect());
604 if (!myTitle.IsEmpty())
606 drawText (aLabelsGroup, myTitle,
608 aBarTop + aBarYOffset,
609 Graphic3d_VTA_BOTTOM);
613 drawColorBar (thePrs, aBarBottom, aBarHeight, aTextWidth, aColorBreadth);
616 drawLabels (aLabelsGroup, aLabels, aBarBottom, aBarHeight, aTextWidth, aColorBreadth);
619 //=======================================================================
620 //function : drawColorBar
622 //=======================================================================
623 void AIS_ColorScale::drawColorBar (const Handle(Prs3d_Presentation)& thePrs,
624 const Standard_Integer theBarBottom,
625 const Standard_Integer theBarHeight,
626 const Standard_Integer theMaxLabelWidth,
627 const Standard_Integer theColorBreadth)
629 const Standard_Real aStepY = Standard_Real(theBarHeight) / Standard_Real(myNbIntervals);
636 const Standard_Integer anXLeft = myLabelPos == Aspect_TOCSP_LEFT
637 ? myXPos + mySpacing + theMaxLabelWidth + (theMaxLabelWidth != 0 ? 1 : 0) * mySpacing
638 : myXPos + mySpacing;
640 Aspect_SequenceOfColor aColors;
641 for (Standard_Integer anIntervalIter = 1; anIntervalIter <= myNbIntervals; ++anIntervalIter)
645 aColors.Prepend (GetIntervalColor (anIntervalIter));
649 aColors.Append (GetIntervalColor (anIntervalIter));
653 Handle(Graphic3d_ArrayOfTriangles) aTriangles;
655 && myColorType == Aspect_TOCSD_USER)
657 // Smooth custom intervals, so that the color in the center of interval is equal to specified one
658 // (thus the halves of first and last intervals have solid color)
659 aTriangles = new Graphic3d_ArrayOfTriangles ((aColors.Length() + 1) * 4, // quads
660 (aColors.Length() + 1) * 2 * 3, // quads as triangles
661 false, true); // per-vertex colors
662 Quantity_Color aColor1 (aColors.Value (1)), aColor2;
663 Standard_Integer aSizeY = Standard_Integer(aStepY / 2);
664 const Standard_Integer anYBottom = theBarBottom + aSizeY;
665 Standard_Integer anYBottomIter = anYBottom;
666 addColoredQuad (aTriangles,
667 anXLeft, theBarBottom,
668 theColorBreadth, aSizeY,
670 for (Standard_Integer aColorIter = 0; aColorIter < myNbIntervals - 1; ++aColorIter)
672 aColor1 = aColors.Value (aColorIter + 1);
673 aColor2 = aColors.Value (aColorIter + 2);
674 aSizeY = anYBottom + Standard_Integer((aColorIter + 1) * aStepY) - anYBottomIter;
675 addColoredQuad (aTriangles,
676 anXLeft, anYBottomIter,
677 theColorBreadth, aSizeY,
679 anYBottomIter += aSizeY;
681 aColor2 = aColors.Value (myNbIntervals);
682 aSizeY = theBarBottom + theBarHeight - anYBottomIter;
683 addColoredQuad (aTriangles,
684 anXLeft, anYBottomIter,
685 theColorBreadth, aSizeY,
690 // smooth transition between standard colors - without solid color regions at the beginning and end of full color range
691 const Quantity_Color aColorsFixed[5] =
693 colorFromValue (0, 0, 4),
694 colorFromValue (1, 0, 4),
695 colorFromValue (2, 0, 4),
696 colorFromValue (3, 0, 4),
697 colorFromValue (4, 0, 4)
699 aTriangles = new Graphic3d_ArrayOfTriangles (4 * 4, // quads
700 4 * 2 * 3, // quads as triangles
701 false, true); // per-vertex colors
702 Standard_Integer anYBottomIter = theBarBottom;
703 addColoredQuad (aTriangles,
704 anXLeft, theBarBottom,
705 theColorBreadth, theBarHeight / 4,
706 aColorsFixed[0], aColorsFixed[1]);
707 anYBottomIter += theBarHeight / 4;
708 addColoredQuad (aTriangles,
709 anXLeft, anYBottomIter,
710 theColorBreadth, theBarHeight / 4,
711 aColorsFixed[1], aColorsFixed[2]);
712 anYBottomIter += theBarHeight / 4;
713 addColoredQuad (aTriangles,
714 anXLeft, anYBottomIter,
715 theColorBreadth, theBarHeight / 4,
716 aColorsFixed[2], aColorsFixed[3]);
717 anYBottomIter += theBarHeight / 4;
718 const Standard_Integer aLastSizeY = theBarBottom + theBarHeight - anYBottomIter;
719 addColoredQuad (aTriangles,
720 anXLeft, anYBottomIter,
721 theColorBreadth, aLastSizeY,
722 aColorsFixed[3], aColorsFixed[4]);
726 // no color smoothing
727 aTriangles = new Graphic3d_ArrayOfTriangles (aColors.Length() * 4, // quads
728 aColors.Length() * 2 * 3, // quads as triangles
729 false, true); // per-vertex colors
730 Standard_Integer anYBottomIter = theBarBottom;
731 for (Standard_Integer aColorIter = 0; aColorIter < myNbIntervals; ++aColorIter)
733 const Quantity_Color& aColor = aColors.Value (aColorIter + 1);
734 const Standard_Integer aSizeY = theBarBottom + Standard_Integer((aColorIter + 1) * aStepY) - anYBottomIter;
735 addColoredQuad (aTriangles,
736 anXLeft, anYBottomIter,
737 theColorBreadth, aSizeY,
739 anYBottomIter += aSizeY;
743 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
744 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
745 aGroup->AddPrimitiveArray (aTriangles);
747 const Quantity_Color aFgColor (hasOwnColor ? myDrawer->Color() : Quantity_NOC_WHITE);
749 anXLeft - 1, theBarBottom - 1,
755 //=======================================================================
756 //function : drawLabels
758 //=======================================================================
759 void AIS_ColorScale::drawLabels (const Handle(Graphic3d_Group)& theGroup,
760 const TColStd_SequenceOfExtendedString& theLabels,
761 const Standard_Integer theBarBottom,
762 const Standard_Integer theBarHeight,
763 const Standard_Integer theMaxLabelWidth,
764 const Standard_Integer theColorBreadth)
766 if (myLabelPos == Aspect_TOCSP_NONE
767 || theLabels.IsEmpty())
772 const Standard_Integer aNbLabels = theLabels.Size();
773 const Standard_Integer aNbIntervals = myIsLabelAtBorder ? aNbLabels - 1 : aNbLabels;
774 const Standard_Real aStepY = Standard_Real(theBarHeight) / Standard_Real(aNbIntervals);
780 Standard_Integer aFilter = 0;
782 const Standard_Integer aTitleHeight = !myTitle.IsEmpty() ? (myTextHeight + 2 * mySpacing) : mySpacing;
783 const Standard_Integer aSpc = myHeight - aTitleHeight - ((Min (aNbLabels, 2) + Abs (aNbLabels - aNbIntervals - 1)) * myTextHeight);
789 const Standard_Real aVal = Standard_Real(aNbLabels) * myTextHeight / aSpc;
790 Standard_Real anIPart = 0.0;
791 Standard_Real anFPart = std::modf (aVal, &anIPart);
792 aFilter = (Standard_Integer )anIPart + (anFPart != 0 ? 1 : 0);
799 Standard_Integer anXLeft = myXPos + mySpacing;
800 const Standard_Integer anAscent = 0;
803 case Aspect_TOCSP_NONE:
804 case Aspect_TOCSP_LEFT:
808 case Aspect_TOCSP_CENTER:
810 anXLeft += (theColorBreadth - theMaxLabelWidth) / 2;
813 case Aspect_TOCSP_RIGHT:
815 anXLeft += theColorBreadth + mySpacing;
820 Standard_Integer i1 = 0;
821 Standard_Integer i2 = aNbLabels - 1;
822 Standard_Integer aLast1 = i1;
823 Standard_Integer aLast2 = i2;
824 const Standard_Integer anYBottom = myIsLabelAtBorder
826 : theBarBottom + Standard_Integer(aStepY / 2);
827 while (i2 - i1 >= aFilter || ( i2 == 0 && i1 == 0 ))
829 Standard_Integer aPos1 = i1;
830 Standard_Integer aPos2 = aNbLabels - 1 - i2;
831 if (aFilter && !(aPos1 % aFilter))
833 drawText (theGroup, theLabels.Value (i1 + 1),
834 anXLeft, anYBottom + Standard_Integer(i1 * aStepY + anAscent),
835 Graphic3d_VTA_CENTER);
838 if (aFilter && !(aPos2 % aFilter))
840 drawText (theGroup, theLabels.Value (i2 + 1),
841 anXLeft, anYBottom + Standard_Integer(i2 * aStepY + anAscent),
842 Graphic3d_VTA_CENTER);
848 Standard_Integer aPos = i1;
849 Standard_Integer i0 = -1;
850 while (aPos <= i2 && i0 == -1)
852 if (aFilter && !(aPos % aFilter)
853 && Abs (aPos - aLast1) >= aFilter
854 && Abs (aPos - aLast2) >= aFilter)
863 drawText (theGroup, theLabels.Value (i0 + 1),
864 anXLeft, anYBottom + Standard_Integer(i0 * aStepY + anAscent),
865 Graphic3d_VTA_CENTER);
869 //=======================================================================
870 //function : drawFrame
872 //=======================================================================
873 void AIS_ColorScale::drawFrame (const Handle(Prs3d_Presentation)& thePrs,
874 const Standard_Integer theX, const Standard_Integer theY,
875 const Standard_Integer theWidth, const Standard_Integer theHeight,
876 const Quantity_Color& theColor)
878 Handle(Graphic3d_ArrayOfPolylines) aPrim = new Graphic3d_ArrayOfPolylines(5);
879 aPrim->AddVertex (theX, theY, 0.0);
880 aPrim->AddVertex (theX + theWidth, theY, 0.0);
881 aPrim->AddVertex (theX + theWidth, theY + theHeight, 0.0);
882 aPrim->AddVertex (theX, theY + theHeight, 0.0);
883 aPrim->AddVertex (theX, theY, 0.0);
885 Handle(Graphic3d_AspectLine3d) anAspect = new Graphic3d_AspectLine3d (theColor, Aspect_TOL_SOLID, 1.0);
886 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
887 aGroup->SetGroupPrimitivesAspect (anAspect);
888 aGroup->AddPrimitiveArray (aPrim);
891 //=======================================================================
892 //function : drawText
894 //=======================================================================
895 void AIS_ColorScale::drawText (const Handle(Graphic3d_Group)& theGroup,
896 const TCollection_ExtendedString& theText,
897 const Standard_Integer theX, const Standard_Integer theY,
898 const Graphic3d_VerticalTextAlignment theVertAlignment)
900 const Handle(Prs3d_TextAspect)& anAspect = myDrawer->TextAspect();
902 Handle(Graphic3d_Text) aText = new Graphic3d_Text ((Standard_ShortReal)anAspect->Height());
903 aText->SetText (theText.ToExtString());
904 aText->SetOrientation (gp_Ax2 (gp_Pnt (theX, theY, 0.0), gp::DZ()));
905 aText->SetOwnAnchorPoint (Standard_False);
906 aText->SetVerticalAlignment (theVertAlignment);
908 theGroup->AddText (aText);
911 //=======================================================================
912 //function : TextWidth
914 //=======================================================================
915 Standard_Integer AIS_ColorScale::TextWidth (const TCollection_ExtendedString& theText) const
917 Standard_Integer aWidth, anAscent, aDescent;
918 TextSize (theText, myTextHeight, aWidth, anAscent, aDescent);
922 //=======================================================================
923 //function : TextHeight
925 //=======================================================================
926 Standard_Integer AIS_ColorScale::TextHeight (const TCollection_ExtendedString& theText) const
928 Standard_Integer aWidth, anAscent, aDescent;
929 TextSize (theText, myTextHeight, aWidth, anAscent, aDescent);
930 return anAscent + aDescent;
933 //=======================================================================
934 //function : TextSize
936 //=======================================================================
937 void AIS_ColorScale::TextSize (const TCollection_ExtendedString& theText,
938 const Standard_Integer theHeight,
939 Standard_Integer& theWidth,
940 Standard_Integer& theAscent,
941 Standard_Integer& theDescent) const
943 if (!HasInteractiveContext())
948 Standard_ShortReal aWidth = 10.0f;
949 Standard_ShortReal anAscent = 1.0f;
950 Standard_ShortReal aDescent = 1.0f;
951 const TCollection_AsciiString aText (theText);
953 const Handle(V3d_Viewer)& aViewer = GetContext()->CurrentViewer();
954 const Handle(Graphic3d_CView)& aView = aViewer->ActiveViewIterator().Value()->View();
955 aViewer->Driver()->TextSize (aView, aText.ToCString(), (Standard_ShortReal)theHeight, aWidth, anAscent, aDescent);
956 theWidth = (Standard_Integer)aWidth;
957 theAscent = (Standard_Integer)anAscent;
958 theDescent = (Standard_Integer)aDescent;