1 // Created on: 1998-04-16
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1998-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 <Select3D_SensitiveGroup.hxx>
19 #include <Precision.hxx>
21 IMPLEMENT_STANDARD_RTTIEXT(Select3D_SensitiveGroup,Select3D_SensitiveSet)
23 //=======================================================================
26 //=======================================================================
27 Select3D_SensitiveGroup::Select3D_SensitiveGroup (const Handle(SelectMgr_EntityOwner)& theOwnerId,
28 const Standard_Boolean theIsMustMatchAll)
29 : Select3D_SensitiveSet (theOwnerId),
30 myMustMatchAll (theIsMustMatchAll),
31 myToCheckOverlapAll (Standard_False),
32 myCenter (0.0, 0.0, 0.0) {}
34 //=======================================================================
37 //=======================================================================
38 Select3D_SensitiveGroup::Select3D_SensitiveGroup (const Handle(SelectMgr_EntityOwner)& theOwnerId,
39 Select3D_EntitySequence& theEntities,
40 const Standard_Boolean theIsMustMatchAll)
41 : Select3D_SensitiveSet (theOwnerId),
42 myEntities (Max (1, theEntities.Size())),
43 myMustMatchAll (theIsMustMatchAll),
44 myToCheckOverlapAll (Standard_False),
45 myCenter (0.0, 0.0, 0.0)
47 for (Select3D_EntitySequenceIter anIter (theEntities); anIter.More(); anIter.Next())
49 const Handle(Select3D_SensitiveEntity)& anEntity = anIter.Value();
50 const Standard_Integer aPrevExtent = myEntities.Extent();
51 if (myEntities.Add (anEntity) <= aPrevExtent)
56 myBndBox.Combine (anEntity->BoundingBox());
57 myBVHPrimIndexes.Append (myEntities.Extent());
58 myCenter.ChangeCoord() += anEntity->CenterOfGeometry().XYZ();
61 myCenter.ChangeCoord().Divide (static_cast<Standard_Real> (myEntities.Extent()));
66 //=======================================================================
68 //purpose : No control of entities inside
69 //=======================================================================
70 void Select3D_SensitiveGroup::Add (Select3D_EntitySequence& theEntities)
72 if (theEntities.IsEmpty())
77 gp_Pnt aCent (0.0, 0.0, 0.0);
78 myEntities.ReSize (myEntities.Extent() + theEntities.Size());
79 for (Select3D_EntitySequenceIter anIter (theEntities); anIter.More(); anIter.Next())
81 const Handle(Select3D_SensitiveEntity)& anEntity = anIter.Value();
82 const Standard_Integer aPrevExtent = myEntities.Extent();
83 if (myEntities.Add (anEntity) <= aPrevExtent)
88 myBndBox.Combine (anEntity->BoundingBox());
89 myBVHPrimIndexes.Append (myEntities.Extent());
90 aCent.ChangeCoord() += anEntity->CenterOfGeometry().XYZ();
92 aCent.ChangeCoord().Divide (myEntities.Extent());
93 myCenter = (myCenter.XYZ() + aCent.XYZ()).Multiplied (0.5);
96 //=======================================================================
99 //=======================================================================
100 void Select3D_SensitiveGroup::Add (const Handle(Select3D_SensitiveEntity)& theSensitive)
102 const Standard_Integer aPrevExtent = myEntities.Extent();
103 if (myEntities.Add (theSensitive) <= aPrevExtent)
108 myBVHPrimIndexes.Append (myEntities.Extent());
109 myBndBox.Combine (theSensitive->BoundingBox());
110 myCenter.ChangeCoord() += theSensitive->CenterOfGeometry().XYZ();
111 if (myEntities.Extent() >= 2)
113 myCenter.ChangeCoord().Multiply (0.5);
117 //=======================================================================
120 //=======================================================================
121 void Select3D_SensitiveGroup::Remove (const Handle(Select3D_SensitiveEntity)& theSensitive)
123 if (!myEntities.RemoveKey (theSensitive))
129 myCenter = gp_Pnt (0.0, 0.0, 0.0);
130 myBVHPrimIndexes.Clear();
131 for (Standard_Integer anIdx = 1; anIdx <= myEntities.Size(); ++anIdx)
133 const Handle(Select3D_SensitiveEntity)& anEntity = myEntities.FindKey (anIdx);
134 myBndBox.Combine (anEntity->BoundingBox());
135 myCenter.ChangeCoord() += anEntity->CenterOfGeometry().XYZ();
136 myBVHPrimIndexes.Append (anIdx);
138 myCenter.ChangeCoord().Divide (static_cast<Standard_Real> (myEntities.Extent()));
141 //=======================================================================
144 //=======================================================================
145 Standard_Boolean Select3D_SensitiveGroup::IsIn (const Handle(Select3D_SensitiveEntity)& theSensitive) const
147 return myEntities.Contains (theSensitive);
150 //=======================================================================
153 //=======================================================================
155 void Select3D_SensitiveGroup::Clear()
159 myCenter = gp_Pnt (0.0, 0.0, 0.0);
160 myBVHPrimIndexes.Clear();
163 //=======================================================================
164 // function : NbSubElements
165 // purpose : Returns the amount of sub-entities
166 //=======================================================================
167 Standard_Integer Select3D_SensitiveGroup::NbSubElements() const
169 return myEntities.Size();
172 //=======================================================================
173 //function : GetConnected
175 //=======================================================================
177 Handle(Select3D_SensitiveEntity) Select3D_SensitiveGroup::GetConnected()
179 Handle(Select3D_SensitiveGroup) aNewEntity = new Select3D_SensitiveGroup (myOwnerId, myMustMatchAll);
180 Select3D_EntitySequence aConnectedEnt;
181 for (Select3D_IndexedMapOfEntity::Iterator anEntityIter (myEntities); anEntityIter.More(); anEntityIter.Next())
183 aConnectedEnt.Append (anEntityIter.Value()->GetConnected());
185 aNewEntity->Add (aConnectedEnt);
189 //=======================================================================
192 //=======================================================================
193 Standard_Boolean Select3D_SensitiveGroup::Matches (SelectBasics_SelectingVolumeManager& theMgr,
194 SelectBasics_PickResult& thePickResult)
196 const Standard_Boolean toMatchAll = theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Point
198 const Standard_Boolean toCheckAll = theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Point
199 && myToCheckOverlapAll;
200 if (!toMatchAll && !toCheckAll)
202 return Select3D_SensitiveSet::Matches (theMgr, thePickResult);
205 SelectBasics_PickResult aPickResult;
206 Standard_Boolean isFailed = Standard_False;
207 for (Select3D_IndexedMapOfEntity::Iterator anEntityIter (myEntities); anEntityIter.More(); anEntityIter.Next())
209 const Handle(Select3D_SensitiveEntity)& aChild = anEntityIter.Value();
210 if (!aChild->Matches (theMgr, aPickResult))
214 isFailed = Standard_True;
223 thePickResult = SelectBasics_PickResult::Min (thePickResult, aPickResult);
228 return Standard_False;
231 thePickResult.SetDistToGeomCenter(distanceToCOG(theMgr));
232 return Standard_True;
235 //=======================================================================
238 //=======================================================================
239 void Select3D_SensitiveGroup::Set (const Handle(SelectMgr_EntityOwner)& theOwnerId)
241 Select3D_SensitiveEntity::Set (theOwnerId);
242 for (Select3D_IndexedMapOfEntity::Iterator anEntityIter (myEntities); anEntityIter.More(); anEntityIter.Next())
244 anEntityIter.Value()->Set (theOwnerId);
248 //=======================================================================
249 // function : BoundingBox
250 // purpose : Returns bounding box of the group. If location
251 // transformation is set, it will be applied
252 //=======================================================================
253 Select3D_BndBox3d Select3D_SensitiveGroup::BoundingBox()
255 if (myBndBox.IsValid())
258 // do not apply the transformation because sensitives AABBs
259 // are already transformed
260 for (Select3D_IndexedMapOfEntity::Iterator anEntityIter (myEntities); anEntityIter.More(); anEntityIter.Next())
262 myBndBox.Combine (anEntityIter.Value()->BoundingBox());
268 //=======================================================================
269 // function : CenterOfGeometry
270 // purpose : Returns center of group. If location transformation
271 // is set, it will be applied
272 //=======================================================================
273 gp_Pnt Select3D_SensitiveGroup::CenterOfGeometry() const
278 //=======================================================================
280 // purpose : Returns bounding box of sensitive entity with index theIdx
281 //=======================================================================
282 Select3D_BndBox3d Select3D_SensitiveGroup::Box (const Standard_Integer theIdx) const
284 const Standard_Integer anElemIdx = myBVHPrimIndexes.Value (theIdx);
285 return myEntities.FindKey (anElemIdx)->BoundingBox();
288 //=======================================================================
290 // purpose : Returns geometry center of sensitive entity with index
291 // theIdx in the vector along the given axis theAxis
292 //=======================================================================
293 Standard_Real Select3D_SensitiveGroup::Center (const Standard_Integer theIdx,
294 const Standard_Integer theAxis) const
296 const Standard_Integer anElemIdx = myBVHPrimIndexes.Value (theIdx);
297 const gp_Pnt aCenter = myEntities.FindKey (anElemIdx)->CenterOfGeometry();
298 return theAxis == 0 ? aCenter.X() : (theAxis == 1 ? aCenter.Y() : aCenter.Z());
301 //=======================================================================
303 // purpose : Swaps items with indexes theIdx1 and theIdx2 in the vector
304 //=======================================================================
305 void Select3D_SensitiveGroup::Swap (const Standard_Integer theIdx1,
306 const Standard_Integer theIdx2)
308 const Standard_Integer anEntIdx1 = myBVHPrimIndexes.Value (theIdx1);
309 const Standard_Integer anEntIdx2 = myBVHPrimIndexes.Value (theIdx2);
311 myBVHPrimIndexes.ChangeValue (theIdx1) = anEntIdx2;
312 myBVHPrimIndexes.ChangeValue (theIdx2) = anEntIdx1;
315 //=======================================================================
317 // purpose : Returns the length of vector of sensitive entities
318 //=======================================================================
319 Standard_Integer Select3D_SensitiveGroup::Size() const
321 return myBVHPrimIndexes.Size();
324 // =======================================================================
325 // function : overlapsElement
326 // purpose : Checks whether the entity with index theIdx overlaps the
327 // current selecting volume
328 // =======================================================================
329 Standard_Boolean Select3D_SensitiveGroup::overlapsElement (SelectBasics_PickResult& thePickResult,
330 SelectBasics_SelectingVolumeManager& theMgr,
331 Standard_Integer theElemIdx,
334 const Standard_Integer aSensitiveIdx = myBVHPrimIndexes.Value (theElemIdx);
335 if (myEntities.FindKey (aSensitiveIdx)->Matches (theMgr, thePickResult))
337 return Standard_True;
340 return Standard_False;
343 // =======================================================================
344 // function : elementIsInside
346 // =======================================================================
347 Standard_Boolean Select3D_SensitiveGroup::elementIsInside (SelectBasics_SelectingVolumeManager& theMgr,
348 Standard_Integer theElemIdx,
349 Standard_Boolean theIsFullInside)
351 SelectBasics_PickResult aDummy;
352 return overlapsElement (aDummy, theMgr, theElemIdx, theIsFullInside);
355 // =======================================================================
356 // function : distanceToCOG
357 // purpose : Calculates distance from the 3d projection of used-picked
358 // screen point to center of the geometry
359 // =======================================================================
360 Standard_Real Select3D_SensitiveGroup::distanceToCOG (SelectBasics_SelectingVolumeManager& theMgr)
362 return theMgr.DistToGeometryCenter (CenterOfGeometry());