1 // Created on: 1995-02-15
2 // Created by: Roberc Coublanc
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <SelectMgr_ViewerSelector.hxx>
19 #include <BVH_Tree.hxx>
20 #include <gp_GTrsf.hxx>
22 #include <OSD_Environment.hxx>
23 #include <Precision.hxx>
24 #include <SelectBasics_EntityOwner.hxx>
25 #include <SelectBasics_SensitiveEntity.hxx>
26 #include <SelectBasics_PickResult.hxx>
27 #include <SelectMgr_EntityOwner.hxx>
28 #include <SelectMgr_SortCriterion.hxx>
29 #include <SelectMgr_SensitiveEntitySet.hxx>
30 #include <TColStd_Array1OfInteger.hxx>
31 #include <TCollection_AsciiString.hxx>
32 #include <TColStd_HArray1OfInteger.hxx>
33 #include <TColStd_ListOfInteger.hxx>
37 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_ViewerSelector,MMgt_TShared)
40 // Comparison operator for sorting selection results
45 CompareResults (const SelectMgr_IndexedDataMapOfOwnerCriterion& aMapOfCriterion)
46 : myMapOfCriterion (aMapOfCriterion)
50 Standard_Boolean operator() (Standard_Integer theLeft, Standard_Integer theRight) const
52 return myMapOfCriterion.FindFromIndex(theLeft) > myMapOfCriterion.FindFromIndex(theRight);
56 void operator = (const CompareResults&);
59 const SelectMgr_IndexedDataMapOfOwnerCriterion& myMapOfCriterion;
62 //! Compute 3d position for detected entity.
63 inline void updatePoint3d (SelectMgr_SortCriterion& theCriterion,
64 const gp_GTrsf& theInversedTrsf,
65 SelectMgr_SelectingVolumeManager& theMgr)
67 theCriterion.Point = theMgr.DetectedPoint (theCriterion.Depth);
68 gp_GTrsf anInvTrsf = theInversedTrsf;
69 if (theCriterion.Entity->HasInitLocation())
71 anInvTrsf = theCriterion.Entity->InvInitLocation() * anInvTrsf;
73 if (anInvTrsf.Form() != gp_Identity)
75 anInvTrsf.Inverted().Transforms (theCriterion.Point.ChangeCoord());
79 static const Graphic3d_Mat4d THE_IDENTITY_MAT;
82 //==================================================
83 // Function: Initialize
85 //==================================================
86 SelectMgr_ViewerSelector::SelectMgr_ViewerSelector():
87 preferclosest(Standard_True),
88 myToUpdateTolerance (Standard_True),
90 myIsLeftChildQueuedFirst (Standard_False),
95 //==================================================
98 //==================================================
99 void SelectMgr_ViewerSelector::Activate (const Handle(SelectMgr_Selection)& theSelection)
101 for (theSelection->Init(); theSelection->More(); theSelection->Next())
103 theSelection->Sensitive()->SetActiveForSelection();
106 theSelection->SetSelectionState (SelectMgr_SOS_Activated);
108 myTolerances.Add (theSelection->Sensitivity());
109 myToUpdateTolerance = Standard_True;
112 //==================================================
113 // Function: Deactivate
115 //==================================================
116 void SelectMgr_ViewerSelector::Deactivate (const Handle(SelectMgr_Selection)& theSelection)
118 for (theSelection->Init(); theSelection->More(); theSelection->Next())
120 theSelection->Sensitive()->ResetSelectionActiveStatus();
123 theSelection->SetSelectionState (SelectMgr_SOS_Deactivated);
125 myTolerances.Decrement (theSelection->Sensitivity());
126 myToUpdateTolerance = Standard_True;
129 //==================================================
132 //==================================================
133 void SelectMgr_ViewerSelector::Clear()
138 //=======================================================================
139 // function: isToScaleFrustum
140 // purpose : Checks if the entity given requires to scale current selecting frustum
141 //=======================================================================
142 Standard_Boolean SelectMgr_ViewerSelector::isToScaleFrustum (const Handle(SelectBasics_SensitiveEntity)& theEntity)
144 return mySelectingVolumeMgr.GetActiveSelectionType() == SelectMgr_SelectingVolumeManager::Point
145 && sensitivity (theEntity) < myTolerances.Tolerance();
148 //=======================================================================
149 // function: sensitivity
150 // purpose : In case if custom tolerance is set, this method will return sum of entity
151 // sensitivity and custom tolerance.
152 //=======================================================================
153 Standard_Integer SelectMgr_ViewerSelector::sensitivity (const Handle(SelectBasics_SensitiveEntity)& theEntity) const
155 return myTolerances.IsCustomTolSet() ?
156 theEntity->SensitivityFactor() + myTolerances.CustomTolerance() : theEntity->SensitivityFactor();
159 //=======================================================================
160 // function: checkOverlap
161 // purpose : Internal function that checks if a particular sensitive
162 // entity theEntity overlaps current selecting volume precisely
163 //=======================================================================
164 void SelectMgr_ViewerSelector::checkOverlap (const Handle(SelectBasics_SensitiveEntity)& theEntity,
165 const gp_GTrsf& theInversedTrsf,
166 SelectMgr_SelectingVolumeManager& theMgr)
168 Handle(SelectMgr_EntityOwner) anOwner (Handle(SelectMgr_EntityOwner)::DownCast (theEntity->OwnerId()));
170 SelectBasics_PickResult aPickResult;
171 if (theEntity->Matches (theMgr, aPickResult))
173 if (!anOwner.IsNull())
175 Handle(SelectMgr_SelectableObject) aSelectable = anOwner->Selectable();
176 if (HasDepthClipping (anOwner) && theMgr.GetActiveSelectionType() == SelectMgr_SelectingVolumeManager::Point)
178 Standard_Boolean isClipped = mySelectingVolumeMgr.IsClipped (aSelectable->GetClipPlanes(),
179 aPickResult.Depth());
184 SelectMgr_SortCriterion aCriterion;
185 myZLayerOrderMap.Find (aSelectable->ZLayer(), aCriterion.ZLayerPosition);
186 aCriterion.Entity = theEntity;
187 aCriterion.Priority = anOwner->Priority();
188 aCriterion.Depth = aPickResult.Depth();
189 aCriterion.MinDist = aPickResult.DistToGeomCenter();
190 aCriterion.Tolerance = theEntity->SensitivityFactor() / 33.0;
191 aCriterion.ToPreferClosest = preferclosest;
193 const Standard_Integer aPrevStoredIndex = mystored.FindIndex (anOwner);
194 if (aPrevStoredIndex != 0)
196 if (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Box)
198 SelectMgr_SortCriterion& aPrevCriterion = mystored.ChangeFromIndex (aPrevStoredIndex);
199 if (aCriterion > aPrevCriterion)
201 updatePoint3d (aCriterion, theInversedTrsf, theMgr);
202 aPrevCriterion = aCriterion;
208 updatePoint3d (aCriterion, theInversedTrsf, theMgr);
209 mystored.Add (anOwner, aCriterion);
215 //=======================================================================
216 // function: computeFrustum
217 // purpose : Internal function that checks if a current selecting frustum
218 // needs to be scaled and transformed for the entity and performs
219 // necessary calculations
220 //=======================================================================
221 void SelectMgr_ViewerSelector::computeFrustum (const Handle(SelectBasics_SensitiveEntity)& theEnt,
222 const SelectMgr_SelectingVolumeManager& theMgr,
223 const gp_GTrsf& theInvTrsf,
224 SelectMgr_FrustumCache& theCachedMgrs,
225 SelectMgr_SelectingVolumeManager& theResMgr)
227 Standard_Integer aScale = isToScaleFrustum (theEnt) ? sensitivity (theEnt) : 1;
228 const gp_GTrsf aTrsfMtr = theEnt->HasInitLocation() ? theEnt->InvInitLocation() * theInvTrsf : theInvTrsf;
229 const Standard_Boolean toScale = aScale != 1;
230 const Standard_Boolean toTransform = aTrsfMtr.Form() != gp_Identity;
231 if (toScale && toTransform)
233 theResMgr = theMgr.ScaleAndTransform (aScale, aTrsfMtr, NULL);
237 if (!theCachedMgrs.IsBound (aScale))
239 theCachedMgrs.Bind (aScale, theMgr.ScaleAndTransform (aScale, gp_Trsf(), NULL));
241 theResMgr = theCachedMgrs.Find (aScale);
243 else if (toTransform)
245 theResMgr = theMgr.ScaleAndTransform (1, aTrsfMtr, NULL);
249 //=======================================================================
250 // function: traverseObject
251 // purpose : Internal function that checks if there is possible overlap
252 // between some entity of selectable object theObject and
253 // current selecting volume
254 //=======================================================================
255 void SelectMgr_ViewerSelector::traverseObject (const Handle(SelectMgr_SelectableObject)& theObject,
256 const SelectMgr_SelectingVolumeManager& theMgr,
257 const Handle(Graphic3d_Camera)& theCamera,
258 const Graphic3d_Mat4d& theProjectionMat,
259 const Graphic3d_Mat4d& theWorldViewMat,
260 const Standard_Integer theViewportWidth,
261 const Standard_Integer theViewportHeight)
263 NCollection_Handle<SelectMgr_SensitiveEntitySet>& anEntitySet =
264 myMapOfObjectSensitives.ChangeFind (theObject);
266 if (anEntitySet->Size() == 0)
269 const NCollection_Handle<BVH_Tree<Standard_Real, 3> >& aSensitivesTree = anEntitySet->BVH();
271 gp_GTrsf aInversedTrsf;
273 if (theObject->HasTransformation() || theObject->TransformPersistence().Flags)
275 if (!theObject->TransformPersistence().Flags)
277 aInversedTrsf = theObject->InversedTransformation();
282 Graphic3d_Mat4d aMat = theObject->TransformPersistence().Compute (
283 theCamera, theProjectionMat, theWorldViewMat, theViewportWidth, theViewportHeight);
285 aTPers.SetValue (1, 1, aMat.GetValue (0, 0));
286 aTPers.SetValue (1, 2, aMat.GetValue (0, 1));
287 aTPers.SetValue (1, 3, aMat.GetValue (0, 2));
288 aTPers.SetValue (2, 1, aMat.GetValue (1, 0));
289 aTPers.SetValue (2, 2, aMat.GetValue (1, 1));
290 aTPers.SetValue (2, 3, aMat.GetValue (1, 2));
291 aTPers.SetValue (3, 1, aMat.GetValue (2, 0));
292 aTPers.SetValue (3, 2, aMat.GetValue (2, 1));
293 aTPers.SetValue (3, 3, aMat.GetValue (2, 2));
294 aTPers.SetTranslationPart (gp_XYZ (aMat.GetValue (0, 3), aMat.GetValue (1, 3), aMat.GetValue (2, 3)));
296 aInversedTrsf = (aTPers * gp_GTrsf (theObject->Transformation())).Inverted();
300 SelectMgr_SelectingVolumeManager aMgr = aInversedTrsf.Form() != gp_Identity
301 ? theMgr.ScaleAndTransform (1, aInversedTrsf, NULL)
304 SelectMgr_FrustumCache aScaledTrnsfFrustums;
306 Standard_Integer aNode = 0; // a root node
307 if (!aMgr.Overlaps (aSensitivesTree->MinPoint (0),
308 aSensitivesTree->MaxPoint (0)))
312 Standard_Integer aStack[32];
313 Standard_Integer aHead = -1;
316 if (!aSensitivesTree->IsOuter (aNode))
318 const Standard_Integer aLeftChildIdx = aSensitivesTree->Child<0> (aNode);
319 const Standard_Integer aRightChildIdx = aSensitivesTree->Child<1> (aNode);
320 const Standard_Boolean isLeftChildIn = aMgr.Overlaps (aSensitivesTree->MinPoint (aLeftChildIdx),
321 aSensitivesTree->MaxPoint (aLeftChildIdx));
322 const Standard_Boolean isRightChildIn = aMgr.Overlaps (aSensitivesTree->MinPoint (aRightChildIdx),
323 aSensitivesTree->MaxPoint (aRightChildIdx));
327 aNode = aLeftChildIdx;
329 aStack[aHead] = aRightChildIdx;
331 else if (isLeftChildIn
334 aNode = isLeftChildIn ? aLeftChildIdx : aRightChildIdx;
343 aNode = aStack[aHead];
349 Standard_Integer aStartIdx = aSensitivesTree->BegPrimitive (aNode);
350 Standard_Integer anEndIdx = aSensitivesTree->EndPrimitive (aNode);
351 for (Standard_Integer anIdx = aStartIdx; anIdx <= anEndIdx; ++anIdx)
353 const Handle(SelectMgr_SensitiveEntity)& aSensitive =
354 anEntitySet->GetSensitiveById (anIdx);
355 if (aSensitive->IsActiveForSelection())
357 const Handle(SelectBasics_SensitiveEntity)& anEnt = aSensitive->BaseSensitive();
358 SelectMgr_SelectingVolumeManager aTmpMgr = aMgr;
359 computeFrustum (anEnt, theMgr, aInversedTrsf, aScaledTrnsfFrustums, aTmpMgr);
360 checkOverlap (anEnt, aInversedTrsf, aTmpMgr);
368 aNode = aStack[aHead];
374 //=======================================================================
375 // function: TraverseSensitives
376 // purpose : Traverses BVH containing all added selectable objects and
377 // finds candidates for further search of overlap
378 //=======================================================================
379 void SelectMgr_ViewerSelector::TraverseSensitives()
383 Standard_Integer aWidth;
384 Standard_Integer aHeight;
385 mySelectingVolumeMgr.WindowSize (aWidth, aHeight);
386 mySelectableObjects.UpdateBVH (mySelectingVolumeMgr.Camera(),
387 mySelectingVolumeMgr.ProjectionMatrix(),
388 mySelectingVolumeMgr.WorldViewMatrix(),
389 mySelectingVolumeMgr.WorldViewProjState(),
392 for (Standard_Integer aBVHSetIt = 0; aBVHSetIt < SelectMgr_SelectableObjectSet::BVHSubsetNb; ++aBVHSetIt)
394 SelectMgr_SelectableObjectSet::BVHSubset aBVHSubset =
395 static_cast<SelectMgr_SelectableObjectSet::BVHSubset> (aBVHSetIt);
397 if (mySelectableObjects.IsEmpty (aBVHSubset))
404 SelectMgr_SelectingVolumeManager aMgr (Standard_False);
406 // for 2D space selection transform selecting volumes to perform overap testing
407 // directly in camera's eye space omitting the camera position, which is not
408 // needed there at all
409 if (aBVHSubset == SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent)
411 const Graphic3d_Mat4d& aMat = mySelectingVolumeMgr.WorldViewMatrix();
412 aTFrustum.SetValue (1, 1, aMat.GetValue (0, 0));
413 aTFrustum.SetValue (1, 2, aMat.GetValue (0, 1));
414 aTFrustum.SetValue (1, 3, aMat.GetValue (0, 2));
415 aTFrustum.SetValue (2, 1, aMat.GetValue (1, 0));
416 aTFrustum.SetValue (2, 2, aMat.GetValue (1, 1));
417 aTFrustum.SetValue (2, 3, aMat.GetValue (1, 2));
418 aTFrustum.SetValue (3, 1, aMat.GetValue (2, 0));
419 aTFrustum.SetValue (3, 2, aMat.GetValue (2, 1));
420 aTFrustum.SetValue (3, 3, aMat.GetValue (2, 2));
421 aTFrustum.SetTranslationPart (gp_XYZ (aMat.GetValue (0, 3), aMat.GetValue (1, 3), aMat.GetValue (2, 3)));
423 // define corresponding frustum builder parameters
424 Handle(SelectMgr_FrustumBuilder) aBuilder = new SelectMgr_FrustumBuilder();
425 aBuilder->SetProjectionMatrix (mySelectingVolumeMgr.ProjectionMatrix());
426 aBuilder->SetWorldViewMatrix (THE_IDENTITY_MAT);
427 aBuilder->SetWindowSize (aWidth, aHeight);
428 aMgr = mySelectingVolumeMgr.ScaleAndTransform (1, aTFrustum, aBuilder);
432 aMgr = mySelectingVolumeMgr;
435 const Handle(Graphic3d_Camera)& aCamera = mySelectingVolumeMgr.Camera();
436 const Graphic3d_Mat4d& aProjectionMat = mySelectingVolumeMgr.ProjectionMatrix();
437 const Graphic3d_Mat4d& aWorldViewMat = aBVHSubset != SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent
438 ? mySelectingVolumeMgr.WorldViewMatrix()
441 const NCollection_Handle<BVH_Tree<Standard_Real, 3> >& aBVHTree = mySelectableObjects.BVH (aBVHSubset);
443 Standard_Integer aNode = 0;
444 if (!aMgr.Overlaps (aBVHTree->MinPoint (0), aBVHTree->MaxPoint (0)))
449 Standard_Integer aStack[32];
450 Standard_Integer aHead = -1;
453 if (!aBVHTree->IsOuter (aNode))
455 const Standard_Integer aLeftChildIdx = aBVHTree->Child<0> (aNode);
456 const Standard_Integer aRightChildIdx = aBVHTree->Child<1> (aNode);
457 const Standard_Boolean isLeftChildIn =
458 aMgr.Overlaps (aBVHTree->MinPoint (aLeftChildIdx), aBVHTree->MaxPoint (aLeftChildIdx));
459 const Standard_Boolean isRightChildIn =
460 aMgr.Overlaps (aBVHTree->MinPoint (aRightChildIdx), aBVHTree->MaxPoint (aRightChildIdx));
464 aNode = aLeftChildIdx;
466 aStack[aHead] = aRightChildIdx;
468 else if (isLeftChildIn
471 aNode = isLeftChildIn ? aLeftChildIdx : aRightChildIdx;
480 aNode = aStack[aHead];
486 Standard_Integer aStartIdx = aBVHTree->BegPrimitive (aNode);
487 Standard_Integer anEndIdx = aBVHTree->EndPrimitive (aNode);
488 for (Standard_Integer anIdx = aStartIdx; anIdx <= anEndIdx; ++anIdx)
490 const Handle(SelectMgr_SelectableObject)& aSelectableObject =
491 mySelectableObjects.GetObjectById (aBVHSubset, anIdx);
493 traverseObject (aSelectableObject, aMgr, aCamera, aProjectionMat, aWorldViewMat, aWidth, aHeight);
500 aNode = aStack[aHead];
509 //==================================================
512 //==================================================
513 Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector
516 Standard_Integer RankInMap = myIndexes->Value (myCurRank);
517 const Handle(SelectBasics_EntityOwner)& toto = mystored.FindKey(RankInMap);
518 Handle(SelectMgr_EntityOwner) Ownr = Handle(SelectMgr_EntityOwner)::DownCast (toto);
522 //=======================================================================
525 //=======================================================================
526 Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector::Picked (const Standard_Integer theRank) const
528 Handle(SelectMgr_EntityOwner) anOwner;
529 if (theRank < 1 || theRank > NbPicked())
534 const Standard_Integer anOwnerIdx = myIndexes->Value (theRank);
535 const Handle(SelectBasics_EntityOwner)& aStoredOwner = mystored.FindKey (anOwnerIdx);
536 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (aStoredOwner);
540 //=======================================================================
541 //function : PickedData
543 //=======================================================================
544 const SelectMgr_SortCriterion& SelectMgr_ViewerSelector::PickedData(const Standard_Integer theRank) const
546 Standard_OutOfRange_Raise_if (theRank < 1 || theRank > NbPicked(), "SelectMgr_ViewerSelector::PickedData() out of range index");
547 const Standard_Integer anOwnerIdx = myIndexes->Value (theRank);
548 return mystored.FindFromIndex (anOwnerIdx);
551 //===================================================
553 // INTERNAL METHODS ....
555 //==================================================
557 //==================================================
558 // Function: Contains
560 //==================================================
561 Standard_Boolean SelectMgr_ViewerSelector::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
563 return mySelectableObjects.Contains (theObject);
566 //==================================================
567 // Function: ActiveModes
568 // Purpose : return all the modes with a given state for an object
569 //==================================================
570 Standard_Boolean SelectMgr_ViewerSelector::Modes (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
571 TColStd_ListOfInteger& theModeList,
572 const SelectMgr_StateOfSelection theWantedState) const
574 Standard_Boolean hasActivatedStates = Contains (theSelectableObject);
575 for (theSelectableObject->Init(); theSelectableObject->More(); theSelectableObject->Next())
577 if (theWantedState == SelectMgr_SOS_Any)
579 theModeList.Append (theSelectableObject->CurrentSelection()->Mode());
581 else if (theWantedState == theSelectableObject->CurrentSelection()->GetSelectionState())
583 theModeList.Append (theSelectableObject->CurrentSelection()->Mode());
587 return hasActivatedStates;
590 //==================================================
591 // Function: IsActive
593 //==================================================
594 Standard_Boolean SelectMgr_ViewerSelector::IsActive (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
595 const Standard_Integer theMode) const
597 if (!Contains (theSelectableObject))
598 return Standard_False;
600 for (theSelectableObject->Init(); theSelectableObject->More(); theSelectableObject->Next())
602 if (theMode == theSelectableObject->CurrentSelection()->Mode())
604 return theSelectableObject->CurrentSelection()->GetSelectionState() == SelectMgr_SOS_Activated;
608 return Standard_False;
611 //==================================================
612 // Function: IsInside
614 //==================================================
615 Standard_Boolean SelectMgr_ViewerSelector::IsInside (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
616 const Standard_Integer theMode) const
618 if (!Contains (theSelectableObject))
619 return Standard_False;
621 for (theSelectableObject->Init(); theSelectableObject->More(); theSelectableObject->Next())
623 if (theMode == theSelectableObject->CurrentSelection()->Mode())
625 return theSelectableObject->CurrentSelection()->GetSelectionState() != SelectMgr_SOS_Unknown;
629 return Standard_False;
633 //=======================================================================
636 //=======================================================================
638 SelectMgr_StateOfSelection SelectMgr_ViewerSelector::Status (const Handle(SelectMgr_Selection)& theSelection) const
640 return theSelection->GetSelectionState();
643 //==================================================
645 // Purpose : gives Information about selectors
646 //==================================================
648 TCollection_AsciiString SelectMgr_ViewerSelector::Status (const Handle(SelectMgr_SelectableObject)& theSelectableObject) const
650 TCollection_AsciiString aStatus ("Status Object :\n\t");
652 for (theSelectableObject->Init(); theSelectableObject->More(); theSelectableObject->Next())
654 if (theSelectableObject->CurrentSelection()->GetSelectionState() != SelectMgr_SOS_Unknown)
656 aStatus = aStatus + "Mode " +
657 TCollection_AsciiString (theSelectableObject->CurrentSelection()->Mode()) +
659 if (theSelectableObject->CurrentSelection()->GetSelectionState() == SelectMgr_SOS_Activated)
661 aStatus = aStatus + " Active \n\t";
665 aStatus = aStatus + " Inactive \n\t";
670 if (!Contains (theSelectableObject))
672 aStatus = aStatus + "Not Present in the selector\n\n";
678 //=======================================================================
679 //function : SortResult
680 //purpose : there is a certain number of entities ranged by criteria
681 // (depth, size, priority, mouse distance from borders or
682 // CDG of the detected primitive. Parsing :
683 // maximum priorities .
684 // then a reasonable compromise between depth and distance...
685 // finally the ranges are stored in myindexes depending on the parsing.
686 // so, it is possible to only read
687 //=======================================================================
688 void SelectMgr_ViewerSelector::SortResult()
690 if(mystored.IsEmpty()) return;
692 const Standard_Integer anExtent = mystored.Extent();
693 if(myIndexes.IsNull() || anExtent != myIndexes->Length())
694 myIndexes = new TColStd_HArray1OfInteger (1, anExtent);
697 TColStd_Array1OfInteger& thearr = myIndexes->ChangeArray1();
699 // indices from 1 to N are loaded
701 for (I=1; I <= anExtent; I++)
704 std::sort (thearr.begin(), thearr.end(), CompareResults (mystored));
708 //=======================================================================
709 //function : HasDepthClipping
711 //=======================================================================
712 Standard_Boolean SelectMgr_ViewerSelector::HasDepthClipping (const Handle(SelectMgr_EntityOwner)& /*theOwner*/) const
714 return Standard_False;
717 //=======================================================================
718 // function : AddSelectableObject
719 // purpose : Adds new object to the map of selectable objects
720 //=======================================================================
721 void SelectMgr_ViewerSelector::AddSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
723 if (!myMapOfObjectSensitives.IsBound (theObject))
725 mySelectableObjects.Append (theObject);
726 NCollection_Handle<SelectMgr_SensitiveEntitySet> anEntitySet = new SelectMgr_SensitiveEntitySet();
727 myMapOfObjectSensitives.Bind (theObject, anEntitySet);
731 //=======================================================================
732 // function : AddSelectionToObject
733 // purpose : Adds new selection to the object and builds its BVH tree
734 //=======================================================================
735 void SelectMgr_ViewerSelector::AddSelectionToObject (const Handle(SelectMgr_SelectableObject)& theObject,
736 const Handle(SelectMgr_Selection)& theSelection)
738 if (myMapOfObjectSensitives.IsBound (theObject))
740 NCollection_Handle<SelectMgr_SensitiveEntitySet>& anEntitySet =
741 myMapOfObjectSensitives.ChangeFind (theObject);
742 anEntitySet->Append (theSelection);
747 AddSelectableObject (theObject);
748 AddSelectionToObject (theObject, theSelection);
752 //=======================================================================
753 // function : MoveSelectableObject
755 //=======================================================================
756 void SelectMgr_ViewerSelector::MoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
758 mySelectableObjects.ChangeSubset (theObject);
761 //=======================================================================
762 // function : RemoveSelectableObject
763 // purpose : Removes selectable object from map of selectable ones
764 //=======================================================================
765 void SelectMgr_ViewerSelector::RemoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
767 if (myMapOfObjectSensitives.IsBound (theObject))
769 mySelectableObjects.Remove (theObject);
770 myMapOfObjectSensitives.UnBind (theObject);
774 //=======================================================================
775 // function : RemoveSelectionOfObject
776 // purpose : Removes selection of the object and marks its BVH tree
778 //=======================================================================
779 void SelectMgr_ViewerSelector::RemoveSelectionOfObject (const Handle(SelectMgr_SelectableObject)& theObject,
780 const Handle(SelectMgr_Selection)& theSelection)
782 if (myMapOfObjectSensitives.IsBound (theObject))
784 NCollection_Handle<SelectMgr_SensitiveEntitySet>& anEntitySet =
785 myMapOfObjectSensitives.ChangeFind (theObject);
786 anEntitySet->Remove (theSelection);
790 //=======================================================================
791 // function : RebuildObjectsTree
792 // purpose : Marks BVH of selectable objects for rebuild
793 //=======================================================================
794 void SelectMgr_ViewerSelector::RebuildObjectsTree (const Standard_Boolean theIsForce)
796 mySelectableObjects.MarkDirty();
800 Standard_Integer aViewportWidth, aViewportHeight;
801 mySelectingVolumeMgr.WindowSize (aViewportWidth, aViewportHeight);
803 Standard_Integer aWidth;
804 Standard_Integer aHeight;
805 mySelectingVolumeMgr.WindowSize (aWidth, aHeight);
806 mySelectableObjects.UpdateBVH (mySelectingVolumeMgr.Camera(),
807 mySelectingVolumeMgr.ProjectionMatrix(),
808 mySelectingVolumeMgr.WorldViewMatrix(),
809 mySelectingVolumeMgr.WorldViewProjState(),
814 //=======================================================================
815 // function : RebuildSensitivesTree
816 // purpose : Marks BVH of sensitive entities of particular selectable
817 // object for rebuild
818 //=======================================================================
819 void SelectMgr_ViewerSelector::RebuildSensitivesTree (const Handle(SelectMgr_SelectableObject)& theObject,
820 const Standard_Boolean theIsForce)
822 if (!Contains (theObject))
825 NCollection_Handle<SelectMgr_SensitiveEntitySet>& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject);
826 anEntitySet->MarkDirty();
834 //=======================================================================
835 // function : resetSelectionActivationStatus
836 // purpose : Marks all added sensitive entities of all objects as
838 //=======================================================================
839 void SelectMgr_ViewerSelector::ResetSelectionActivationStatus()
841 SelectMgr_MapOfObjectSensitivesIterator aSensitivesIter (myMapOfObjectSensitives);
842 for ( ; aSensitivesIter.More(); aSensitivesIter.Next())
844 NCollection_Handle<SelectMgr_SensitiveEntitySet>& anEntitySet =
845 aSensitivesIter.ChangeValue();
846 Standard_Integer anEntitiesNb = anEntitySet->Size();
847 for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx)
849 anEntitySet->GetSensitiveById (anIdx)->ResetSelectionActiveStatus();
854 //=======================================================================
855 // function : DetectedEntity
856 // purpose : Returns sensitive entity that was detected during the
857 // previous run of selection algorithm
858 //=======================================================================
859 const Handle(SelectBasics_SensitiveEntity)& SelectMgr_ViewerSelector::DetectedEntity() const
861 const Standard_Integer aRankInMap = myIndexes->Value(myCurRank);
862 return mystored.FindFromIndex (aRankInMap).Entity;
865 //=======================================================================
866 // function : ActiveOwners
867 // purpose : Returns the list of active entity owners
868 //=======================================================================
869 void SelectMgr_ViewerSelector::ActiveOwners (NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners) const
871 for (SelectMgr_MapOfObjectSensitivesIterator anIter (myMapOfObjectSensitives); anIter.More(); anIter.Next())
873 const NCollection_Handle<SelectMgr_SensitiveEntitySet>& anEntitySet = anIter.Value();
874 Standard_Integer anEntitiesNb = anEntitySet->Size();
875 for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx)
877 if (anEntitySet->GetSensitiveById (anIdx)->IsActiveForSelection())
879 theOwners.Append (anEntitySet->GetSensitiveById (anIdx)->BaseSensitive()->OwnerId());
885 //=======================================================================
886 //function : AllowOverlapDetection
887 //purpose : Sets the detection type: if theIsToAllow is false,
888 // only fully included sensitives will be detected, otherwise
889 // the algorithm will mark both included and overlapped entities
891 //=======================================================================
892 void SelectMgr_ViewerSelector::AllowOverlapDetection (const Standard_Boolean theIsToAllow)
894 mySelectingVolumeMgr.AllowOverlapDetection (theIsToAllow);