1c8bda348568f7e610169f19a651c8de462314c0
[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                                                      SelectBasics_PickResult& thePickResult) const 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) const Standard_OVERRIDE;
116
117   //! Intersection test between defined volume and given point
118   Standard_EXPORT virtual Standard_Boolean Overlaps (const gp_Pnt& thePnt,
119                                                      SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
120
121   //! Intersection test between defined volume and given point
122   Standard_EXPORT virtual Standard_Boolean Overlaps (const gp_Pnt& thePnt) const 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                                                      SelectBasics_PickResult& thePickResult) const 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                                                      SelectBasics_PickResult& thePickResult) const 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                                                      SelectBasics_PickResult& thePickResult) const 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                                                       SelectBasics_PickResult& thePickResult) const 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) const 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   //! Is used for rectangular selection only
162   //! If theIsToAllow is false, only fully included sensitives will be detected, otherwise the algorithm will
163   //! mark both included and overlapped entities as matched
164   Standard_EXPORT virtual void AllowOverlapDetection (const Standard_Boolean theIsToAllow);
165
166   Standard_EXPORT virtual Standard_Boolean IsOverlapAllowed() const Standard_OVERRIDE;
167
168   //! Return view clipping planes.
169   const Handle(Graphic3d_SequenceOfHClipPlane)& ViewClipping() const { return myViewClipPlanes; }
170
171   //! Return object clipping planes.
172   const Handle(Graphic3d_SequenceOfHClipPlane)& ObjectClipping() const { return myObjectClipPlanes; }
173
174   //! Valid for point selection only!
175   //! Computes depth range for clipping planes.
176   //! @param theViewPlanes global view planes
177   //! @param theObjPlanes  object planes
178   Standard_EXPORT void SetViewClipping (const Handle(Graphic3d_SequenceOfHClipPlane)& theViewPlanes,
179                                         const Handle(Graphic3d_SequenceOfHClipPlane)& theObjPlanes);
180
181   //! Copy clipping planes from another volume manager.
182   Standard_EXPORT void SetViewClipping (const SelectMgr_SelectingVolumeManager& theOther);
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   //! Return mouse coordinates for Point selection mode.
200   virtual gp_Pnt2d GetMousePosition() const Standard_OVERRIDE
201   {
202     if (myActiveSelectionType != Point)
203     {
204       return gp_Pnt2d (RealLast(), RealLast());
205     }
206     const SelectMgr_RectangularFrustum* aFr = reinterpret_cast<const SelectMgr_RectangularFrustum*> (mySelectingVolumes[myActiveSelectionType / 2].get());
207     return aFr->GetMousePosition();
208   }
209
210   //! Returns active selecting volume that was built during last
211   //! run of OCCT selection mechanism
212   Handle(SelectMgr_BaseFrustum) ActiveVolume() const
213   {
214     if (myActiveSelectionType == Unknown)
215       return Handle(SelectMgr_BaseFrustum)();
216
217     return mySelectingVolumes[myActiveSelectionType / 2];
218   }
219
220   //! Stores plane equation coefficients (in the following form:
221   //! Ax + By + Cz + D = 0) to the given vector
222   virtual void GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const Standard_OVERRIDE
223   {
224     if (myActiveSelectionType == Unknown)
225     {
226       thePlaneEquations.Clear();
227       return;
228     }
229
230     return mySelectingVolumes[myActiveSelectionType / 2]->GetPlanes (thePlaneEquations);
231   }
232
233 private:
234   enum { Frustum, FrustumSet, VolumeTypesNb };       //!< Defines the amount of available selecting volumes
235
236   Handle(SelectMgr_BaseFrustum)          mySelectingVolumes[VolumeTypesNb]; //!< Array of selecting volumes
237   Handle(Graphic3d_SequenceOfHClipPlane) myViewClipPlanes;                  //!< view clipping planes
238   Handle(Graphic3d_SequenceOfHClipPlane) myObjectClipPlanes;                //!< object clipping planes
239   Standard_Boolean                       myToAllowOverlap;                  //!< Defines if partially overlapped entities will me detected or not
240 };
241
242 #endif