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