0031716: Visualization, Select3D_SensitiveSet::matches() - avoid building BVH in...
[occt.git] / src / Select3D / Select3D_SensitiveSet.cxx
1 // Created on: 2014-05-29
2 // Created by: Varvara POSKONINA
3 // Copyright (c) 2005-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <Select3D_SensitiveSet.hxx>
17
18 #include <BVH_LinearBuilder.hxx>
19
20 IMPLEMENT_STANDARD_RTTIEXT(Select3D_SensitiveSet,Select3D_SensitiveEntity)
21
22 namespace
23 {
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);
26 }
27
28 //=======================================================================
29 // function : DefaultBVHBuilder
30 // purpose  :
31 //=======================================================================
32 const Handle(Select3D_BVHBuilder3d)& Select3D_SensitiveSet::DefaultBVHBuilder()
33 {
34   return THE_SENS_SET_BUILDER;
35 }
36
37 //=======================================================================
38 // function : SetDefaultBVHBuilder
39 // purpose  :
40 //=======================================================================
41 void Select3D_SensitiveSet::SetDefaultBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder)
42 {
43   THE_SENS_SET_BUILDER = theBuilder;
44 }
45
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),
52   myDetectedIdx (-1)
53 {
54   myContent.SetSensitiveSet (this);
55   myContent.SetBuilder (THE_SENS_SET_BUILDER);
56   myContent.MarkDirty();
57 }
58
59 //=======================================================================
60 // function : BVH
61 // purpose  : Builds BVH tree for sensitive set
62 //=======================================================================
63 void Select3D_SensitiveSet::BVH()
64 {
65   myContent.GetBVH();
66 }
67
68 namespace
69 {
70   //! This structure describes the node in BVH
71   struct NodeInStack
72   {
73     NodeInStack (Standard_Integer theId = 0,
74                  Standard_Boolean theIsFullInside = false) : Id (theId), IsFullInside (theIsFullInside) {}
75
76     Standard_Integer Id;           //!< node identifier
77     Standard_Boolean IsFullInside; //!< if the node is completely inside the current selection volume
78   };
79 }
80
81 //=======================================================================
82 // function : processElements
83 // purpose  :
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)
92 {
93   SelectBasics_PickResult aPickResult;
94   for (Standard_Integer anIdx = theFirstElem; anIdx <= theLastElem; anIdx++)
95   {
96     if (!theMgr.IsOverlapAllowed()) // inclusion test
97     {
98       if (!elementIsInside (theMgr, anIdx, theIsFullInside))
99       {
100         if (theToCheckAllInside)
101         {
102           continue;
103         }
104         return Standard_False;
105       }
106     }
107     else // overlap test
108     {
109       if (!overlapsElement (aPickResult, theMgr, anIdx, theIsFullInside))
110       {
111         continue;
112       }
113
114       if (thePickResult.Depth() > aPickResult.Depth())
115       {
116         thePickResult = aPickResult;
117         myDetectedIdx = anIdx;
118       }
119     }
120     ++theMatchesNb;
121   }
122
123   return Standard_True;
124 }
125
126 //=======================================================================
127 // function : Matches
128 // purpose  :
129 //=======================================================================
130 Standard_Boolean Select3D_SensitiveSet::matches (SelectBasics_SelectingVolumeManager& theMgr,
131                                                  SelectBasics_PickResult& thePickResult,
132                                                  Standard_Boolean theToCheckAllInside)
133 {
134   myDetectedIdx = -1;
135   
136   if (myContent.Size() < 1)
137   {
138     return Standard_False;
139   }
140
141   const Select3D_BndBox3d& aGlobalBox = myContent.Box();
142   Standard_Boolean isFullInside = Standard_True;
143
144   if (!theMgr.Overlaps(aGlobalBox.CornerMin(),
145                        aGlobalBox.CornerMax(),
146                        &isFullInside))
147   {
148     return Standard_False;
149   }
150
151   Standard_Integer aMatchesNb = -1;
152
153   const bool toCheckFullInside = (theMgr.GetActiveSelectionType() != SelectBasics_SelectingVolumeManager::Point);
154   if (toCheckFullInside && isFullInside)
155   {
156     Standard_Integer aSize = myContent.Size();
157     if (!processElements (theMgr, 0, aSize - 1, Standard_True, theToCheckAllInside, thePickResult, aMatchesNb))
158     {
159       return Standard_False;
160     }
161   }
162   else
163   {
164     const BVH_Tree<Standard_Real, 3, BVH_BinaryTree>* aBVH = myContent.GetBVH().get();
165     NodeInStack aStack[BVH_Constants_MaxTreeDepth];
166     NodeInStack aNode;
167
168     Standard_Integer aHead = -1;
169
170     for (;;)
171     {
172       const BVH_Vec4i& aData = aBVH->NodeInfoBuffer()[aNode.Id];
173
174       if (aData.x() == 0) // is inner node
175       {
176         NodeInStack aLeft (aData.y(), toCheckFullInside), aRight(aData.z(), toCheckFullInside);
177         Standard_Boolean toCheckLft = Standard_True, toCheckRgh = Standard_True;
178         if (!aNode.IsFullInside)
179         {
180           toCheckLft = theMgr.Overlaps (aBVH->MinPoint (aLeft.Id), aBVH->MaxPoint (aLeft.Id), toCheckFullInside ? &aLeft.IsFullInside : NULL);
181           if (!toCheckLft)
182           {
183             aLeft.IsFullInside = Standard_False;
184           }
185
186           toCheckRgh = theMgr.Overlaps (aBVH->MinPoint (aRight.Id), aBVH->MaxPoint (aRight.Id), toCheckFullInside ? &aRight.IsFullInside : NULL);
187           if (!toCheckRgh)
188           {
189             aRight.IsFullInside = Standard_False;
190           }
191         }
192
193         if (!theMgr.IsOverlapAllowed()) // inclusion test
194         {
195           if (!theToCheckAllInside)
196           {
197             if (!toCheckLft || !toCheckRgh)
198             {
199               return Standard_False; // no inclusion
200             }
201
202             // skip extra checks
203             toCheckLft &= !aLeft.IsFullInside;
204             toCheckRgh &= !aRight.IsFullInside;
205           }
206         }
207
208         if (toCheckLft || toCheckRgh)
209         {
210           aNode = toCheckLft ? aLeft : aRight;
211           if (toCheckLft && toCheckRgh)
212           {
213             aStack[++aHead] = aRight;
214           }
215         }
216         else
217         {
218           if (aHead < 0)
219             break;
220
221           aNode = aStack[aHead--];
222         }
223       }
224       else
225       {
226         if (!processElements (theMgr, aData.y(), aData.z(), aNode.IsFullInside, theToCheckAllInside, thePickResult, aMatchesNb))
227         {
228           return Standard_False;
229         }
230
231         if (aHead < 0)
232           break;
233
234         aNode = aStack[aHead--];
235       }
236     }
237   }
238
239   if (aMatchesNb != -1)
240   {
241     thePickResult.SetDistToGeomCenter (distanceToCOG (theMgr));
242   }
243
244   return aMatchesNb != -1
245      || (!theToCheckAllInside && !theMgr.IsOverlapAllowed());
246 }
247
248 //=======================================================================
249 // function : BoundingBox
250 // purpose  : This method should be redefined in Select3D_SensitiveSet
251 //            descendants
252 //=======================================================================
253 Select3D_BndBox3d Select3D_SensitiveSet::BoundingBox()
254 {
255   return Select3D_BndBox3d (SelectMgr_Vec3 (RealLast()),
256                             SelectMgr_Vec3 (RealFirst()));
257 }
258
259 //=======================================================================
260 // function : CenterOfGeometry
261 // purpose  : This method should be redefined in Select3D_SensitiveSet
262 //            descendants
263 //=======================================================================
264 gp_Pnt Select3D_SensitiveSet::CenterOfGeometry() const
265 {
266   return gp_Pnt (RealLast(), RealLast(), RealLast());
267 }
268
269 //=======================================================================
270 // function : Clear
271 // purpose  : Destroys cross-reference to avoid memory leak
272 //=======================================================================
273 void Select3D_SensitiveSet::Clear()
274 {
275   //
276 }
277
278 //=======================================================================
279 //function : DumpJson
280 //purpose  :
281 //=======================================================================
282 void Select3D_SensitiveSet::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
283 {
284   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
285   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Select3D_SensitiveEntity)
286
287   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myContent)
288
289   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDetectedIdx)
290
291   Select3D_BndBox3d aBoundingBox = ((Select3D_SensitiveSet*)this)->BoundingBox();
292   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &aBoundingBox)
293 }