f751596e |
1 | // Created on: 2014-08-15 |
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 _SelectMgr_SelectableObjectSet_HeaderFile |
17 | #define _SelectMgr_SelectableObjectSet_HeaderFile |
18 | |
19 | #include <BVH_PrimitiveSet.hxx> |
099f3513 |
20 | #include <BVH_Tree.hxx> |
21 | #include <Graphic3d_Mat4d.hxx> |
22 | #include <Graphic3d_WorldViewProjState.hxx> |
23 | #include <NCollection_Handle.hxx> |
d4aaad5b |
24 | #include <NCollection_IndexedMap.hxx> |
099f3513 |
25 | #include <SelectMgr_SelectableObject.hxx> |
d4aaad5b |
26 | |
099f3513 |
27 | //! The purpose of this class is to organize all selectable objects into data structure, allowing to build |
28 | //! set of BVH trees for each transformation persistence subclass of selectable objects. This allow to minify |
29 | //! number of updates for BVH trees - for example 2D persistent object subclass depends only on camera's projection |
30 | //! and the corresponding BVH tree needs to be updated when camera's projection parameters change, while another |
31 | //! tree for non-persistent objects can be left unchanged in this case. |
32 | class SelectMgr_SelectableObjectSet |
f751596e |
33 | { |
34 | public: |
35 | |
099f3513 |
36 | //! This enumeration declares names for subsets of selectable objects. Each subset has independent BVH tree. |
37 | //! The class maintains subsets of selectable objects by their persistence flag. This allows to restric |
38 | //! rebuilding of the trees for particular subset when the camera change does not implicitly require it: |
39 | //! - BVHSubset_3d refers to the subset of normal world-space 3D objects. Associated BVH tree does not depend |
40 | //! on the camera's state at all. |
41 | //! This subset uses binned BVH builder with 32 bins and 1 element per leaf. |
42 | //! - BVHSubset_3dPersistent refers to the subset of 3D persistent selectable objects (rotate, pan, zoom persistence). |
43 | //! Associated BVH tree needs to be updated when either the camera's projection and position change. |
44 | //! This subset uses linear BVH builder with 32 levels of depth and 1 element per leaf. |
45 | //! - BVHSubset_2dPersistent refers to the subset of 2D persistent selectable objects. Associated BVH tree |
46 | //! needs to be updated only when camera's projection changes. Bounding volumes for this object subclass |
47 | //! is represented directly in eye space coordinates. |
48 | //! This subset uses linear BVH builder with 32 levels of depth and 1 element per leaf. |
49 | enum BVHSubset |
50 | { |
51 | BVHSubset_3d, |
52 | BVHSubset_3dPersistent, |
53 | BVHSubset_2dPersistent, |
54 | BVHSubsetNb |
55 | }; |
56 | |
57 | public: |
58 | |
59 | //! Class to iterate sequentually over all objects from every subset. |
60 | class Iterator |
61 | { |
62 | //! Short-cut definition of map iterator type |
63 | typedef NCollection_IndexedMap<Handle(SelectMgr_SelectableObject)>::Iterator ObjectMapIterator; |
64 | |
65 | public: |
66 | |
67 | //! Default constructor without initialization. |
68 | Iterator() : mySet (NULL), mySubsetIdx (BVHSubsetNb) {} |
69 | |
70 | //! Constructs and initializes the iterator. |
71 | Iterator (const SelectMgr_SelectableObjectSet& theSet) { Init (theSet); } |
72 | |
73 | //! Initializes the iterator. |
74 | void Init (const SelectMgr_SelectableObjectSet& theSet) |
75 | { |
76 | mySet = &theSet; |
77 | mySubsetIdx = 0; |
78 | mySubsetIt = ObjectMapIterator (theSet.myObjects[mySubsetIdx]); |
79 | More(); |
80 | } |
81 | |
82 | //! Returns false when there is no more objects to iterate over. |
83 | Standard_Boolean More() |
84 | { |
85 | if (mySubsetIt.More()) |
86 | { |
87 | return Standard_True; |
88 | } |
89 | else if ((mySubsetIdx == BVHSubsetNb - 1) || mySet == NULL) |
90 | { |
91 | return Standard_False; |
92 | } |
93 | mySubsetIt = ObjectMapIterator (mySet->myObjects[++mySubsetIdx]); |
94 | return More(); |
95 | } |
96 | |
97 | //! Steps to next selectable object in the set. |
98 | void Next() { mySubsetIt.Next(); } |
99 | |
100 | //! Returns current object. |
101 | const Handle(SelectMgr_SelectableObject)& Value() const { return mySubsetIt.Value(); } |
102 | |
103 | private: |
104 | const SelectMgr_SelectableObjectSet* mySet; |
105 | Standard_Integer mySubsetIdx; |
106 | ObjectMapIterator mySubsetIt; |
107 | }; |
108 | |
109 | public: |
110 | |
111 | //! Creates new empty objects set and initializes BVH tree builders for each subset. |
825aa485 |
112 | Standard_EXPORT SelectMgr_SelectableObjectSet(); |
f751596e |
113 | |
d4aaad5b |
114 | //! Releases resources of selectable object set. |
115 | virtual ~SelectMgr_SelectableObjectSet() { } |
f751596e |
116 | |
099f3513 |
117 | //! Adds the new selectable object to the set. The selectable object is placed into one of the |
118 | //! predefined subsets depending on its persistence type. After adding an object, this method |
119 | //! marks the corresponding BVH tree for rebuild. |
120 | //! @return true if selectable object is added, otherwise returns false (selectable object is already in the set). |
825aa485 |
121 | Standard_EXPORT Standard_Boolean Append (const Handle(SelectMgr_SelectableObject)& theObject); |
f751596e |
122 | |
099f3513 |
123 | //! Removes the selectable object from the set. The selectable object is removed from the subset |
124 | //! it has been placed into. After removing an object, this method marks the corresponding |
125 | //! BVH tree for rebuild. |
126 | //! @return true if selectable object is removed, otherwise returns false (selectable object is not in the set). |
825aa485 |
127 | Standard_EXPORT Standard_Boolean Remove (const Handle(SelectMgr_SelectableObject)& theObject); |
f751596e |
128 | |
099f3513 |
129 | //! Performs necessary updates when object's persistence types changes. |
130 | //! This method should be called right after changing transformation persistence flags of the |
131 | //! objects and before updating BVH tree - to provide up-to-date state of the object set. |
132 | Standard_EXPORT void ChangeSubset (const Handle(SelectMgr_SelectableObject)& theObject); |
f751596e |
133 | |
099f3513 |
134 | //! Updates outdated BVH trees and remembers the last state of the |
135 | //! camera view-projection matrices and viewport (window) dimensions. |
136 | Standard_EXPORT void UpdateBVH (const Handle(Graphic3d_Camera)& theCamera, |
137 | const Graphic3d_Mat4d& theProjectionMat, |
138 | const Graphic3d_Mat4d& theWorldViewMat, |
139 | const Graphic3d_WorldViewProjState& theViewState, |
140 | const Standard_Integer theViewportWidth, |
141 | const Standard_Integer theViewportHeight); |
201c2208 |
142 | |
099f3513 |
143 | //! Marks every BVH subset for update. |
144 | Standard_EXPORT void MarkDirty(); |
f751596e |
145 | |
099f3513 |
146 | //! Returns true if this objects set contains theObject given. |
147 | Standard_Boolean Contains (const Handle(SelectMgr_SelectableObject)& theObject) const |
148 | { |
149 | return myObjects[BVHSubset_3d].Contains (theObject) |
150 | || myObjects[BVHSubset_3dPersistent].Contains (theObject) |
151 | || myObjects[BVHSubset_2dPersistent].Contains (theObject); |
152 | } |
f751596e |
153 | |
099f3513 |
154 | //! Returns true if the object set does not contain any selectable objects. |
155 | Standard_Boolean IsEmpty() const |
156 | { |
157 | return myObjects[BVHSubset_3d].IsEmpty() |
158 | && myObjects[BVHSubset_3dPersistent].IsEmpty() |
159 | && myObjects[BVHSubset_2dPersistent].IsEmpty(); |
160 | } |
f751596e |
161 | |
099f3513 |
162 | //! Returns true if the specified object subset is empty. |
163 | Standard_Boolean IsEmpty (const BVHSubset theSubset) const |
825aa485 |
164 | { |
099f3513 |
165 | return myObjects[theSubset].IsEmpty(); |
825aa485 |
166 | } |
f751596e |
167 | |
099f3513 |
168 | //! Returns object from subset theSubset by theIndex given. The method allows to get selectable object |
169 | //! referred by the index of an element of the subset's BVH tree. |
170 | const Handle(SelectMgr_SelectableObject)& GetObjectById (const BVHSubset theSubset, |
171 | const Standard_Integer theIndex) const |
172 | { |
173 | return myObjects[theSubset].FindKey (theIndex + 1); |
174 | } |
175 | |
176 | //! Returns computed BVH for the theSubset given. |
177 | const NCollection_Handle<BVH_Tree<Standard_Real, 3> >& BVH(const BVHSubset theSubset) const |
178 | { |
179 | return myBVH[theSubset]; |
180 | } |
181 | |
182 | private: |
183 | |
184 | //! Returns an appropriate subset of theObject given depending on its persistence type. |
185 | Standard_Integer appropriateSubset (const Handle(SelectMgr_SelectableObject)& theObject) |
186 | { |
778cd667 |
187 | if (theObject->TransformPersistence().IsNull()) |
099f3513 |
188 | { |
189 | return SelectMgr_SelectableObjectSet::BVHSubset_3d; |
190 | } |
778cd667 |
191 | else if (theObject->TransformPersistence()->Mode() == Graphic3d_TMF_2d) |
099f3513 |
192 | { |
193 | return SelectMgr_SelectableObjectSet::BVHSubset_2dPersistent; |
194 | } |
195 | else |
196 | { |
197 | return SelectMgr_SelectableObjectSet::BVHSubset_3dPersistent; |
198 | } |
199 | } |
200 | |
201 | //! Returns current subset of theObject given. |
202 | Standard_Integer currentSubset (const Handle(SelectMgr_SelectableObject)& theObject) |
825aa485 |
203 | { |
099f3513 |
204 | for (Standard_Integer aSubsetIdx = 0; aSubsetIdx < BVHSubsetNb; ++aSubsetIdx) |
205 | { |
206 | if (myObjects[aSubsetIdx].Contains (theObject)) |
207 | { |
208 | return aSubsetIdx; |
209 | } |
210 | } |
211 | return -1; |
825aa485 |
212 | } |
f751596e |
213 | |
214 | private: |
215 | |
099f3513 |
216 | NCollection_IndexedMap<Handle(SelectMgr_SelectableObject)> myObjects[BVHSubsetNb]; //!< Map of objects for each subset |
217 | NCollection_Handle<BVH_Tree<Standard_Real, 3> > myBVH[BVHSubsetNb]; //!< BVH tree computed for each subset |
218 | NCollection_Handle<BVH_Builder<Standard_Real, 3> > myBuilder[BVHSubsetNb]; //!< Builder allocated for each subset |
219 | Standard_Boolean myIsDirty[BVHSubsetNb]; //!< Dirty flag for each subset |
220 | Graphic3d_WorldViewProjState myLastViewState; //!< Last view-projection state used for construction of BVH |
221 | Standard_Integer myLastWidth; //!< Last viewport's (window's) width used for construction of BVH |
222 | Standard_Integer myLastHeight; //!< Last viewport's (window's) height used for construction of BVH |
223 | friend class Iterator; |
f751596e |
224 | }; |
225 | |
226 | #endif // _SelectMgr_SelectableObjectSet_HeaderFile |