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 SelectBasics_PickResult& thePickResult,
71 const Handle(SelectBasics_SensitiveEntity)& theEntity,
72 const gp_GTrsf& theInversedTrsf,
73 const SelectMgr_SelectingVolumeManager& theMgr) const
75 if (theMgr.GetActiveSelectionType() != SelectMgr_SelectingVolumeManager::Point)
80 if (thePickResult.HasPickedPoint())
82 theCriterion.Point = thePickResult.PickedPoint();
86 theCriterion.Point = theMgr.DetectedPoint (theCriterion.Depth);
89 gp_GTrsf anInvTrsf = theInversedTrsf;
90 if (theCriterion.Entity->HasInitLocation())
92 anInvTrsf = theCriterion.Entity->InvInitLocation() * anInvTrsf;
94 if (anInvTrsf.Form() != gp_Identity)
96 anInvTrsf.Inverted().Transforms (theCriterion.Point.ChangeCoord());
99 if (mySelectingVolumeMgr.Camera().IsNull())
101 theCriterion.Tolerance = theEntity->SensitivityFactor() / 33.0;
103 else if (mySelectingVolumeMgr.Camera()->IsOrthographic())
105 theCriterion.Tolerance = myCameraScale * theEntity->SensitivityFactor();
109 const Standard_Real aDistFromEye = (theCriterion.Point.XYZ() - myCameraEye.XYZ()).Dot (myCameraDir.XYZ());
110 theCriterion.Tolerance = aDistFromEye * myCameraScale * theEntity->SensitivityFactor();
114 //==================================================
115 // Function: Initialize
117 //==================================================
118 SelectMgr_ViewerSelector::SelectMgr_ViewerSelector():
119 preferclosest(Standard_True),
120 myToUpdateTolerance (Standard_True),
123 myIsLeftChildQueuedFirst (Standard_False),
126 myEntitySetBuilder = new BVH_BinnedBuilder<Standard_Real, 3, 4> (BVH_Constants_LeafNodeSizeSingle, BVH_Constants_MaxTreeDepth, Standard_True);
129 //==================================================
130 // Function: Activate
132 //==================================================
133 void SelectMgr_ViewerSelector::Activate (const Handle(SelectMgr_Selection)& theSelection)
135 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
137 aSelEntIter.Value()->SetActiveForSelection();
140 theSelection->SetSelectionState (SelectMgr_SOS_Activated);
142 myTolerances.Add (theSelection->Sensitivity());
143 myToUpdateTolerance = Standard_True;
146 //==================================================
147 // Function: Deactivate
149 //==================================================
150 void SelectMgr_ViewerSelector::Deactivate (const Handle(SelectMgr_Selection)& theSelection)
152 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
154 aSelEntIter.Value()->ResetSelectionActiveStatus();
157 theSelection->SetSelectionState (SelectMgr_SOS_Deactivated);
159 myTolerances.Decrement (theSelection->Sensitivity());
160 myToUpdateTolerance = Standard_True;
163 //==================================================
166 //==================================================
167 void SelectMgr_ViewerSelector::Clear()
172 //=======================================================================
173 // function: isToScaleFrustum
174 // purpose : Checks if the entity given requires to scale current selecting frustum
175 //=======================================================================
176 Standard_Boolean SelectMgr_ViewerSelector::isToScaleFrustum (const Handle(SelectBasics_SensitiveEntity)& theEntity)
178 return mySelectingVolumeMgr.GetActiveSelectionType() == SelectMgr_SelectingVolumeManager::Point
179 && sensitivity (theEntity) < myTolerances.Tolerance();
182 //=======================================================================
183 // function: sensitivity
184 // purpose : In case if custom tolerance is set, this method will return sum of entity
185 // sensitivity and custom tolerance.
186 //=======================================================================
187 Standard_Integer SelectMgr_ViewerSelector::sensitivity (const Handle(SelectBasics_SensitiveEntity)& theEntity) const
189 return myTolerances.IsCustomTolSet() ?
190 theEntity->SensitivityFactor() + myTolerances.CustomTolerance() : theEntity->SensitivityFactor();
193 //=======================================================================
194 // function: checkOverlap
195 // purpose : Internal function that checks if a particular sensitive
196 // entity theEntity overlaps current selecting volume precisely
197 //=======================================================================
198 void SelectMgr_ViewerSelector::checkOverlap (const Handle(SelectBasics_SensitiveEntity)& theEntity,
199 const gp_GTrsf& theInversedTrsf,
200 SelectMgr_SelectingVolumeManager& theMgr)
202 Handle(SelectMgr_EntityOwner) anOwner (Handle(SelectMgr_EntityOwner)::DownCast (theEntity->OwnerId()));
203 Handle(SelectMgr_SelectableObject) aSelectable;
204 Standard_Boolean toRestoresViewClipEnabled = Standard_False;
205 if (!anOwner.IsNull())
207 aSelectable = anOwner->Selectable();
209 if (!aSelectable.IsNull())
211 if (!aSelectable->ClipPlanes().IsNull()
212 && aSelectable->ClipPlanes()->ToOverrideGlobal())
214 theMgr.SetViewClippingEnabled (Standard_False);
215 toRestoresViewClipEnabled = Standard_True;
217 else if (!aSelectable->TransformPersistence().IsNull())
219 if (aSelectable->TransformPersistence()->IsZoomOrRotate()
220 && !theMgr.ViewClipping().IsNull())
222 // Zoom/rotate persistence object lives in two worlds at the same time.
223 // Global clipping planes can not be trivially applied without being converted
224 // into local space of transformation persistence object.
225 // As more simple alternative - just clip entire object by its anchor point defined in the world space.
226 const Handle(Graphic3d_SequenceOfHClipPlane)& aViewPlanes = theMgr.ViewClipping();
228 const gp_Pnt anAnchor = aSelectable->TransformPersistence()->AnchorPoint();
229 for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (*aViewPlanes); aPlaneIt.More(); aPlaneIt.Next())
231 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
237 const Graphic3d_Vec4d aCheckPnt (anAnchor.X(), anAnchor.Y(), anAnchor.Z(), 1.0);
238 if (aPlane->ProbePoint (aCheckPnt) == Graphic3d_ClipState_Out)
245 theMgr.SetViewClippingEnabled (Standard_False);
246 toRestoresViewClipEnabled = Standard_True;
250 SelectBasics_PickResult aPickResult;
251 const Standard_Boolean isMatched = theEntity->Matches(theMgr, aPickResult);
252 if (toRestoresViewClipEnabled)
254 theMgr.SetViewClippingEnabled (Standard_True);
263 if (HasDepthClipping (anOwner)
264 && !aSelectable.IsNull()
265 && theMgr.GetActiveSelectionType() == SelectMgr_SelectingVolumeManager::Point)
267 Standard_Boolean isClipped = mySelectingVolumeMgr.IsClipped (*aSelectable->ClipPlanes(),
268 aPickResult.Depth());
273 SelectMgr_SortCriterion aCriterion;
274 myZLayerOrderMap.Find (!aSelectable.IsNull() ? aSelectable->ZLayer() : Graphic3d_ZLayerId_Default, aCriterion.ZLayerPosition);
275 aCriterion.Entity = theEntity;
276 aCriterion.Priority = anOwner->Priority();
277 aCriterion.Depth = aPickResult.Depth();
278 aCriterion.MinDist = aPickResult.DistToGeomCenter();
279 aCriterion.ToPreferClosest = preferclosest;
281 if (SelectMgr_SortCriterion* aPrevCriterion = mystored.ChangeSeek (anOwner))
283 ++aPrevCriterion->NbOwnerMatches;
284 aCriterion.NbOwnerMatches = aPrevCriterion->NbOwnerMatches;
285 if (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Box)
287 if (aCriterion > *aPrevCriterion)
289 updatePoint3d (aCriterion, aPickResult, theEntity, theInversedTrsf, theMgr);
290 *aPrevCriterion = aCriterion;
296 aCriterion.NbOwnerMatches = 1;
297 updatePoint3d (aCriterion, aPickResult, theEntity, theInversedTrsf, theMgr);
298 mystored.Add (anOwner, aCriterion);
302 //=======================================================================
303 // function: computeFrustum
304 // purpose : Internal function that checks if a current selecting frustum
305 // needs to be scaled and transformed for the entity and performs
306 // necessary calculations
307 //=======================================================================
308 void SelectMgr_ViewerSelector::computeFrustum (const Handle(SelectBasics_SensitiveEntity)& theEnt,
309 const SelectMgr_SelectingVolumeManager& theMgr,
310 const gp_GTrsf& theInvTrsf,
311 SelectMgr_FrustumCache& theCachedMgrs,
312 SelectMgr_SelectingVolumeManager& theResMgr)
314 Standard_Integer aScale = isToScaleFrustum (theEnt) ? sensitivity (theEnt) : 1;
315 const gp_GTrsf aTrsfMtr = theEnt->HasInitLocation() ? theEnt->InvInitLocation() * theInvTrsf : theInvTrsf;
316 const Standard_Boolean toScale = aScale != 1;
317 const Standard_Boolean toTransform = aTrsfMtr.Form() != gp_Identity;
318 if (toScale && toTransform)
320 theResMgr = theMgr.ScaleAndTransform (aScale, aTrsfMtr, NULL);
324 if (!theCachedMgrs.IsBound (aScale))
326 theCachedMgrs.Bind (aScale, theMgr.ScaleAndTransform (aScale, gp_Trsf(), NULL));
328 theResMgr = theCachedMgrs.Find (aScale);
330 else if (toTransform)
332 theResMgr = theMgr.ScaleAndTransform (1, aTrsfMtr, NULL);
336 //=======================================================================
337 // function: traverseObject
338 // purpose : Internal function that checks if there is possible overlap
339 // between some entity of selectable object theObject and
340 // current selecting volume
341 //=======================================================================
342 void SelectMgr_ViewerSelector::traverseObject (const Handle(SelectMgr_SelectableObject)& theObject,
343 const SelectMgr_SelectingVolumeManager& theMgr,
344 const Handle(Graphic3d_Camera)& theCamera,
345 const Graphic3d_Mat4d& theProjectionMat,
346 const Graphic3d_Mat4d& theWorldViewMat,
347 const Standard_Integer theViewportWidth,
348 const Standard_Integer theViewportHeight)
350 Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject);
351 if (anEntitySet->Size() == 0)
356 const opencascade::handle<BVH_Tree<Standard_Real, 3> >& aSensitivesTree = anEntitySet->BVH();
357 gp_GTrsf aInversedTrsf;
358 if (theObject->HasTransformation() || !theObject->TransformPersistence().IsNull())
360 if (theObject->TransformPersistence().IsNull())
362 aInversedTrsf = theObject->InversedTransformation();
367 Graphic3d_Mat4d aMat = theObject->TransformPersistence()->Compute (theCamera, theProjectionMat, theWorldViewMat, theViewportWidth, theViewportHeight);
369 aTPers.SetValue (1, 1, aMat.GetValue (0, 0));
370 aTPers.SetValue (1, 2, aMat.GetValue (0, 1));
371 aTPers.SetValue (1, 3, aMat.GetValue (0, 2));
372 aTPers.SetValue (2, 1, aMat.GetValue (1, 0));
373 aTPers.SetValue (2, 2, aMat.GetValue (1, 1));
374 aTPers.SetValue (2, 3, aMat.GetValue (1, 2));
375 aTPers.SetValue (3, 1, aMat.GetValue (2, 0));
376 aTPers.SetValue (3, 2, aMat.GetValue (2, 1));
377 aTPers.SetValue (3, 3, aMat.GetValue (2, 2));
378 aTPers.SetTranslationPart (gp_XYZ (aMat.GetValue (0, 3), aMat.GetValue (1, 3), aMat.GetValue (2, 3)));
380 aInversedTrsf = (aTPers * gp_GTrsf (theObject->Transformation())).Inverted();
384 SelectMgr_SelectingVolumeManager aMgr = aInversedTrsf.Form() != gp_Identity
385 ? theMgr.ScaleAndTransform (1, aInversedTrsf, NULL)
388 SelectMgr_FrustumCache aScaledTrnsfFrustums;
390 Standard_Integer aNode = 0; // a root node
391 if (!aMgr.Overlaps (aSensitivesTree->MinPoint (0),
392 aSensitivesTree->MaxPoint (0)))
397 const Standard_Integer aFirstStored = mystored.Extent() + 1;
399 Standard_Integer aStack[BVH_Constants_MaxTreeDepth];
400 Standard_Integer aHead = -1;
403 if (!aSensitivesTree->IsOuter (aNode))
405 const Standard_Integer aLeftChildIdx = aSensitivesTree->Child<0> (aNode);
406 const Standard_Integer aRightChildIdx = aSensitivesTree->Child<1> (aNode);
407 const Standard_Boolean isLeftChildIn = aMgr.Overlaps (aSensitivesTree->MinPoint (aLeftChildIdx),
408 aSensitivesTree->MaxPoint (aLeftChildIdx));
409 const Standard_Boolean isRightChildIn = aMgr.Overlaps (aSensitivesTree->MinPoint (aRightChildIdx),
410 aSensitivesTree->MaxPoint (aRightChildIdx));
414 aNode = aLeftChildIdx;
416 aStack[aHead] = aRightChildIdx;
418 else if (isLeftChildIn
421 aNode = isLeftChildIn ? aLeftChildIdx : aRightChildIdx;
430 aNode = aStack[aHead];
436 Standard_Integer aStartIdx = aSensitivesTree->BegPrimitive (aNode);
437 Standard_Integer anEndIdx = aSensitivesTree->EndPrimitive (aNode);
438 for (Standard_Integer anIdx = aStartIdx; anIdx <= anEndIdx; ++anIdx)
440 const Handle(SelectMgr_SensitiveEntity)& aSensitive = anEntitySet->GetSensitiveById (anIdx);
441 if (aSensitive->IsActiveForSelection())
443 const Handle(SelectBasics_SensitiveEntity)& anEnt = aSensitive->BaseSensitive();
444 SelectMgr_SelectingVolumeManager aTmpMgr = aMgr;
445 computeFrustum (anEnt, theMgr, aInversedTrsf, aScaledTrnsfFrustums, aTmpMgr);
446 checkOverlap (anEnt, aInversedTrsf, aTmpMgr);
454 aNode = aStack[aHead];
459 // in case of Box/Polyline selection - keep only Owners having all Entities detected
460 if (mySelectingVolumeMgr.IsOverlapAllowed()
461 || (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Box
462 && theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Polyline))
467 for (Standard_Integer aStoredIter = mystored.Extent(); aStoredIter >= aFirstStored; --aStoredIter)
469 const SelectMgr_SortCriterion& aCriterion = mystored.FindFromIndex (aStoredIter);
470 const Handle(SelectBasics_EntityOwner)& anOwner = aCriterion.Entity->OwnerId();
471 Standard_Integer aNbOwnerEntities = 0;
472 for (SelectMgr_IndexedMapOfHSensitive::Iterator aSensIter (anEntitySet->Sensitives()); aSensIter.More(); aSensIter.Next())
474 if (aSensIter.Value()->BaseSensitive()->OwnerId() == anOwner)
476 if (++aNbOwnerEntities > aCriterion.NbOwnerMatches)
478 // Remove from index map.
479 // Considering NCollection_IndexedDataMap implementation, the values for lower indexes will not be modified.
480 // Hence, just keep iterating in backward direction.
481 mystored.RemoveFromIndex (aStoredIter);
489 //=======================================================================
490 // function: TraverseSensitives
491 // purpose : Traverses BVH containing all added selectable objects and
492 // finds candidates for further search of overlap
493 //=======================================================================
494 void SelectMgr_ViewerSelector::TraverseSensitives()
498 Standard_Integer aWidth;
499 Standard_Integer aHeight;
500 mySelectingVolumeMgr.WindowSize (aWidth, aHeight);
501 mySelectableObjects.UpdateBVH (mySelectingVolumeMgr.Camera(),
502 mySelectingVolumeMgr.ProjectionMatrix(),
503 mySelectingVolumeMgr.WorldViewMatrix(),
504 mySelectingVolumeMgr.WorldViewProjState(),
506 const Handle(Graphic3d_Camera)& aCamera = mySelectingVolumeMgr.Camera();
507 if (!aCamera.IsNull())
509 myCameraEye = aCamera->Eye().XYZ();
510 myCameraDir = aCamera->Direction().XYZ();
511 myCameraScale = aCamera->IsOrthographic()
513 : 2.0 * Tan (aCamera->FOVy() * M_PI / 360.0);
514 const double aPixelSize = Max (1.0 / aWidth, 1.0 / aHeight);
515 myCameraScale *= aPixelSize;
518 for (Standard_Integer aBVHSetIt = 0; aBVHSetIt < SelectMgr_SelectableObjectSet::BVHSubsetNb; ++aBVHSetIt)
520 SelectMgr_SelectableObjectSet::BVHSubset aBVHSubset =
521 static_cast<SelectMgr_SelectableObjectSet::BVHSubset> (aBVHSetIt);
523 if (mySelectableObjects.IsEmpty (aBVHSubset))
530 SelectMgr_SelectingVolumeManager aMgr (Standard_False);
532 // for 2D space selection transform selecting volumes to perform overap testing
533 // directly in camera's eye space omitting the camera position, which is not
534 // needed there at all
535 if (aBVHSubset == SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent)
537 const Graphic3d_Mat4d& aMat = mySelectingVolumeMgr.WorldViewMatrix();
538 aTFrustum.SetValue (1, 1, aMat.GetValue (0, 0));
539 aTFrustum.SetValue (1, 2, aMat.GetValue (0, 1));
540 aTFrustum.SetValue (1, 3, aMat.GetValue (0, 2));
541 aTFrustum.SetValue (2, 1, aMat.GetValue (1, 0));
542 aTFrustum.SetValue (2, 2, aMat.GetValue (1, 1));
543 aTFrustum.SetValue (2, 3, aMat.GetValue (1, 2));
544 aTFrustum.SetValue (3, 1, aMat.GetValue (2, 0));
545 aTFrustum.SetValue (3, 2, aMat.GetValue (2, 1));
546 aTFrustum.SetValue (3, 3, aMat.GetValue (2, 2));
547 aTFrustum.SetTranslationPart (gp_XYZ (aMat.GetValue (0, 3), aMat.GetValue (1, 3), aMat.GetValue (2, 3)));
549 // define corresponding frustum builder parameters
550 Handle(SelectMgr_FrustumBuilder) aBuilder = new SelectMgr_FrustumBuilder();
551 aBuilder->SetProjectionMatrix (mySelectingVolumeMgr.ProjectionMatrix());
552 aBuilder->SetWorldViewMatrix (SelectMgr_ViewerSelector_THE_IDENTITY_MAT);
553 aBuilder->SetWindowSize (aWidth, aHeight);
554 aMgr = mySelectingVolumeMgr.ScaleAndTransform (1, aTFrustum, aBuilder);
558 aMgr = mySelectingVolumeMgr;
561 const Graphic3d_Mat4d& aProjectionMat = mySelectingVolumeMgr.ProjectionMatrix();
562 const Graphic3d_Mat4d& aWorldViewMat = aBVHSubset != SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent
563 ? mySelectingVolumeMgr.WorldViewMatrix()
564 : SelectMgr_ViewerSelector_THE_IDENTITY_MAT;
566 const opencascade::handle<BVH_Tree<Standard_Real, 3> >& aBVHTree = mySelectableObjects.BVH (aBVHSubset);
568 Standard_Integer aNode = 0;
569 if (!aMgr.Overlaps (aBVHTree->MinPoint (0), aBVHTree->MaxPoint (0)))
574 Standard_Integer aStack[BVH_Constants_MaxTreeDepth];
575 Standard_Integer aHead = -1;
578 if (!aBVHTree->IsOuter (aNode))
580 const Standard_Integer aLeftChildIdx = aBVHTree->Child<0> (aNode);
581 const Standard_Integer aRightChildIdx = aBVHTree->Child<1> (aNode);
582 const Standard_Boolean isLeftChildIn =
583 aMgr.Overlaps (aBVHTree->MinPoint (aLeftChildIdx), aBVHTree->MaxPoint (aLeftChildIdx));
584 const Standard_Boolean isRightChildIn =
585 aMgr.Overlaps (aBVHTree->MinPoint (aRightChildIdx), aBVHTree->MaxPoint (aRightChildIdx));
589 aNode = aLeftChildIdx;
591 aStack[aHead] = aRightChildIdx;
593 else if (isLeftChildIn
596 aNode = isLeftChildIn ? aLeftChildIdx : aRightChildIdx;
605 aNode = aStack[aHead];
611 Standard_Integer aStartIdx = aBVHTree->BegPrimitive (aNode);
612 Standard_Integer anEndIdx = aBVHTree->EndPrimitive (aNode);
613 for (Standard_Integer anIdx = aStartIdx; anIdx <= anEndIdx; ++anIdx)
615 const Handle(SelectMgr_SelectableObject)& aSelectableObject =
616 mySelectableObjects.GetObjectById (aBVHSubset, anIdx);
618 traverseObject (aSelectableObject, aMgr, aCamera, aProjectionMat, aWorldViewMat, aWidth, aHeight);
625 aNode = aStack[aHead];
634 //==================================================
635 // Function: ClearPicked
637 //==================================================
638 void SelectMgr_ViewerSelector::ClearPicked()
643 //==================================================
646 //==================================================
647 Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector
650 Standard_Integer RankInMap = myIndexes->Value (myCurRank);
651 const Handle(SelectBasics_EntityOwner)& toto = mystored.FindKey(RankInMap);
652 Handle(SelectMgr_EntityOwner) Ownr = Handle(SelectMgr_EntityOwner)::DownCast (toto);
656 //=======================================================================
659 //=======================================================================
660 Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector::Picked (const Standard_Integer theRank) const
662 Handle(SelectMgr_EntityOwner) anOwner;
663 if (theRank < 1 || theRank > NbPicked())
668 const Standard_Integer anOwnerIdx = myIndexes->Value (theRank);
669 const Handle(SelectBasics_EntityOwner)& aStoredOwner = mystored.FindKey (anOwnerIdx);
670 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (aStoredOwner);
674 //=======================================================================
675 //function : PickedData
677 //=======================================================================
678 const SelectMgr_SortCriterion& SelectMgr_ViewerSelector::PickedData(const Standard_Integer theRank) const
680 Standard_OutOfRange_Raise_if (theRank < 1 || theRank > NbPicked(), "SelectMgr_ViewerSelector::PickedData() out of range index");
681 const Standard_Integer anOwnerIdx = myIndexes->Value (theRank);
682 return mystored.FindFromIndex (anOwnerIdx);
685 //===================================================
687 // INTERNAL METHODS ....
689 //==================================================
691 //==================================================
692 // Function: SetEntitySetBuilder
694 //==================================================
695 void SelectMgr_ViewerSelector::SetEntitySetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder)
697 myEntitySetBuilder = theBuilder;
698 for (SelectMgr_MapOfObjectSensitives::Iterator aSetIter (myMapOfObjectSensitives); aSetIter.More(); aSetIter.Next())
700 aSetIter.ChangeValue()->SetBuilder (myEntitySetBuilder);
704 //==================================================
705 // Function: Contains
707 //==================================================
708 Standard_Boolean SelectMgr_ViewerSelector::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
710 return mySelectableObjects.Contains (theObject);
713 //==================================================
714 // Function: ActiveModes
715 // Purpose : return all the modes with a given state for an object
716 //==================================================
717 Standard_Boolean SelectMgr_ViewerSelector::Modes (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
718 TColStd_ListOfInteger& theModeList,
719 const SelectMgr_StateOfSelection theWantedState) const
721 Standard_Boolean hasActivatedStates = Contains (theSelectableObject);
722 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theSelectableObject->Selections()); aSelIter.More(); aSelIter.Next())
724 if (theWantedState == SelectMgr_SOS_Any)
726 theModeList.Append (aSelIter.Value()->Mode());
728 else if (theWantedState == aSelIter.Value()->GetSelectionState())
730 theModeList.Append (aSelIter.Value()->Mode());
734 return hasActivatedStates;
737 //==================================================
738 // Function: IsActive
740 //==================================================
741 Standard_Boolean SelectMgr_ViewerSelector::IsActive (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
742 const Standard_Integer theMode) const
744 if (!Contains (theSelectableObject))
745 return Standard_False;
747 const Handle(SelectMgr_Selection)& aSel = theSelectableObject->Selection (theMode);
748 return !aSel.IsNull()
749 && aSel->GetSelectionState() == SelectMgr_SOS_Activated;
752 //==================================================
753 // Function: IsInside
755 //==================================================
756 Standard_Boolean SelectMgr_ViewerSelector::IsInside (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
757 const Standard_Integer theMode) const
759 if (!Contains (theSelectableObject))
760 return Standard_False;
762 const Handle(SelectMgr_Selection)& aSel = theSelectableObject->Selection (theMode);
763 return !aSel.IsNull()
764 && aSel->GetSelectionState() != SelectMgr_SOS_Unknown;
768 //=======================================================================
771 //=======================================================================
773 SelectMgr_StateOfSelection SelectMgr_ViewerSelector::Status (const Handle(SelectMgr_Selection)& theSelection) const
775 return theSelection->GetSelectionState();
778 //==================================================
780 // Purpose : gives Information about selectors
781 //==================================================
783 TCollection_AsciiString SelectMgr_ViewerSelector::Status (const Handle(SelectMgr_SelectableObject)& theSelectableObject) const
785 TCollection_AsciiString aStatus ("Status Object :\n\t");
786 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theSelectableObject->Selections()); aSelIter.More(); aSelIter.Next())
788 if (aSelIter.Value()->GetSelectionState() != SelectMgr_SOS_Unknown)
790 aStatus = aStatus + "Mode " + TCollection_AsciiString (aSelIter.Value()->Mode()) + " present - "
791 + (aSelIter.Value()->GetSelectionState() == SelectMgr_SOS_Activated ? " Active \n\t" : " Inactive \n\t");
795 if (!Contains (theSelectableObject))
797 aStatus = aStatus + "Not Present in the selector\n\n";
803 //=======================================================================
804 //function : SortResult
805 //purpose : there is a certain number of entities ranged by criteria
806 // (depth, size, priority, mouse distance from borders or
807 // CDG of the detected primitive. Parsing :
808 // maximum priorities .
809 // then a reasonable compromise between depth and distance...
810 // finally the ranges are stored in myindexes depending on the parsing.
811 // so, it is possible to only read
812 //=======================================================================
813 void SelectMgr_ViewerSelector::SortResult()
815 if(mystored.IsEmpty()) return;
817 const Standard_Integer anExtent = mystored.Extent();
818 if(myIndexes.IsNull() || anExtent != myIndexes->Length())
819 myIndexes = new TColStd_HArray1OfInteger (1, anExtent);
821 TColStd_Array1OfInteger& anIndexArray = myIndexes->ChangeArray1();
822 for (Standard_Integer anIndexIter = 1; anIndexIter <= anExtent; ++anIndexIter)
824 anIndexArray.SetValue (anIndexIter, anIndexIter);
826 std::sort (anIndexArray.begin(), anIndexArray.end(), CompareResults (mystored));
829 //=======================================================================
830 //function : HasDepthClipping
832 //=======================================================================
833 Standard_Boolean SelectMgr_ViewerSelector::HasDepthClipping (const Handle(SelectMgr_EntityOwner)& /*theOwner*/) const
835 return Standard_False;
838 //=======================================================================
839 // function : AddSelectableObject
840 // purpose : Adds new object to the map of selectable objects
841 //=======================================================================
842 void SelectMgr_ViewerSelector::AddSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
844 if (!myMapOfObjectSensitives.IsBound (theObject))
846 mySelectableObjects.Append (theObject);
847 Handle(SelectMgr_SensitiveEntitySet) anEntitySet = new SelectMgr_SensitiveEntitySet (myEntitySetBuilder);
848 myMapOfObjectSensitives.Bind (theObject, anEntitySet);
852 //=======================================================================
853 // function : AddSelectionToObject
854 // purpose : Adds new selection to the object and builds its BVH tree
855 //=======================================================================
856 void SelectMgr_ViewerSelector::AddSelectionToObject (const Handle(SelectMgr_SelectableObject)& theObject,
857 const Handle(SelectMgr_Selection)& theSelection)
859 if (Handle(SelectMgr_SensitiveEntitySet)* anEntitySet = myMapOfObjectSensitives.ChangeSeek (theObject))
861 (*anEntitySet)->Append (theSelection);
862 (*anEntitySet)->BVH();
866 AddSelectableObject (theObject);
867 AddSelectionToObject (theObject, theSelection);
871 //=======================================================================
872 // function : MoveSelectableObject
874 //=======================================================================
875 void SelectMgr_ViewerSelector::MoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
877 mySelectableObjects.ChangeSubset (theObject);
880 //=======================================================================
881 // function : RemoveSelectableObject
882 // purpose : Removes selectable object from map of selectable ones
883 //=======================================================================
884 void SelectMgr_ViewerSelector::RemoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
886 Handle(SelectMgr_SelectableObject) anObj = theObject;
887 if (myMapOfObjectSensitives.UnBind (theObject))
889 mySelectableObjects.Remove (theObject);
893 //=======================================================================
894 // function : RemoveSelectionOfObject
895 // purpose : Removes selection of the object and marks its BVH tree
897 //=======================================================================
898 void SelectMgr_ViewerSelector::RemoveSelectionOfObject (const Handle(SelectMgr_SelectableObject)& theObject,
899 const Handle(SelectMgr_Selection)& theSelection)
901 if (Handle(SelectMgr_SensitiveEntitySet)* anEntitySet = myMapOfObjectSensitives.ChangeSeek (theObject))
903 (*anEntitySet)->Remove (theSelection);
907 //=======================================================================
908 // function : RebuildObjectsTree
909 // purpose : Marks BVH of selectable objects for rebuild
910 //=======================================================================
911 void SelectMgr_ViewerSelector::RebuildObjectsTree (const Standard_Boolean theIsForce)
913 mySelectableObjects.MarkDirty();
917 Standard_Integer aViewportWidth, aViewportHeight;
918 mySelectingVolumeMgr.WindowSize (aViewportWidth, aViewportHeight);
920 Standard_Integer aWidth;
921 Standard_Integer aHeight;
922 mySelectingVolumeMgr.WindowSize (aWidth, aHeight);
923 mySelectableObjects.UpdateBVH (mySelectingVolumeMgr.Camera(),
924 mySelectingVolumeMgr.ProjectionMatrix(),
925 mySelectingVolumeMgr.WorldViewMatrix(),
926 mySelectingVolumeMgr.WorldViewProjState(),
931 //=======================================================================
932 // function : RebuildSensitivesTree
933 // purpose : Marks BVH of sensitive entities of particular selectable
934 // object for rebuild
935 //=======================================================================
936 void SelectMgr_ViewerSelector::RebuildSensitivesTree (const Handle(SelectMgr_SelectableObject)& theObject,
937 const Standard_Boolean theIsForce)
939 if (!Contains (theObject))
942 Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject);
943 anEntitySet->MarkDirty();
951 //=======================================================================
952 // function : resetSelectionActivationStatus
953 // purpose : Marks all added sensitive entities of all objects as
955 //=======================================================================
956 void SelectMgr_ViewerSelector::ResetSelectionActivationStatus()
958 for (SelectMgr_MapOfObjectSensitivesIterator aSensitivesIter (myMapOfObjectSensitives); aSensitivesIter.More(); aSensitivesIter.Next())
960 Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = aSensitivesIter.ChangeValue();
961 const Standard_Integer anEntitiesNb = anEntitySet->Size();
962 for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx)
964 anEntitySet->GetSensitiveById (anIdx)->ResetSelectionActiveStatus();
969 //=======================================================================
970 // function : DetectedEntity
971 // purpose : Returns sensitive entity that was detected during the
972 // previous run of selection algorithm
973 //=======================================================================
974 const Handle(SelectBasics_SensitiveEntity)& SelectMgr_ViewerSelector::DetectedEntity() const
976 const Standard_Integer aRankInMap = myIndexes->Value(myCurRank);
977 return mystored.FindFromIndex (aRankInMap).Entity;
980 //=======================================================================
981 // function : ActiveOwners
982 // purpose : Returns the list of active entity owners
983 //=======================================================================
984 void SelectMgr_ViewerSelector::ActiveOwners (NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners) const
986 for (SelectMgr_MapOfObjectSensitivesIterator anIter (myMapOfObjectSensitives); anIter.More(); anIter.Next())
988 const Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = anIter.Value();
989 const Standard_Integer anEntitiesNb = anEntitySet->Size();
990 for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx)
992 const Handle(SelectMgr_SensitiveEntity)& aSensitive = anEntitySet->GetSensitiveById (anIdx);
993 if (aSensitive->IsActiveForSelection())
995 theOwners.Append (aSensitive->BaseSensitive()->OwnerId());
1001 //=======================================================================
1002 //function : AllowOverlapDetection
1003 //purpose : Sets the detection type: if theIsToAllow is false,
1004 // only fully included sensitives will be detected, otherwise
1005 // the algorithm will mark both included and overlapped entities
1007 //=======================================================================
1008 void SelectMgr_ViewerSelector::AllowOverlapDetection (const Standard_Boolean theIsToAllow)
1010 mySelectingVolumeMgr.AllowOverlapDetection (theIsToAllow);