1 // Created on: 2014-05-22
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_SelectingVolumeManager_HeaderFile
17 #define _SelectMgr_SelectingVolumeManager_HeaderFile
18 
19 #include <SelectBasics_SelectingVolumeManager.hxx>
20 
21 #include <SelectMgr_BaseIntersector.hxx>
22 #include <SelectMgr_VectorTypes.hxx>
23 #include <SelectMgr_ViewClipRange.hxx>
24 
25 //! This class is used to switch between active selecting volumes depending
26 //! on selection type chosen by the user.
27 //! The sample of correct selection volume initialization procedure:
28 //! @code
29 //!   aMgr.InitPointSelectingVolume (aMousePos);
30 //!   aMgr.SetPixelTolerance (aTolerance);
31 //!   aMgr.SetCamera (aCamera);
32 //!   aMgr.SetWindowSize (aWidth, aHeight);
33 //!   aMgr.BuildSelectingVolume();
34 //! @endcode
35 class SelectMgr_SelectingVolumeManager : public SelectBasics_SelectingVolumeManager
36 {
37 public:
38 
39   //! Creates instances of all available selecting volume types
40   Standard_EXPORT SelectMgr_SelectingVolumeManager();
41 
~SelectMgr_SelectingVolumeManager()42   virtual ~SelectMgr_SelectingVolumeManager() {}
43 
44   //! Creates, initializes and activates rectangular selecting frustum for point selection
45   Standard_EXPORT void InitPointSelectingVolume (const gp_Pnt2d& thePoint);
46 
47   //! Creates, initializes and activates rectangular selecting frustum for box selection
48   Standard_EXPORT void InitBoxSelectingVolume (const gp_Pnt2d& theMinPt,
49                                                const gp_Pnt2d& theMaxPt);
50 
51   //! Creates, initializes and activates set of triangular selecting frustums for polyline selection
52   Standard_EXPORT void InitPolylineSelectingVolume (const TColgp_Array1OfPnt2d& thePoints);
53 
54   //! Creates and activates axis selector for point selection
55   Standard_EXPORT void InitAxisSelectingVolume (const gp_Ax1& theAxis);
56 
57   //! Sets as active the custom selecting volume
58   Standard_EXPORT void InitSelectingVolume (const Handle(SelectMgr_BaseIntersector)& theVolume);
59 
60   //! Builds previously initialized selecting volume.
61   Standard_EXPORT void BuildSelectingVolume();
62 
63   //! Returns active selecting volume that was built during last
64   //! run of OCCT selection mechanism
Handle(SelectMgr_BaseIntersector)65   const Handle(SelectMgr_BaseIntersector)& ActiveVolume() const { return myActiveSelectingVolume; }
66 
67   // Returns active selection type (point, box, polyline)
68   Standard_EXPORT virtual Standard_Integer GetActiveSelectionType() const Standard_OVERRIDE;
69 
70   //! IMPORTANT: Scaling makes sense only for frustum built on a single point!
71   //!            Note that this method does not perform any checks on type of the frustum.
72   //!
73   //! Returns a copy of the frustum resized according to the scale factor given
74   //! and transforms it using the matrix given.
75   //! There are no default parameters, but in case if:
76   //!    - transformation only is needed: @theScaleFactor must be initialized as any negative value;
77   //!    - scale only is needed: @theTrsf must be set to gp_Identity.
78   //! Builder is an optional argument that represents corresponding settings for re-constructing transformed
79   //! frustum from scratch. Can be null if reconstruction is not expected furthermore.
80   Standard_EXPORT virtual SelectMgr_SelectingVolumeManager ScaleAndTransform (const Standard_Integer theScaleFactor,
81                                                                               const gp_GTrsf& theTrsf,
82                                                                               const Handle(SelectMgr_FrustumBuilder)& theBuilder) const;
83 
84 public:
85 
86   //! Returns current camera definition.
87   Standard_EXPORT const Handle(Graphic3d_Camera)& Camera() const;
88 
89   //! Updates camera projection and orientation matrices in all selecting volumes
90   //! Note: this method should be called after selection volume building
91   //! else exception will be thrown
92   Standard_EXPORT void SetCamera (const Handle(Graphic3d_Camera) theCamera);
93 
94   //! Updates viewport in all selecting volumes
95   //! Note: this method should be called after selection volume building
96   //! else exception will be thrown
97   Standard_EXPORT void SetViewport (const Standard_Real theX,
98                                     const Standard_Real theY,
99                                     const Standard_Real theWidth,
100                                     const Standard_Real theHeight);
101 
102   //! Updates pixel tolerance in all selecting volumes
103   //! Note: this method should be called after selection volume building
104   //! else exception will be thrown
105   Standard_EXPORT void SetPixelTolerance (const Standard_Integer theTolerance);
106 
107   //! Returns window size
108   Standard_EXPORT void WindowSize (Standard_Integer& theWidth, Standard_Integer& theHeight) const;
109 
110   //! Updates window size in all selecting volumes
111   //! Note: this method should be called after selection volume building
112   //! else exception will be thrown
113   Standard_EXPORT void SetWindowSize (const Standard_Integer theWidth, const Standard_Integer theHeight);
114 
115 
116   //! SAT intersection test between defined volume and given axis-aligned box
117   Standard_EXPORT virtual Standard_Boolean OverlapsBox (const SelectMgr_Vec3& theBoxMin,
118                                                         const SelectMgr_Vec3& theBoxMax,
119                                                         SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
120 
121   //! Returns true if selecting volume is overlapped by axis-aligned bounding box
122   //! with minimum corner at point theMinPt and maximum at point theMaxPt
123   Standard_EXPORT  virtual Standard_Boolean OverlapsBox (const SelectMgr_Vec3& theBoxMin,
124                                                          const SelectMgr_Vec3& theBoxMax,
125                                                          Standard_Boolean*     theInside = NULL) const Standard_OVERRIDE;
126 
127   //! Intersection test between defined volume and given point
128   Standard_EXPORT virtual Standard_Boolean OverlapsPoint (const gp_Pnt& thePnt,
129                                                           SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
130 
131   //! Intersection test between defined volume and given point
132   Standard_EXPORT virtual Standard_Boolean OverlapsPoint (const gp_Pnt& thePnt) const Standard_OVERRIDE;
133 
134   //! SAT intersection test between defined volume and given ordered set of points,
135   //! representing line segments. The test may be considered of interior part or
136   //! boundary line defined by segments depending on given sensitivity type
137   Standard_EXPORT virtual Standard_Boolean OverlapsPolygon (const TColgp_Array1OfPnt& theArrayOfPts,
138                                                             Standard_Integer theSensType,
139                                                             SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
140 
141   //! Checks if line segment overlaps selecting frustum
142   Standard_EXPORT virtual Standard_Boolean OverlapsSegment (const gp_Pnt& thePnt1,
143                                                             const gp_Pnt& thePnt2,
144                                                             SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
145 
146   //! SAT intersection test between defined volume and given triangle. The test may
147   //! be considered of interior part or boundary line defined by triangle vertices
148   //! depending on given sensitivity type
149   Standard_EXPORT  virtual Standard_Boolean OverlapsTriangle (const gp_Pnt& thePnt1,
150                                                               const gp_Pnt& thePnt2,
151                                                               const gp_Pnt& thePnt3,
152                                                               Standard_Integer theSensType,
153                                                               SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
154 
155   //! Intersection test between defined volume and given sphere
156   Standard_EXPORT virtual Standard_Boolean OverlapsSphere (const gp_Pnt& theCenter,
157                                                            const Standard_Real theRadius,
158                                                            SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
159 
160   //! Intersection test between defined volume and given sphere
161   Standard_EXPORT virtual Standard_Boolean OverlapsSphere (const gp_Pnt& theCenter,
162                                                            const Standard_Real theRadius,
163                                                            Standard_Boolean* theInside = NULL) const Standard_OVERRIDE;
164 
165   //! Returns true if selecting volume is overlapped by cylinder (or cone) with radiuses theBottomRad
166   //! and theTopRad, height theHeight and transformation to apply theTrsf.
167   Standard_EXPORT virtual Standard_Boolean OverlapsCylinder (const Standard_Real theBottomRad,
168                                                              const Standard_Real theTopRad,
169                                                              const Standard_Real theHeight,
170                                                              const gp_Trsf& theTrsf,
171                                                              SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
172 
173   //! Returns true if selecting volume is overlapped by cylinder (or cone) with radiuses theBottomRad
174   //! and theTopRad, height theHeight and transformation to apply theTrsf.
175   Standard_EXPORT virtual Standard_Boolean OverlapsCylinder (const Standard_Real theBottomRad,
176                                                              const Standard_Real theTopRad,
177                                                              const Standard_Real theHeight,
178                                                              const gp_Trsf& theTrsf,
179                                                              Standard_Boolean* theInside = NULL) const Standard_OVERRIDE;
180 
181   //! Measures distance between 3d projection of user-picked
182   //! screen point and given point theCOG
183   Standard_EXPORT virtual Standard_Real DistToGeometryCenter (const gp_Pnt& theCOG) const Standard_OVERRIDE;
184 
185   //! Calculates the point on a view ray that was detected during the run of selection algo by given depth.
186   //! Throws exception if active selection type is not Point.
187   Standard_EXPORT virtual gp_Pnt DetectedPoint (const Standard_Real theDepth) const Standard_OVERRIDE;
188 
189   //! If theIsToAllow is false, only fully included sensitives will be detected, otherwise the algorithm will
190   //! mark both included and overlapped entities as matched
191   Standard_EXPORT virtual void AllowOverlapDetection (const Standard_Boolean theIsToAllow);
192 
193   Standard_EXPORT virtual Standard_Boolean IsOverlapAllowed() const Standard_OVERRIDE;
194 
195   //! Return view clipping planes.
Handle(Graphic3d_SequenceOfHClipPlane)196   const Handle(Graphic3d_SequenceOfHClipPlane)& ViewClipping() const { return myViewClipPlanes; }
197 
198   //! Return object clipping planes.
Handle(Graphic3d_SequenceOfHClipPlane)199   const Handle(Graphic3d_SequenceOfHClipPlane)& ObjectClipping() const { return myObjectClipPlanes; }
200 
201   //! Valid for point selection only!
202   //! Computes depth range for clipping planes.
203   //! @param theViewPlanes  [in] global view planes
204   //! @param theObjPlanes   [in] object planes
205   //! @param theWorldSelMgr [in] selection volume in world space for computing clipping plane ranges
206   Standard_EXPORT void SetViewClipping (const Handle(Graphic3d_SequenceOfHClipPlane)& theViewPlanes,
207                                         const Handle(Graphic3d_SequenceOfHClipPlane)& theObjPlanes,
208                                         const SelectMgr_SelectingVolumeManager* theWorldSelMgr);
209 
210   //! Copy clipping planes from another volume manager.
211   Standard_EXPORT void SetViewClipping (const SelectMgr_SelectingVolumeManager& theOther);
212 
213   //! Return clipping range.
ViewClipRanges() const214   const SelectMgr_ViewClipRange& ViewClipRanges() const { return myViewClipRange; }
215 
216   //! Set clipping range.
SetViewClipRanges(const SelectMgr_ViewClipRange & theRange)217   void SetViewClipRanges (const SelectMgr_ViewClipRange& theRange) { myViewClipRange = theRange; }
218 
219   //! A set of helper functions that return rectangular selecting frustum data
220   Standard_EXPORT const gp_Pnt* GetVertices() const;
221 
222   //! Valid only for point and rectangular selection.
223   //! Returns projection of 2d mouse picked point or projection
224   //! of center of 2d rectangle (for point and rectangular selection
225   //! correspondingly) onto near view frustum plane
226   Standard_EXPORT virtual gp_Pnt GetNearPickedPnt() const Standard_OVERRIDE;
227 
228   //! Valid only for point and rectangular selection.
229   //! Returns projection of 2d mouse picked point or projection
230   //! of center of 2d rectangle (for point and rectangular selection
231   //! correspondingly) onto far view frustum plane
232   Standard_EXPORT virtual gp_Pnt GetFarPickedPnt() const Standard_OVERRIDE;
233 
234   //! Valid only for point and rectangular selection.
235   //! Returns view ray direction
236   Standard_EXPORT virtual gp_Dir GetViewRayDirection() const Standard_OVERRIDE;
237 
238   //! Checks if it is possible to scale current active selecting volume
239   Standard_EXPORT virtual Standard_Boolean IsScalableActiveVolume() const Standard_OVERRIDE;
240 
241   //! Returns mouse coordinates for Point selection mode.
242   //! @return infinite point in case of unsupport of mouse position for this active selection volume.
243   Standard_EXPORT virtual gp_Pnt2d GetMousePosition() const Standard_OVERRIDE;
244 
245   //! Stores plane equation coefficients (in the following form:
246   //! Ax + By + Cz + D = 0) to the given vector
247   Standard_EXPORT virtual void GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const Standard_OVERRIDE;
248 
249   //! Dumps the content of me into the stream
250   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
251 
252 public:
253 
254   Standard_DEPRECATED("Deprecated method - InitPointSelectingVolume() and Build() methods should be used instead")
255   Standard_EXPORT void BuildSelectingVolume (const gp_Pnt2d& thePoint);
256 
257   Standard_DEPRECATED("Deprecated method - InitBoxSelectingVolume() and Build() should be used instead")
258   Standard_EXPORT void BuildSelectingVolume (const gp_Pnt2d& theMinPt,
259                                              const gp_Pnt2d& theMaxPt);
260 
261   Standard_DEPRECATED("Deprecated method - InitPolylineSelectingVolume() and Build() should be used instead")
262   Standard_EXPORT void BuildSelectingVolume (const TColgp_Array1OfPnt2d& thePoints);
263 
264 private:
265   Handle(SelectMgr_BaseIntersector)      myActiveSelectingVolume;
266   Handle(Graphic3d_SequenceOfHClipPlane) myViewClipPlanes;                  //!< view clipping planes
267   Handle(Graphic3d_SequenceOfHClipPlane) myObjectClipPlanes;                //!< object clipping planes
268   SelectMgr_ViewClipRange                myViewClipRange;
269   Standard_Boolean                       myToAllowOverlap;                  //!< Defines if partially overlapped entities will me detected or not
270 };
271 
272 #endif
273