0024023: Revamp the OCCT Handle -- general
[occt.git] / src / SelectMgr / SelectMgr_ViewerSelector.hxx
1 // Created on: 1995-02-15
2 // Created by: Roberc Coublanc
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // Modified by  ...
18 //              ROB JAN/07/98 : Improve Storage of detected entities
19 //              AGV OCT/23/03 : Optimize the method SortResult() (OCC4201)
20
21 #ifndef _SelectMgr_ViewerSelector_HeaderFile
22 #define _SelectMgr_ViewerSelector_HeaderFile
23
24 #include <MMgt_TShared.hxx>
25 #include <NCollection_Handle.hxx>
26 #include <NCollection_DataMap.hxx>
27 #include <OSD_Chronometer.hxx>
28 #include <TColStd_SequenceOfInteger.hxx>
29 #include <TColStd_HArray1OfInteger.hxx>
30
31 #include <Standard.hxx>
32 #include <Standard_Type.hxx>
33
34 #include <SelectMgr_IndexedDataMapOfOwnerCriterion.hxx>
35 #include <SelectMgr_SelectingVolumeManager.hxx>
36 #include <SelectMgr_Selection.hxx>
37 #include <SelectMgr_SelectableObject.hxx>
38 #include <SelectMgr_SelectableObjectSet.hxx>
39 #include <SelectMgr_StateOfSelection.hxx>
40 #include <Standard_OStream.hxx>
41
42 class SelectMgr_SelectionManager;
43 class SelectMgr_Selection;
44 class SelectMgr_SensitiveEntitySet;
45 class SelectMgr_EntityOwner;
46 class TCollection_AsciiString;
47 class SelectBasics_SensitiveEntity;
48 class SelectMgr_SelectableObjectSet;
49
50 typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), NCollection_Handle<SelectMgr_SensitiveEntitySet> > SelectMgr_MapOfObjectSensitives;
51 typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), NCollection_Handle<SelectMgr_SensitiveEntitySet> >::Iterator SelectMgr_MapOfObjectSensitivesIterator;
52
53 typedef NCollection_DataMap<Handle(SelectMgr_EntityOwner), Standard_Integer> SelectMgr_MapOfOwnerDetectedEntities;
54 typedef NCollection_DataMap<Handle(SelectMgr_EntityOwner), Standard_Integer>::Iterator SelectMgr_MapOfOwnerDetectedEntitiesIterator;
55
56 //! An internal class for calculation of current largest tolerance value which will be applied
57 //! for creation of selecting frustum by default. Each time the selection set is deactivated,
58 //! maximum tolerance value will be recalculated. If a user enables custom precision using
59 //! StdSelect_ViewerSelector3d::SetPixelTolerance, it will be applied to all sensitive entities
60 //! without any checks.
61 class SelectMgr_ToleranceMap
62 {
63 public:
64
65   //! Sets tolerance values to -1.0
66   SelectMgr_ToleranceMap();
67
68   Standard_EXPORT ~SelectMgr_ToleranceMap();
69
70   //! Adds the value given to map, checks if the current tolerance value
71   //! should be replaced by theTolerance
72   Standard_EXPORT void Add (const Standard_Real& theTolerance);
73
74   //! Decrements a counter of the tolerance given, checks if the current tolerance value
75   //! should be recalculated
76   Standard_EXPORT void Decrement (const Standard_Real& theTolerance);
77
78   //! Returns a current tolerance that must be applied
79   Standard_EXPORT Standard_Real Tolerance();
80
81   //! Sets tolerance to the given one and disables adaptive checks
82   Standard_EXPORT void SetCustomTolerance (const Standard_Real theTolerance);
83
84   //! Unsets a custom tolerance and enables adaptive checks
85   Standard_EXPORT void ResetDefaults();
86
87 private:
88   NCollection_DataMap<Standard_Real, Standard_Integer> myTolerances;
89   Standard_Real                                        myLargestKey;
90   Standard_Real                                        myCustomTolerance;
91 };
92
93 //! A framework to define finding, sorting the sensitive
94 //! primitives in a view. Services are also provided to
95 //! define the return of the owners of those primitives
96 //! selected. The primitives are sorted by criteria such
97 //! as priority of the primitive or its depth in the view
98 //! relative to that of other primitives.
99 //! Note that in 3D, the inheriting framework
100 //! StdSelect_ViewerSelector3d   is only to be used
101 //! if you do not want to use the services provided by
102 //! AIS.
103 //! Two tools are available to find and select objects
104 //! found at a given position in the view. If you want to
105 //! select the owners of all the objects detected at
106 //! point x,y,z you use the Init - More - Next - Picked
107 //! loop. If, on the other hand, you want to select only
108 //! one object detected at that point, you use the Init -
109 //! More - OnePicked loop. In this iteration, More is
110 //! used to see if an object was picked and
111 //! OnePicked, to get the object closest to the pick position.
112 //! Viewer selectors are driven by
113 //! SelectMgr_SelectionManager, and manipulate
114 //! the SelectMgr_Selection objects given to them by
115 //! the selection manager.
116 //!
117 //! Tolerances are applied to the entities in the following way:
118 //! 1. tolerance value stored in mytolerance will be used to calculate initial
119 //!    selecting frustum, which will be applied for intersection testing during
120 //!    BVH traverse;
121 //! 2. if tolerance of sensitive entity is less than mytolerance, the frustum for
122 //!    intersection detection will be resized according to its sensitivity.
123 class SelectMgr_ViewerSelector : public MMgt_TShared
124 {
125 public:
126
127   //! Empties all the tables, removes all selections...
128   Standard_EXPORT void Clear();
129
130   //! returns the Sensitivity of picking
131   Standard_Real Sensitivity() const;
132
133   //! Sorts the detected entites by priority and distance.
134   //!          to be redefined if other criterion are used...
135   Standard_EXPORT void SortResult();
136
137   //! Begins an iteration scanning for the owners detected at a position in the view.
138   void Init();
139
140   //!  Continues the interation scanning for the owners
141   //!   detected at a position in the view, or
142   //! -   continues the iteration scanning for the owner
143   //!   closest to the position in the view.
144   Standard_EXPORT Standard_Boolean More();
145
146   //! Returns the next owner found in the iteration. This is
147   //! a scan for the owners detected at a position in the view.
148   void Next();
149
150   //! Returns the current selected entity detected by the selector;
151   Standard_EXPORT Handle(SelectMgr_EntityOwner) Picked() const;
152
153   //! Returns the picked element with the highest priority,
154   //! and which is the closest to the last successful mouse position.
155   Standard_EXPORT Handle(SelectMgr_EntityOwner) OnePicked();
156
157   //! Set preference of selecting one object for OnePicked() method:
158   //! - If True, objects with less depth (distance fron the view plane) are
159   //!   preferred regardless of priority (priority is used then to choose among
160   //!   objects with similar depth),
161   //! - If False, objects with higher priority are preferred regardless of the
162   //!   depth which is used to choose among objects of the same priority.
163   void SetPickClosest (const Standard_Boolean preferClosest);
164
165   //! Returns the number of owners found at a position in
166   //! the view by the Init - More - Next - Picked iteration.
167   Standard_EXPORT Standard_Integer NbPicked() const;
168
169   //! Returns the  entity which is at rank <aRank>
170   //!          in the list of stored ones.
171   Standard_EXPORT Handle(SelectMgr_EntityOwner) Picked (const Standard_Integer aRank) const;
172
173   Standard_EXPORT Standard_Boolean Contains (const Handle(SelectMgr_SelectableObject)& theObject) const;
174
175   //! Returns the list of selection modes ModeList found in
176   //! this selector for the selectable object aSelectableObject.
177   //! Returns true if aSelectableObject is referenced inside
178   //! this selector; returns false if the object is not present
179   //! in this selector.
180   Standard_EXPORT Standard_Boolean Modes (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
181                                           TColStd_ListOfInteger& theModeList,
182                                           const SelectMgr_StateOfSelection theWantedState = SelectMgr_SOS_Any) const;
183
184   //! Returns true if the selectable object
185   //! aSelectableObject having the selection mode aMode
186   //! is active in this selector.
187   Standard_EXPORT Standard_Boolean IsActive (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
188                                              const Standard_Integer theMode) const;
189
190   //! Returns true if the selectable object
191   //! aSelectableObject having the selection mode aMode
192   //! is in this selector.
193   Standard_EXPORT Standard_Boolean IsInside (const Handle(SelectMgr_SelectableObject)& theSelectableObject,
194                                              const Standard_Integer theMode) const;
195
196   //! Returns the selection status Status of the selection aSelection.
197   Standard_EXPORT SelectMgr_StateOfSelection Status (const Handle(SelectMgr_Selection)& theSelection) const;
198
199   Standard_EXPORT TCollection_AsciiString Status (const Handle(SelectMgr_SelectableObject)& theSelectableObject) const;
200
201   //! Returns the list of active entity owners
202   Standard_EXPORT void ActiveOwners (NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners) const;
203
204   //! Adds new object to the map of selectable objects
205   Standard_EXPORT void AddSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject);
206
207   //! Adds new selection to the object and builds its BVH tree
208   Standard_EXPORT void AddSelectionToObject (const Handle(SelectMgr_SelectableObject)& theObject,
209                                              const Handle(SelectMgr_Selection)& theSelection);
210
211   //! Removes selectable object from map of selectable ones
212   Standard_EXPORT void RemoveSelectableObject (const Handle(SelectMgr_SelectableObject)& theObject);
213
214   //! Removes selection of the object and marks its BVH tree for rebuild
215   Standard_EXPORT void RemoveSelectionOfObject (const Handle(SelectMgr_SelectableObject)& theObject,
216                                                 const Handle(SelectMgr_Selection)& theSelection);
217
218   //! Marks BVH of selectable objects for rebuild. Parameter theIsForce set as true
219   //! guarantees that 1st level BVH for the viewer selector will be rebuilt during this call
220   Standard_EXPORT void RebuildObjectsTree (const Standard_Boolean theIsForce = Standard_False);
221
222   //! Marks BVH of sensitive entities of particular selectable object for rebuild. Parameter
223   //! theIsForce set as true guarantees that 2nd level BVH for the object given will be
224   //! rebuilt during this call
225   Standard_EXPORT void RebuildSensitivesTree (const Handle(SelectMgr_SelectableObject)& theObject,
226                                               const Standard_Boolean theIsForce = Standard_False);
227
228   //! Initializes internal iterator for stored detected sensitive entities
229   Standard_EXPORT void InitDetected();
230
231   //! Makes a step along the map of detected sensitive entities and their owners
232   Standard_EXPORT void NextDetected();
233
234   //! Returns true if iterator of map of detected sensitive entities has reached
235   //! its end
236   Standard_EXPORT Standard_Boolean MoreDetected();
237
238   //! Returns sensitive entity that was detected during the previous run of
239   //! selection algorithm
240   Standard_EXPORT const Handle(SelectBasics_SensitiveEntity)& DetectedEntity() const;
241
242   //! Returns instance of selecting volume manager of the viewer selector
243   Standard_EXPORT SelectMgr_SelectingVolumeManager& GetManager();
244
245   friend class SelectMgr_SelectionManager;
246
247   DEFINE_STANDARD_RTTI(SelectMgr_ViewerSelector, MMgt_TShared)
248
249 protected:
250
251   Standard_EXPORT SelectMgr_ViewerSelector();
252
253   //! Traverses BVH containing all added selectable objects and
254   //! finds candidates for further search of overlap
255   Standard_EXPORT void TraverseSensitives();
256
257   //! Returns True if the owner provides clipping by depth
258   //! for its sensitives. Override this method to tell the selector
259   //! to use the DepthClipping method for the owner.
260   //! Default implementation returns False for every owner.
261   //! @param theOwner [in] the onwer to check.
262   //! @return True if owner provides depth limits for sensitive clipping.
263   Standard_EXPORT virtual Standard_Boolean HasDepthClipping (const Handle(SelectMgr_EntityOwner)& theOwner) const;
264
265   //! Internal function that checks if there is possible overlap
266   //! between some entity of selectable object theObject and
267   //! current selecting volume
268   void traverseObject (const Handle(SelectMgr_SelectableObject)& theObject);
269
270   //! Internal function that checks if a particular sensitive
271   //! entity theEntity overlaps current selecting volume precisely
272   void checkOverlap (const Handle(SelectBasics_SensitiveEntity)& theEntity,
273                      const Standard_Integer theEntityIdx,
274                      SelectMgr_SelectingVolumeManager& theMgr);
275
276   //! Marks all added sensitive entities of all objects as non-selectable
277   void resetSelectionActivationStatus();
278
279   //! Checks if the entity given requires to scale current selecting frustum
280   Standard_Boolean isToScaleFrustum (const Handle(SelectBasics_SensitiveEntity)& theEntity);
281
282   //! Applies given scale and transformation matrices to the default selecting volume manager
283   SelectMgr_SelectingVolumeManager scaleAndTransform (const Standard_Real theScale,
284                                                       const gp_Trsf& theTrsf);
285
286 private:
287
288   void Activate (const Handle(SelectMgr_Selection)& theSelection);
289
290   void Deactivate (const Handle(SelectMgr_Selection)& theSelection);
291
292   //! removes a Selection from the Selector
293   void Remove (const Handle(SelectMgr_Selection)& aSelection);
294
295 protected:
296
297   Standard_Boolean preferclosest;
298   Standard_Real mytolerance;
299   Standard_Boolean myToUpdateTolerance;
300   SelectMgr_IndexedDataMapOfOwnerCriterion mystored;
301   SelectMgr_SelectingVolumeManager mySelectingVolumeMgr;
302   mutable NCollection_Handle<SelectMgr_SelectableObjectSet> mySelectableObjects;
303   SelectMgr_ToleranceMap myTolerances;
304
305 private:
306
307   Handle(TColStd_HArray1OfInteger)              myIndexes;
308   Standard_Integer                             myCurRank;
309   Standard_Boolean                             myIsLeftChildQueuedFirst;
310   Standard_Integer                             myEntityIdx;
311   SelectMgr_MapOfObjectSensitives              myMapOfObjectSensitives;
312   SelectMgr_MapOfOwnerDetectedEntities         myMapOfDetected;
313   SelectMgr_MapOfOwnerDetectedEntitiesIterator myDetectedIter;
314 };
315
316 DEFINE_STANDARD_HANDLE(SelectMgr_ViewerSelector, MMgt_TShared)
317
318 #include <SelectMgr_ViewerSelector.lxx>
319
320 #endif