95bb8f6c6506fecb8d6d1e5b0440c48da47deada
[occt.git] / src / SelectMgr / SelectMgr_SelectingVolumeManager.hxx
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 <NCollection_Handle.hxx>
20
21 #include <Graphic3d_Camera.hxx>
22 #include <Graphic3d_SequenceOfHClipPlane.hxx>
23 #include <Graphic3d_WorldViewProjState.hxx>
24
25 #include <SelectMgr_BaseFrustum.hxx>
26 #include <SelectMgr_RectangularFrustum.hxx>
27 #include <SelectMgr_TriangularFrustumSet.hxx>
28 #include <SelectBasics_SelectingVolumeManager.hxx>
29
30 //! This class is used to switch between active selecting volumes depending
31 //! on selection type chosen by the user
32 class SelectMgr_SelectingVolumeManager : public SelectBasics_SelectingVolumeManager
33 {
34 public:
35
36   //! Creates instances of all available selecting volume types
37   Standard_EXPORT SelectMgr_SelectingVolumeManager (Standard_Boolean theToAllocateFrustums = Standard_True);
38
39   virtual ~SelectMgr_SelectingVolumeManager() {};
40
41   //! IMPORTANT: Scaling makes sense only for frustum built on a single point!
42   //!            Note that this method does not perform any checks on type of the frustum.
43   //!
44   //! Returns a copy of the frustum resized according to the scale factor given
45   //! and transforms it using the matrix given.
46   //! There are no default parameters, but in case if:
47   //!    - transformation only is needed: @theScaleFactor must be initialized as any negative value;
48   //!    - scale only is needed: @theTrsf must be set to gp_Identity.
49   //! Builder is an optional argument that represents corresponding settings for re-constructing transformed
50   //! frustum from scratch. Can be null if reconstruction is not expected furthermore.
51   Standard_EXPORT virtual SelectMgr_SelectingVolumeManager ScaleAndTransform (const Standard_Integer theScaleFactor,
52                                                                               const gp_GTrsf& theTrsf,
53                                                                               const Handle(SelectMgr_FrustumBuilder)& theBuilder = NULL) const;
54
55   Standard_EXPORT virtual Standard_Integer GetActiveSelectionType() const Standard_OVERRIDE;
56
57   Standard_EXPORT void SetActiveSelectionType (const SelectionType& theType);
58
59   //! Returns current camera definition.
60   const Handle(Graphic3d_Camera)& Camera() const { return mySelectingVolumes[Frustum]->Camera(); }
61
62   //! Updates camera projection and orientation matrices in all selecting volumes
63   Standard_EXPORT void SetCamera (const Handle(Graphic3d_Camera) theCamera);
64
65   //! Updates camera projection and orientation matrices in all selecting volumes
66   Standard_EXPORT void SetCamera (const Graphic3d_Mat4d& theProjection,
67                                   const Graphic3d_Mat4d& theWorldView,
68                                   const Standard_Boolean theIsOrthographic,
69                                   const Graphic3d_WorldViewProjState& theWVPState = Graphic3d_WorldViewProjState());
70
71   //! @return current projection transformation common for all selecting volumes
72   Standard_EXPORT const Graphic3d_Mat4d& ProjectionMatrix() const;
73
74   //! @return current world view transformation common for all selecting volumes
75   Standard_EXPORT const Graphic3d_Mat4d& WorldViewMatrix() const;
76
77   Standard_EXPORT void WindowSize (Standard_Integer& theWidth, Standard_Integer& theHeight) const;
78
79   //! @return current camera world view projection transformation state common for all selecting volumes
80   Standard_EXPORT const Graphic3d_WorldViewProjState& WorldViewProjState() const;
81
82   //! Updates viewport in all selecting volumes
83   Standard_EXPORT void SetViewport (const Standard_Real theX,
84                                     const Standard_Real theY,
85                                     const Standard_Real theWidth,
86                                     const Standard_Real theHeight);
87
88   //! Updates pixel tolerance in all selecting volumes
89   Standard_EXPORT void SetPixelTolerance (const Standard_Integer theTolerance);
90
91   //! Updates window size in all selecting volumes
92   Standard_EXPORT void SetWindowSize (const Standard_Integer theWidth, const Standard_Integer theHeight);
93
94
95   //! Builds rectangular selecting frustum for point selection
96   Standard_EXPORT void BuildSelectingVolume (const gp_Pnt2d& thePoint);
97
98   //! Builds rectangular selecting frustum for box selection
99   Standard_EXPORT void BuildSelectingVolume (const gp_Pnt2d& theMinPt,
100                                              const gp_Pnt2d& theMaxPt);
101
102   //! Builds set of triangular selecting frustums for polyline selection
103   Standard_EXPORT void BuildSelectingVolume (const TColgp_Array1OfPnt2d& thePoints);
104
105
106   //! SAT intersection test between defined volume and given axis-aligned box
107   Standard_EXPORT virtual Standard_Boolean Overlaps (const SelectMgr_Vec3& theBoxMin,
108                                                       const SelectMgr_Vec3& theBoxMax,
109                                                      Standard_Real& theDepth) Standard_OVERRIDE;
110
111   //! Returns true if selecting volume is overlapped by axis-aligned bounding box
112   //! with minimum corner at point theMinPt and maximum at point theMaxPt
113   Standard_EXPORT  virtual Standard_Boolean Overlaps (const SelectMgr_Vec3& theBoxMin,
114                                                       const SelectMgr_Vec3& theBoxMax,
115                                                       Standard_Boolean*     theInside = NULL) Standard_OVERRIDE;
116
117   //! Intersection test between defined volume and given point
118   Standard_EXPORT virtual Standard_Boolean Overlaps (const gp_Pnt& thePnt,
119                                                      Standard_Real& theDepth) Standard_OVERRIDE;
120
121   //! Intersection test between defined volume and given point
122   Standard_EXPORT virtual Standard_Boolean Overlaps (const gp_Pnt& thePnt) Standard_OVERRIDE;
123
124   //! SAT intersection test between defined volume and given ordered set of points,
125   //! representing line segments. The test may be considered of interior part or
126   //! boundary line defined by segments depending on given sensitivity type
127   Standard_EXPORT virtual Standard_Boolean Overlaps (const Handle(TColgp_HArray1OfPnt)& theArrayOfPts,
128                                                      Standard_Integer theSensType,
129                                                      Standard_Real& theDepth) Standard_OVERRIDE;
130
131   //! SAT intersection test between defined volume and given ordered set of points,
132   //! representing line segments. The test may be considered of interior part or
133   //! boundary line defined by segments depending on given sensitivity type
134   Standard_EXPORT virtual Standard_Boolean Overlaps (const TColgp_Array1OfPnt& theArrayOfPts,
135                                                      Standard_Integer theSensType,
136                                                      Standard_Real& theDepth) Standard_OVERRIDE;
137
138   //! Checks if line segment overlaps selecting frustum
139   Standard_EXPORT virtual Standard_Boolean Overlaps (const gp_Pnt& thePnt1,
140                                                      const gp_Pnt& thePnt2,
141                                                      Standard_Real& theDepth) Standard_OVERRIDE;
142
143   //! SAT intersection test between defined volume and given triangle. The test may
144   //! be considered of interior part or boundary line defined by triangle vertices
145   //! depending on given sensitivity type
146   Standard_EXPORT  virtual Standard_Boolean Overlaps (const gp_Pnt& thePnt1,
147                                                       const gp_Pnt& thePnt2,
148                                                       const gp_Pnt& thePnt3,
149                                                       Standard_Integer theSensType,
150                                                       Standard_Real& theDepth) Standard_OVERRIDE;
151
152
153   //! Measures distance between 3d projection of user-picked
154   //! screen point and given point theCOG
155   Standard_EXPORT virtual Standard_Real DistToGeometryCenter (const gp_Pnt& theCOG) Standard_OVERRIDE;
156
157   //! Calculates the point on a view ray that was detected during the run of selection algo by given depth.
158   //! Throws exception if active selection type is not Point.
159   Standard_EXPORT virtual gp_Pnt DetectedPoint (const Standard_Real theDepth) const Standard_OVERRIDE;
160
161   //! Checks if the point of sensitive in which selection was detected belongs
162   //! to the region defined by clipping planes
163   Standard_EXPORT virtual Standard_Boolean IsClipped (const Graphic3d_SequenceOfHClipPlane& thePlanes,
164                                                       const Standard_Real& theDepth);
165
166   //! Is used for rectangular selection only
167   //! If theIsToAllow is false, only fully included sensitives will be detected, otherwise the algorithm will
168   //! mark both included and overlapped entities as matched
169   Standard_EXPORT virtual void AllowOverlapDetection (const Standard_Boolean theIsToAllow);
170
171   Standard_EXPORT virtual Standard_Boolean IsOverlapAllowed() const Standard_OVERRIDE;
172
173   //! Return view clipping planes.
174   const Handle(Graphic3d_SequenceOfHClipPlane)& ViewClipping() const { return myViewClipPlanes; }
175
176   //! Valid for point selection only!
177   //! Computes depth range for global (defined for the whole view) clipping planes.
178   Standard_EXPORT void SetViewClipping (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes);
179
180   //! Set if view clipping plane is enabled or not.
181   //! @return previous flag value
182   Standard_EXPORT Standard_Boolean SetViewClippingEnabled (const Standard_Boolean theToEnable);
183
184   //! A set of helper functions that return rectangular selecting frustum data
185   Standard_EXPORT const gp_Pnt* GetVertices() const;
186
187   //! Valid only for point and rectangular selection.
188   //! Returns projection of 2d mouse picked point or projection
189   //! of center of 2d rectangle (for point and rectangular selection
190   //! correspondingly) onto near view frustum plane
191   Standard_EXPORT virtual gp_Pnt GetNearPickedPnt() const Standard_OVERRIDE;
192
193   //! Valid only for point and rectangular selection.
194   //! Returns projection of 2d mouse picked point or projection
195   //! of center of 2d rectangle (for point and rectangular selection
196   //! correspondingly) onto far view frustum plane
197   Standard_EXPORT virtual gp_Pnt GetFarPickedPnt() const Standard_OVERRIDE;
198
199   //! Returns active selecting volume that was built during last
200   //! run of OCCT selection mechanism
201   Handle(SelectMgr_BaseFrustum) ActiveVolume() const
202   {
203     if (myActiveSelectionType == Unknown)
204       return Handle(SelectMgr_BaseFrustum)();
205
206     return mySelectingVolumes[myActiveSelectionType / 2];
207   }
208
209   //! Stores plane equation coefficients (in the following form:
210   //! Ax + By + Cz + D = 0) to the given vector
211   virtual void GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const Standard_OVERRIDE
212   {
213     if (myActiveSelectionType == Unknown)
214     {
215       thePlaneEquations.Clear();
216       return;
217     }
218
219     return mySelectingVolumes[myActiveSelectionType / 2]->GetPlanes (thePlaneEquations);
220   }
221
222 private:
223   enum { Frustum, FrustumSet, VolumeTypesNb };       //!< Defines the amount of available selecting volumes
224
225   Handle(SelectMgr_BaseFrustum)          mySelectingVolumes[VolumeTypesNb]; //!< Array of selecting volumes
226   Handle(Graphic3d_SequenceOfHClipPlane) myViewClipPlanes;                  //!< view clipping planes
227   Standard_Boolean                       myToAllowOverlap;                  //!< Defines if partially overlapped entities will me detected or not
228 };
229
230 #endif