1 // Created on: 2014-05-29
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 <Select3D_SensitiveSet.hxx>
18 #include <BVH_LinearBuilder.hxx>
20 IMPLEMENT_STANDARD_RTTIEXT(Select3D_SensitiveSet,Select3D_SensitiveEntity)
24 //! Default BVH tree builder for sensitive set (optimal for large set of small primitives - for not too long construction time).
25 static Handle(Select3D_BVHBuilder3d) THE_SENS_SET_BUILDER = new BVH_LinearBuilder<Standard_Real, 3> (BVH_Constants_LeafNodeSizeSmall, BVH_Constants_MaxTreeDepth);
28 //=======================================================================
29 // function : DefaultBVHBuilder
31 //=======================================================================
32 const Handle(Select3D_BVHBuilder3d)& Select3D_SensitiveSet::DefaultBVHBuilder()
34 return THE_SENS_SET_BUILDER;
37 //=======================================================================
38 // function : SetDefaultBVHBuilder
40 //=======================================================================
41 void Select3D_SensitiveSet::SetDefaultBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder)
43 THE_SENS_SET_BUILDER = theBuilder;
46 //=======================================================================
47 // function : Select3D_SensitiveSet
48 // purpose : Creates new empty sensitive set and its content
49 //=======================================================================
50 Select3D_SensitiveSet::Select3D_SensitiveSet (const Handle(SelectMgr_EntityOwner)& theOwnerId)
51 : Select3D_SensitiveEntity (theOwnerId),
54 myContent.SetSensitiveSet (this);
55 myContent.SetBuilder (THE_SENS_SET_BUILDER);
56 myContent.MarkDirty();
59 //=======================================================================
61 // purpose : Builds BVH tree for sensitive set
62 //=======================================================================
63 void Select3D_SensitiveSet::BVH()
70 //! This structure describes the node in BVH
73 NodeInStack (Standard_Integer theId = 0,
74 Standard_Boolean theIsFullInside = false) : Id (theId), IsFullInside (theIsFullInside) {}
76 Standard_Integer Id; //!< node identifier
77 Standard_Boolean IsFullInside; //!< if the node is completely inside the current selection volume
81 //=======================================================================
82 // function : processElements
84 //=======================================================================
85 Standard_Boolean Select3D_SensitiveSet::processElements (SelectBasics_SelectingVolumeManager& theMgr,
86 Standard_Integer theFirstElem,
87 Standard_Integer theLastElem,
88 Standard_Boolean theIsFullInside,
89 Standard_Boolean theToCheckAllInside,
90 SelectBasics_PickResult& thePickResult,
91 Standard_Integer& theMatchesNb)
93 SelectBasics_PickResult aPickResult;
94 for (Standard_Integer anIdx = theFirstElem; anIdx <= theLastElem; anIdx++)
96 if (!theMgr.IsOverlapAllowed()) // inclusion test
98 if (!elementIsInside (theMgr, anIdx, theIsFullInside))
100 if (theToCheckAllInside)
104 return Standard_False;
109 if (!overlapsElement (aPickResult, theMgr, anIdx, theIsFullInside))
114 if (thePickResult.Depth() > aPickResult.Depth())
116 thePickResult = aPickResult;
117 myDetectedIdx = anIdx;
123 return Standard_True;
126 //=======================================================================
127 // function : Matches
129 //=======================================================================
130 Standard_Boolean Select3D_SensitiveSet::matches (SelectBasics_SelectingVolumeManager& theMgr,
131 SelectBasics_PickResult& thePickResult,
132 Standard_Boolean theToCheckAllInside)
136 if (myContent.Size() < 1)
138 return Standard_False;
141 const Select3D_BndBox3d& aGlobalBox = myContent.Box();
142 Standard_Boolean isFullInside = Standard_True;
144 if (!theMgr.Overlaps(aGlobalBox.CornerMin(),
145 aGlobalBox.CornerMax(),
148 return Standard_False;
151 Standard_Integer aMatchesNb = -1;
153 const bool toCheckFullInside = (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Point);
154 if (toCheckFullInside && isFullInside)
156 Standard_Integer aSize = myContent.Size();
157 if (!processElements (theMgr, 0, aSize - 1, Standard_True, theToCheckAllInside, thePickResult, aMatchesNb))
159 return Standard_False;
164 const BVH_Tree<Standard_Real, 3, BVH_BinaryTree>* aBVH = myContent.GetBVH().get();
165 NodeInStack aStack[BVH_Constants_MaxTreeDepth];
168 Standard_Integer aHead = -1;
172 const BVH_Vec4i& aData = aBVH->NodeInfoBuffer()[aNode.Id];
174 if (aData.x() == 0) // is inner node
176 NodeInStack aLeft (aData.y(), toCheckFullInside), aRight(aData.z(), toCheckFullInside);
177 Standard_Boolean toCheckLft = Standard_True, toCheckRgh = Standard_True;
178 if (!aNode.IsFullInside)
180 toCheckLft = theMgr.Overlaps (aBVH->MinPoint (aLeft.Id), aBVH->MaxPoint (aLeft.Id), toCheckFullInside ? &aLeft.IsFullInside : NULL);
183 aLeft.IsFullInside = Standard_False;
186 toCheckRgh = theMgr.Overlaps (aBVH->MinPoint (aRight.Id), aBVH->MaxPoint (aRight.Id), toCheckFullInside ? &aRight.IsFullInside : NULL);
189 aRight.IsFullInside = Standard_False;
193 if (!theMgr.IsOverlapAllowed()) // inclusion test
195 if (!theToCheckAllInside)
197 if (!toCheckLft || !toCheckRgh)
199 return Standard_False; // no inclusion
203 toCheckLft &= !aLeft.IsFullInside;
204 toCheckRgh &= !aRight.IsFullInside;
208 if (toCheckLft || toCheckRgh)
210 aNode = toCheckLft ? aLeft : aRight;
211 if (toCheckLft && toCheckRgh)
213 aStack[++aHead] = aRight;
221 aNode = aStack[aHead--];
226 if (!processElements (theMgr, aData.y(), aData.z(), aNode.IsFullInside, theToCheckAllInside, thePickResult, aMatchesNb))
228 return Standard_False;
234 aNode = aStack[aHead--];
239 if (aMatchesNb != -1)
241 thePickResult.SetDistToGeomCenter (distanceToCOG (theMgr));
244 return aMatchesNb != -1
245 || (!theToCheckAllInside && !theMgr.IsOverlapAllowed());
248 //=======================================================================
249 // function : BoundingBox
250 // purpose : This method should be redefined in Select3D_SensitiveSet
252 //=======================================================================
253 Select3D_BndBox3d Select3D_SensitiveSet::BoundingBox()
255 return Select3D_BndBox3d (SelectMgr_Vec3 (RealLast()),
256 SelectMgr_Vec3 (RealFirst()));
259 //=======================================================================
260 // function : CenterOfGeometry
261 // purpose : This method should be redefined in Select3D_SensitiveSet
263 //=======================================================================
264 gp_Pnt Select3D_SensitiveSet::CenterOfGeometry() const
266 return gp_Pnt (RealLast(), RealLast(), RealLast());
269 //=======================================================================
271 // purpose : Destroys cross-reference to avoid memory leak
272 //=======================================================================
273 void Select3D_SensitiveSet::Clear()
278 //=======================================================================
279 //function : DumpJson
281 //=======================================================================
282 void Select3D_SensitiveSet::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
284 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
285 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Select3D_SensitiveEntity)
287 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myContent)
289 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDetectedIdx)
291 Select3D_BndBox3d aBoundingBox = ((Select3D_SensitiveSet*)this)->BoundingBox();
292 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &aBoundingBox)