1 // Created on: 2015-12-23
2 // Created by: Anastasia BORISOVA
3 // Copyright (c) 2015 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_Manipulator.hxx>
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_ManipulatorOwner.hxx>
20 #include <gce_MakeDir.hxx>
21 #include <GeomAPI_ExtremaCurveCurve.hxx>
22 #include <GeomAPI_IntCS.hxx>
23 #include <Geom_Circle.hxx>
24 #include <Geom_Line.hxx>
25 #include <Geom_Plane.hxx>
26 #include <Geom_Transformation.hxx>
27 #include <Prs3d_Arrow.hxx>
28 #include <Prs3d_Root.hxx>
29 #include <Prs3d_ShadingAspect.hxx>
30 #include <Prs3d_ToolDisk.hxx>
31 #include <Prs3d_ToolSphere.hxx>
32 #include <Select3D_SensitiveCircle.hxx>
33 #include <Select3D_SensitivePoint.hxx>
34 #include <Select3D_SensitiveSegment.hxx>
35 #include <Select3D_SensitiveTriangulation.hxx>
36 #include <Select3D_SensitivePrimitiveArray.hxx>
37 #include <SelectMgr_SequenceOfOwner.hxx>
38 #include <TColgp_Array1OfPnt.hxx>
39 #include <V3d_View.hxx>
41 IMPLEMENT_STANDARD_HANDLE (AIS_Manipulator, AIS_InteractiveObject)
42 IMPLEMENT_STANDARD_RTTIEXT(AIS_Manipulator, AIS_InteractiveObject)
44 IMPLEMENT_HSEQUENCE(AIS_ManipulatorObjectSequence)
48 //! Return Ax1 for specified direction of Ax2.
49 static gp_Ax1 getAx1FromAx2Dir (const gp_Ax2& theAx2,
54 case 0: return gp_Ax1 (theAx2.Location(), theAx2.XDirection());
55 case 1: return gp_Ax1 (theAx2.Location(), theAx2.YDirection());
56 case 2: return theAx2.Axis();
58 throw Standard_ProgramError ("AIS_Manipulator - Invalid axis index");
62 //=======================================================================
65 //=======================================================================
66 void AIS_Manipulator::init()
68 // Create axis in the default coordinate system. The custom position is applied in local transformation.
69 myAxes[0] = Axis (gp::OX(), Quantity_NOC_RED);
70 myAxes[1] = Axis (gp::OY(), Quantity_NOC_GREEN);
71 myAxes[2] = Axis (gp::OZ(), Quantity_NOC_BLUE1);
73 Graphic3d_MaterialAspect aShadingMaterial;
74 aShadingMaterial.SetReflectionModeOff (Graphic3d_TOR_SPECULAR);
75 aShadingMaterial.SetMaterialType (Graphic3d_MATERIAL_ASPECT);
77 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
78 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_SOLID);
79 myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
80 myDrawer->ShadingAspect()->SetMaterial (aShadingMaterial);
82 Graphic3d_MaterialAspect aHilightMaterial;
83 aHilightMaterial.SetColor (Quantity_NOC_AZURE);
84 aHilightMaterial.SetReflectionModeOff (Graphic3d_TOR_AMBIENT);
85 aHilightMaterial.SetReflectionModeOff (Graphic3d_TOR_DIFFUSE);
86 aHilightMaterial.SetReflectionModeOff (Graphic3d_TOR_SPECULAR);
87 aHilightMaterial.SetReflectionModeOff (Graphic3d_TOR_EMISSION);
88 aHilightMaterial.SetMaterialType (Graphic3d_MATERIAL_ASPECT);
90 myHighlightAspect = new Prs3d_ShadingAspect();
91 myHighlightAspect->Aspect()->SetInteriorStyle (Aspect_IS_SOLID);
92 myHighlightAspect->SetMaterial (aHilightMaterial);
95 SetZLayer (Graphic3d_ZLayerId_Topmost);
98 //=======================================================================
99 //function : getHighlightPresentation
101 //=======================================================================
102 Handle(Prs3d_Presentation) AIS_Manipulator::getHighlightPresentation (const Handle(SelectMgr_EntityOwner)& theOwner) const
104 Handle(Prs3d_Presentation) aDummyPrs;
105 Handle(AIS_ManipulatorOwner) anOwner = Handle(AIS_ManipulatorOwner)::DownCast (theOwner);
106 if (anOwner.IsNull())
111 switch (anOwner->Mode())
113 case AIS_MM_Translation: return myAxes[anOwner->Index()].TranslatorHighlightPrs();
114 case AIS_MM_Rotation : return myAxes[anOwner->Index()].RotatorHighlightPrs();
115 case AIS_MM_Scaling : return myAxes[anOwner->Index()].ScalerHighlightPrs();
116 case AIS_MM_None : break;
122 //=======================================================================
123 //function : getGroup
125 //=======================================================================
126 Handle(Graphic3d_Group) AIS_Manipulator::getGroup (const Standard_Integer theIndex, const AIS_ManipulatorMode theMode) const
128 Handle(Graphic3d_Group) aDummyGroup;
130 if (theIndex < 0 || theIndex > 2)
137 case AIS_MM_Translation: return myAxes[theIndex].TranslatorGroup();
138 case AIS_MM_Rotation : return myAxes[theIndex].RotatorGroup();
139 case AIS_MM_Scaling : return myAxes[theIndex].ScalerGroup();
140 case AIS_MM_None : break;
146 //=======================================================================
147 //function : Constructor
149 //=======================================================================
150 AIS_Manipulator::AIS_Manipulator()
151 : myPosition (gp::XOY()),
153 myCurrentMode (AIS_MM_None),
154 myIsActivationOnDetection (Standard_False),
155 myIsZoomPersistentMode (Standard_True),
156 myHasStartedTransformation (Standard_False),
157 myStartPosition (gp::XOY()),
158 myStartPick (0.0, 0.0, 0.0),
162 SetMutable (Standard_True);
163 SetDisplayMode (AIS_Shaded);
167 //=======================================================================
168 //function : Constructor
170 //=======================================================================
171 AIS_Manipulator::AIS_Manipulator (const gp_Ax2& thePosition)
172 : myPosition (thePosition),
174 myCurrentMode (AIS_MM_None),
175 myIsActivationOnDetection (Standard_False),
176 myIsZoomPersistentMode (Standard_True),
177 myHasStartedTransformation (Standard_False),
178 myStartPosition (gp::XOY()),
179 myStartPick (0.0, 0.0, 0.0),
183 SetMutable (Standard_True);
184 SetDisplayMode (AIS_Shaded);
188 //=======================================================================
191 //=======================================================================
192 void AIS_Manipulator::SetPart (const Standard_Integer theAxisIndex, const AIS_ManipulatorMode theMode, const Standard_Boolean theIsEnabled)
194 Standard_ProgramError_Raise_if (theAxisIndex < 0 || theAxisIndex > 2, "AIS_Manipulator::SetMode(): axis index should be between 0 and 2");
197 case AIS_MM_Translation:
198 myAxes[theAxisIndex].SetTranslation (theIsEnabled);
201 case AIS_MM_Rotation:
202 myAxes[theAxisIndex].SetRotation (theIsEnabled);
206 myAxes[theAxisIndex].SetScaling (theIsEnabled);
214 //=======================================================================
215 //function : EnableMode
217 //=======================================================================
218 void AIS_Manipulator::EnableMode (const AIS_ManipulatorMode theMode)
225 const Handle(AIS_InteractiveContext)& aContext = GetContext();
226 if (aContext.IsNull())
231 aContext->Activate (this, theMode);
234 //=======================================================================
235 //function : attachToBox
237 //=======================================================================
238 void AIS_Manipulator::attachToBox (const Bnd_Box& theBox)
245 Standard_Real anXmin = 0.0, anYmin = 0.0, aZmin = 0.0, anXmax = 0.0, anYmax = 0.0, aZmax = 0.0;
246 theBox.Get (anXmin, anYmin, aZmin, anXmax, anYmax, aZmax);
248 gp_Ax2 aPosition = gp::XOY();
249 aPosition.SetLocation (gp_Pnt ((anXmin + anXmax) * 0.5, (anYmin + anYmax) * 0.5, (aZmin + aZmax) * 0.5));
250 SetPosition (aPosition);
253 //=======================================================================
254 //function : adjustSize
256 //=======================================================================
257 void AIS_Manipulator::adjustSize (const Bnd_Box& theBox)
259 Standard_Real aXmin = 0., aYmin = 0., aZmin = 0., aXmax = 0., aYmax = 0., aZmax = 0.0;
260 theBox.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
261 Standard_Real aXSize = aXmax - aXmin;
262 Standard_Real aYSize = aYmax - aYmin;
263 Standard_Real aZSize = aZmax - aZmin;
265 SetSize ((Standard_ShortReal) (Max (aXSize, Max (aYSize, aZSize)) * 0.5));
268 //=======================================================================
271 //=======================================================================
272 void AIS_Manipulator::Attach (const Handle(AIS_InteractiveObject)& theObject, const OptionsForAttach& theOptions)
274 if (theObject->IsKind (STANDARD_TYPE(AIS_Manipulator)))
279 Handle(AIS_ManipulatorObjectSequence) aSeq = new AIS_ManipulatorObjectSequence();
280 aSeq->Append (theObject);
281 Attach (aSeq, theOptions);
284 //=======================================================================
287 //=======================================================================
288 void AIS_Manipulator::Attach (const Handle(AIS_ManipulatorObjectSequence)& theObjects, const OptionsForAttach& theOptions)
290 if (theObjects->Size() < 1)
295 SetOwner (theObjects);
297 const Handle(AIS_InteractiveObject)& aCurObject = theObjects->Value (theObjects->Lower());
298 aCurObject->BoundingBox (aBox);
300 if (theOptions.AdjustPosition)
305 if (theOptions.AdjustSize)
310 const Handle(AIS_InteractiveContext)& aContext = Object()->GetContext();
311 if (!aContext.IsNull())
313 if (!aContext->IsDisplayed (this))
315 aContext->Display (this, Standard_False);
319 aContext->Update (this, Standard_False);
320 aContext->RecomputeSelectionOnly (this);
323 aContext->Load (this);
326 if (theOptions.EnableModes)
328 EnableMode (AIS_MM_Rotation);
329 EnableMode (AIS_MM_Translation);
330 EnableMode (AIS_MM_Scaling);
334 //=======================================================================
337 //=======================================================================
338 void AIS_Manipulator::Detach()
340 DeactivateCurrentMode();
347 Handle(AIS_InteractiveObject) anObject = Object();
348 const Handle(AIS_InteractiveContext)& aContext = anObject->GetContext();
349 if (!aContext.IsNull())
351 aContext->Remove (this, Standard_False);
357 //=======================================================================
360 //=======================================================================
361 Handle(AIS_ManipulatorObjectSequence) AIS_Manipulator::Objects() const
363 return Handle(AIS_ManipulatorObjectSequence)::DownCast (GetOwner());
366 //=======================================================================
369 //=======================================================================
370 Handle(AIS_InteractiveObject) AIS_Manipulator::Object (const Standard_Integer theIndex) const
372 Handle(AIS_ManipulatorObjectSequence) anOwner = Handle(AIS_ManipulatorObjectSequence)::DownCast (GetOwner());
374 Standard_ProgramError_Raise_if (theIndex < anOwner->Lower() || theIndex > anOwner->Upper(), "AIS_Manipulator::Object(): wrong index value");
376 if (anOwner.IsNull() || anOwner->IsEmpty())
381 return anOwner->Value (theIndex);
384 //=======================================================================
387 //=======================================================================
388 Handle(AIS_InteractiveObject) AIS_Manipulator::Object() const
393 //=======================================================================
394 //function : ObjectTransformation
396 //=======================================================================
397 Standard_Boolean AIS_Manipulator::ObjectTransformation (const Standard_Integer theMaxX, const Standard_Integer theMaxY,
398 const Handle(V3d_View)& theView, gp_Trsf& theTrsf)
400 // Initialize start reference data
401 if (!myHasStartedTransformation)
403 myStartTrsfs.Clear();
404 Handle(AIS_ManipulatorObjectSequence) anObjects = Objects();
405 for (AIS_ManipulatorObjectSequence::Iterator anObjIter (*anObjects); anObjIter.More(); anObjIter.Next())
407 myStartTrsfs.Append (anObjIter.Value()->LocalTransformation());
409 myStartPosition = myPosition;
412 // Get 3d point with projection vector
413 Graphic3d_Vec3d anInputPoint, aProj;
414 theView->ConvertWithProj (theMaxX, theMaxY, anInputPoint.x(), anInputPoint.y(), anInputPoint.z(), aProj.x(), aProj.y(), aProj.z());
415 const gp_Lin anInputLine (gp_Pnt (anInputPoint.x(), anInputPoint.y(), anInputPoint.z()), gp_Dir (aProj.x(), aProj.y(), aProj.z()));
416 switch (myCurrentMode)
418 case AIS_MM_Translation:
420 gp_Lin aLine (myStartPick, myAxes[myCurrentIndex].Position().Direction());
421 Handle(Geom_Curve) anInputCurve = new Geom_Line (anInputLine);
422 Handle(Geom_Curve) aCurve = new Geom_Line (aLine);
423 GeomAPI_ExtremaCurveCurve anExtrema (anInputCurve, aCurve);
425 anExtrema.NearestPoints (aP1, aP2);
426 const gp_Pnt aNewPosition = aP2;
428 if (!myHasStartedTransformation)
430 myStartPick = aNewPosition;
431 myHasStartedTransformation = Standard_True;
432 return Standard_True;
435 if (aNewPosition.Distance (myStartPick) < Precision::Confusion())
437 return Standard_False;
441 aNewTrsf.SetTranslation (gp_Vec(myStartPick, aNewPosition));
443 return Standard_True;
445 case AIS_MM_Rotation:
447 const gp_Pnt aPosLoc = myStartPosition.Location();
448 const gp_Ax1 aCurrAxis = getAx1FromAx2Dir (myStartPosition, myCurrentIndex);
450 Handle(Geom_Curve) anInputCurve = new Geom_Line (anInputLine);
451 Handle(Geom_Surface) aSurface = new Geom_Plane (aPosLoc, aCurrAxis.Direction());
452 GeomAPI_IntCS aIntersector (anInputCurve, aSurface);
453 if (!aIntersector.IsDone() || aIntersector.NbPoints() < 1)
455 return Standard_False;
458 const gp_Pnt aNewPosition = aIntersector.Point (1);
459 if (!myHasStartedTransformation)
461 myStartPick = aNewPosition;
462 myHasStartedTransformation = Standard_True;
463 gp_Dir aStartAxis = gce_MakeDir (aPosLoc, myStartPick);
464 myPrevState = aStartAxis.AngleWithRef (gce_MakeDir(aPosLoc, aNewPosition), aCurrAxis.Direction());
465 return Standard_True;
468 if (aNewPosition.Distance (myStartPick) < Precision::Confusion())
470 return Standard_False;
473 gp_Dir aStartAxis = aPosLoc.IsEqual (myStartPick, Precision::Confusion())
474 ? getAx1FromAx2Dir (myStartPosition, (myCurrentIndex + 1) % 3).Direction()
475 : gce_MakeDir (aPosLoc, myStartPick);
477 gp_Dir aCurrentAxis = gce_MakeDir (aPosLoc, aNewPosition);
478 Standard_Real anAngle = aStartAxis.AngleWithRef (aCurrentAxis, aCurrAxis.Direction());
480 // Change value of an angle if it should have different sign.
481 if (anAngle * myPrevState < 0 && Abs (anAngle) < M_PI_2)
483 Standard_Real aSign = myPrevState > 0 ? -1.0 : 1.0;
484 anAngle = aSign * (M_PI * 2 - anAngle);
487 if (Abs (anAngle) < Precision::Confusion())
489 return Standard_False;
493 aNewTrsf.SetRotation (aCurrAxis, anAngle);
495 myPrevState = anAngle;
496 return Standard_True;
500 gp_Lin aLine (myStartPosition.Location(), myAxes[myCurrentIndex].Position().Direction());
501 Handle(Geom_Curve) anInputCurve = new Geom_Line (anInputLine);
502 Handle(Geom_Curve) aCurve = new Geom_Line (aLine);
503 GeomAPI_ExtremaCurveCurve anExtrema (anInputCurve, aCurve);
504 gp_Pnt aNewPosition, aTmp;
505 anExtrema.NearestPoints (aTmp, aNewPosition);
507 if (!myHasStartedTransformation)
509 myStartPick = aNewPosition;
510 myHasStartedTransformation = Standard_True;
511 return Standard_True;
514 if (aNewPosition.Distance (myStartPick) < Precision::Confusion()
515 || aNewPosition.Distance (myStartPosition.Location()) < Precision::Confusion())
517 return Standard_False;
520 Standard_Real aCoeff = myStartPosition.Location().Distance (aNewPosition)
521 / myStartPosition.Location().Distance (myStartPick);
523 aNewTrsf.SetScale (myPosition.Location(), aCoeff);
526 return Standard_True;
530 return Standard_False;
533 return Standard_False;
536 //=======================================================================
537 //function : StartTransform
539 //=======================================================================
540 void AIS_Manipulator::StartTransform (const Standard_Integer theX, const Standard_Integer theY, const Handle(V3d_View)& theView)
542 if (myHasStartedTransformation)
548 ObjectTransformation (theX, theY, theView, aTrsf);
551 //=======================================================================
552 //function : StopTransform
554 //=======================================================================
555 void AIS_Manipulator::StopTransform (const Standard_Boolean theToApply)
557 if (!IsAttached() || !myHasStartedTransformation)
562 myHasStartedTransformation = Standard_False;
568 Handle(AIS_ManipulatorObjectSequence) anObjects = Objects();
569 AIS_ManipulatorObjectSequence::Iterator anObjIter (*anObjects);
570 NCollection_Sequence<gp_Trsf>::Iterator aTrsfIter (myStartTrsfs);
571 for (; anObjIter.More(); anObjIter.Next(), aTrsfIter.Next())
573 anObjIter.ChangeValue()->SetLocalTransformation (aTrsfIter.Value());
575 SetPosition (myStartPosition);
578 //=======================================================================
579 //function : Transform
581 //=======================================================================
582 void AIS_Manipulator::Transform (const gp_Trsf& theTrsf)
584 if (!IsAttached() || !myHasStartedTransformation)
590 Handle(AIS_ManipulatorObjectSequence) anObjects = Objects();
591 AIS_ManipulatorObjectSequence::Iterator anObjIter (*anObjects);
592 NCollection_Sequence<gp_Trsf>::Iterator aTrsfIter (myStartTrsfs);
593 for (; anObjIter.More(); anObjIter.Next(), aTrsfIter.Next())
595 anObjIter.ChangeValue()->SetLocalTransformation (theTrsf * aTrsfIter.Value());
599 if ((myCurrentMode == AIS_MM_Translation && myBehaviorOnTransform.FollowTranslation)
600 || (myCurrentMode == AIS_MM_Rotation && myBehaviorOnTransform.FollowRotation))
602 gp_Pnt aPos = myStartPosition.Location().Transformed (theTrsf);
603 gp_Dir aVDir = myStartPosition.Direction().Transformed (theTrsf);
604 gp_Dir aXDir = myStartPosition.XDirection().Transformed (theTrsf);
605 SetPosition (gp_Ax2 (aPos, aVDir, aXDir));
609 //=======================================================================
610 //function : Transform
612 //=======================================================================
613 gp_Trsf AIS_Manipulator::Transform (const Standard_Integer thePX, const Standard_Integer thePY,
614 const Handle(V3d_View)& theView)
617 if (ObjectTransformation (thePX, thePY, theView, aTrsf))
625 //=======================================================================
626 //function : SetPosition
628 //=======================================================================
629 void AIS_Manipulator::SetPosition (const gp_Ax2& thePosition)
631 if (!myPosition.Location().IsEqual (thePosition.Location(), Precision::Confusion())
632 || !myPosition.Direction().IsEqual (thePosition.Direction(), Precision::Angular())
633 || !myPosition.XDirection().IsEqual (thePosition.XDirection(), Precision::Angular()))
635 myPosition = thePosition;
636 myAxes[0].SetPosition (getAx1FromAx2Dir (thePosition, 0));
637 myAxes[1].SetPosition (getAx1FromAx2Dir (thePosition, 1));
638 myAxes[2].SetPosition (getAx1FromAx2Dir (thePosition, 2));
639 updateTransformation();
643 //=======================================================================
644 //function : updateTransformation
645 //purpose : set local transformation to avoid graphics recomputation
646 //=======================================================================
647 void AIS_Manipulator::updateTransformation()
651 if (!myIsZoomPersistentMode)
653 aTrsf.SetTransformation (myPosition, gp::XOY());
657 const gp_Dir& aVDir = myPosition.Direction();
658 const gp_Dir& aXDir = myPosition.XDirection();
659 aTrsf.SetTransformation (gp_Ax2 (gp::Origin(), aVDir, aXDir), gp::XOY());
662 Handle(Geom_Transformation) aGeomTrsf = new Geom_Transformation (aTrsf);
663 // we explicitly call here setLocalTransformation() of the base class
664 // since AIS_Manipulator::setLocalTransformation() implementation throws exception
665 // as protection from external calls
666 AIS_InteractiveObject::setLocalTransformation (aGeomTrsf);
667 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
669 myAxes[anIt].Transform (aGeomTrsf);
672 if (myIsZoomPersistentMode)
674 if (TransformPersistence().IsNull()
675 || TransformPersistence()->Mode() != Graphic3d_TMF_ZoomPers
676 || !TransformPersistence()->AnchorPoint().IsEqual (myPosition.Location(), 0.0))
678 setTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_ZoomPers, myPosition.Location()));
683 //=======================================================================
686 //=======================================================================
687 void AIS_Manipulator::SetSize (const Standard_ShortReal theSideLength)
689 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
691 myAxes[anIt].SetSize (theSideLength);
697 //=======================================================================
700 //=======================================================================
701 void AIS_Manipulator::SetGap (const Standard_ShortReal theValue)
703 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
705 myAxes[anIt].SetIndent (theValue);
711 //=======================================================================
712 //function : DeactivateCurrentMode
714 //=======================================================================
715 void AIS_Manipulator::DeactivateCurrentMode()
717 if (!myIsActivationOnDetection)
719 Handle(Graphic3d_Group) aGroup = getGroup (myCurrentIndex, myCurrentMode);
725 Handle(Prs3d_ShadingAspect) anAspect = new Prs3d_ShadingAspect();
726 anAspect->Aspect()->SetInteriorStyle (Aspect_IS_SOLID);
727 anAspect->SetMaterial (myDrawer->ShadingAspect()->Material());
728 anAspect->SetTransparency (myDrawer->ShadingAspect()->Transparency());
729 anAspect->SetColor (myAxes[myCurrentIndex].Color());
731 aGroup->SetGroupPrimitivesAspect (anAspect->Aspect());
735 myCurrentMode = AIS_MM_None;
737 if (myHasStartedTransformation)
739 myHasStartedTransformation = Standard_False;
743 //=======================================================================
744 //function : SetZoomPersistence
746 //=======================================================================
747 void AIS_Manipulator::SetZoomPersistence (const Standard_Boolean theToEnable)
749 if (myIsZoomPersistentMode != theToEnable)
754 myIsZoomPersistentMode = theToEnable;
758 setTransformPersistence (Handle(Graphic3d_TransformPers)());
761 updateTransformation();
764 //=======================================================================
765 //function : SetTransformPersistence
767 //=======================================================================
768 void AIS_Manipulator::SetTransformPersistence (const Handle(Graphic3d_TransformPers)& theTrsfPers)
770 Standard_ASSERT_RETURN (!myIsZoomPersistentMode,
771 "AIS_Manipulator::SetTransformPersistence: "
772 "Custom settings are not allowed by this class in ZoomPersistence mode",);
774 setTransformPersistence (theTrsfPers);
777 //=======================================================================
778 //function : setTransformPersistence
780 //=======================================================================
781 void AIS_Manipulator::setTransformPersistence (const Handle(Graphic3d_TransformPers)& theTrsfPers)
783 AIS_InteractiveObject::SetTransformPersistence (theTrsfPers);
785 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
787 myAxes[anIt].SetTransformPersistence (theTrsfPers);
791 //=======================================================================
792 //function : setLocalTransformation
794 //=======================================================================
795 void AIS_Manipulator::setLocalTransformation (const Handle(Geom_Transformation)& /*theTrsf*/)
797 Standard_ASSERT_INVOKE ("AIS_Manipulator::setLocalTransformation: "
798 "Custom transformation is not supported by this class");
801 //=======================================================================
804 //=======================================================================
805 void AIS_Manipulator::Compute (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
806 const Handle(Prs3d_Presentation)& thePrs,
807 const Standard_Integer theMode)
809 if (theMode != AIS_Shaded)
814 thePrs->SetInfiniteState (Standard_True);
815 thePrs->SetMutable (Standard_True);
816 Handle(Graphic3d_Group) aGroup;
817 Handle(Prs3d_ShadingAspect) anAspect = new Prs3d_ShadingAspect();
818 anAspect->Aspect()->SetInteriorStyle (Aspect_IS_SOLID);
819 anAspect->SetMaterial (myDrawer->ShadingAspect()->Material());
820 anAspect->SetTransparency (myDrawer->ShadingAspect()->Transparency());
823 myCenter.Init (myAxes[0].AxisRadius() * 2.0f, gp::Origin());
824 aGroup = Prs3d_Root::NewGroup (thePrs);
825 aGroup->SetPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
826 aGroup->AddPrimitiveArray (myCenter.Array());
828 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
831 aGroup = Prs3d_Root::NewGroup (thePrs);
833 Handle(Prs3d_ShadingAspect) anAspectAx = new Prs3d_ShadingAspect (new Graphic3d_AspectFillArea3d(*anAspect->Aspect()));
834 anAspectAx->SetColor (myAxes[anIt].Color());
835 aGroup->SetGroupPrimitivesAspect (anAspectAx->Aspect());
836 myAxes[anIt].Compute (thePrsMgr, thePrs, anAspectAx);
837 myAxes[anIt].SetTransformPersistence (TransformPersistence());
840 updateTransformation();
843 //=======================================================================
844 //function : HilightSelected
846 //=======================================================================
847 void AIS_Manipulator::HilightSelected (const Handle(PrsMgr_PresentationManager3d)& thePM,
848 const SelectMgr_SequenceOfOwner& theSeq)
850 if (theSeq.IsEmpty())
855 if (myIsActivationOnDetection)
860 if (!theSeq (1)->IsKind (STANDARD_TYPE (AIS_ManipulatorOwner)))
862 thePM->Color (this, GetContext()->HighlightStyle(), 0);
866 Handle(AIS_ManipulatorOwner) anOwner = Handle(AIS_ManipulatorOwner)::DownCast (theSeq (1));
867 myHighlightAspect->Aspect()->SetInteriorColor (GetContext()->HighlightStyle()->Color());
868 Handle(Graphic3d_Group) aGroup = getGroup (anOwner->Index(), anOwner->Mode());
874 aGroup->SetGroupPrimitivesAspect (myHighlightAspect->Aspect());
876 myCurrentIndex = anOwner->Index();
877 myCurrentMode = anOwner->Mode();
880 //=======================================================================
881 //function : ClearSelected
883 //=======================================================================
884 void AIS_Manipulator::ClearSelected()
886 DeactivateCurrentMode();
889 //=======================================================================
890 //function : HilightOwnerWithColor
892 //=======================================================================
893 void AIS_Manipulator::HilightOwnerWithColor (const Handle(PrsMgr_PresentationManager3d)& thePM,
894 const Handle(Prs3d_Drawer)& theStyle,
895 const Handle(SelectMgr_EntityOwner)& theOwner)
897 Handle(AIS_ManipulatorOwner) anOwner = Handle(AIS_ManipulatorOwner)::DownCast (theOwner);
898 Handle(Prs3d_Presentation) aPresentation = getHighlightPresentation (anOwner);
899 if (aPresentation.IsNull())
903 aPresentation->Highlight (theStyle);
904 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aPresentation->Groups());
905 aGroupIter.More(); aGroupIter.Next())
907 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
909 && aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
911 aGrp->SetGroupPrimitivesAspect (myHighlightAspect->Aspect());
914 aPresentation->SetZLayer (Graphic3d_ZLayerId_Topmost);
915 thePM->AddToImmediateList (aPresentation);
917 if (myIsActivationOnDetection)
921 DeactivateCurrentMode();
924 myCurrentIndex = anOwner->Index();
925 myCurrentMode = anOwner->Mode();
929 //=======================================================================
930 //function : ComputeSelection
932 //=======================================================================
933 void AIS_Manipulator::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
934 const Standard_Integer theMode)
937 AIS_ManipulatorMode aMode = (AIS_ManipulatorMode) theMode;
938 if (aMode == AIS_MM_None)
942 Handle(SelectMgr_EntityOwner) anOwner;
943 if (aMode == AIS_MM_None)
945 anOwner = new SelectMgr_EntityOwner (this, 5);
948 if (aMode == AIS_MM_Translation || aMode == AIS_MM_None)
950 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
952 const Axis& anAxis = myAxes[anIt];
953 if (aMode != AIS_MM_None)
955 anOwner = new AIS_ManipulatorOwner(this, anIt, AIS_MM_Translation, 9);
957 // define sensitivity by line
958 Handle(Select3D_SensitiveSegment) aLine = new Select3D_SensitiveSegment (anOwner, gp::Origin(), anAxis.TranslatorTipPosition());
959 aLine->SetSensitivityFactor (15);
960 theSelection->Add (aLine);
962 // enlarge sensitivity by triangulation
963 Handle(Select3D_SensitivePrimitiveArray) aTri = new Select3D_SensitivePrimitiveArray (anOwner);
964 aTri->InitTriangulation (anAxis.TriangleArray()->Attributes(), anAxis.TriangleArray()->Indices(), TopLoc_Location());
965 theSelection->Add (aTri);
969 if (aMode == AIS_MM_Rotation || aMode == AIS_MM_None)
971 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
973 const Axis& anAxis = myAxes[anIt];
974 if (aMode != AIS_MM_None)
976 anOwner = new AIS_ManipulatorOwner (this, anIt, AIS_MM_Rotation, 9);
978 // define sensitivity by circle
979 Handle(Geom_Circle) aGeomCircle = new Geom_Circle (gp_Ax2 (gp::Origin(), anAxis.ReferenceAxis().Direction()), anAxis.RotatorDiskRadius());
980 Handle(Select3D_SensitiveCircle) aCircle = new Select3D_SensitiveCircle (anOwner, aGeomCircle, Standard_False, anAxis.FacettesNumber());
981 aCircle->SetSensitivityFactor (15);
982 theSelection->Add (aCircle);
983 // enlarge sensitivity by triangulation
984 Handle(Select3D_SensitiveTriangulation) aTri = new Select3D_SensitiveTriangulation (anOwner, myAxes[anIt].RotatorDisk().Triangulation(), TopLoc_Location(), Standard_True);
985 theSelection->Add (aTri);
989 if (aMode == AIS_MM_Scaling || aMode == AIS_MM_None)
991 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
993 if (aMode != AIS_MM_None)
995 anOwner = new AIS_ManipulatorOwner (this, anIt, AIS_MM_Scaling, 9);
997 // define sensitivity by point
998 Handle(Select3D_SensitivePoint) aPnt = new Select3D_SensitivePoint (anOwner, myAxes[anIt].ScalerCubePosition());
999 aPnt->SetSensitivityFactor (15);
1000 theSelection->Add (aPnt);
1001 // enlarge sensitivity by triangulation
1002 Handle(Select3D_SensitiveTriangulation) aTri = new Select3D_SensitiveTriangulation (anOwner, myAxes[anIt].ScalerCube().Triangulation(), TopLoc_Location(), Standard_True);
1003 theSelection->Add (aTri);
1008 //=======================================================================
1012 //=======================================================================
1013 void AIS_Manipulator::Disk::Init (const Standard_ShortReal theInnerRadius,
1014 const Standard_ShortReal theOuterRadius,
1015 const gp_Ax1& thePosition,
1016 const Standard_Integer theSlicesNb,
1017 const Standard_Integer theStacksNb)
1019 myPosition = thePosition;
1020 myInnerRad = theInnerRadius;
1021 myOuterRad = theOuterRadius;
1023 Prs3d_ToolDisk aTool (theInnerRadius, theOuterRadius, theSlicesNb, theStacksNb);
1024 gp_Ax3 aSystem (myPosition.Location(), myPosition.Direction());
1026 aTrsf.SetTransformation (aSystem, gp_Ax3());
1027 aTool.FillArray (myArray, myTriangulation, aTrsf);
1030 //=======================================================================
1034 //=======================================================================
1035 void AIS_Manipulator::Sphere::Init (const Standard_ShortReal theRadius,
1036 const gp_Pnt& thePosition,
1037 const Standard_Integer theSlicesNb,
1038 const Standard_Integer theStacksNb)
1040 myPosition = thePosition;
1041 myRadius = theRadius;
1043 Prs3d_ToolSphere aTool (theRadius, theSlicesNb, theStacksNb);
1045 aTrsf.SetTranslation (gp_Vec(gp::Origin(), thePosition));
1046 aTool.FillArray (myArray, myTriangulation, aTrsf);
1049 //=======================================================================
1053 //=======================================================================
1054 void AIS_Manipulator::Cube::Init (const gp_Ax1& thePosition, const Standard_ShortReal theSize)
1056 myArray = new Graphic3d_ArrayOfTriangles (12 * 3, 0, Standard_True);
1058 Poly_Array1OfTriangle aPolyTriangles (1, 12);
1059 TColgp_Array1OfPnt aPoints (1, 36);
1060 NCollection_Array1<gp_Dir> aNormals (1, 12);
1061 myTriangulation = new Poly_Triangulation (aPoints, aPolyTriangles);
1063 gp_Ax2 aPln (thePosition.Location(), thePosition.Direction());
1064 gp_Pnt aBottomLeft = thePosition.Location().XYZ() - aPln.XDirection().XYZ() * theSize * 0.5 - aPln.YDirection().XYZ() * theSize * 0.5;
1065 gp_Pnt aV2 = aBottomLeft.XYZ() + aPln.YDirection().XYZ() * theSize;
1066 gp_Pnt aV3 = aBottomLeft.XYZ() + aPln.YDirection().XYZ() * theSize + aPln.XDirection().XYZ() * theSize;
1067 gp_Pnt aV4 = aBottomLeft.XYZ() + aPln.XDirection().XYZ() * theSize;
1068 gp_Pnt aTopRight = thePosition.Location().XYZ() + thePosition.Direction().XYZ() * theSize
1069 + aPln.XDirection().XYZ() * theSize * 0.5 + aPln.YDirection().XYZ() * theSize * 0.5;
1070 gp_Pnt aV5 = aTopRight.XYZ() - aPln.YDirection().XYZ() * theSize;
1071 gp_Pnt aV6 = aTopRight.XYZ() - aPln.YDirection().XYZ() * theSize - aPln.XDirection().XYZ() * theSize;
1072 gp_Pnt aV7 = aTopRight.XYZ() - aPln.XDirection().XYZ() * theSize;
1074 gp_Dir aRight ((gp_Vec(aTopRight, aV7) ^ gp_Vec(aTopRight, aV2)).XYZ());
1075 gp_Dir aFront ((gp_Vec(aV3, aV4) ^ gp_Vec(aV3, aV5)).XYZ());
1078 addTriangle (0, aBottomLeft, aV2, aV3, -thePosition.Direction());
1079 addTriangle (1, aBottomLeft, aV3, aV4, -thePosition.Direction());
1082 addTriangle (2, aV3, aV4, aV5, aFront);
1083 addTriangle (3, aV3, aV5, aTopRight, aFront);
1086 addTriangle (4, aBottomLeft, aV2, aV7, -aFront);
1087 addTriangle (5, aBottomLeft, aV7, aV6, -aFront);
1090 addTriangle (6, aV7, aV6, aV5, thePosition.Direction());
1091 addTriangle (7, aTopRight, aV7, aV5, thePosition.Direction());
1094 addTriangle (8, aV6, aV5, aV4, -aRight);
1095 addTriangle (9, aBottomLeft, aV6, aV4, -aRight);
1098 addTriangle (10, aV3, aTopRight, aV7, aRight);
1099 addTriangle (11, aV3, aV7, aV2, aRight);
1102 //=======================================================================
1104 //function : addTriangle
1106 //=======================================================================
1107 void AIS_Manipulator::Cube::addTriangle (const Standard_Integer theIndex,
1108 const gp_Pnt& theP1, const gp_Pnt& theP2, const gp_Pnt& theP3,
1109 const gp_Dir& theNormal)
1111 myTriangulation->ChangeNodes().SetValue (theIndex * 3 + 1, theP1);
1112 myTriangulation->ChangeNodes().SetValue (theIndex * 3 + 2, theP2);
1113 myTriangulation->ChangeNodes().SetValue (theIndex * 3 + 3, theP3);
1115 myTriangulation->ChangeTriangles().SetValue (theIndex + 1, Poly_Triangle (theIndex * 3 + 1, theIndex * 3 + 2, theIndex * 3 + 3));
1116 myArray->AddVertex (theP1, theNormal);
1117 myArray->AddVertex (theP2, theNormal);
1118 myArray->AddVertex (theP3, theNormal);
1121 //=======================================================================
1123 //function : Constructor
1125 //=======================================================================
1126 AIS_Manipulator::Axis::Axis (const gp_Ax1& theAxis,
1127 const Quantity_Color& theColor,
1128 const Standard_ShortReal theLength)
1129 : myReferenceAxis (theAxis),
1130 myPosition (theAxis),
1132 myHasTranslation (Standard_True),
1133 myLength (theLength),
1134 myAxisRadius (0.5f),
1135 myHasScaling (Standard_True),
1137 myHasRotation (Standard_True),
1138 myInnerRadius (myLength + myBoxSize),
1139 myDiskThickness (myBoxSize * 0.5f),
1141 myFacettesNumber (20),
1142 myCircleRadius (myLength + myBoxSize + myBoxSize * 0.5f * 0.5f)
1147 //=======================================================================
1149 //function : Compute
1151 //=======================================================================
1153 void AIS_Manipulator::Axis::Compute (const Handle(PrsMgr_PresentationManager)& thePrsMgr,
1154 const Handle(Prs3d_Presentation)& thePrs,
1155 const Handle(Prs3d_ShadingAspect)& theAspect)
1157 if (myHasTranslation)
1159 const Standard_Real anArrowLength = 0.25 * myLength;
1160 const Standard_Real aCylinderLength = myLength - anArrowLength;
1161 myArrowTipPos = gp_Pnt (0.0, 0.0, 0.0).Translated (myReferenceAxis.Direction().XYZ() * aCylinderLength);
1163 myTriangleArray = Prs3d_Arrow::DrawShaded (gp_Ax1(gp::Origin(), myReferenceAxis.Direction()),
1169 myTranslatorGroup = Prs3d_Root::NewGroup (thePrs);
1170 myTranslatorGroup->SetGroupPrimitivesAspect (theAspect->Aspect());
1171 myTranslatorGroup->AddPrimitiveArray (myTriangleArray);
1173 if (myHighlightTranslator.IsNull())
1175 myHighlightTranslator = new Prs3d_Presentation (thePrsMgr->StructureManager());
1179 myHighlightTranslator->Clear();
1182 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (myHighlightTranslator);
1183 aGroup->SetGroupPrimitivesAspect (theAspect->Aspect());
1184 aGroup->AddPrimitiveArray (myTriangleArray);
1190 myCubePos = myReferenceAxis.Direction().XYZ() * (myLength + myIndent);
1191 myCube.Init (gp_Ax1 (myCubePos, myReferenceAxis.Direction()), myBoxSize);
1193 myScalerGroup = Prs3d_Root::NewGroup (thePrs);
1194 myScalerGroup->SetGroupPrimitivesAspect (theAspect->Aspect());
1195 myScalerGroup->AddPrimitiveArray (myCube.Array());
1197 if (myHighlightScaler.IsNull())
1199 myHighlightScaler = new Prs3d_Presentation (thePrsMgr->StructureManager());
1203 myHighlightScaler->Clear();
1206 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (myHighlightScaler);
1207 aGroup->SetGroupPrimitivesAspect (theAspect->Aspect());
1208 aGroup->AddPrimitiveArray (myCube.Array());
1214 myCircleRadius = myInnerRadius + myIndent * 2 + myDiskThickness * 0.5f;
1215 myCircle.Init (myInnerRadius + myIndent * 2, myInnerRadius + myDiskThickness + myIndent * 2, gp_Ax1(gp::Origin(), myReferenceAxis.Direction()), myFacettesNumber * 2);
1216 myRotatorGroup = Prs3d_Root::NewGroup (thePrs);
1217 myRotatorGroup->SetGroupPrimitivesAspect (theAspect->Aspect());
1218 myRotatorGroup->AddPrimitiveArray (myCircle.Array());
1220 if (myHighlightRotator.IsNull())
1222 myHighlightRotator = new Prs3d_Presentation (thePrsMgr->StructureManager());
1226 myHighlightRotator->Clear();
1229 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (myHighlightRotator);
1230 aGroup->SetGroupPrimitivesAspect (theAspect->Aspect());
1231 aGroup->AddPrimitiveArray (myCircle.Array());