319fd115ce29434388dd522a4a5902c95699fd21
[occt.git] / src / Select3D / Select3D_SensitiveSet.hxx
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
19 #include <Standard.hxx>
20 #include <Standard_Type.hxx>
21 #include <Standard_Type.hxx>
22
23 #include <NCollection_Handle.hxx>
24
25 #include <Select3D_BndBox3d.hxx>
26 #include <SelectBasics_EntityOwner.hxx>
27 #include <Select3D_SensitiveEntity.hxx>
28
29 class Select3D_BVHPrimitiveContent;
30
31 //! This class is base class for handling overlap detection of complex sensitive
32 //! entities. It provides an interface for building BVH tree for some set of entities.
33 //! Thereby, each iteration of overlap detection is a traverse of BVH tree in fact.
34 //! To use speed-up hierarchical structure in a custom complex sensitive entity, it is
35 //! necessary to make that custom entity a descendant of this class and organize sub-entities
36 //! in some container which allows referencing to elements by index. Note that methods taking
37 //! index as a parameter are used for BVH build and the range of given index is [0; Size() - 1].
38 //! For example of usage see Select3D_SensitiveTriangulation.
39 class Select3D_SensitiveSet : public Select3D_SensitiveEntity
40 {
41 public:
42
43   //! Creates new empty sensitive set and its content
44   Standard_EXPORT Select3D_SensitiveSet (const Handle(SelectBasics_EntityOwner)& theOwnerId);
45
46   Standard_EXPORT ~Select3D_SensitiveSet() {};
47
48 public:
49
50   //! Returns the amount of sub-entities of the complex entity
51   virtual Standard_Integer Size() const = 0;
52
53   //! Returns bounding box of sub-entity with index theIdx in sub-entity list
54   virtual Select3D_BndBox3d Box (const Standard_Integer theIdx) const = 0;
55
56   //! Returns geometry center of sensitive entity index theIdx along the given axis theAxis
57   virtual Standard_Real Center (const Standard_Integer theIdx,
58                                 const Standard_Integer theAxis) const = 0;
59
60   //! Swaps items with indexes theIdx1 and theIdx2
61   virtual void Swap (const Standard_Integer theIdx1,
62                      const Standard_Integer theIdx2) = 0;
63
64   //! Checks whether one or more entities of the set overlap current selecting volume.
65   //! Implements the traverse of BVH tree built for the set
66   Standard_EXPORT virtual Standard_Boolean Matches (SelectBasics_SelectingVolumeManager& theMgr,
67                                                     SelectBasics_PickResult& thePickResult) Standard_OVERRIDE;
68
69   //! Builds BVH tree for sensitive set.
70   //! Must be called manually to build BVH tree for any sensitive set
71   //! in case if its content was initialized not in a constructor,
72   //! but element by element
73   Standard_EXPORT void BVH() Standard_OVERRIDE;
74
75   //! Marks BVH tree of the set as outdated. It will be rebuild
76   //! at the next call of BVH()
77   Standard_EXPORT void MarkDirty();
78
79   //! Returns bounding box of the whole set.
80   //! This method should be redefined in Select3D_SensitiveSet descendants
81   Standard_EXPORT virtual Select3D_BndBox3d BoundingBox() Standard_OVERRIDE;
82
83   //! Returns center of the whole set.
84   //! This method should be redefined in Select3D_SensitiveSet descendants
85   Standard_EXPORT virtual gp_Pnt CenterOfGeometry() const Standard_OVERRIDE;
86
87   //! Destroys cross-reference to avoid memory leak
88   Standard_EXPORT virtual void Clear() Standard_OVERRIDE;
89
90   //! Returns a number of nodes in 1 BVH leaf
91   Standard_EXPORT Standard_Integer GetLeafNodeSize() const;
92
93 public:
94   DEFINE_STANDARD_RTTIEXT(Select3D_SensitiveSet,Select3D_SensitiveEntity)
95
96 protected:
97
98   //! Checks whether the entity with index theIdx overlaps the current selecting volume
99   virtual Standard_Boolean overlapsElement (SelectBasics_SelectingVolumeManager& theMgr,
100                                             Standard_Integer theElemIdx,
101                                             Standard_Real& theMatchDepth) = 0;
102
103   //! Checks whether the entity with index theIdx is inside the current selecting volume
104   virtual Standard_Boolean elementIsInside (SelectBasics_SelectingVolumeManager& theMgr,
105                                             const Standard_Integer               theElemIdx) = 0;
106
107   //! Calculates distance from the 3d projection of used-picked screen point to center of the geometry
108   virtual Standard_Real distanceToCOG (SelectBasics_SelectingVolumeManager& theMgr) = 0;
109
110 protected:
111   Standard_Integer myDetectedIdx;     //!< Index of detected primitive in BVH sorted primitive array, for debug purposes
112
113 private:
114
115   Standard_Boolean                                 myIsLeftChildQueuedFirst;     //!< Flag for slight randomization of BVH traverse
116   NCollection_Handle<Select3D_BVHPrimitiveContent> myContent;                    //!< A link between sensitive entity and BVH_PrimitiveSet
117 };
118
119 DEFINE_STANDARD_HANDLE(Select3D_SensitiveSet, Select3D_SensitiveEntity)
120
121 #endif // _Select3D_SensitiveSet_Header