1 // Created on: 2014-08-15
2 // Created by: Varvara POSKONINA
3 // Copyright (c) 2005-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <SelectMgr_SensitiveEntitySet.hxx>
18 #include <BVH_BinnedBuilder.hxx>
20 #include <Select3D_SensitiveEntity.hxx>
21 #include <SelectMgr_SensitiveEntity.hxx>
23 //=======================================================================
24 // function : SelectMgr_SensitiveEntitySet
26 //=======================================================================
27 SelectMgr_SensitiveEntitySet::SelectMgr_SensitiveEntitySet()
29 myBuilder = new BVH_BinnedBuilder<Standard_Real, 3, 32> (1, 32, Standard_True);
32 //=======================================================================
34 // purpose : Adds new entity to the set and marks BVH tree for rebuild
35 //=======================================================================
36 void SelectMgr_SensitiveEntitySet::Append (const SelectMgr_HSensitiveEntity& theEntity)
38 if (!theEntity->BaseSensitive()->IsKind ("Select3D_SensitiveEntity"))
40 theEntity->ResetSelectionActiveStatus();
43 myEntities.Append (theEntity);
44 myEntityIdxs.Append (myEntities.Size());
48 //=======================================================================
50 // purpose : Adds every entity of selection theSelection to the set
51 // and marks BVH tree for rebuild
52 //=======================================================================
53 void SelectMgr_SensitiveEntitySet::Append (const Handle(SelectMgr_Selection)& theSelection)
55 for (theSelection->Init(); theSelection->More(); theSelection->Next())
57 if (!theSelection->Sensitive()->BaseSensitive()->IsKind ("Select3D_SensitiveEntity"))
59 theSelection->Sensitive()->ResetSelectionActiveStatus();
62 myEntities.Append (theSelection->Sensitive());
63 myEntityIdxs.Append (myEntities.Size());
68 //=======================================================================
70 // purpose : Removes entity from the set and marks BVH tree for rebuild
71 //=======================================================================
72 void SelectMgr_SensitiveEntitySet::Remove (const SelectMgr_HSensitiveEntity& theEntity)
74 for (Standard_Integer anEntityIdx = 1; anEntityIdx <= myEntities.Size(); ++anEntityIdx)
76 if (myEntities.Value (anEntityIdx) == theEntity)
78 myEntities.Remove (anEntityIdx);
80 for (Standard_Integer anEntityIndexesIter = 1; anEntityIndexesIter <= myEntities.Size(); ++anEntityIndexesIter)
82 myEntityIdxs.Append (anEntityIndexesIter);
90 //=======================================================================
92 // purpose : Removes every entity of selection theSelection from the set
93 // and marks BVH tree for rebuild
94 //=======================================================================
95 void SelectMgr_SensitiveEntitySet::Remove (const Handle(SelectMgr_Selection)& theSelection)
97 for (theSelection->Init(); theSelection->More(); theSelection->Next())
99 for (Standard_Integer anEntityIdx = 1; anEntityIdx <= myEntities.Size(); ++anEntityIdx)
101 if (myEntities.Value (anEntityIdx) == theSelection->Sensitive())
103 myEntities.Remove (anEntityIdx);
109 if (BVH_Object<Standard_Real, 3>::myIsDirty)
111 myEntityIdxs.Clear();
112 for (Standard_Integer anEntityIdxsIter = 1; anEntityIdxsIter <= myEntities.Size(); ++anEntityIdxsIter)
114 myEntityIdxs.Append (anEntityIdxsIter);
119 //=======================================================================
121 // purpose : Returns bounding box of entity with index theIdx
122 //=======================================================================
123 Select3D_BndBox3d SelectMgr_SensitiveEntitySet::Box (const Standard_Integer theIndex) const
125 Standard_Integer anEntityIdx = myEntityIdxs.Value (theIndex + 1);
126 return myEntities.Value (anEntityIdx)->BaseSensitive()->BoundingBox();
129 //=======================================================================
131 // purpose : Returns geometry center of sensitive entity index theIdx
132 // along the given axis theAxis
133 //=======================================================================
134 Standard_Real SelectMgr_SensitiveEntitySet::Center (const Standard_Integer theIndex,
135 const Standard_Integer theAxis) const
137 Standard_Integer anEntityIdx = myEntityIdxs.Value (theIndex + 1);
138 const Handle(SelectBasics_SensitiveEntity)& aBasicEntity =
139 myEntities.Value (anEntityIdx)->BaseSensitive();
140 const Handle(Select3D_SensitiveEntity)& aSensitive =
141 Handle(Select3D_SensitiveEntity)::DownCast (aBasicEntity);
142 const gp_Pnt aCenter = aSensitive->CenterOfGeometry();
143 Standard_Real aCenterCoord = 0.0;
144 aCenterCoord = theAxis == 0 ? aCenter.X() :
145 (theAxis == 1 ? aCenter.Y() : aCenter.Z());
150 //=======================================================================
152 // purpose : Swaps items with indexes theIdx1 and theIdx2
153 //=======================================================================
154 void SelectMgr_SensitiveEntitySet::Swap (const Standard_Integer theIndex1,
155 const Standard_Integer theIndex2)
157 Standard_Integer anEntityIdx1 = myEntityIdxs.Value (theIndex1 + 1);
158 Standard_Integer anEntityIdx2 = myEntityIdxs.Value (theIndex2 + 1);
159 myEntityIdxs.ChangeValue (theIndex1 + 1) = anEntityIdx2;
160 myEntityIdxs.ChangeValue (theIndex2 + 1) = anEntityIdx1;
163 //=======================================================================
165 // purpose : Returns the amount of entities
166 //=======================================================================
167 Standard_Integer SelectMgr_SensitiveEntitySet::Size() const
169 return myEntityIdxs.Size();
172 //=======================================================================
173 // function : GetSensitiveById
174 // purpose : Returns the entity with index theIndex in the set
175 //=======================================================================
176 const SelectMgr_HSensitiveEntity& SelectMgr_SensitiveEntitySet::GetSensitiveById
177 (const Standard_Integer theIndex) const
179 Standard_Integer anIdx = myEntityIdxs.Value (theIndex + 1);
180 return myEntities.Value (anIdx);