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 //=======================================================================
84 //=======================================================================
85 Standard_Boolean Select3D_SensitiveSet::matches (SelectBasics_SelectingVolumeManager& theMgr,
86 SelectBasics_PickResult& thePickResult,
87 Standard_Boolean theToCheckAllInside)
90 const BVH_Tree<Standard_Real, 3, BVH_BinaryTree>* aBVH = myContent.GetBVH().get();
91 if (myContent.Size() < 1 || !theMgr.Overlaps (aBVH->MinPoint (0),
94 return Standard_False;
97 NodeInStack aStack[BVH_Constants_MaxTreeDepth];
100 Standard_Integer aHead = -1;
102 Standard_Integer aMatchesNb = -1;
103 SelectBasics_PickResult aPickResult;
104 const bool toCheckFullInside = (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Point);
107 const BVH_Vec4i& aData = aBVH->NodeInfoBuffer()[aNode.Id];
109 if (aData.x() == 0) // is inner node
111 NodeInStack aLeft (aData.y(), toCheckFullInside), aRight(aData.z(), toCheckFullInside);
112 Standard_Boolean toCheckLft = Standard_True, toCheckRgh = Standard_True;
113 if (!aNode.IsFullInside)
115 toCheckLft = theMgr.Overlaps (aBVH->MinPoint (aLeft.Id), aBVH->MaxPoint (aLeft.Id), toCheckFullInside ? &aLeft.IsFullInside : NULL);
118 aLeft.IsFullInside = Standard_False;
121 toCheckRgh = theMgr.Overlaps (aBVH->MinPoint (aRight.Id), aBVH->MaxPoint (aRight.Id), toCheckFullInside ? &aRight.IsFullInside : NULL);
124 aRight.IsFullInside = Standard_False;
128 if (!theMgr.IsOverlapAllowed()) // inclusion test
130 if (!theToCheckAllInside)
132 if (!toCheckLft || !toCheckRgh)
134 return Standard_False; // no inclusion
138 toCheckLft &= !aLeft.IsFullInside;
139 toCheckRgh &= !aRight.IsFullInside;
143 if (toCheckLft || toCheckRgh)
145 aNode = toCheckLft ? aLeft : aRight;
146 if (toCheckLft && toCheckRgh)
148 aStack[++aHead] = aRight;
156 aNode = aStack[aHead--];
161 for (Standard_Integer anElemIdx = aData.y(); anElemIdx <= aData.z(); ++anElemIdx)
163 if (!theMgr.IsOverlapAllowed()) // inclusion test
165 if (!elementIsInside (theMgr, anElemIdx, aNode.IsFullInside))
167 if (theToCheckAllInside)
171 return Standard_False;
176 if (!overlapsElement (aPickResult, theMgr, anElemIdx, aNode.IsFullInside))
181 if (thePickResult.Depth() > aPickResult.Depth())
183 thePickResult = aPickResult;
184 myDetectedIdx = anElemIdx;
193 aNode = aStack[aHead--];
197 if (aMatchesNb != -1)
199 thePickResult.SetDistToGeomCenter(distanceToCOG(theMgr));
202 return aMatchesNb != -1
203 || (!theToCheckAllInside && !theMgr.IsOverlapAllowed());
206 //=======================================================================
207 // function : BoundingBox
208 // purpose : This method should be redefined in Select3D_SensitiveSet
210 //=======================================================================
211 Select3D_BndBox3d Select3D_SensitiveSet::BoundingBox()
213 return Select3D_BndBox3d (SelectMgr_Vec3 (RealLast()),
214 SelectMgr_Vec3 (RealFirst()));
217 //=======================================================================
218 // function : CenterOfGeometry
219 // purpose : This method should be redefined in Select3D_SensitiveSet
221 //=======================================================================
222 gp_Pnt Select3D_SensitiveSet::CenterOfGeometry() const
224 return gp_Pnt (RealLast(), RealLast(), RealLast());
227 //=======================================================================
229 // purpose : Destroys cross-reference to avoid memory leak
230 //=======================================================================
231 void Select3D_SensitiveSet::Clear()
236 //=======================================================================
237 //function : DumpJson
239 //=======================================================================
240 void Select3D_SensitiveSet::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
242 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
243 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Select3D_SensitiveEntity)
245 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myContent)
247 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDetectedIdx)
249 Select3D_BndBox3d aBoundingBox = ((Select3D_SensitiveSet*)this)->BoundingBox();
250 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &aBoundingBox)