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