f751596e |
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 | #ifndef _Select3D_SensitiveSet_Header |
17 | #define _Select3D_SensitiveSet_Header |
18 | |
f5b72419 |
19 | #include <BVH_PrimitiveSet3d.hxx> |
f751596e |
20 | #include <Select3D_BndBox3d.hxx> |
f5b72419 |
21 | #include <Select3D_BVHBuilder3d.hxx> |
f751596e |
22 | #include <Select3D_SensitiveEntity.hxx> |
85114087 |
23 | #include <SelectBasics_EntityOwner.hxx> |
f751596e |
24 | |
25 | //! This class is base class for handling overlap detection of complex sensitive |
26 | //! entities. It provides an interface for building BVH tree for some set of entities. |
27 | //! Thereby, each iteration of overlap detection is a traverse of BVH tree in fact. |
2157d6ac |
28 | //! To use speed-up hierarchical structure in a custom complex sensitive entity, it is |
f751596e |
29 | //! necessary to make that custom entity a descendant of this class and organize sub-entities |
30 | //! in some container which allows referencing to elements by index. Note that methods taking |
31 | //! index as a parameter are used for BVH build and the range of given index is [0; Size() - 1]. |
32 | //! For example of usage see Select3D_SensitiveTriangulation. |
33 | class Select3D_SensitiveSet : public Select3D_SensitiveEntity |
34 | { |
85114087 |
35 | DEFINE_STANDARD_RTTIEXT(Select3D_SensitiveSet, Select3D_SensitiveEntity) |
f5b72419 |
36 | public: |
37 | |
38 | //! Return global instance to default BVH builder. |
39 | Standard_EXPORT static const Handle(Select3D_BVHBuilder3d)& DefaultBVHBuilder(); |
40 | |
41 | //! Assign new BVH builder to be used by default for new sensitive sets (assigning is NOT thread-safe!). |
42 | Standard_EXPORT static void SetDefaultBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder); |
43 | |
f751596e |
44 | public: |
45 | |
46 | //! Creates new empty sensitive set and its content |
47 | Standard_EXPORT Select3D_SensitiveSet (const Handle(SelectBasics_EntityOwner)& theOwnerId); |
48 | |
f751596e |
49 | public: |
50 | |
51 | //! Returns the amount of sub-entities of the complex entity |
52 | virtual Standard_Integer Size() const = 0; |
53 | |
54 | //! Returns bounding box of sub-entity with index theIdx in sub-entity list |
55 | virtual Select3D_BndBox3d Box (const Standard_Integer theIdx) const = 0; |
56 | |
57 | //! Returns geometry center of sensitive entity index theIdx along the given axis theAxis |
58 | virtual Standard_Real Center (const Standard_Integer theIdx, |
59 | const Standard_Integer theAxis) const = 0; |
60 | |
61 | //! Swaps items with indexes theIdx1 and theIdx2 |
62 | virtual void Swap (const Standard_Integer theIdx1, |
63 | const Standard_Integer theIdx2) = 0; |
64 | |
65 | //! Checks whether one or more entities of the set overlap current selecting volume. |
66 | //! Implements the traverse of BVH tree built for the set |
67 | Standard_EXPORT virtual Standard_Boolean Matches (SelectBasics_SelectingVolumeManager& theMgr, |
68 | SelectBasics_PickResult& thePickResult) Standard_OVERRIDE; |
69 | |
70 | //! Builds BVH tree for sensitive set. |
71 | //! Must be called manually to build BVH tree for any sensitive set |
72 | //! in case if its content was initialized not in a constructor, |
73 | //! but element by element |
85114087 |
74 | Standard_EXPORT virtual void BVH() Standard_OVERRIDE; |
f751596e |
75 | |
f5b72419 |
76 | //! Sets the method (builder) used to construct BVH. |
77 | void SetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) { myContent.SetBuilder (theBuilder); } |
78 | |
f751596e |
79 | //! Marks BVH tree of the set as outdated. It will be rebuild |
80 | //! at the next call of BVH() |
85114087 |
81 | void MarkDirty() { myContent.MarkDirty(); } |
f751596e |
82 | |
83 | //! Returns bounding box of the whole set. |
84 | //! This method should be redefined in Select3D_SensitiveSet descendants |
85 | Standard_EXPORT virtual Select3D_BndBox3d BoundingBox() Standard_OVERRIDE; |
86 | |
87 | //! Returns center of the whole set. |
88 | //! This method should be redefined in Select3D_SensitiveSet descendants |
89 | Standard_EXPORT virtual gp_Pnt CenterOfGeometry() const Standard_OVERRIDE; |
90 | |
91 | //! Destroys cross-reference to avoid memory leak |
92 | Standard_EXPORT virtual void Clear() Standard_OVERRIDE; |
93 | |
2157d6ac |
94 | //! Returns a number of nodes in 1 BVH leaf |
f5b72419 |
95 | Standard_Integer GetLeafNodeSize() const { return myContent.Builder()->LeafNodeSize(); } |
f751596e |
96 | |
97 | protected: |
98 | |
8b9a309b |
99 | //! Checks whether the entity with index theIdx overlaps the current selecting volume. |
100 | //! @param theMatchDepth set to the current minimum depth by Select3D_SensitiveSet; should be set to the new depth when overlapping is detected |
f751596e |
101 | virtual Standard_Boolean overlapsElement (SelectBasics_SelectingVolumeManager& theMgr, |
102 | Standard_Integer theElemIdx, |
103 | Standard_Real& theMatchDepth) = 0; |
104 | |
2157d6ac |
105 | //! Checks whether the entity with index theIdx is inside the current selecting volume |
106 | virtual Standard_Boolean elementIsInside (SelectBasics_SelectingVolumeManager& theMgr, |
107 | const Standard_Integer theElemIdx) = 0; |
108 | |
f751596e |
109 | //! Calculates distance from the 3d projection of used-picked screen point to center of the geometry |
110 | virtual Standard_Real distanceToCOG (SelectBasics_SelectingVolumeManager& theMgr) = 0; |
111 | |
112 | protected: |
f751596e |
113 | |
85114087 |
114 | //! The purpose of this class is to provide a link between BVH_PrimitiveSet |
115 | //! and Select3D_SensitiveSet instance to build BVH tree for set of sensitives. |
f5b72419 |
116 | class BvhPrimitiveSet : public BVH_PrimitiveSet3d |
85114087 |
117 | { |
118 | public: |
119 | |
120 | //! Empty constructor. |
f5b72419 |
121 | BvhPrimitiveSet() : BVH_PrimitiveSet3d (Handle(Select3D_BVHBuilder3d)()) {} |
85114087 |
122 | |
123 | //! Destructor. |
124 | ~BvhPrimitiveSet() {} |
125 | |
126 | //! Setup sensitivity set. |
127 | void SetSensitiveSet (Select3D_SensitiveSet* theSensitiveSet) |
128 | { |
129 | mySensitiveSet = theSensitiveSet; |
130 | MarkDirty(); |
131 | } |
132 | |
133 | //! Returns the length of set of sensitives |
134 | virtual Standard_Integer Size() const Standard_OVERRIDE { return mySensitiveSet->Size(); } |
135 | |
136 | //! Returns bounding box of sensitive with index theIdx |
137 | virtual Select3D_BndBox3d Box (const Standard_Integer theIdx) const Standard_OVERRIDE { return mySensitiveSet->Box (theIdx); } |
138 | |
139 | //! Make inherited method Box() visible to avoid CLang warning |
f5b72419 |
140 | using BVH_PrimitiveSet3d::Box; |
85114087 |
141 | |
142 | //! Returns center of sensitive with index theIdx in the set along the given axis theAxis |
143 | virtual Standard_Real Center (const Standard_Integer theIdx, |
144 | const Standard_Integer theAxis) const Standard_OVERRIDE { return mySensitiveSet->Center (theIdx, theAxis); } |
145 | |
146 | //! Swaps items with indexes theIdx1 and theIdx2 in the set |
147 | virtual void Swap (const Standard_Integer theIdx1, |
148 | const Standard_Integer theIdx2) Standard_OVERRIDE { mySensitiveSet->Swap (theIdx1, theIdx2); } |
149 | |
150 | //! Returns the tree built for set of sensitives |
f5b72419 |
151 | const opencascade::handle<BVH_Tree<Standard_Real, 3> >& GetBVH() { return BVH(); } |
85114087 |
152 | |
153 | protected: |
154 | Select3D_SensitiveSet* mySensitiveSet; //!< Set of sensitive entities |
155 | }; |
156 | |
157 | protected: |
158 | |
159 | BvhPrimitiveSet myContent; //!< A link between sensitive entity and BVH_PrimitiveSet |
160 | Standard_Integer myDetectedIdx; //!< Index of detected primitive in BVH sorted primitive array |
f751596e |
161 | |
f751596e |
162 | }; |
163 | |
164 | DEFINE_STANDARD_HANDLE(Select3D_SensitiveSet, Select3D_SensitiveEntity) |
165 | |
166 | #endif // _Select3D_SensitiveSet_Header |