1 // Copyright (c) 2021 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13 
14 #ifndef _SelectMgr_BaseIntersector_HeaderFile
15 #define _SelectMgr_BaseIntersector_HeaderFile
16 
17 #include <gp_GTrsf.hxx>
18 #include <Graphic3d_Mat4d.hxx>
19 #include <Graphic3d_WorldViewProjState.hxx>
20 #include <NCollection_Vector.hxx>
21 #include <Select3D_TypeOfSensitivity.hxx>
22 #include <SelectBasics_PickResult.hxx>
23 #include <SelectMgr_SelectionType.hxx>
24 #include <SelectMgr_VectorTypes.hxx>
25 #include <TColgp_Array1OfPnt2d.hxx>
26 #include <TColgp_Array1OfPnt.hxx>
27 
28 class Graphic3d_Camera;
29 class SelectMgr_FrustumBuilder;
30 class SelectMgr_ViewClipRange;
31 
32 //! This class is an interface for different types of selecting intersector,
33 //! defining different selection types, like point, box or polyline
34 //! selection. It contains signatures of functions for detection of
35 //! overlap by sensitive entity and initializes some data for building
36 //! the selecting intersector
37 class SelectMgr_BaseIntersector : public Standard_Transient
38 {
39 public:
40 
41   //! Creates new empty selecting volume
42   Standard_EXPORT SelectMgr_BaseIntersector();
43 
44   //! Destructor
45   Standard_EXPORT virtual ~SelectMgr_BaseIntersector();
46 
47   //! Builds intersector according to internal parameters
48   virtual void Build() = 0;
49 
50   //! Returns selection type of this intersector
GetSelectionType() const51   SelectMgr_SelectionType GetSelectionType() const { return mySelectionType; }
52 
53 public:
54 
55   //! Checks if it is possible to scale this intersector.
56   virtual Standard_Boolean IsScalable() const = 0;
57 
58   //! Sets pixel tolerance.
59   //! It makes sense only for scalable intersectors (built on a single point).
60   //! This method does nothing for the base class.
61   Standard_EXPORT virtual void SetPixelTolerance (const Standard_Integer theTol);
62 
63   //! Note that this method does not perform any checks on type of the frustum.
64   //! @param theScaleFactor [in] scale factor for new intersector or negative value if undefined;
65   //!                            IMPORTANT: scaling makes sense only for scalable ::IsScalable() intersectors (built on a single point)!
66   //! @param theTrsf [in] transformation for new intersector or gp_Identity if undefined
67   //! @param theBuilder [in] an optional argument that represents corresponding settings for re-constructing transformed frustum from scratch;
68   //!                        could be NULL if reconstruction is not expected furthermore
69   //! @return a copy of the frustum resized according to the scale factor given and transforms it using the matrix given
70   virtual Handle(SelectMgr_BaseIntersector) ScaleAndTransform (const Standard_Integer theScaleFactor,
71                                                                const gp_GTrsf& theTrsf,
72                                                                const Handle(SelectMgr_FrustumBuilder)& theBuilder) const = 0;
73 
74 public:
75 
76   //! Return camera definition.
Handle(Graphic3d_Camera)77   const Handle(Graphic3d_Camera)& Camera() const { return myCamera; }
78 
79   //! Saves camera definition.
80   Standard_EXPORT virtual void SetCamera (const Handle(Graphic3d_Camera)& theCamera);
81 
82   //! Returns current window size.
83   //! This method doesn't set any output values for the base class.
84   Standard_EXPORT virtual void WindowSize (Standard_Integer& theWidth,
85                                            Standard_Integer& theHeight) const;
86 
87   //! Sets current window size.
88   //! This method does nothing for the base class.
89   Standard_EXPORT virtual void SetWindowSize (const Standard_Integer theWidth,
90                                               const Standard_Integer theHeight);
91 
92   //! Sets viewport parameters.
93   //! This method does nothing for the base class.
94   Standard_EXPORT virtual void SetViewport (const Standard_Real theX,
95                                             const Standard_Real theY,
96                                             const Standard_Real theWidth,
97                                             const Standard_Real theHeight);
98 
99   //! Returns near point of intersector.
100   //! This method returns zero point for the base class.
101   Standard_EXPORT virtual const gp_Pnt& GetNearPnt() const;
102 
103   //! Returns far point of intersector.
104   //! This method returns zero point for the base class.
105   Standard_EXPORT virtual const gp_Pnt& GetFarPnt() const;
106 
107   //! Returns direction ray of intersector.
108   //! This method returns zero direction for the base class.
109   Standard_EXPORT virtual const gp_Dir& GetViewRayDirection() const;
110 
111   //! Returns current mouse coordinates.
112   //! This method returns infinite point for the base class.
113   Standard_EXPORT virtual const gp_Pnt2d& GetMousePosition() const;
114 
115   //! Stores plane equation coefficients (in the following form:
116   //! Ax + By + Cz + D = 0) to the given vector.
117   //! This method only clears input vector for the base class.
GetPlanes(NCollection_Vector<SelectMgr_Vec4> & thePlaneEquations) const118   virtual void GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const
119   {
120     thePlaneEquations.Clear();
121   }
122 
123 public:
124 
125   //! SAT intersection test between defined volume and given axis-aligned box
126   virtual Standard_Boolean OverlapsBox (const SelectMgr_Vec3& theBoxMin,
127                                         const SelectMgr_Vec3& theBoxMax,
128                                         const SelectMgr_ViewClipRange& theClipRange,
129                                         SelectBasics_PickResult& thePickResult) const = 0;
130 
131   //! Returns true if selecting volume is overlapped by axis-aligned bounding box
132   //! with minimum corner at point theMinPt and maximum at point theMaxPt
133   virtual Standard_Boolean OverlapsBox (const SelectMgr_Vec3& theBoxMin,
134                                         const SelectMgr_Vec3& theBoxMax,
135                                         Standard_Boolean*     theInside = NULL) const = 0;
136 
137   //! Intersection test between defined volume and given point
138   virtual Standard_Boolean OverlapsPoint (const gp_Pnt& thePnt,
139                                           const SelectMgr_ViewClipRange& theClipRange,
140                                           SelectBasics_PickResult& thePickResult) const = 0;
141 
142   //! Intersection test between defined volume and given point
143   //! Does not perform depth calculation, so this method is defined as helper function for inclusion test.
144   //! Therefore, its implementation makes sense only for rectangular frustum with box selection mode activated.
145   virtual Standard_Boolean OverlapsPoint (const gp_Pnt& thePnt) const = 0;
146 
147   //! SAT intersection test between defined volume and given ordered set of points,
148   //! representing line segments. The test may be considered of interior part or
149   //! boundary line defined by segments depending on given sensitivity type
150   virtual Standard_Boolean OverlapsPolygon (const TColgp_Array1OfPnt& theArrayOfPnts,
151                                             Select3D_TypeOfSensitivity theSensType,
152                                             const SelectMgr_ViewClipRange& theClipRange,
153                                             SelectBasics_PickResult& thePickResult) const = 0;
154 
155   //! Checks if line segment overlaps selecting frustum
156   virtual Standard_Boolean OverlapsSegment (const gp_Pnt& thePnt1,
157                                             const gp_Pnt& thePnt2,
158                                             const SelectMgr_ViewClipRange& theClipRange,
159                                             SelectBasics_PickResult& thePickResult) const = 0;
160 
161   //! SAT intersection test between defined volume and given triangle. The test may
162   //! be considered of interior part or boundary line defined by triangle vertices
163   //! depending on given sensitivity type
164   virtual Standard_Boolean OverlapsTriangle (const gp_Pnt& thePnt1,
165                                              const gp_Pnt& thePnt2,
166                                              const gp_Pnt& thePnt3,
167                                              Select3D_TypeOfSensitivity theSensType,
168                                              const SelectMgr_ViewClipRange& theClipRange,
169                                              SelectBasics_PickResult& thePickResult) const = 0;
170 
171   //! Returns true if selecting volume is overlapped by sphere with center theCenter
172   //! and radius theRadius
173   Standard_EXPORT virtual Standard_Boolean OverlapsSphere (const gp_Pnt& theCenter,
174                                                            const Standard_Real theRadius,
175                                                            Standard_Boolean* theInside = NULL) const = 0;
176 
177   //! Returns true if selecting volume is overlapped by sphere with center theCenter
178   //! and radius theRadius
179   Standard_EXPORT virtual Standard_Boolean OverlapsSphere (const gp_Pnt& theCenter,
180                                                            const Standard_Real theRadius,
181                                                            const SelectMgr_ViewClipRange& theClipRange,
182                                                            SelectBasics_PickResult& thePickResult) const = 0;
183 
184   //! Returns true if selecting volume is overlapped by cylinder (or cone) with radiuses theBottomRad
185   //! and theTopRad, height theHeight and transformation to apply theTrsf.
186   virtual Standard_Boolean OverlapsCylinder (const Standard_Real theBottomRad,
187                                              const Standard_Real theTopRad,
188                                              const Standard_Real theHeight,
189                                              const gp_Trsf& theTrsf,
190                                              const SelectMgr_ViewClipRange& theClipRange,
191                                              SelectBasics_PickResult& thePickResult) const = 0;
192 
193   //! Returns true if selecting volume is overlapped by cylinder (or cone) with radiuses theBottomRad
194   //! and theTopRad, height theHeight and transformation to apply theTrsf.
195   virtual Standard_Boolean OverlapsCylinder (const Standard_Real theBottomRad,
196                                              const Standard_Real theTopRad,
197                                              const Standard_Real theHeight,
198                                              const gp_Trsf& theTrsf,
199                                              Standard_Boolean* theInside = NULL) const = 0;
200 
201 public:
202 
203   //! Measures distance between 3d projection of user-picked
204   //! screen point and given point theCOG.
205   //! It makes sense only for intersectors built on a single point.
206   //! This method returns infinite value for the base class.
207   Standard_EXPORT virtual Standard_Real DistToGeometryCenter (const gp_Pnt& theCOG) const;
208 
209   //! Calculates the point on a view ray that was detected during the run of selection algo by given depth.
210   //! It makes sense only for intersectors built on a single point.
211   //! This method returns infinite point for the base class.
212   Standard_EXPORT virtual gp_Pnt DetectedPoint (const Standard_Real theDepth) const;
213 
214   //! Dumps the content of me into the stream
215   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
216 
217   //! Checks whether the ray that starts at the point theLoc and directs with the direction theRayDir intersects
218   //! with the sphere with center at theCenter and radius TheRadius
219   Standard_EXPORT virtual Standard_Boolean RaySphereIntersection (const gp_Pnt& theCenter,
220                                                                   const Standard_Real theRadius,
221                                                                   const gp_Pnt& theLoc,
222                                                                   const gp_Dir& theRayDir,
223                                                                   Standard_Real& theTimeEnter,
224                                                                   Standard_Real& theTimeLeave) const;
225 
226   //! Checks whether the ray that starts at the point theLoc and directs with the direction theRayDir intersects
227   //! with the cylinder (or cone) with radiuses theBottomRad and theTopRad and height theHeights
228   Standard_EXPORT virtual Standard_Boolean RayCylinderIntersection (const Standard_Real theBottomRadius,
229                                                                     const Standard_Real theTopRadius,
230                                                                     const Standard_Real theHeight,
231                                                                     const gp_Pnt& theLoc,
232                                                                     const gp_Dir& theRayDir,
233                                                                     Standard_Real& theTimeEnter,
234                                                                     Standard_Real& theTimeLeave) const;
235 
236   DEFINE_STANDARD_RTTIEXT(SelectMgr_BaseIntersector,Standard_Transient)
237 
238 protected:
239 
240   Handle(Graphic3d_Camera) myCamera;        //!< camera definition (if builder isn't NULL it is the same as its camera)
241   SelectMgr_SelectionType  mySelectionType; //!< type of selection
242 };
243 
244 #endif // _SelectMgr_BaseIntersector_HeaderFile
245