1 // Created on: 2017-07-25
2 // Created by: Anastasia BOBYLEVA
3 // Copyright (c) 2017-2019 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <AIS_ViewCube.hxx>
18 #include <AIS_AnimationCamera.hxx>
19 #include <AIS_InteractiveContext.hxx>
21 #include <Graphic3d_ViewAffinity.hxx>
22 #include <NCollection_Lerp.hxx>
24 #include <Prs3d_Arrow.hxx>
25 #include <Prs3d_DatumAspect.hxx>
26 #include <Prs3d_Root.hxx>
27 #include <Prs3d_Text.hxx>
28 #include <Prs3d_ToolDisk.hxx>
29 #include <Prs3d_ToolSphere.hxx>
30 #include <Select3D_SensitivePrimitiveArray.hxx>
31 #include <SelectMgr_SequenceOfOwner.hxx>
33 #include <V3d_View.hxx>
35 IMPLEMENT_STANDARD_RTTIEXT(AIS_ViewCube, AIS_InteractiveObject)
36 IMPLEMENT_STANDARD_RTTIEXT(AIS_ViewCubeOwner, SelectMgr_EntityOwner)
40 static const Standard_Integer THE_NB_ROUND_SPLITS = 8;
41 static const Standard_Integer THE_NB_DISK_SLICES = 20;
42 static const Standard_Integer THE_NB_ARROW_FACETTES = 20;
44 //! Return the number of non-zero components.
45 static Standard_Integer nbDirectionComponents (const gp_Dir& theDir)
47 Standard_Integer aNbComps = 0;
48 for (Standard_Integer aCompIter = 1; aCompIter <= 3; ++aCompIter)
50 if (Abs (theDir.Coord (aCompIter)) > gp::Resolution())
59 //! Simple sensitive element for picking by point only.
60 class AIS_ViewCubeSensitive : public Select3D_SensitivePrimitiveArray
62 DEFINE_STANDARD_RTTI_INLINE(AIS_ViewCubeSensitive, Select3D_SensitivePrimitiveArray)
65 AIS_ViewCubeSensitive (const Handle(SelectMgr_EntityOwner)& theOwner,
66 const Handle(Graphic3d_ArrayOfTriangles)& theTris)
67 : Select3D_SensitivePrimitiveArray (theOwner)
69 InitTriangulation (theTris->Attributes(), theTris->Indices(), TopLoc_Location());
72 //! Checks whether element overlaps current selecting volume.
73 virtual Standard_Boolean Matches (SelectBasics_SelectingVolumeManager& theMgr,
74 SelectBasics_PickResult& thePickResult) Standard_OVERRIDE
76 return isValidRay (theMgr)
77 && Select3D_SensitivePrimitiveArray::Matches (theMgr, thePickResult);
80 //! Checks if picking ray can be used for detection.
81 bool isValidRay (const SelectBasics_SelectingVolumeManager& theMgr) const
83 if (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Point)
85 // disallow rectangular selection
89 if (AIS_ViewCubeOwner* anOwner = dynamic_cast<AIS_ViewCubeOwner* >(myOwnerId.get()))
91 const Standard_Real anAngleToler = 10.0 * M_PI / 180.0;
92 const gp_Vec aRay (theMgr.GetNearPickedPnt(), theMgr.GetFarPickedPnt());
93 const gp_Dir aDir = V3d::GetProjAxis (anOwner->MainOrientation());
94 return !aRay.IsNormal (aDir, anAngleToler);
100 //=======================================================================
101 //function : IsBoxSide
103 //=======================================================================
104 bool AIS_ViewCube::IsBoxSide (V3d_TypeOfOrientation theOrient)
106 return nbDirectionComponents (V3d::GetProjAxis (theOrient)) == 1;
109 //=======================================================================
110 //function : IsBoxEdge
112 //=======================================================================
113 bool AIS_ViewCube::IsBoxEdge (V3d_TypeOfOrientation theOrient)
115 return nbDirectionComponents (V3d::GetProjAxis (theOrient)) == 2;
118 //=======================================================================
119 //function : IsBoxCorner
121 //=======================================================================
122 bool AIS_ViewCube::IsBoxCorner (V3d_TypeOfOrientation theOrient)
124 return nbDirectionComponents (V3d::GetProjAxis (theOrient)) == 3;
127 //=======================================================================
128 //function : AIS_ViewCube
130 //=======================================================================
131 AIS_ViewCube::AIS_ViewCube()
132 : myBoxEdgeAspect (new Prs3d_ShadingAspect()),
133 myBoxCornerAspect (new Prs3d_ShadingAspect()),
135 myBoxEdgeMinSize (2.0),
137 myBoxFacetExtension (1.0),
139 myCornerMinSize (2.0),
141 myToDisplayAxes (true),
142 myToDisplayEdges (true),
143 myToDisplayVertices (true),
145 myViewAnimation (new AIS_AnimationCamera ("AIS_ViewCube", Handle(V3d_View)())),
146 myStartState(new Graphic3d_Camera()),
147 myEndState (new Graphic3d_Camera()),
149 myToAutoStartAnim (true),
150 myIsFixedAnimation (true),
151 myToFitSelected (true),
152 myToResetCameraUp (false)
154 myInfiniteState = true;
156 myDrawer->SetZLayer (Graphic3d_ZLayerId_Topmost);
157 myTransformPersistence = new Graphic3d_TransformPers (Graphic3d_TMF_TriedronPers, Aspect_TOTP_LEFT_LOWER, Graphic3d_Vec2i (100, 100));
159 myDrawer->SetTextAspect (new Prs3d_TextAspect());
160 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
162 myDynHilightDrawer = new Prs3d_Drawer();
163 myDynHilightDrawer->SetLink (myDrawer);
164 myDynHilightDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
166 setDefaultAttributes();
167 setDefaultHighlightAttributes();
169 // setup default labels
170 myBoxSideLabels.Bind (V3d_TypeOfOrientation_Zup_Front, "FRONT");
171 myBoxSideLabels.Bind (V3d_TypeOfOrientation_Zup_Back, "BACK");
172 myBoxSideLabels.Bind (V3d_TypeOfOrientation_Zup_Top, "TOP");
173 myBoxSideLabels.Bind (V3d_TypeOfOrientation_Zup_Bottom, "BOTTOM");
174 myBoxSideLabels.Bind (V3d_TypeOfOrientation_Zup_Left, "LEFT");
175 myBoxSideLabels.Bind (V3d_TypeOfOrientation_Zup_Right, "RIGHT");
177 myAxesLabels.Bind (Prs3d_DP_XAxis, "X");
178 myAxesLabels.Bind (Prs3d_DP_YAxis, "Y");
179 myAxesLabels.Bind (Prs3d_DP_ZAxis, "Z");
181 // define default size
185 //=======================================================================
186 //function : setDefaultAttributes
188 //=======================================================================
189 void AIS_ViewCube::setDefaultAttributes()
191 myDrawer->TextAspect()->SetHorizontalJustification(Graphic3d_HTA_CENTER);
192 myDrawer->TextAspect()->SetVerticalJustification (Graphic3d_VTA_CENTER);
193 myDrawer->TextAspect()->SetColor (Quantity_NOC_BLACK);
194 myDrawer->TextAspect()->SetFont (Font_NOF_SANS_SERIF);
195 myDrawer->TextAspect()->SetHeight (16.0);
196 // this should be forced back-face culling regardless Closed flag
197 myDrawer->TextAspect()->Aspect()->SetSuppressBackFaces (true);
199 Graphic3d_MaterialAspect aMat (Graphic3d_NOM_UserDefined);
200 aMat.SetColor (Quantity_NOC_WHITE);
201 aMat.SetAmbientColor (Quantity_NOC_GRAY60);
203 const Handle(Graphic3d_AspectFillArea3d)& aShading = myDrawer->ShadingAspect()->Aspect();
204 aShading->SetInteriorStyle (Aspect_IS_SOLID);
205 // this should be forced back-face culling regardless Closed flag
206 aShading->SetSuppressBackFaces (true);
207 aShading->SetInteriorColor (aMat.Color());
208 aShading->SetFrontMaterial (aMat);
209 myDrawer->SetFaceBoundaryDraw (false);
211 *myBoxEdgeAspect ->Aspect() = *aShading;
212 myBoxEdgeAspect->SetColor (Quantity_NOC_GRAY30);
213 *myBoxCornerAspect->Aspect() = *aShading;
214 myBoxCornerAspect->SetColor (Quantity_NOC_GRAY30);
217 //=======================================================================
218 //function : setDefaultHighlightAttributes
220 //=======================================================================
221 void AIS_ViewCube::setDefaultHighlightAttributes()
223 Graphic3d_MaterialAspect aHighlightMaterial;
224 aHighlightMaterial.SetReflectionModeOff (Graphic3d_TOR_AMBIENT);
225 aHighlightMaterial.SetReflectionModeOff (Graphic3d_TOR_DIFFUSE);
226 aHighlightMaterial.SetReflectionModeOff (Graphic3d_TOR_SPECULAR);
227 aHighlightMaterial.SetReflectionModeOff (Graphic3d_TOR_EMISSION);
228 aHighlightMaterial.SetMaterialType (Graphic3d_MATERIAL_ASPECT);
229 myDynHilightDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
230 myDynHilightDrawer->ShadingAspect()->SetMaterial (aHighlightMaterial);
231 myDynHilightDrawer->ShadingAspect()->SetColor (Quantity_NOC_CYAN1);
232 myDynHilightDrawer->SetZLayer (Graphic3d_ZLayerId_Topmost);
233 myDynHilightDrawer->SetColor (Quantity_NOC_CYAN1);
236 //=======================================================================
239 //=======================================================================
240 void AIS_ViewCube::SetYup (Standard_Boolean theIsYup,
241 Standard_Boolean theToUpdateLabels)
243 if (myIsYup == theIsYup)
250 static const V3d_TypeOfOrientation THE_ZUP_ORI_LIST[6] =
252 V3d_TypeOfOrientation_Zup_Front, V3d_TypeOfOrientation_Zup_Back,
253 V3d_TypeOfOrientation_Zup_Top, V3d_TypeOfOrientation_Zup_Bottom,
254 V3d_TypeOfOrientation_Zup_Left, V3d_TypeOfOrientation_Zup_Right
256 static const V3d_TypeOfOrientation THE_YUP_ORI_LIST[6] =
258 V3d_TypeOfOrientation_Yup_Front, V3d_TypeOfOrientation_Yup_Back,
259 V3d_TypeOfOrientation_Yup_Top, V3d_TypeOfOrientation_Yup_Bottom,
260 V3d_TypeOfOrientation_Yup_Left, V3d_TypeOfOrientation_Yup_Right
262 if (theToUpdateLabels)
264 NCollection_Array1<TCollection_AsciiString> aLabels (0, 5);
265 for (Standard_Integer aLabelIter = 0; aLabelIter < 6; ++aLabelIter)
267 myBoxSideLabels.Find (!myIsYup ? THE_YUP_ORI_LIST[aLabelIter] : THE_ZUP_ORI_LIST[aLabelIter],
268 aLabels.ChangeValue (aLabelIter));
270 for (Standard_Integer aLabelIter = 0; aLabelIter < 6; ++aLabelIter)
272 myBoxSideLabels.Bind (myIsYup ? THE_YUP_ORI_LIST[aLabelIter] : THE_ZUP_ORI_LIST[aLabelIter],
273 aLabels.Value (aLabelIter));
280 //=======================================================================
281 //function : ResetStyles
283 //=======================================================================
284 void AIS_ViewCube::ResetStyles()
287 UnsetHilightAttributes();
289 myBoxEdgeMinSize = 2.0;
290 myCornerMinSize = 2.0;
294 myToDisplayAxes = true;
295 myToDisplayEdges = true;
296 myToDisplayVertices = true;
298 myBoxFacetExtension = 1.0;
303 //=======================================================================
306 //=======================================================================
307 void AIS_ViewCube::SetSize (Standard_Real theValue,
308 Standard_Boolean theToAdaptAnother)
310 const bool isNewSize = Abs (mySize - theValue) > Precision::Confusion();
312 if (theToAdaptAnother)
314 if (myBoxFacetExtension > 0.0)
316 SetBoxFacetExtension (mySize * 0.15);
318 if (myAxesPadding > 0.0)
320 SetAxesPadding (mySize * 0.1);
322 SetFontHeight (mySize * 0.16);
330 //=======================================================================
331 //function : SetRoundRadius
333 //=======================================================================
334 void AIS_ViewCube::SetRoundRadius (const Standard_Real theValue)
336 Standard_OutOfRange_Raise_if (theValue < 0.0 || theValue > 0.5,
337 "AIS_ViewCube::SetRoundRadius(): theValue should be in [0; 0.5]");
338 if (Abs (myRoundRadius - theValue) > Precision::Confusion())
340 myRoundRadius = theValue;
345 //=======================================================================
346 //function : createRoundRectangleTriangles
348 //=======================================================================
349 void AIS_ViewCube::createRoundRectangleTriangles (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
350 Standard_Integer& theNbNodes,
351 Standard_Integer& theNbTris,
352 const gp_XY& theSize,
353 Standard_Real theRadius,
354 const gp_Trsf& theTrsf)
356 const Standard_Real aRadius = Min (theRadius, Min (theSize.X(), theSize.Y()) * 0.5);
357 const gp_XY aHSize (theSize.X() * 0.5 - aRadius, theSize.Y() * 0.5 - aRadius);
358 const gp_Dir aNorm = gp::DZ().Transformed (theTrsf);
359 const Standard_Integer aVertFirst = !theTris.IsNull() ? theTris->VertexNumber() : 0;
362 const Standard_Integer aNbNodes = (THE_NB_ROUND_SPLITS + 1) * 4 + 1;
363 theNbNodes += aNbNodes;
364 theNbTris += aNbNodes;
365 if (theTris.IsNull())
370 theTris->AddVertex (gp_Pnt (0.0, 0.0, 0.0).Transformed (theTrsf));
371 for (Standard_Integer aNodeIter = 0; aNodeIter <= THE_NB_ROUND_SPLITS; ++aNodeIter)
373 const Standard_Real anAngle = NCollection_Lerp<Standard_Real>::Interpolate (M_PI * 0.5, 0.0, Standard_Real(aNodeIter) / Standard_Real(THE_NB_ROUND_SPLITS));
374 theTris->AddVertex (gp_Pnt (aHSize.X() + aRadius * Cos (anAngle), aHSize.Y() + aRadius * Sin (anAngle), 0.0).Transformed (theTrsf));
376 for (Standard_Integer aNodeIter = 0; aNodeIter <= THE_NB_ROUND_SPLITS; ++aNodeIter)
378 const Standard_Real anAngle = NCollection_Lerp<Standard_Real>::Interpolate (0.0, -M_PI * 0.5, Standard_Real(aNodeIter) / Standard_Real(THE_NB_ROUND_SPLITS));
379 theTris->AddVertex (gp_Pnt (aHSize.X() + aRadius * Cos (anAngle), -aHSize.Y() + aRadius * Sin (anAngle), 0.0).Transformed (theTrsf));
381 for (Standard_Integer aNodeIter = 0; aNodeIter <= THE_NB_ROUND_SPLITS; ++aNodeIter)
383 const Standard_Real anAngle = NCollection_Lerp<Standard_Real>::Interpolate (-M_PI * 0.5, -M_PI, Standard_Real(aNodeIter) / Standard_Real(THE_NB_ROUND_SPLITS));
384 theTris->AddVertex (gp_Pnt (-aHSize.X() + aRadius * Cos (anAngle), -aHSize.Y() + aRadius * Sin (anAngle), 0.0).Transformed (theTrsf));
386 for (Standard_Integer aNodeIter = 0; aNodeIter <= THE_NB_ROUND_SPLITS; ++aNodeIter)
388 const Standard_Real anAngle = NCollection_Lerp<Standard_Real>::Interpolate (-M_PI, -M_PI * 1.5, Standard_Real(aNodeIter) / Standard_Real(THE_NB_ROUND_SPLITS));
389 theTris->AddVertex (gp_Pnt (-aHSize.X() + aRadius * Cos (anAngle), aHSize.Y() + aRadius * Sin (anAngle), 0.0).Transformed (theTrsf));
392 // split triangle fan
393 theTris->AddTriangleFanEdges (aVertFirst + 1, theTris->VertexNumber(), true);
399 if (theTris.IsNull())
404 theTris->AddVertex (gp_Pnt (-aHSize.X(), -aHSize.Y(), 0.0).Transformed (theTrsf));
405 theTris->AddVertex (gp_Pnt (-aHSize.X(), aHSize.Y(), 0.0).Transformed (theTrsf));
406 theTris->AddVertex (gp_Pnt ( aHSize.X(), aHSize.Y(), 0.0).Transformed (theTrsf));
407 theTris->AddVertex (gp_Pnt ( aHSize.X(), -aHSize.Y(), 0.0).Transformed (theTrsf));
408 theTris->AddQuadTriangleEdges (aVertFirst + 1, aVertFirst + 2, aVertFirst + 3, aVertFirst + 4);
411 for (Standard_Integer aVertIter = aVertFirst + 1; aVertIter <= theTris->VertexNumber(); ++aVertIter)
413 theTris->SetVertexNormal (aVertIter, -aNorm);
417 //=======================================================================
418 //function : createBoxPartTriangles
420 //=======================================================================
421 void AIS_ViewCube::createBoxPartTriangles (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
422 Standard_Integer& theNbNodes,
423 Standard_Integer& theNbTris,
424 V3d_TypeOfOrientation theDir) const
426 if (IsBoxSide (theDir))
428 createBoxSideTriangles (theTris, theNbNodes, theNbTris, theDir);
430 else if (IsBoxEdge (theDir)
433 createBoxEdgeTriangles (theTris, theNbNodes, theNbTris, theDir);
435 else if (IsBoxCorner (theDir)
436 && myToDisplayVertices)
438 createBoxCornerTriangles (theTris, theNbNodes, theNbTris, theDir);
442 //=======================================================================
443 //function : createBoxSideTriangles
445 //=======================================================================
446 void AIS_ViewCube::createBoxSideTriangles (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
447 Standard_Integer& theNbNodes,
448 Standard_Integer& theNbTris,
449 V3d_TypeOfOrientation theDirection) const
451 const gp_Dir aDir = V3d::GetProjAxis (theDirection);
452 const gp_Pnt aPos = aDir.XYZ() * (mySize * 0.5 + myBoxFacetExtension);
453 const gp_Ax2 aPosition (aPos, aDir.Reversed());
455 gp_Ax3 aSystem (aPosition);
457 aTrsf.SetTransformation (aSystem, gp_Ax3());
459 createRoundRectangleTriangles (theTris, theNbNodes, theNbTris,
460 gp_XY (mySize, mySize), myRoundRadius * mySize, aTrsf);
463 //=======================================================================
464 //function : createBoxEdgeTriangles
466 //=======================================================================
467 void AIS_ViewCube::createBoxEdgeTriangles (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
468 Standard_Integer& theNbNodes,
469 Standard_Integer& theNbTris,
470 V3d_TypeOfOrientation theDirection) const
472 const Standard_Real aThickness = Max (myBoxFacetExtension * gp_XY (1.0, 1.0).Modulus() - myBoxEdgeGap, myBoxEdgeMinSize);
474 const gp_Dir aDir = V3d::GetProjAxis (theDirection);
475 const gp_Pnt aPos = aDir.XYZ() * (mySize * 0.5 * gp_XY (1.0, 1.0).Modulus() + myBoxFacetExtension * Cos (M_PI_4));
476 const gp_Ax2 aPosition (aPos, aDir.Reversed());
478 gp_Ax3 aSystem (aPosition);
480 aTrsf.SetTransformation (aSystem, gp_Ax3());
482 createRoundRectangleTriangles (theTris, theNbNodes, theNbTris,
483 gp_XY (aThickness, mySize), myRoundRadius * mySize, aTrsf);
486 //=======================================================================
487 //function : createBoxCornerTriangles
489 //=======================================================================
490 void AIS_ViewCube::createBoxCornerTriangles (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
491 Standard_Integer& theNbNodes,
492 Standard_Integer& theNbTris,
493 V3d_TypeOfOrientation theDir) const
495 const Standard_Real aHSize = mySize * 0.5;
496 const gp_Dir aDir = V3d::GetProjAxis (theDir);
497 const gp_XYZ aHSizeDir = aDir.XYZ() * (aHSize * gp_Vec (1.0, 1.0, 1.0).Magnitude());
498 const Standard_Integer aVertFirst = !theTris.IsNull() ? theTris->VertexNumber() : 0;
499 if (myRoundRadius > 0.0)
501 theNbNodes += THE_NB_DISK_SLICES + 1;
502 theNbTris += THE_NB_DISK_SLICES + 1;
503 if (theTris.IsNull())
508 const Standard_Real anEdgeHWidth = myBoxFacetExtension * gp_XY (1.0, 1.0).Modulus() * 0.5;
509 const Standard_Real aHeight = anEdgeHWidth * Sqrt (2.0 / 3.0); // tetrahedron height
510 const gp_Pnt aPos = aDir.XYZ() * (aHSize * gp_Vec (1.0, 1.0, 1.0).Magnitude() + aHeight);
511 const gp_Ax2 aPosition (aPos, aDir.Reversed());
512 gp_Ax3 aSystem (aPosition);
514 aTrsf.SetTransformation (aSystem, gp_Ax3());
515 const Standard_Real aRadius = Max (myBoxFacetExtension * 0.5 / Cos (M_PI_4), myCornerMinSize);
517 theTris->AddVertex (gp_Pnt (0.0, 0.0, 0.0).Transformed (aTrsf));
518 for (Standard_Integer aNodeIter = 0; aNodeIter < THE_NB_DISK_SLICES; ++aNodeIter)
520 const Standard_Real anAngle = NCollection_Lerp<Standard_Real>::Interpolate (2.0 * M_PI, 0.0, Standard_Real(aNodeIter) / Standard_Real(THE_NB_DISK_SLICES));
521 theTris->AddVertex (gp_Pnt (aRadius * Cos (anAngle), aRadius * Sin (anAngle), 0.0).Transformed (aTrsf));
523 theTris->AddTriangleFanEdges (aVertFirst + 1, theTris->VertexNumber(), true);
529 if (theTris.IsNull())
534 theTris->AddVertex (aHSizeDir + myBoxFacetExtension * gp_Dir (aDir.X(), 0.0, 0.0).XYZ());
535 theTris->AddVertex (aHSizeDir + myBoxFacetExtension * gp_Dir (0.0, aDir.Y(), 0.0).XYZ());
536 theTris->AddVertex (aHSizeDir + myBoxFacetExtension * gp_Dir (0.0, 0.0, aDir.Z()).XYZ());
538 const gp_XYZ aNode1 = theTris->Vertice (aVertFirst + 1).XYZ();
539 const gp_XYZ aNode2 = theTris->Vertice (aVertFirst + 2).XYZ();
540 const gp_XYZ aNode3 = theTris->Vertice (aVertFirst + 3).XYZ();
541 const gp_XYZ aNormTri = ((aNode2 - aNode1).Crossed (aNode3 - aNode1));
542 if (aNormTri.Dot (aDir.XYZ()) < 0.0)
544 theTris->AddTriangleEdges (aVertFirst + 1, aVertFirst + 3, aVertFirst + 2);
548 theTris->AddTriangleEdges (aVertFirst + 1, aVertFirst + 2, aVertFirst + 3);
552 for (Standard_Integer aVertIter = aVertFirst + 1; aVertIter <= theTris->VertexNumber(); ++aVertIter)
554 theTris->SetVertexNormal (aVertIter, aDir);
558 //=======================================================================
561 //=======================================================================
562 void AIS_ViewCube::Compute (const Handle(PrsMgr_PresentationManager3d)& ,
563 const Handle(Prs3d_Presentation)& thePrs,
564 const Standard_Integer theMode)
566 thePrs->SetInfiniteState (true);
572 const gp_Pnt aLocation = (mySize * 0.5 + myBoxFacetExtension + myAxesPadding) * gp_XYZ (-1.0, -1.0, -1.0);
577 const Standard_Real anAxisSize = mySize + 2.0 * myBoxFacetExtension + myAxesPadding;
578 const Handle(Prs3d_DatumAspect)& aDatumAspect = myDrawer->DatumAspect();
579 for (Standard_Integer anAxisIter = Prs3d_DP_XAxis; anAxisIter <= Prs3d_DP_ZAxis; ++anAxisIter)
581 const Prs3d_DatumParts aPart = (Prs3d_DatumParts )anAxisIter;
582 if (!aDatumAspect->DrawDatumPart (aPart))
590 case Prs3d_DP_XAxis: anAx1 = gp_Ax1 (aLocation, gp::DX()); break;
591 case Prs3d_DP_YAxis: anAx1 = gp_Ax1 (aLocation, gp::DY()); break;
592 case Prs3d_DP_ZAxis: anAx1 = gp_Ax1 (aLocation, gp::DZ()); break;
596 Handle(Graphic3d_Group) anAxisGroup = thePrs->NewGroup();
597 anAxisGroup->SetGroupPrimitivesAspect (aDatumAspect->ShadingAspect (aPart)->Aspect());
599 const Standard_Real anArrowLength = 0.2 * anAxisSize;
600 Handle(Graphic3d_ArrayOfTriangles) aTriangleArray = Prs3d_Arrow::DrawShaded (anAx1, 1.0, anAxisSize, 3.0, anArrowLength, THE_NB_ARROW_FACETTES);
601 anAxisGroup->AddPrimitiveArray (aTriangleArray);
603 TCollection_AsciiString anAxisLabel;
604 if (aDatumAspect->ToDrawLabels()
605 && myAxesLabels.Find (aPart, anAxisLabel)
606 && !anAxisLabel.IsEmpty())
608 Handle(Graphic3d_Group) anAxisLabelGroup = thePrs->NewGroup();
609 gp_Pnt aTextOrigin = anAx1.Location().Translated (gp_Vec (anAx1.Direction().X() * (anAxisSize + anArrowLength),
610 anAx1.Direction().Y() * (anAxisSize + anArrowLength),
611 anAx1.Direction().Z() * (anAxisSize + anArrowLength)));
612 Prs3d_Text::Draw (anAxisLabelGroup, aDatumAspect->TextAspect(), TCollection_ExtendedString (anAxisLabel), aTextOrigin);
618 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
619 Handle(Prs3d_ShadingAspect) anAspectCen = new Prs3d_ShadingAspect();
620 anAspectCen->SetColor (Quantity_NOC_WHITE);
621 aGroup->SetGroupPrimitivesAspect (anAspectCen->Aspect());
622 Prs3d_ToolSphere aTool (4.0, THE_NB_DISK_SLICES, THE_NB_DISK_SLICES);
624 aTrsf.SetTranslation (gp_Vec (gp::Origin(), aLocation));
625 Handle(Graphic3d_ArrayOfTriangles) aCenterArray;
626 aTool.FillArray (aCenterArray, aTrsf);
627 aGroup->AddPrimitiveArray (aCenterArray);
633 Standard_Integer aNbNodes = 0, aNbTris = 0;
634 for (Standard_Integer aPartIter = V3d_Xpos; aPartIter <= Standard_Integer(V3d_Zneg); ++aPartIter)
636 createBoxPartTriangles (Handle(Graphic3d_ArrayOfTriangles)(), aNbNodes, aNbTris, (V3d_TypeOfOrientation )aPartIter);
640 Handle(Graphic3d_ArrayOfTriangles) aTris = new Graphic3d_ArrayOfTriangles (aNbNodes, aNbTris * 3, Graphic3d_ArrayFlags_VertexNormal);
641 Handle(Graphic3d_ArrayOfSegments) aSegs;
642 if (myDrawer->FaceBoundaryDraw())
644 aSegs = new Graphic3d_ArrayOfSegments (aNbNodes, aNbNodes * 2, Graphic3d_ArrayFlags_None);
646 aNbNodes = aNbTris = 0;
647 for (Standard_Integer aPartIter = V3d_Xpos; aPartIter <= Standard_Integer(V3d_Zneg); ++aPartIter)
649 Standard_Integer aTriNodesFrom = aTris->VertexNumber();
650 const Standard_Integer aTriFrom = aNbTris;
651 createBoxPartTriangles (aTris, aNbNodes, aNbTris, (V3d_TypeOfOrientation )aPartIter);
657 const Standard_Integer aFirstNode = aSegs->VertexNumber();
658 for (Standard_Integer aVertIter = (aNbTris - aTriFrom) > 2 ? aTriNodesFrom + 2 : aTriNodesFrom + 1; // skip triangle fan center
659 aVertIter <= aTris->VertexNumber(); ++aVertIter)
661 aSegs->AddVertex (aTris->Vertice (aVertIter));
663 aSegs->AddPolylineEdges (aFirstNode + 1, aSegs->VertexNumber(), true);
667 Handle(Graphic3d_Group) aGroupSides = thePrs->NewGroup();
668 aGroupSides->SetClosed (true); // should be replaced by forced back-face culling aspect
669 aGroupSides->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
670 aGroupSides->AddPrimitiveArray (aTris);
675 Handle(Graphic3d_Group) aGroupSegs = thePrs->NewGroup();
676 aGroupSegs->SetGroupPrimitivesAspect (myDrawer->FaceBoundaryAspect()->Aspect());
677 aGroupSegs->AddPrimitiveArray (aSegs);
681 // Display box sides labels
682 Handle(Graphic3d_Group) aTextGroup = thePrs->NewGroup();
683 aTextGroup->SetGroupPrimitivesAspect (myDrawer->TextAspect()->Aspect());
684 for (Standard_Integer aPartIter = V3d_Xpos; aPartIter <= Standard_Integer(V3d_Zneg); ++aPartIter)
686 const V3d_TypeOfOrientation anOrient = (V3d_TypeOfOrientation )aPartIter;
688 TCollection_AsciiString aLabel;
689 if (!myBoxSideLabels.Find (anOrient, aLabel)
695 const gp_Dir aDir = V3d::GetProjAxis (anOrient);
696 gp_Dir anUp = myIsYup ? gp::DY() : gp::DZ();
699 if (anOrient == V3d_Ypos
700 || anOrient == V3d_Yneg)
707 if (anOrient == V3d_Zpos)
711 else if (anOrient == V3d_Zneg)
717 const Standard_Real anOffset = 2.0; // extra offset to avoid overlapping with triangulation
718 const gp_Pnt aPos = aDir.XYZ() * (mySize * 0.5 + myBoxFacetExtension + anOffset);
719 const gp_Ax2 aPosition (aPos, aDir, anUp.Crossed (aDir));
720 Prs3d_Text::Draw (aTextGroup, myDrawer->TextAspect(), aLabel, aPosition);
726 Standard_Integer aNbNodes = 0, aNbTris = 0;
727 for (Standard_Integer aPartIter = V3d_XposYpos; aPartIter <= Standard_Integer(V3d_YposZneg); ++aPartIter)
729 createBoxPartTriangles (Handle(Graphic3d_ArrayOfTriangles)(), aNbNodes, aNbTris, (V3d_TypeOfOrientation )aPartIter);
733 Handle(Graphic3d_ArrayOfTriangles) aTris = new Graphic3d_ArrayOfTriangles (aNbNodes, aNbTris * 3, Graphic3d_ArrayFlags_VertexNormal);
734 aNbNodes = aNbTris = 0;
735 for (Standard_Integer aPartIter = V3d_XposYpos; aPartIter <= Standard_Integer(V3d_YposZneg); ++aPartIter)
737 const V3d_TypeOfOrientation anOrient = (V3d_TypeOfOrientation )aPartIter;
738 createBoxPartTriangles (aTris, aNbNodes, aNbTris, anOrient);
741 Handle(Graphic3d_Group) aGroupEdges = thePrs->NewGroup();
742 aGroupEdges->SetClosed (true);
743 aGroupEdges->SetGroupPrimitivesAspect (myBoxEdgeAspect->Aspect());
744 aGroupEdges->AddPrimitiveArray (aTris);
748 // Display box corners
750 Standard_Integer aNbNodes = 0, aNbTris = 0;
751 for (Standard_Integer aPartIter = V3d_XposYposZpos; aPartIter <= Standard_Integer(V3d_XnegYnegZneg); ++aPartIter)
753 createBoxPartTriangles (Handle(Graphic3d_ArrayOfTriangles)(), aNbNodes, aNbTris, (V3d_TypeOfOrientation )aPartIter);
757 Handle(Graphic3d_ArrayOfTriangles) aTris = new Graphic3d_ArrayOfTriangles (aNbNodes, aNbTris * 3, Graphic3d_ArrayFlags_VertexNormal);
758 aNbNodes = aNbTris = 0;
759 for (Standard_Integer aPartIter = V3d_XposYposZpos; aPartIter <= Standard_Integer(V3d_XnegYnegZneg); ++aPartIter)
761 const V3d_TypeOfOrientation anOrient = (V3d_TypeOfOrientation )aPartIter;
762 createBoxPartTriangles (aTris, aNbNodes, aNbTris, anOrient);
765 Handle(Graphic3d_Group) aGroupCorners = thePrs->NewGroup();
766 aGroupCorners->SetClosed (true);
767 aGroupCorners->SetGroupPrimitivesAspect (myBoxCornerAspect->Aspect());
768 aGroupCorners->AddPrimitiveArray (aTris);
773 //=======================================================================
774 //function : ComputeSelection
776 //=======================================================================
777 void AIS_ViewCube::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
778 const Standard_Integer theMode)
785 for (Standard_Integer aPartIter = 0; aPartIter <= Standard_Integer(V3d_XnegYnegZneg); ++aPartIter)
787 const V3d_TypeOfOrientation anOri = (V3d_TypeOfOrientation )aPartIter;
788 Standard_Integer aNbNodes = 0, aNbTris = 0;
789 createBoxPartTriangles (Handle(Graphic3d_ArrayOfTriangles)(), aNbNodes, aNbTris, anOri);
795 Handle(Graphic3d_ArrayOfTriangles) aTris = new Graphic3d_ArrayOfTriangles (aNbNodes, aNbTris * 3, Graphic3d_ArrayFlags_None);
796 aNbNodes = aNbTris = 0;
797 createBoxPartTriangles (aTris, aNbNodes, aNbTris, anOri);
799 Standard_Integer aSensitivity = 2;
800 if (IsBoxCorner (anOri))
804 else if (IsBoxEdge (anOri))
808 Handle(AIS_ViewCubeOwner) anOwner = new AIS_ViewCubeOwner (this, anOri);
809 Handle(AIS_ViewCubeSensitive) aTriSens = new AIS_ViewCubeSensitive (anOwner, aTris);
810 aTriSens->SetSensitivityFactor (aSensitivity);
811 theSelection->Add (aTriSens);
815 //=======================================================================
816 //function : HasAnimation
818 //=======================================================================
819 Standard_Boolean AIS_ViewCube::HasAnimation() const
821 return !myViewAnimation->IsStopped();
824 //=======================================================================
825 //function : viewFitAll
827 //=======================================================================
828 void AIS_ViewCube::viewFitAll (const Handle(V3d_View)& theView,
829 const Handle(Graphic3d_Camera)& theCamera)
831 Bnd_Box aBndBox = myToFitSelected ? GetContext()->BoundingBoxOfSelection() : theView->View()->MinMaxValues();
835 aBndBox = theView->View()->MinMaxValues();
837 if (!aBndBox.IsVoid())
839 theView->FitMinMax (theCamera, aBndBox, 0.01, 10.0 * Precision::Confusion());
843 //=======================================================================
844 //function : StartAnimation
846 //=======================================================================
847 void AIS_ViewCube::StartAnimation (const Handle(AIS_ViewCubeOwner)& theOwner)
849 Handle(V3d_View) aView = GetContext()->LastActiveView();
850 if (theOwner.IsNull()
856 myStartState->Copy (aView->Camera());
857 myEndState ->Copy (aView->Camera());
861 Handle(Graphic3d_Camera) aBackupCamera = aView->Camera();
862 const bool wasImmediateUpdate = aView->SetImmediateUpdate (false);
863 aView->SetCamera (myEndState);
864 aView->SetProj (theOwner->MainOrientation(), myIsYup);
865 aView->SetCamera (aBackupCamera);
866 aView->SetImmediateUpdate (wasImmediateUpdate);
869 const gp_Dir aNewDir = myEndState->Direction();
870 if (!myToResetCameraUp
871 && !aNewDir.IsEqual (myStartState->Direction(), Precision::Angular()))
873 // find the Up direction closest to current instead of default one
874 const gp_Ax1 aNewDirAx1 (gp::Origin(), aNewDir);
875 const gp_Dir anOldUp = myStartState->Up();
876 const gp_Dir anUpList[4] =
879 myEndState->Up().Rotated (aNewDirAx1, M_PI_2),
880 myEndState->Up().Rotated (aNewDirAx1, M_PI),
881 myEndState->Up().Rotated (aNewDirAx1, M_PI * 1.5),
884 Standard_Real aBestAngle = Precision::Infinite();
886 for (Standard_Integer anUpIter = 0; anUpIter < 4; ++anUpIter)
888 Standard_Real anAngle = anUpList[anUpIter].Angle (anOldUp);
889 if (aBestAngle > anAngle)
891 aBestAngle = anAngle;
892 anUpBest = anUpList[anUpIter];
895 myEndState->SetUp (anUpBest);
898 viewFitAll (aView, myEndState);
901 myViewAnimation->SetView (aView);
902 myViewAnimation->SetCameraStart (myStartState);
903 myViewAnimation->SetCameraEnd (myEndState);
904 myViewAnimation->SetOwnDuration (myDuration);
905 myViewAnimation->StartTimer (0.0, 1.0, true, false);
908 //=======================================================================
909 //function : updateAnimation
911 //=======================================================================
912 Standard_Boolean AIS_ViewCube::updateAnimation()
914 const Standard_Real aPts = myViewAnimation->UpdateTimer();
915 if (aPts >= myDuration)
917 myViewAnimation->Stop();
918 onAnimationFinished();
919 myViewAnimation->SetView (Handle(V3d_View)());
920 return Standard_False;
922 return Standard_True;
925 //=======================================================================
926 //function : UpdateAnimation
928 //=======================================================================
929 Standard_Boolean AIS_ViewCube::UpdateAnimation (const Standard_Boolean theToUpdate)
931 Handle(V3d_View) aView = myViewAnimation->View();
933 || !updateAnimation())
935 return Standard_False;
941 aView->IsInvalidated() ? aView->Redraw() : aView->RedrawImmediate();
945 return Standard_True;
948 //=======================================================================
949 //function : HandleClick
951 //=======================================================================
952 void AIS_ViewCube::HandleClick (const Handle(AIS_ViewCubeOwner)& theOwner)
954 if (!myToAutoStartAnim)
959 StartAnimation (theOwner);
960 if (!myIsFixedAnimation)
964 for (; HasAnimation(); )
966 UpdateAnimation (true);
970 //=======================================================================
971 //function : HilightOwnerWithColor
973 //=======================================================================
974 void AIS_ViewCube::HilightOwnerWithColor (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
975 const Handle(Prs3d_Drawer)& theStyle,
976 const Handle(SelectMgr_EntityOwner)& theOwner)
978 if (theOwner.IsNull()
979 || !thePrsMgr->IsImmediateModeOn())
984 const Graphic3d_ZLayerId aLayer = theStyle->ZLayer() != Graphic3d_ZLayerId_UNKNOWN ? theStyle->ZLayer() : myDrawer->ZLayer();
985 const AIS_ViewCubeOwner* aCubeOwner = dynamic_cast<AIS_ViewCubeOwner* >(theOwner.get());
987 Handle(Prs3d_Presentation) aHiPrs = GetHilightPresentation (thePrsMgr);
989 aHiPrs->CStructure()->ViewAffinity = thePrsMgr->StructureManager()->ObjectAffinity (Handle(Standard_Transient)(this));
990 aHiPrs->SetTransformPersistence (TransformPersistence());
991 aHiPrs->SetZLayer (aLayer);
994 Handle(Graphic3d_Group) aGroup = aHiPrs->NewGroup();
995 aGroup->SetGroupPrimitivesAspect (theStyle->ShadingAspect()->Aspect());
996 Standard_Integer aNbNodes = 0, aNbTris = 0;
997 createBoxPartTriangles (Handle(Graphic3d_ArrayOfTriangles)(), aNbNodes, aNbTris, aCubeOwner->MainOrientation());
1000 Handle(Graphic3d_ArrayOfTriangles) aTris = new Graphic3d_ArrayOfTriangles (aNbNodes, aNbTris * 3, Graphic3d_ArrayFlags_None);
1001 aNbNodes = aNbTris = 0;
1002 createBoxPartTriangles (aTris, aNbNodes, aNbTris, aCubeOwner->MainOrientation());
1003 aGroup->AddPrimitiveArray (aTris);
1007 if (thePrsMgr->IsImmediateModeOn())
1009 thePrsMgr->AddToImmediateList (aHiPrs);
1013 //=======================================================================
1014 //function : HilightSelected
1016 //=======================================================================
1017 void AIS_ViewCube::HilightSelected (const Handle(PrsMgr_PresentationManager3d)& ,
1018 const SelectMgr_SequenceOfOwner& theSeq)
1020 // this method should never be called since AIS_InteractiveObject::HandleClick() has been overridden
1021 if (theSeq.Size() == 1)
1023 //HandleClick (Handle(AIS_ViewCubeOwner)::DownCast (theSeq.First()));