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 #ifndef _Select3D_SensitiveSet_Header
17 #define _Select3D_SensitiveSet_Header
19 #include <BVH_PrimitiveSet3d.hxx>
20 #include <Select3D_BndBox3d.hxx>
21 #include <Select3D_BVHBuilder3d.hxx>
22 #include <Select3D_SensitiveEntity.hxx>
23 #include <SelectBasics_EntityOwner.hxx>
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.
28 //! To use speed-up hierarchical structure in a custom complex sensitive entity, it is
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
35 DEFINE_STANDARD_RTTIEXT(Select3D_SensitiveSet, Select3D_SensitiveEntity)
38 //! Return global instance to default BVH builder.
39 Standard_EXPORT static const Handle(Select3D_BVHBuilder3d)& DefaultBVHBuilder();
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);
46 //! Creates new empty sensitive set and its content
47 Standard_EXPORT Select3D_SensitiveSet (const Handle(SelectBasics_EntityOwner)& theOwnerId);
51 //! Returns the amount of sub-entities of the complex entity
52 virtual Standard_Integer Size() const = 0;
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;
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;
61 //! Swaps items with indexes theIdx1 and theIdx2
62 virtual void Swap (const Standard_Integer theIdx1,
63 const Standard_Integer theIdx2) = 0;
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 virtual Standard_Boolean Matches (SelectBasics_SelectingVolumeManager& theMgr,
68 SelectBasics_PickResult& thePickResult) Standard_OVERRIDE
70 return matches (theMgr, thePickResult, false);
73 //! Builds BVH tree for sensitive set.
74 //! Must be called manually to build BVH tree for any sensitive set
75 //! in case if its content was initialized not in a constructor,
76 //! but element by element
77 Standard_EXPORT virtual void BVH() Standard_OVERRIDE;
79 //! Sets the method (builder) used to construct BVH.
80 void SetBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) { myContent.SetBuilder (theBuilder); }
82 //! Marks BVH tree of the set as outdated. It will be rebuild
83 //! at the next call of BVH()
84 void MarkDirty() { myContent.MarkDirty(); }
86 //! Returns bounding box of the whole set.
87 //! This method should be redefined in Select3D_SensitiveSet descendants
88 Standard_EXPORT virtual Select3D_BndBox3d BoundingBox() Standard_OVERRIDE;
90 //! Returns center of the whole set.
91 //! This method should be redefined in Select3D_SensitiveSet descendants
92 Standard_EXPORT virtual gp_Pnt CenterOfGeometry() const Standard_OVERRIDE;
94 //! Destroys cross-reference to avoid memory leak
95 Standard_EXPORT virtual void Clear() Standard_OVERRIDE;
97 //! Returns a number of nodes in 1 BVH leaf
98 Standard_Integer GetLeafNodeSize() const { return myContent.Builder()->LeafNodeSize(); }
102 //! Checks whether one or more entities of the set overlap current selecting volume.
103 //! Implements the traverse of BVH tree built for the set
104 //! @param theMgr selection manager
105 //! @param thePickResult picking result (for picking by ray)
106 //! @param theToCheckAllInside flag indicating that even with SelectMgr_SelectingVolumeManager::IsOverlapAllowed() returning FALSE
107 //! the method will return TRUE if at least one sub-element is fully inside selection volume ::elementIsInside();
108 //! this is useful for entities allowing local selection of sub-elements using single Owner object.
109 Standard_EXPORT Standard_Boolean matches (SelectBasics_SelectingVolumeManager& theMgr,
110 SelectBasics_PickResult& thePickResult,
111 Standard_Boolean theToCheckAllInside);
113 //! Checks whether the entity with index theIdx (partially) overlaps the current selecting volume.
114 //! @param thePickResult [OUT] picking result, should update minimum depth
115 //! @param theMgr [IN] selection manager
116 //! @param theElemIdx [IN] element index within BVH tree to check
117 //! @param theIsFullInside [IN] when TRUE indicates that entire BVH node is already inside selection volume (in case of rectangle selection);
118 //! in this case algorithm might skip checking the element and just register it as detected
119 virtual Standard_Boolean overlapsElement (SelectBasics_PickResult& thePickResult,
120 SelectBasics_SelectingVolumeManager& theMgr,
121 Standard_Integer theElemIdx,
122 Standard_Boolean theIsFullInside) = 0;
124 //! Checks whether the entity with index theIdx is (fully) inside the current selecting volume
125 //! @param theMgr [IN] selection manager
126 //! @param theElemIdx [IN] element index within BVH tree to check
127 //! @param theIsFullInside [IN] when TRUE indicates that entire BVH node is already inside selection volume (in case of rectangle selection);
128 //! in this case algorithm might skip checking the element and just register it as detected
129 virtual Standard_Boolean elementIsInside (SelectBasics_SelectingVolumeManager& theMgr,
130 Standard_Integer theElemIdx,
131 Standard_Boolean theIsFullInside) = 0;
133 //! Calculates distance from the 3d projection of used-picked screen point to center of the geometry
134 virtual Standard_Real distanceToCOG (SelectBasics_SelectingVolumeManager& theMgr) = 0;
138 //! The purpose of this class is to provide a link between BVH_PrimitiveSet
139 //! and Select3D_SensitiveSet instance to build BVH tree for set of sensitives.
140 class BvhPrimitiveSet : public BVH_PrimitiveSet3d
144 //! Empty constructor.
145 BvhPrimitiveSet() : BVH_PrimitiveSet3d (Handle(Select3D_BVHBuilder3d)()) {}
148 ~BvhPrimitiveSet() {}
150 //! Setup sensitivity set.
151 void SetSensitiveSet (Select3D_SensitiveSet* theSensitiveSet)
153 mySensitiveSet = theSensitiveSet;
157 //! Returns the length of set of sensitives
158 virtual Standard_Integer Size() const Standard_OVERRIDE { return mySensitiveSet->Size(); }
160 //! Returns bounding box of sensitive with index theIdx
161 virtual Select3D_BndBox3d Box (const Standard_Integer theIdx) const Standard_OVERRIDE { return mySensitiveSet->Box (theIdx); }
163 //! Make inherited method Box() visible to avoid CLang warning
164 using BVH_PrimitiveSet3d::Box;
166 //! Returns center of sensitive with index theIdx in the set along the given axis theAxis
167 virtual Standard_Real Center (const Standard_Integer theIdx,
168 const Standard_Integer theAxis) const Standard_OVERRIDE { return mySensitiveSet->Center (theIdx, theAxis); }
170 //! Swaps items with indexes theIdx1 and theIdx2 in the set
171 virtual void Swap (const Standard_Integer theIdx1,
172 const Standard_Integer theIdx2) Standard_OVERRIDE { mySensitiveSet->Swap (theIdx1, theIdx2); }
174 //! Returns the tree built for set of sensitives
175 const opencascade::handle<BVH_Tree<Standard_Real, 3> >& GetBVH() { return BVH(); }
178 Select3D_SensitiveSet* mySensitiveSet; //!< Set of sensitive entities
183 BvhPrimitiveSet myContent; //!< A link between sensitive entity and BVH_PrimitiveSet
184 Standard_Integer myDetectedIdx; //!< Index of detected primitive in BVH sorted primitive array
188 DEFINE_STANDARD_HANDLE(Select3D_SensitiveSet, Select3D_SensitiveEntity)
190 #endif // _Select3D_SensitiveSet_Header