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, Standard_Transient)
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 static const Graphic3d_Mat4d SelectMgr_ViewerSelector_THE_IDENTITY_MAT;
65 //=======================================================================
66 // function : updatePoint3d
68 //=======================================================================
69 void SelectMgr_ViewerSelector::updatePoint3d (SelectMgr_SortCriterion& theCriterion,
70 const Handle(SelectBasics_SensitiveEntity)& theEntity,
71 const gp_GTrsf& theInversedTrsf,
72 const SelectMgr_SelectingVolumeManager& theMgr) const
74 if (theMgr.GetActiveSelectionType() != SelectMgr_SelectingVolumeManager::Point)
79 theCriterion.Point = theMgr.DetectedPoint (theCriterion.Depth);
80 gp_GTrsf anInvTrsf = theInversedTrsf;
81 if (theCriterion.Entity->HasInitLocation())
83 anInvTrsf = theCriterion.Entity->InvInitLocation() * anInvTrsf;
85 if (anInvTrsf.Form() != gp_Identity)
87 anInvTrsf.Inverted().Transforms (theCriterion.Point.ChangeCoord());
90 if (mySelectingVolumeMgr.Camera().IsNull())
92 theCriterion.Tolerance = theEntity->SensitivityFactor() / 33.0;
94 else if (mySelectingVolumeMgr.Camera()->IsOrthographic())
96 theCriterion.Tolerance = myCameraScale * theEntity->SensitivityFactor();
100 const Standard_Real aDistFromEye = (theCriterion.Point.XYZ() - myCameraEye.XYZ()).Dot (myCameraDir.XYZ());
101 theCriterion.Tolerance = aDistFromEye * myCameraScale * theEntity->SensitivityFactor();
105 //==================================================
106 // Function: Initialize
108 //==================================================
109 SelectMgr_ViewerSelector::SelectMgr_ViewerSelector():
110 preferclosest(Standard_True),
111 myToUpdateTolerance (Standard_True),
114 myIsLeftChildQueuedFirst (Standard_False),
117 myEntitySetBuilder = new BVH_BinnedBuilder<Standard_Real, 3, 4> (BVH_Constants_LeafNodeSizeSingle, BVH_Constants_MaxTreeDepth, Standard_True);
120 //==================================================
121 // Function: Activate
123 //==================================================
124 void SelectMgr_ViewerSelector::Activate (const Handle(SelectMgr_Selection)& theSelection)
126 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
128 aSelEntIter.Value()->SetActiveForSelection();
131 theSelection->SetSelectionState (SelectMgr_SOS_Activated);
133 myTolerances.Add (theSelection->Sensitivity());
134 myToUpdateTolerance = Standard_True;
137 //==================================================
138 // Function: Deactivate
140 //==================================================
141 void SelectMgr_ViewerSelector::Deactivate (const Handle(SelectMgr_Selection)& theSelection)
143 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
145 aSelEntIter.Value()->ResetSelectionActiveStatus();
148 theSelection->SetSelectionState (SelectMgr_SOS_Deactivated);
150 myTolerances.Decrement (theSelection->Sensitivity());
151 myToUpdateTolerance = Standard_True;
154 //==================================================
157 //==================================================
158 void SelectMgr_ViewerSelector::Clear()
163 //=======================================================================
164 // function: isToScaleFrustum
165 // purpose : Checks if the entity given requires to scale current selecting frustum
166 //=======================================================================
167 Standard_Boolean SelectMgr_ViewerSelector::isToScaleFrustum (const Handle(SelectBasics_SensitiveEntity)& theEntity)
169 return mySelectingVolumeMgr.GetActiveSelectionType() == SelectMgr_SelectingVolumeManager::Point
170 && sensitivity (theEntity) < myTolerances.Tolerance();
173 //=======================================================================
174 // function: sensitivity
175 // purpose : In case if custom tolerance is set, this method will return sum of entity
176 // sensitivity and custom tolerance.
177 //=======================================================================
178 Standard_Integer SelectMgr_ViewerSelector::sensitivity (const Handle(SelectBasics_SensitiveEntity)& theEntity) const
180 return myTolerances.IsCustomTolSet() ?
181 theEntity->SensitivityFactor() + myTolerances.CustomTolerance() : theEntity->SensitivityFactor();
184 //=======================================================================
185 // function: checkOverlap
186 // purpose : Internal function that checks if a particular sensitive
187 // entity theEntity overlaps current selecting volume precisely
188 //=======================================================================
189 void SelectMgr_ViewerSelector::checkOverlap (const Handle(SelectBasics_SensitiveEntity)& theEntity,
190 const gp_GTrsf& theInversedTrsf,
191 SelectMgr_SelectingVolumeManager& theMgr)
193 Handle(SelectMgr_EntityOwner) anOwner (Handle(SelectMgr_EntityOwner)::DownCast (theEntity->OwnerId()));
194 Handle(SelectMgr_SelectableObject) aSelectable;
195 Standard_Boolean toRestoresViewClipEnabled = Standard_False;
196 if (!anOwner.IsNull())
198 aSelectable = anOwner->Selectable();
200 if (!aSelectable.IsNull())
202 if (!aSelectable->ClipPlanes().IsNull()
203 && aSelectable->ClipPlanes()->ToOverrideGlobal())
205 theMgr.SetViewClippingEnabled (Standard_False);
206 toRestoresViewClipEnabled = Standard_True;
208 else if (!aSelectable->TransformPersistence().IsNull())
210 if (aSelectable->TransformPersistence()->IsZoomOrRotate()
211 && !theMgr.ViewClipping().IsNull())
213 // Zoom/rotate persistence object lives in two worlds at the same time.
214 // Global clipping planes can not be trivially applied without being converted
215 // into local space of transformation persistence object.
216 // As more simple alternative - just clip entire object by its anchor point defined in the world space.
217 const Handle(Graphic3d_SequenceOfHClipPlane)& aViewPlanes = theMgr.ViewClipping();
219 const gp_Pnt anAnchor = aSelectable->TransformPersistence()->AnchorPoint();
220 for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (*aViewPlanes); aPlaneIt.More(); aPlaneIt.Next())
222 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
228 const Graphic3d_Vec4d aCheckPnt (anAnchor.X(), anAnchor.Y(), anAnchor.Z(), 1.0);
229 if (aPlane->ProbePoint (aCheckPnt) == Graphic3d_ClipState_Out)
236 theMgr.SetViewClippingEnabled (Standard_False);
237 toRestoresViewClipEnabled = Standard_True;
241 SelectBasics_PickResult aPickResult;
242 const Standard_Boolean isMatched = theEntity->Matches(theMgr, aPickResult);
243 if (toRestoresViewClipEnabled)
245 theMgr.SetViewClippingEnabled (Standard_True);
254 if (HasDepthClipping (anOwner)
255 && !aSelectable.IsNull()
256 && theMgr.GetActiveSelectionType() == SelectMgr_SelectingVolumeManager::Point)
258 Standard_Boolean isClipped = mySelectingVolumeMgr.IsClipped (*aSelectable->ClipPlanes(),
259 aPickResult.Depth());
264 SelectMgr_SortCriterion aCriterion;
265 myZLayerOrderMap.Find (!aSelectable.IsNull() ? aSelectable->ZLayer() : Graphic3d_ZLayerId_Default, aCriterion.ZLayerPosition);
266 aCriterion.Entity = theEntity;
267 aCriterion.Priority = anOwner->Priority();
268 aCriterion.Depth = aPickResult.Depth();
269 aCriterion.MinDist = aPickResult.DistToGeomCenter();
270 aCriterion.ToPreferClosest = preferclosest;
272 if (SelectMgr_SortCriterion* aPrevCriterion = mystored.ChangeSeek (anOwner))
274 ++aPrevCriterion->NbOwnerMatches;
275 aCriterion.NbOwnerMatches = aPrevCriterion->NbOwnerMatches;
276 if (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Box)
278 if (aCriterion > *aPrevCriterion)
280 updatePoint3d (aCriterion, theEntity, theInversedTrsf, theMgr);
281 *aPrevCriterion = aCriterion;
287 aCriterion.NbOwnerMatches = 1;
288 updatePoint3d (aCriterion, theEntity, theInversedTrsf, theMgr);
289 mystored.Add (anOwner, aCriterion);
293 //=======================================================================
294 // function: computeFrustum
295 // purpose : Internal function that checks if a current selecting frustum
296 // needs to be scaled and transformed for the entity and performs
297 // necessary calculations
298 //=======================================================================
299 void SelectMgr_ViewerSelector::computeFrustum (const Handle(SelectBasics_SensitiveEntity)& theEnt,
300 const SelectMgr_SelectingVolumeManager& theMgr,
301 const gp_GTrsf& theInvTrsf,
302 SelectMgr_FrustumCache& theCachedMgrs,
303 SelectMgr_SelectingVolumeManager& theResMgr)
305 Standard_Integer aScale = isToScaleFrustum (theEnt) ? sensitivity (theEnt) : 1;
306 const gp_GTrsf aTrsfMtr = theEnt->HasInitLocation() ? theEnt->InvInitLocation() * theInvTrsf : theInvTrsf;
307 const Standard_Boolean toScale = aScale != 1;
308 const Standard_Boolean toTransform = aTrsfMtr.Form() != gp_Identity;
309 if (toScale && toTransform)
311 theResMgr = theMgr.ScaleAndTransform (aScale, aTrsfMtr, NULL);
315 if (!theCachedMgrs.IsBound (aScale))
317 theCachedMgrs.Bind (aScale, theMgr.ScaleAndTransform (aScale, gp_Trsf(), NULL));
319 theResMgr = theCachedMgrs.Find (aScale);
321 else if (toTransform)
323 theResMgr = theMgr.ScaleAndTransform (1, aTrsfMtr, NULL);
327 //=======================================================================
328 // function: traverseObject
329 // purpose : Internal function that checks if there is possible overlap
330 // between some entity of selectable object theObject and
331 // current selecting volume
332 //=======================================================================
333 void SelectMgr_ViewerSelector::traverseObject (const Handle(SelectMgr_SelectableObject)& theObject,
334 const SelectMgr_SelectingVolumeManager& theMgr,
335 const Handle(Graphic3d_Camera)& theCamera,
336 const Graphic3d_Mat4d& theProjectionMat,
337 const Graphic3d_Mat4d& theWorldViewMat,
338 const Standard_Integer theViewportWidth,
339 const Standard_Integer theViewportHeight)
341 Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject);
342 if (anEntitySet->Size() == 0)
347 const opencascade::handle<BVH_Tree<Standard_Real, 3> >& aSensitivesTree = anEntitySet->BVH();
348 gp_GTrsf aInversedTrsf;
349 if (theObject->HasTransformation() || !theObject->TransformPersistence().IsNull())
351 if (theObject->TransformPersistence().IsNull())
353 aInversedTrsf = theObject->InversedTransformation();
358 Graphic3d_Mat4d aMat = theObject->TransformPersistence()->Compute (theCamera, theProjectionMat, theWorldViewMat, theViewportWidth, theViewportHeight);
360 aTPers.SetValue (1, 1, aMat.GetValue (0, 0));
361 aTPers.SetValue (1, 2, aMat.GetValue (0, 1));
362 aTPers.SetValue (1, 3, aMat.GetValue (0, 2));
363 aTPers.SetValue (2, 1, aMat.GetValue (1, 0));
364 aTPers.SetValue (2, 2, aMat.GetValue (1, 1));
365 aTPers.SetValue (2, 3, aMat.GetValue (1, 2));
366 aTPers.SetValue (3, 1, aMat.GetValue (2, 0));
367 aTPers.SetValue (3, 2, aMat.GetValue (2, 1));
368 aTPers.SetValue (3, 3, aMat.GetValue (2, 2));
369 aTPers.SetTranslationPart (gp_XYZ (aMat.GetValue (0, 3), aMat.GetValue (1, 3), aMat.GetValue (2, 3)));
371 aInversedTrsf = (aTPers * gp_GTrsf (theObject->Transformation())).Inverted();
375 SelectMgr_SelectingVolumeManager aMgr = aInversedTrsf.Form() != gp_Identity
376 ? theMgr.ScaleAndTransform (1, aInversedTrsf, NULL)
379 SelectMgr_FrustumCache aScaledTrnsfFrustums;
381 Standard_Integer aNode = 0; // a root node
382 if (!aMgr.Overlaps (aSensitivesTree->MinPoint (0),
383 aSensitivesTree->MaxPoint (0)))
388 const Standard_Integer aFirstStored = mystored.Extent() + 1;
390 Standard_Integer aStack[BVH_Constants_MaxTreeDepth];
391 Standard_Integer aHead = -1;
394 if (!aSensitivesTree->IsOuter (aNode))
396 const Standard_Integer aLeftChildIdx = aSensitivesTree->Child<0> (aNode);
397 const Standard_Integer aRightChildIdx = aSensitivesTree->Child<1> (aNode);
398 const Standard_Boolean isLeftChildIn = aMgr.Overlaps (aSensitivesTree->MinPoint (aLeftChildIdx),
399 aSensitivesTree->MaxPoint (aLeftChildIdx));
400 const Standard_Boolean isRightChildIn = aMgr.Overlaps (aSensitivesTree->MinPoint (aRightChildIdx),
401 aSensitivesTree->MaxPoint (aRightChildIdx));
405 aNode = aLeftChildIdx;
407 aStack[aHead] = aRightChildIdx;
409 else if (isLeftChildIn
412 aNode = isLeftChildIn ? aLeftChildIdx : aRightChildIdx;
421 aNode = aStack[aHead];
427 Standard_Integer aStartIdx = aSensitivesTree->BegPrimitive (aNode);
428 Standard_Integer anEndIdx = aSensitivesTree->EndPrimitive (aNode);
429 for (Standard_Integer anIdx = aStartIdx; anIdx <= anEndIdx; ++anIdx)
431 const Handle(SelectMgr_SensitiveEntity)& aSensitive = anEntitySet->GetSensitiveById (anIdx);
432 if (aSensitive->IsActiveForSelection())
434 const Handle(SelectBasics_SensitiveEntity)& anEnt = aSensitive->BaseSensitive();
435 SelectMgr_SelectingVolumeManager aTmpMgr = aMgr;
436 computeFrustum (anEnt, theMgr, aInversedTrsf, aScaledTrnsfFrustums, aTmpMgr);
437 checkOverlap (anEnt, aInversedTrsf, aTmpMgr);
445 aNode = aStack[aHead];
450 // in case of Box/Polyline selection - keep only Owners having all Entities detected
451 if (mySelectingVolumeMgr.IsOverlapAllowed()
452 || (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Box
453 && theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Polyline))
458 for (Standard_Integer aStoredIter = mystored.Extent(); aStoredIter >= aFirstStored; --aStoredIter)
460 const SelectMgr_SortCriterion& aCriterion = mystored.FindFromIndex (aStoredIter);
461 const Handle(SelectBasics_EntityOwner)& anOwner = aCriterion.Entity->OwnerId();
462 Standard_Integer aNbOwnerEntities = 0;
463 for (SelectMgr_IndexedMapOfHSensitive::Iterator aSensIter (anEntitySet->Sensitives()); aSensIter.More(); aSensIter.Next())
465 if (aSensIter.Value()->BaseSensitive()->OwnerId() == anOwner)
467 if (++aNbOwnerEntities > aCriterion.NbOwnerMatches)
469 // Remove from index map.
470 // Considering NCollection_IndexedDataMap implementation, the values for lower indexes will not be modified.
471 // Hence, just keep iterating in backward direction.
472 mystored.RemoveFromIndex (aStoredIter);
480 //=======================================================================
481 // function: TraverseSensitives
482 // purpose : Traverses BVH containing all added selectable objects and
483 // finds candidates for further search of overlap
484 //=======================================================================
485 void SelectMgr_ViewerSelector::TraverseSensitives()
489 Standard_Integer aWidth;
490 Standard_Integer aHeight;
491 mySelectingVolumeMgr.WindowSize (aWidth, aHeight);
492 mySelectableObjects.UpdateBVH (mySelectingVolumeMgr.Camera(),
493 mySelectingVolumeMgr.ProjectionMatrix(),
494 mySelectingVolumeMgr.WorldViewMatrix(),
495 mySelectingVolumeMgr.WorldViewProjState(),
497 const Handle(Graphic3d_Camera)& aCamera = mySelectingVolumeMgr.Camera();
498 if (!aCamera.IsNull())
500 myCameraEye = aCamera->Eye().XYZ();
501 myCameraDir = aCamera->Direction().XYZ();
502 myCameraScale = aCamera->IsOrthographic()
504 : 2.0 * Tan (aCamera->FOVy() * M_PI / 360.0);
505 const double aPixelSize = Max (1.0 / aWidth, 1.0 / aHeight);
506 myCameraScale *= aPixelSize;
509 for (Standard_Integer aBVHSetIt = 0; aBVHSetIt < SelectMgr_SelectableObjectSet::BVHSubsetNb; ++aBVHSetIt)
511 SelectMgr_SelectableObjectSet::BVHSubset aBVHSubset =
512 static_cast<SelectMgr_SelectableObjectSet::BVHSubset> (aBVHSetIt);
514 if (mySelectableObjects.IsEmpty (aBVHSubset))
521 SelectMgr_SelectingVolumeManager aMgr (Standard_False);
523 // for 2D space selection transform selecting volumes to perform overap testing
524 // directly in camera's eye space omitting the camera position, which is not
525 // needed there at all
526 if (aBVHSubset == SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent)
528 const Graphic3d_Mat4d& aMat = mySelectingVolumeMgr.WorldViewMatrix();
529 aTFrustum.SetValue (1, 1, aMat.GetValue (0, 0));
530 aTFrustum.SetValue (1, 2, aMat.GetValue (0, 1));
531 aTFrustum.SetValue (1, 3, aMat.GetValue (0, 2));
532 aTFrustum.SetValue (2, 1, aMat.GetValue (1, 0));
533 aTFrustum.SetValue (2, 2, aMat.GetValue (1, 1));
534 aTFrustum.SetValue (2, 3, aMat.GetValue (1, 2));
535 aTFrustum.SetValue (3, 1, aMat.GetValue (2, 0));
536 aTFrustum.SetValue (3, 2, aMat.GetValue (2, 1));
537 aTFrustum.SetValue (3, 3, aMat.GetValue (2, 2));
538 aTFrustum.SetTranslationPart (gp_XYZ (aMat.GetValue (0, 3), aMat.GetValue (1, 3), aMat.GetValue (2, 3)));
540 // define corresponding frustum builder parameters
541 Handle(SelectMgr_FrustumBuilder) aBuilder = new SelectMgr_FrustumBuilder();
542 aBuilder->SetProjectionMatrix (mySelectingVolumeMgr.ProjectionMatrix());
543 aBuilder->SetWorldViewMatrix (SelectMgr_ViewerSelector_THE_IDENTITY_MAT);
544 aBuilder->SetWindowSize (aWidth, aHeight);
545 aMgr = mySelectingVolumeMgr.ScaleAndTransform (1, aTFrustum, aBuilder);
549 aMgr = mySelectingVolumeMgr;
552 const Graphic3d_Mat4d& aProjectionMat = mySelectingVolumeMgr.ProjectionMatrix();
553 const Graphic3d_Mat4d& aWorldViewMat = aBVHSubset != SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent
554 ? mySelectingVolumeMgr.WorldViewMatrix()
555 : SelectMgr_ViewerSelector_THE_IDENTITY_MAT;
557 const opencascade::handle<BVH_Tree<Standard_Real, 3> >& aBVHTree = mySelectableObjects.BVH (aBVHSubset);
559 Standard_Integer aNode = 0;
560 if (!aMgr.Overlaps (aBVHTree->MinPoint (0), aBVHTree->MaxPoint (0)))
565 Standard_Integer aStack[BVH_Constants_MaxTreeDepth];
566 Standard_Integer aHead = -1;
569 if (!aBVHTree->IsOuter (aNode))
571 const Standard_Integer aLeftChildIdx = aBVHTree->Child<0> (aNode);
572 const Standard_Integer aRightChildIdx = aBVHTree->Child<1> (aNode);
573 const Standard_Boolean isLeftChildIn =
574 aMgr.Overlaps (aBVHTree->MinPoint (aLeftChildIdx), aBVHTree->MaxPoint (aLeftChildIdx));
575 const Standard_Boolean isRightChildIn =
576 aMgr.Overlaps (aBVHTree->MinPoint (aRightChildIdx), aBVHTree->MaxPoint (aRightChildIdx));
580 aNode = aLeftChildIdx;
582 aStack[aHead] = aRightChildIdx;
584 else if (isLeftChildIn
587 aNode = isLeftChildIn ? aLeftChildIdx : aRightChildIdx;
596 aNode = aStack[aHead];
602 Standard_Integer aStartIdx = aBVHTree->BegPrimitive (aNode);
603 Standard_Integer anEndIdx = aBVHTree->EndPrimitive (aNode);
604 for (Standard_Integer anIdx = aStartIdx; anIdx <= anEndIdx; ++anIdx)
606 const Handle(SelectMgr_SelectableObject)& aSelectableObject =
607 mySelectableObjects.GetObjectById (aBVHSubset, anIdx);
609 traverseObject (aSelectableObject, aMgr, aCamera, aProjectionMat, aWorldViewMat, aWidth, aHeight);
616 aNode = aStack[aHead];
625 //==================================================
626 // Function: ClearPicked
628 //==================================================
629 void SelectMgr_ViewerSelector::ClearPicked()
634 //==================================================
637 //==================================================
638 Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector
641 Standard_Integer RankInMap = myIndexes->Value (myCurRank);
642 const Handle(SelectBasics_EntityOwner)& toto = mystored.FindKey(RankInMap);
643 Handle(SelectMgr_EntityOwner) Ownr = Handle(SelectMgr_EntityOwner)::DownCast (toto);
647 //=======================================================================
650 //=======================================================================
651 Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector::Picked (const Standard_Integer theRank) const
653 Handle(SelectMgr_EntityOwner) anOwner;
654 if (theRank < 1 || theRank > NbPicked())
659 const Standard_Integer anOwnerIdx = myIndexes->Value (theRank);
660 const Handle(SelectBasics_EntityOwner)& aStoredOwner = mystored.FindKey (anOwnerIdx);
661 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (aStoredOwner);
665 //=======================================================================
666 //function : PickedData
668 //=======================================================================
669 const SelectMgr_SortCriterion& SelectMgr_ViewerSelector::PickedData(const Standard_Integer theRank) const
671 Standard_OutOfRange_Raise_if (theRank < 1 || theRank > NbPicked(), "SelectMgr_ViewerSelector::PickedData() out of range index");
672 const Standard_Integer anOwnerIdx = myIndexes->Value (theRank);
673 return mystored.FindFromIndex (anOwnerIdx);
676 //===================================================
678 // INTERNAL METHODS ....
680 //==================================================
682 //==================================================
683 // Function: SetEntitySetBuilder
685 //==================================================
686 void SelectMgr_ViewerSelector::SetEntitySetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder)
688 myEntitySetBuilder = theBuilder;
689 for (SelectMgr_MapOfObjectSensitives::Iterator aSetIter (myMapOfObjectSensitives); aSetIter.More(); aSetIter.Next())
691 aSetIter.ChangeValue()->SetBuilder (myEntitySetBuilder);
695 //==================================================
696 // Function: Contains
698 //==================================================
699 Standard_Boolean SelectMgr_ViewerSelector::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
701 return mySelectableObjects.Contains (theObject);
704 //==================================================
705 // Function: ActiveModes
706 // Purpose : return all the modes with a given state for an object
707 //==================================================
708 Standard_Boolean SelectMgr_ViewerSelector::Modes (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
709 TColStd_ListOfInteger& theModeList,
710 const SelectMgr_StateOfSelection theWantedState) const
712 Standard_Boolean hasActivatedStates = Contains (theSelectableObject);
713 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theSelectableObject->Selections()); aSelIter.More(); aSelIter.Next())
715 if (theWantedState == SelectMgr_SOS_Any)
717 theModeList.Append (aSelIter.Value()->Mode());
719 else if (theWantedState == aSelIter.Value()->GetSelectionState())
721 theModeList.Append (aSelIter.Value()->Mode());
725 return hasActivatedStates;
728 //==================================================
729 // Function: IsActive
731 //==================================================
732 Standard_Boolean SelectMgr_ViewerSelector::IsActive (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
733 const Standard_Integer theMode) const
735 if (!Contains (theSelectableObject))
736 return Standard_False;
738 const Handle(SelectMgr_Selection)& aSel = theSelectableObject->Selection (theMode);
739 return !aSel.IsNull()
740 && aSel->GetSelectionState() == SelectMgr_SOS_Activated;
743 //==================================================
744 // Function: IsInside
746 //==================================================
747 Standard_Boolean SelectMgr_ViewerSelector::IsInside (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
748 const Standard_Integer theMode) const
750 if (!Contains (theSelectableObject))
751 return Standard_False;
753 const Handle(SelectMgr_Selection)& aSel = theSelectableObject->Selection (theMode);
754 return !aSel.IsNull()
755 && aSel->GetSelectionState() != SelectMgr_SOS_Unknown;
759 //=======================================================================
762 //=======================================================================
764 SelectMgr_StateOfSelection SelectMgr_ViewerSelector::Status (const Handle(SelectMgr_Selection)& theSelection) const
766 return theSelection->GetSelectionState();
769 //==================================================
771 // Purpose : gives Information about selectors
772 //==================================================
774 TCollection_AsciiString SelectMgr_ViewerSelector::Status (const Handle(SelectMgr_SelectableObject)& theSelectableObject) const
776 TCollection_AsciiString aStatus ("Status Object :\n\t");
777 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theSelectableObject->Selections()); aSelIter.More(); aSelIter.Next())
779 if (aSelIter.Value()->GetSelectionState() != SelectMgr_SOS_Unknown)
781 aStatus = aStatus + "Mode " + TCollection_AsciiString (aSelIter.Value()->Mode()) + " present - "
782 + (aSelIter.Value()->GetSelectionState() == SelectMgr_SOS_Activated ? " Active \n\t" : " Inactive \n\t");
786 if (!Contains (theSelectableObject))
788 aStatus = aStatus + "Not Present in the selector\n\n";
794 //=======================================================================
795 //function : SortResult
796 //purpose : there is a certain number of entities ranged by criteria
797 // (depth, size, priority, mouse distance from borders or
798 // CDG of the detected primitive. Parsing :
799 // maximum priorities .
800 // then a reasonable compromise between depth and distance...
801 // finally the ranges are stored in myindexes depending on the parsing.
802 // so, it is possible to only read
803 //=======================================================================
804 void SelectMgr_ViewerSelector::SortResult()
806 if(mystored.IsEmpty()) return;
808 const Standard_Integer anExtent = mystored.Extent();
809 if(myIndexes.IsNull() || anExtent != myIndexes->Length())
810 myIndexes = new TColStd_HArray1OfInteger (1, anExtent);
812 TColStd_Array1OfInteger& anIndexArray = myIndexes->ChangeArray1();
813 for (Standard_Integer anIndexIter = 1; anIndexIter <= anExtent; ++anIndexIter)
815 anIndexArray.SetValue (anIndexIter, anIndexIter);
817 std::sort (anIndexArray.begin(), anIndexArray.end(), CompareResults (mystored));
820 //=======================================================================
821 //function : HasDepthClipping
823 //=======================================================================
824 Standard_Boolean SelectMgr_ViewerSelector::HasDepthClipping (const Handle(SelectMgr_EntityOwner)& /*theOwner*/) const
826 return Standard_False;
829 //=======================================================================
830 // function : AddSelectableObject
831 // purpose : Adds new object to the map of selectable objects
832 //=======================================================================
833 void SelectMgr_ViewerSelector::AddSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
835 if (!myMapOfObjectSensitives.IsBound (theObject))
837 mySelectableObjects.Append (theObject);
838 Handle(SelectMgr_SensitiveEntitySet) anEntitySet = new SelectMgr_SensitiveEntitySet (myEntitySetBuilder);
839 myMapOfObjectSensitives.Bind (theObject, anEntitySet);
843 //=======================================================================
844 // function : AddSelectionToObject
845 // purpose : Adds new selection to the object and builds its BVH tree
846 //=======================================================================
847 void SelectMgr_ViewerSelector::AddSelectionToObject (const Handle(SelectMgr_SelectableObject)& theObject,
848 const Handle(SelectMgr_Selection)& theSelection)
850 if (Handle(SelectMgr_SensitiveEntitySet)* anEntitySet = myMapOfObjectSensitives.ChangeSeek (theObject))
852 (*anEntitySet)->Append (theSelection);
853 (*anEntitySet)->BVH();
857 AddSelectableObject (theObject);
858 AddSelectionToObject (theObject, theSelection);
862 //=======================================================================
863 // function : MoveSelectableObject
865 //=======================================================================
866 void SelectMgr_ViewerSelector::MoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
868 mySelectableObjects.ChangeSubset (theObject);
871 //=======================================================================
872 // function : RemoveSelectableObject
873 // purpose : Removes selectable object from map of selectable ones
874 //=======================================================================
875 void SelectMgr_ViewerSelector::RemoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
877 Handle(SelectMgr_SelectableObject) anObj = theObject;
878 if (myMapOfObjectSensitives.UnBind (theObject))
880 mySelectableObjects.Remove (theObject);
884 //=======================================================================
885 // function : RemoveSelectionOfObject
886 // purpose : Removes selection of the object and marks its BVH tree
888 //=======================================================================
889 void SelectMgr_ViewerSelector::RemoveSelectionOfObject (const Handle(SelectMgr_SelectableObject)& theObject,
890 const Handle(SelectMgr_Selection)& theSelection)
892 if (Handle(SelectMgr_SensitiveEntitySet)* anEntitySet = myMapOfObjectSensitives.ChangeSeek (theObject))
894 (*anEntitySet)->Remove (theSelection);
898 //=======================================================================
899 // function : RebuildObjectsTree
900 // purpose : Marks BVH of selectable objects for rebuild
901 //=======================================================================
902 void SelectMgr_ViewerSelector::RebuildObjectsTree (const Standard_Boolean theIsForce)
904 mySelectableObjects.MarkDirty();
908 Standard_Integer aViewportWidth, aViewportHeight;
909 mySelectingVolumeMgr.WindowSize (aViewportWidth, aViewportHeight);
911 Standard_Integer aWidth;
912 Standard_Integer aHeight;
913 mySelectingVolumeMgr.WindowSize (aWidth, aHeight);
914 mySelectableObjects.UpdateBVH (mySelectingVolumeMgr.Camera(),
915 mySelectingVolumeMgr.ProjectionMatrix(),
916 mySelectingVolumeMgr.WorldViewMatrix(),
917 mySelectingVolumeMgr.WorldViewProjState(),
922 //=======================================================================
923 // function : RebuildSensitivesTree
924 // purpose : Marks BVH of sensitive entities of particular selectable
925 // object for rebuild
926 //=======================================================================
927 void SelectMgr_ViewerSelector::RebuildSensitivesTree (const Handle(SelectMgr_SelectableObject)& theObject,
928 const Standard_Boolean theIsForce)
930 if (!Contains (theObject))
933 Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject);
934 anEntitySet->MarkDirty();
942 //=======================================================================
943 // function : resetSelectionActivationStatus
944 // purpose : Marks all added sensitive entities of all objects as
946 //=======================================================================
947 void SelectMgr_ViewerSelector::ResetSelectionActivationStatus()
949 for (SelectMgr_MapOfObjectSensitivesIterator aSensitivesIter (myMapOfObjectSensitives); aSensitivesIter.More(); aSensitivesIter.Next())
951 Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = aSensitivesIter.ChangeValue();
952 const Standard_Integer anEntitiesNb = anEntitySet->Size();
953 for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx)
955 anEntitySet->GetSensitiveById (anIdx)->ResetSelectionActiveStatus();
960 //=======================================================================
961 // function : DetectedEntity
962 // purpose : Returns sensitive entity that was detected during the
963 // previous run of selection algorithm
964 //=======================================================================
965 const Handle(SelectBasics_SensitiveEntity)& SelectMgr_ViewerSelector::DetectedEntity() const
967 const Standard_Integer aRankInMap = myIndexes->Value(myCurRank);
968 return mystored.FindFromIndex (aRankInMap).Entity;
971 //=======================================================================
972 // function : ActiveOwners
973 // purpose : Returns the list of active entity owners
974 //=======================================================================
975 void SelectMgr_ViewerSelector::ActiveOwners (NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners) const
977 for (SelectMgr_MapOfObjectSensitivesIterator anIter (myMapOfObjectSensitives); anIter.More(); anIter.Next())
979 const Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = anIter.Value();
980 const Standard_Integer anEntitiesNb = anEntitySet->Size();
981 for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx)
983 const Handle(SelectMgr_SensitiveEntity)& aSensitive = anEntitySet->GetSensitiveById (anIdx);
984 if (aSensitive->IsActiveForSelection())
986 theOwners.Append (aSensitive->BaseSensitive()->OwnerId());
992 //=======================================================================
993 //function : AllowOverlapDetection
994 //purpose : Sets the detection type: if theIsToAllow is false,
995 // only fully included sensitives will be detected, otherwise
996 // the algorithm will mark both included and overlapped entities
998 //=======================================================================
999 void SelectMgr_ViewerSelector::AllowOverlapDetection (const Standard_Boolean theIsToAllow)
1001 mySelectingVolumeMgr.AllowOverlapDetection (theIsToAllow);