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 if (theSelection->GetSelectionState() != SelectMgr_SOS_Activated)
142 theSelection->SetSelectionState (SelectMgr_SOS_Activated);
144 myTolerances.Add (theSelection->Sensitivity());
145 myToUpdateTolerance = Standard_True;
149 //==================================================
150 // Function: Deactivate
152 //==================================================
153 void SelectMgr_ViewerSelector::Deactivate (const Handle(SelectMgr_Selection)& theSelection)
155 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
157 aSelEntIter.Value()->ResetSelectionActiveStatus();
160 if (theSelection->GetSelectionState() == SelectMgr_SOS_Activated)
162 theSelection->SetSelectionState (SelectMgr_SOS_Deactivated);
164 myTolerances.Decrement (theSelection->Sensitivity());
165 myToUpdateTolerance = Standard_True;
169 //==================================================
172 //==================================================
173 void SelectMgr_ViewerSelector::Clear()
178 //=======================================================================
179 // function: isToScaleFrustum
180 // purpose : Checks if the entity given requires to scale current selecting frustum
181 //=======================================================================
182 Standard_Boolean SelectMgr_ViewerSelector::isToScaleFrustum (const Handle(SelectBasics_SensitiveEntity)& theEntity)
184 return mySelectingVolumeMgr.GetActiveSelectionType() == SelectMgr_SelectingVolumeManager::Point
185 && sensitivity (theEntity) < myTolerances.Tolerance();
188 //=======================================================================
189 // function: sensitivity
190 // purpose : In case if custom tolerance is set, this method will return sum of entity
191 // sensitivity and custom tolerance.
192 //=======================================================================
193 Standard_Integer SelectMgr_ViewerSelector::sensitivity (const Handle(SelectBasics_SensitiveEntity)& theEntity) const
195 return myTolerances.IsCustomTolSet() ?
196 theEntity->SensitivityFactor() + myTolerances.CustomTolerance() : theEntity->SensitivityFactor();
199 //=======================================================================
200 // function: checkOverlap
201 // purpose : Internal function that checks if a particular sensitive
202 // entity theEntity overlaps current selecting volume precisely
203 //=======================================================================
204 void SelectMgr_ViewerSelector::checkOverlap (const Handle(SelectBasics_SensitiveEntity)& theEntity,
205 const gp_GTrsf& theInversedTrsf,
206 SelectMgr_SelectingVolumeManager& theMgr)
208 Handle(SelectMgr_EntityOwner) anOwner (Handle(SelectMgr_EntityOwner)::DownCast (theEntity->OwnerId()));
209 Handle(SelectMgr_SelectableObject) aSelectable;
210 Standard_Boolean toRestoresViewClipEnabled = Standard_False;
211 if (!anOwner.IsNull())
213 aSelectable = anOwner->Selectable();
215 if (!aSelectable.IsNull())
217 if (!aSelectable->ClipPlanes().IsNull()
218 && aSelectable->ClipPlanes()->ToOverrideGlobal())
220 theMgr.SetViewClippingEnabled (Standard_False);
221 toRestoresViewClipEnabled = Standard_True;
223 else if (!aSelectable->TransformPersistence().IsNull())
225 if (aSelectable->TransformPersistence()->IsZoomOrRotate()
226 && !theMgr.ViewClipping().IsNull())
228 // Zoom/rotate persistence object lives in two worlds at the same time.
229 // Global clipping planes can not be trivially applied without being converted
230 // into local space of transformation persistence object.
231 // As more simple alternative - just clip entire object by its anchor point defined in the world space.
232 const Handle(Graphic3d_SequenceOfHClipPlane)& aViewPlanes = theMgr.ViewClipping();
234 const gp_Pnt anAnchor = aSelectable->TransformPersistence()->AnchorPoint();
235 for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (*aViewPlanes); aPlaneIt.More(); aPlaneIt.Next())
237 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
243 const Graphic3d_Vec4d aCheckPnt (anAnchor.X(), anAnchor.Y(), anAnchor.Z(), 1.0);
244 if (aPlane->ProbePoint (aCheckPnt) == Graphic3d_ClipState_Out)
251 theMgr.SetViewClippingEnabled (Standard_False);
252 toRestoresViewClipEnabled = Standard_True;
256 SelectBasics_PickResult aPickResult;
257 const Standard_Boolean isMatched = theEntity->Matches(theMgr, aPickResult);
258 if (toRestoresViewClipEnabled)
260 theMgr.SetViewClippingEnabled (Standard_True);
269 if (HasDepthClipping (anOwner)
270 && !aSelectable.IsNull()
271 && theMgr.GetActiveSelectionType() == SelectMgr_SelectingVolumeManager::Point)
273 Standard_Boolean isClipped = mySelectingVolumeMgr.IsClipped (*aSelectable->ClipPlanes(),
274 aPickResult.Depth());
279 SelectMgr_SortCriterion aCriterion;
280 myZLayerOrderMap.Find (!aSelectable.IsNull() ? aSelectable->ZLayer() : Graphic3d_ZLayerId_Default, aCriterion.ZLayerPosition);
281 aCriterion.Entity = theEntity;
282 aCriterion.Priority = anOwner->Priority();
283 aCriterion.Depth = aPickResult.Depth();
284 aCriterion.MinDist = aPickResult.DistToGeomCenter();
285 aCriterion.ToPreferClosest = preferclosest;
287 if (SelectMgr_SortCriterion* aPrevCriterion = mystored.ChangeSeek (anOwner))
289 ++aPrevCriterion->NbOwnerMatches;
290 aCriterion.NbOwnerMatches = aPrevCriterion->NbOwnerMatches;
291 if (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Box)
293 if (aCriterion > *aPrevCriterion)
295 updatePoint3d (aCriterion, aPickResult, theEntity, theInversedTrsf, theMgr);
296 *aPrevCriterion = aCriterion;
302 aCriterion.NbOwnerMatches = 1;
303 updatePoint3d (aCriterion, aPickResult, theEntity, theInversedTrsf, theMgr);
304 mystored.Add (anOwner, aCriterion);
308 //=======================================================================
309 // function: computeFrustum
310 // purpose : Internal function that checks if a current selecting frustum
311 // needs to be scaled and transformed for the entity and performs
312 // necessary calculations
313 //=======================================================================
314 void SelectMgr_ViewerSelector::computeFrustum (const Handle(SelectBasics_SensitiveEntity)& theEnt,
315 const SelectMgr_SelectingVolumeManager& theMgr,
316 const gp_GTrsf& theInvTrsf,
317 SelectMgr_FrustumCache& theCachedMgrs,
318 SelectMgr_SelectingVolumeManager& theResMgr)
320 Standard_Integer aScale = isToScaleFrustum (theEnt) ? sensitivity (theEnt) : 1;
321 const gp_GTrsf aTrsfMtr = theEnt->HasInitLocation() ? theEnt->InvInitLocation() * theInvTrsf : theInvTrsf;
322 const Standard_Boolean toScale = aScale != 1;
323 const Standard_Boolean toTransform = aTrsfMtr.Form() != gp_Identity;
324 if (toScale && toTransform)
326 theResMgr = theMgr.ScaleAndTransform (aScale, aTrsfMtr, NULL);
330 if (!theCachedMgrs.IsBound (aScale))
332 theCachedMgrs.Bind (aScale, theMgr.ScaleAndTransform (aScale, gp_Trsf(), NULL));
334 theResMgr = theCachedMgrs.Find (aScale);
336 else if (toTransform)
338 theResMgr = theMgr.ScaleAndTransform (1, aTrsfMtr, NULL);
342 //=======================================================================
343 // function: traverseObject
344 // purpose : Internal function that checks if there is possible overlap
345 // between some entity of selectable object theObject and
346 // current selecting volume
347 //=======================================================================
348 void SelectMgr_ViewerSelector::traverseObject (const Handle(SelectMgr_SelectableObject)& theObject,
349 const SelectMgr_SelectingVolumeManager& theMgr,
350 const Handle(Graphic3d_Camera)& theCamera,
351 const Graphic3d_Mat4d& theProjectionMat,
352 const Graphic3d_Mat4d& theWorldViewMat,
353 const Standard_Integer theViewportWidth,
354 const Standard_Integer theViewportHeight)
356 Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject);
357 if (anEntitySet->Size() == 0)
362 const opencascade::handle<BVH_Tree<Standard_Real, 3> >& aSensitivesTree = anEntitySet->BVH();
363 gp_GTrsf aInversedTrsf;
364 if (theObject->HasTransformation() || !theObject->TransformPersistence().IsNull())
366 if (theObject->TransformPersistence().IsNull())
368 aInversedTrsf = theObject->InversedTransformation();
373 Graphic3d_Mat4d aMat = theObject->TransformPersistence()->Compute (theCamera, theProjectionMat, theWorldViewMat, theViewportWidth, theViewportHeight);
375 aTPers.SetValue (1, 1, aMat.GetValue (0, 0));
376 aTPers.SetValue (1, 2, aMat.GetValue (0, 1));
377 aTPers.SetValue (1, 3, aMat.GetValue (0, 2));
378 aTPers.SetValue (2, 1, aMat.GetValue (1, 0));
379 aTPers.SetValue (2, 2, aMat.GetValue (1, 1));
380 aTPers.SetValue (2, 3, aMat.GetValue (1, 2));
381 aTPers.SetValue (3, 1, aMat.GetValue (2, 0));
382 aTPers.SetValue (3, 2, aMat.GetValue (2, 1));
383 aTPers.SetValue (3, 3, aMat.GetValue (2, 2));
384 aTPers.SetTranslationPart (gp_XYZ (aMat.GetValue (0, 3), aMat.GetValue (1, 3), aMat.GetValue (2, 3)));
386 aInversedTrsf = (aTPers * gp_GTrsf (theObject->Transformation())).Inverted();
390 SelectMgr_SelectingVolumeManager aMgr = aInversedTrsf.Form() != gp_Identity
391 ? theMgr.ScaleAndTransform (1, aInversedTrsf, NULL)
394 SelectMgr_FrustumCache aScaledTrnsfFrustums;
396 Standard_Integer aNode = 0; // a root node
397 if (!aMgr.Overlaps (aSensitivesTree->MinPoint (0),
398 aSensitivesTree->MaxPoint (0)))
403 const Standard_Integer aFirstStored = mystored.Extent() + 1;
405 Standard_Integer aStack[BVH_Constants_MaxTreeDepth];
406 Standard_Integer aHead = -1;
409 if (!aSensitivesTree->IsOuter (aNode))
411 const Standard_Integer aLeftChildIdx = aSensitivesTree->Child<0> (aNode);
412 const Standard_Integer aRightChildIdx = aSensitivesTree->Child<1> (aNode);
413 const Standard_Boolean isLeftChildIn = aMgr.Overlaps (aSensitivesTree->MinPoint (aLeftChildIdx),
414 aSensitivesTree->MaxPoint (aLeftChildIdx));
415 const Standard_Boolean isRightChildIn = aMgr.Overlaps (aSensitivesTree->MinPoint (aRightChildIdx),
416 aSensitivesTree->MaxPoint (aRightChildIdx));
420 aNode = aLeftChildIdx;
422 aStack[aHead] = aRightChildIdx;
424 else if (isLeftChildIn
427 aNode = isLeftChildIn ? aLeftChildIdx : aRightChildIdx;
436 aNode = aStack[aHead];
442 Standard_Integer aStartIdx = aSensitivesTree->BegPrimitive (aNode);
443 Standard_Integer anEndIdx = aSensitivesTree->EndPrimitive (aNode);
444 for (Standard_Integer anIdx = aStartIdx; anIdx <= anEndIdx; ++anIdx)
446 const Handle(SelectMgr_SensitiveEntity)& aSensitive = anEntitySet->GetSensitiveById (anIdx);
447 if (aSensitive->IsActiveForSelection())
449 const Handle(SelectBasics_SensitiveEntity)& anEnt = aSensitive->BaseSensitive();
450 SelectMgr_SelectingVolumeManager aTmpMgr = aMgr;
451 computeFrustum (anEnt, theMgr, aInversedTrsf, aScaledTrnsfFrustums, aTmpMgr);
452 checkOverlap (anEnt, aInversedTrsf, aTmpMgr);
460 aNode = aStack[aHead];
465 // in case of Box/Polyline selection - keep only Owners having all Entities detected
466 if (mySelectingVolumeMgr.IsOverlapAllowed()
467 || (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Box
468 && theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Polyline))
473 for (Standard_Integer aStoredIter = mystored.Extent(); aStoredIter >= aFirstStored; --aStoredIter)
475 const SelectMgr_SortCriterion& aCriterion = mystored.FindFromIndex (aStoredIter);
476 const Handle(SelectBasics_EntityOwner)& anOwner = aCriterion.Entity->OwnerId();
477 Standard_Integer aNbOwnerEntities = 0;
478 for (SelectMgr_IndexedMapOfHSensitive::Iterator aSensIter (anEntitySet->Sensitives()); aSensIter.More(); aSensIter.Next())
480 if (aSensIter.Value()->BaseSensitive()->OwnerId() == anOwner)
482 if (++aNbOwnerEntities > aCriterion.NbOwnerMatches)
484 // Remove from index map.
485 // Considering NCollection_IndexedDataMap implementation, the values for lower indexes will not be modified.
486 // Hence, just keep iterating in backward direction.
487 mystored.RemoveFromIndex (aStoredIter);
495 //=======================================================================
496 // function: TraverseSensitives
497 // purpose : Traverses BVH containing all added selectable objects and
498 // finds candidates for further search of overlap
499 //=======================================================================
500 void SelectMgr_ViewerSelector::TraverseSensitives()
504 Standard_Integer aWidth;
505 Standard_Integer aHeight;
506 mySelectingVolumeMgr.WindowSize (aWidth, aHeight);
507 mySelectableObjects.UpdateBVH (mySelectingVolumeMgr.Camera(),
508 mySelectingVolumeMgr.ProjectionMatrix(),
509 mySelectingVolumeMgr.WorldViewMatrix(),
510 mySelectingVolumeMgr.WorldViewProjState(),
512 const Handle(Graphic3d_Camera)& aCamera = mySelectingVolumeMgr.Camera();
513 if (!aCamera.IsNull())
515 myCameraEye = aCamera->Eye().XYZ();
516 myCameraDir = aCamera->Direction().XYZ();
517 myCameraScale = aCamera->IsOrthographic()
519 : 2.0 * Tan (aCamera->FOVy() * M_PI / 360.0);
520 const double aPixelSize = Max (1.0 / aWidth, 1.0 / aHeight);
521 myCameraScale *= aPixelSize;
524 for (Standard_Integer aBVHSetIt = 0; aBVHSetIt < SelectMgr_SelectableObjectSet::BVHSubsetNb; ++aBVHSetIt)
526 SelectMgr_SelectableObjectSet::BVHSubset aBVHSubset =
527 static_cast<SelectMgr_SelectableObjectSet::BVHSubset> (aBVHSetIt);
529 if (mySelectableObjects.IsEmpty (aBVHSubset))
536 SelectMgr_SelectingVolumeManager aMgr (Standard_False);
538 // for 2D space selection transform selecting volumes to perform overap testing
539 // directly in camera's eye space omitting the camera position, which is not
540 // needed there at all
541 if (aBVHSubset == SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent)
543 const Graphic3d_Mat4d& aMat = mySelectingVolumeMgr.WorldViewMatrix();
544 aTFrustum.SetValue (1, 1, aMat.GetValue (0, 0));
545 aTFrustum.SetValue (1, 2, aMat.GetValue (0, 1));
546 aTFrustum.SetValue (1, 3, aMat.GetValue (0, 2));
547 aTFrustum.SetValue (2, 1, aMat.GetValue (1, 0));
548 aTFrustum.SetValue (2, 2, aMat.GetValue (1, 1));
549 aTFrustum.SetValue (2, 3, aMat.GetValue (1, 2));
550 aTFrustum.SetValue (3, 1, aMat.GetValue (2, 0));
551 aTFrustum.SetValue (3, 2, aMat.GetValue (2, 1));
552 aTFrustum.SetValue (3, 3, aMat.GetValue (2, 2));
553 aTFrustum.SetTranslationPart (gp_XYZ (aMat.GetValue (0, 3), aMat.GetValue (1, 3), aMat.GetValue (2, 3)));
555 // define corresponding frustum builder parameters
556 Handle(SelectMgr_FrustumBuilder) aBuilder = new SelectMgr_FrustumBuilder();
557 aBuilder->SetProjectionMatrix (mySelectingVolumeMgr.ProjectionMatrix());
558 aBuilder->SetWorldViewMatrix (SelectMgr_ViewerSelector_THE_IDENTITY_MAT);
559 aBuilder->SetWindowSize (aWidth, aHeight);
560 aMgr = mySelectingVolumeMgr.ScaleAndTransform (1, aTFrustum, aBuilder);
564 aMgr = mySelectingVolumeMgr;
567 const Graphic3d_Mat4d& aProjectionMat = mySelectingVolumeMgr.ProjectionMatrix();
568 const Graphic3d_Mat4d& aWorldViewMat = aBVHSubset != SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent
569 ? mySelectingVolumeMgr.WorldViewMatrix()
570 : SelectMgr_ViewerSelector_THE_IDENTITY_MAT;
572 const opencascade::handle<BVH_Tree<Standard_Real, 3> >& aBVHTree = mySelectableObjects.BVH (aBVHSubset);
574 Standard_Integer aNode = 0;
575 if (!aMgr.Overlaps (aBVHTree->MinPoint (0), aBVHTree->MaxPoint (0)))
580 Standard_Integer aStack[BVH_Constants_MaxTreeDepth];
581 Standard_Integer aHead = -1;
584 if (!aBVHTree->IsOuter (aNode))
586 const Standard_Integer aLeftChildIdx = aBVHTree->Child<0> (aNode);
587 const Standard_Integer aRightChildIdx = aBVHTree->Child<1> (aNode);
588 const Standard_Boolean isLeftChildIn =
589 aMgr.Overlaps (aBVHTree->MinPoint (aLeftChildIdx), aBVHTree->MaxPoint (aLeftChildIdx));
590 const Standard_Boolean isRightChildIn =
591 aMgr.Overlaps (aBVHTree->MinPoint (aRightChildIdx), aBVHTree->MaxPoint (aRightChildIdx));
595 aNode = aLeftChildIdx;
597 aStack[aHead] = aRightChildIdx;
599 else if (isLeftChildIn
602 aNode = isLeftChildIn ? aLeftChildIdx : aRightChildIdx;
611 aNode = aStack[aHead];
617 Standard_Integer aStartIdx = aBVHTree->BegPrimitive (aNode);
618 Standard_Integer anEndIdx = aBVHTree->EndPrimitive (aNode);
619 for (Standard_Integer anIdx = aStartIdx; anIdx <= anEndIdx; ++anIdx)
621 const Handle(SelectMgr_SelectableObject)& aSelectableObject =
622 mySelectableObjects.GetObjectById (aBVHSubset, anIdx);
624 traverseObject (aSelectableObject, aMgr, aCamera, aProjectionMat, aWorldViewMat, aWidth, aHeight);
631 aNode = aStack[aHead];
640 //==================================================
641 // Function: ClearPicked
643 //==================================================
644 void SelectMgr_ViewerSelector::ClearPicked()
649 //==================================================
652 //==================================================
653 Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector
656 Standard_Integer RankInMap = myIndexes->Value (myCurRank);
657 const Handle(SelectBasics_EntityOwner)& toto = mystored.FindKey(RankInMap);
658 Handle(SelectMgr_EntityOwner) Ownr = Handle(SelectMgr_EntityOwner)::DownCast (toto);
662 //=======================================================================
665 //=======================================================================
666 Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector::Picked (const Standard_Integer theRank) const
668 Handle(SelectMgr_EntityOwner) anOwner;
669 if (theRank < 1 || theRank > NbPicked())
674 const Standard_Integer anOwnerIdx = myIndexes->Value (theRank);
675 const Handle(SelectBasics_EntityOwner)& aStoredOwner = mystored.FindKey (anOwnerIdx);
676 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (aStoredOwner);
680 //=======================================================================
681 //function : PickedData
683 //=======================================================================
684 const SelectMgr_SortCriterion& SelectMgr_ViewerSelector::PickedData(const Standard_Integer theRank) const
686 Standard_OutOfRange_Raise_if (theRank < 1 || theRank > NbPicked(), "SelectMgr_ViewerSelector::PickedData() out of range index");
687 const Standard_Integer anOwnerIdx = myIndexes->Value (theRank);
688 return mystored.FindFromIndex (anOwnerIdx);
691 //===================================================
693 // INTERNAL METHODS ....
695 //==================================================
697 //==================================================
698 // Function: SetEntitySetBuilder
700 //==================================================
701 void SelectMgr_ViewerSelector::SetEntitySetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder)
703 myEntitySetBuilder = theBuilder;
704 for (SelectMgr_MapOfObjectSensitives::Iterator aSetIter (myMapOfObjectSensitives); aSetIter.More(); aSetIter.Next())
706 aSetIter.ChangeValue()->SetBuilder (myEntitySetBuilder);
710 //==================================================
711 // Function: Contains
713 //==================================================
714 Standard_Boolean SelectMgr_ViewerSelector::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
716 return mySelectableObjects.Contains (theObject);
719 //==================================================
720 // Function: ActiveModes
721 // Purpose : return all the modes with a given state for an object
722 //==================================================
723 Standard_Boolean SelectMgr_ViewerSelector::Modes (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
724 TColStd_ListOfInteger& theModeList,
725 const SelectMgr_StateOfSelection theWantedState) const
727 Standard_Boolean hasActivatedStates = Contains (theSelectableObject);
728 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theSelectableObject->Selections()); aSelIter.More(); aSelIter.Next())
730 if (theWantedState == SelectMgr_SOS_Any)
732 theModeList.Append (aSelIter.Value()->Mode());
734 else if (theWantedState == aSelIter.Value()->GetSelectionState())
736 theModeList.Append (aSelIter.Value()->Mode());
740 return hasActivatedStates;
743 //==================================================
744 // Function: IsActive
746 //==================================================
747 Standard_Boolean SelectMgr_ViewerSelector::IsActive (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_Activated;
758 //==================================================
759 // Function: IsInside
761 //==================================================
762 Standard_Boolean SelectMgr_ViewerSelector::IsInside (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
763 const Standard_Integer theMode) const
765 if (!Contains (theSelectableObject))
766 return Standard_False;
768 const Handle(SelectMgr_Selection)& aSel = theSelectableObject->Selection (theMode);
769 return !aSel.IsNull()
770 && aSel->GetSelectionState() != SelectMgr_SOS_Unknown;
774 //=======================================================================
777 //=======================================================================
779 SelectMgr_StateOfSelection SelectMgr_ViewerSelector::Status (const Handle(SelectMgr_Selection)& theSelection) const
781 return theSelection->GetSelectionState();
784 //==================================================
786 // Purpose : gives Information about selectors
787 //==================================================
789 TCollection_AsciiString SelectMgr_ViewerSelector::Status (const Handle(SelectMgr_SelectableObject)& theSelectableObject) const
791 TCollection_AsciiString aStatus ("Status Object :\n\t");
792 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theSelectableObject->Selections()); aSelIter.More(); aSelIter.Next())
794 if (aSelIter.Value()->GetSelectionState() != SelectMgr_SOS_Unknown)
796 aStatus = aStatus + "Mode " + TCollection_AsciiString (aSelIter.Value()->Mode()) + " present - "
797 + (aSelIter.Value()->GetSelectionState() == SelectMgr_SOS_Activated ? " Active \n\t" : " Inactive \n\t");
801 if (!Contains (theSelectableObject))
803 aStatus = aStatus + "Not Present in the selector\n\n";
809 //=======================================================================
810 //function : SortResult
811 //purpose : there is a certain number of entities ranged by criteria
812 // (depth, size, priority, mouse distance from borders or
813 // CDG of the detected primitive. Parsing :
814 // maximum priorities .
815 // then a reasonable compromise between depth and distance...
816 // finally the ranges are stored in myindexes depending on the parsing.
817 // so, it is possible to only read
818 //=======================================================================
819 void SelectMgr_ViewerSelector::SortResult()
821 if(mystored.IsEmpty()) return;
823 const Standard_Integer anExtent = mystored.Extent();
824 if(myIndexes.IsNull() || anExtent != myIndexes->Length())
825 myIndexes = new TColStd_HArray1OfInteger (1, anExtent);
827 TColStd_Array1OfInteger& anIndexArray = myIndexes->ChangeArray1();
828 for (Standard_Integer anIndexIter = 1; anIndexIter <= anExtent; ++anIndexIter)
830 anIndexArray.SetValue (anIndexIter, anIndexIter);
832 std::sort (anIndexArray.begin(), anIndexArray.end(), CompareResults (mystored));
835 //=======================================================================
836 //function : HasDepthClipping
838 //=======================================================================
839 Standard_Boolean SelectMgr_ViewerSelector::HasDepthClipping (const Handle(SelectMgr_EntityOwner)& /*theOwner*/) const
841 return Standard_False;
844 //=======================================================================
845 // function : AddSelectableObject
846 // purpose : Adds new object to the map of selectable objects
847 //=======================================================================
848 void SelectMgr_ViewerSelector::AddSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
850 if (!myMapOfObjectSensitives.IsBound (theObject))
852 mySelectableObjects.Append (theObject);
853 Handle(SelectMgr_SensitiveEntitySet) anEntitySet = new SelectMgr_SensitiveEntitySet (myEntitySetBuilder);
854 myMapOfObjectSensitives.Bind (theObject, anEntitySet);
858 //=======================================================================
859 // function : AddSelectionToObject
860 // purpose : Adds new selection to the object and builds its BVH tree
861 //=======================================================================
862 void SelectMgr_ViewerSelector::AddSelectionToObject (const Handle(SelectMgr_SelectableObject)& theObject,
863 const Handle(SelectMgr_Selection)& theSelection)
865 if (Handle(SelectMgr_SensitiveEntitySet)* anEntitySet = myMapOfObjectSensitives.ChangeSeek (theObject))
867 (*anEntitySet)->Append (theSelection);
868 (*anEntitySet)->BVH();
872 AddSelectableObject (theObject);
873 AddSelectionToObject (theObject, theSelection);
877 //=======================================================================
878 // function : MoveSelectableObject
880 //=======================================================================
881 void SelectMgr_ViewerSelector::MoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
883 mySelectableObjects.ChangeSubset (theObject);
886 //=======================================================================
887 // function : RemoveSelectableObject
888 // purpose : Removes selectable object from map of selectable ones
889 //=======================================================================
890 void SelectMgr_ViewerSelector::RemoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject)
892 Handle(SelectMgr_SelectableObject) anObj = theObject;
893 if (myMapOfObjectSensitives.UnBind (theObject))
895 mySelectableObjects.Remove (theObject);
899 //=======================================================================
900 // function : RemoveSelectionOfObject
901 // purpose : Removes selection of the object and marks its BVH tree
903 //=======================================================================
904 void SelectMgr_ViewerSelector::RemoveSelectionOfObject (const Handle(SelectMgr_SelectableObject)& theObject,
905 const Handle(SelectMgr_Selection)& theSelection)
907 if (Handle(SelectMgr_SensitiveEntitySet)* anEntitySet = myMapOfObjectSensitives.ChangeSeek (theObject))
909 (*anEntitySet)->Remove (theSelection);
913 //=======================================================================
914 // function : RebuildObjectsTree
915 // purpose : Marks BVH of selectable objects for rebuild
916 //=======================================================================
917 void SelectMgr_ViewerSelector::RebuildObjectsTree (const Standard_Boolean theIsForce)
919 mySelectableObjects.MarkDirty();
923 Standard_Integer aViewportWidth, aViewportHeight;
924 mySelectingVolumeMgr.WindowSize (aViewportWidth, aViewportHeight);
926 Standard_Integer aWidth;
927 Standard_Integer aHeight;
928 mySelectingVolumeMgr.WindowSize (aWidth, aHeight);
929 mySelectableObjects.UpdateBVH (mySelectingVolumeMgr.Camera(),
930 mySelectingVolumeMgr.ProjectionMatrix(),
931 mySelectingVolumeMgr.WorldViewMatrix(),
932 mySelectingVolumeMgr.WorldViewProjState(),
937 //=======================================================================
938 // function : RebuildSensitivesTree
939 // purpose : Marks BVH of sensitive entities of particular selectable
940 // object for rebuild
941 //=======================================================================
942 void SelectMgr_ViewerSelector::RebuildSensitivesTree (const Handle(SelectMgr_SelectableObject)& theObject,
943 const Standard_Boolean theIsForce)
945 if (!Contains (theObject))
948 Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = myMapOfObjectSensitives.ChangeFind (theObject);
949 anEntitySet->MarkDirty();
957 //=======================================================================
958 // function : resetSelectionActivationStatus
959 // purpose : Marks all added sensitive entities of all objects as
961 //=======================================================================
962 void SelectMgr_ViewerSelector::ResetSelectionActivationStatus()
964 for (SelectMgr_MapOfObjectSensitivesIterator aSensitivesIter (myMapOfObjectSensitives); aSensitivesIter.More(); aSensitivesIter.Next())
966 Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = aSensitivesIter.ChangeValue();
967 const Standard_Integer anEntitiesNb = anEntitySet->Size();
968 for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx)
970 anEntitySet->GetSensitiveById (anIdx)->ResetSelectionActiveStatus();
975 //=======================================================================
976 // function : DetectedEntity
977 // purpose : Returns sensitive entity that was detected during the
978 // previous run of selection algorithm
979 //=======================================================================
980 const Handle(SelectBasics_SensitiveEntity)& SelectMgr_ViewerSelector::DetectedEntity() const
982 const Standard_Integer aRankInMap = myIndexes->Value(myCurRank);
983 return mystored.FindFromIndex (aRankInMap).Entity;
986 //=======================================================================
987 // function : ActiveOwners
988 // purpose : Returns the list of active entity owners
989 //=======================================================================
990 void SelectMgr_ViewerSelector::ActiveOwners (NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners) const
992 for (SelectMgr_MapOfObjectSensitivesIterator anIter (myMapOfObjectSensitives); anIter.More(); anIter.Next())
994 const Handle(SelectMgr_SensitiveEntitySet)& anEntitySet = anIter.Value();
995 const Standard_Integer anEntitiesNb = anEntitySet->Size();
996 for (Standard_Integer anIdx = 0; anIdx < anEntitiesNb; ++anIdx)
998 const Handle(SelectMgr_SensitiveEntity)& aSensitive = anEntitySet->GetSensitiveById (anIdx);
999 if (aSensitive->IsActiveForSelection())
1001 theOwners.Append (aSensitive->BaseSensitive()->OwnerId());
1007 //=======================================================================
1008 //function : AllowOverlapDetection
1009 //purpose : Sets the detection type: if theIsToAllow is false,
1010 // only fully included sensitives will be detected, otherwise
1011 // the algorithm will mark both included and overlapped entities
1013 //=======================================================================
1014 void SelectMgr_ViewerSelector::AllowOverlapDetection (const Standard_Boolean theIsToAllow)
1016 mySelectingVolumeMgr.AllowOverlapDetection (theIsToAllow);