0032281: Visualization - add Select3D_SensitiveCylinder
[occt.git] / src / SelectMgr / SelectMgr_Frustum.hxx
1 // Created on: 2015-03-16
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_Frustum_HeaderFile
17 #define _SelectMgr_Frustum_HeaderFile
18
19 #include <SelectMgr_BaseFrustum.hxx>
20
21 //! This is an internal class containing representation of rectangular selecting frustum, created in case
22 //! of point and box selection, and algorithms for overlap detection between selecting
23 //! frustum and sensitive entities. The principle of frustum calculation:
24 //! - for point selection: on a near view frustum plane rectangular neighborhood of
25 //!                        user-picked point is created according to the pixel tolerance
26 //!                        given and then this rectangle is projected onto far view frustum
27 //!                        plane. This rectangles define the parallel bases of selecting frustum;
28 //! - for box selection: box points are projected onto near and far view frustum planes.
29 //!                      These 2 projected rectangles define parallel bases of selecting frustum.
30 //! Overlap detection tests are implemented according to the terms of separating axis
31 //! theorem (SAT).
32 //! Vertex order:
33 //! - for triangular frustum: V0_Near, V1_Near, V2_Near,
34 //!                           V0_Far, V1_Far, V2_Far;
35 //! - for rectangular frustum: LeftTopNear, LeftTopFar,
36 //!                            LeftBottomNear,LeftBottomFar,
37 //!                            RightTopNear, RightTopFar,
38 //!                            RightBottomNear, RightBottomFar.
39 //! Plane order in array:
40 //! - for triangular frustum: V0V1, V1V2, V0V2, Near, Far;
41 //! - for rectangular frustum: Top, Bottom, Left, Right, Near, Far.
42 //! Uncollinear edge directions order:
43 //! - for rectangular frustum: Horizontal, Vertical,
44 //!                            LeftLower, RightLower,
45 //!                            LeftUpper, RightUpper;
46 //! - for triangular frustum: V0_Near - V0_Far, V1_Near - V1_Far, V2_Near - V2_Far,
47 //!                           V1_Near - V0_Near, V2_Near - V1_Near, V2_Near - V0_Near.
48 template <int N>
49 class SelectMgr_Frustum : public SelectMgr_BaseFrustum
50 {
51 public:
52
53   SelectMgr_Frustum() : SelectMgr_BaseFrustum()
54   {
55     memset (myMaxOrthoVertsProjections, 0, sizeof (myMaxOrthoVertsProjections));
56     memset (myMinOrthoVertsProjections, 0, sizeof (myMinOrthoVertsProjections));
57     memset (myMaxVertsProjections, 0, sizeof (myMaxVertsProjections));
58     memset (myMinVertsProjections, 0, sizeof (myMinVertsProjections));
59   }
60
61   //! Dumps the content of me into the stream
62   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
63
64 protected:
65
66   // SAT Tests for different objects
67
68   //! Returns true if selecting volume is overlapped by axis-aligned bounding box
69   //! with minimum corner at point theMinPt and maximum at point theMaxPt
70   Standard_Boolean hasBoxOverlap (const SelectMgr_Vec3& theBoxMin,
71                                   const SelectMgr_Vec3& theBoxMax,
72                                   Standard_Boolean*     theInside = NULL) const;
73
74   //! SAT intersection test between defined volume and given point
75   Standard_Boolean hasPointOverlap (const gp_Pnt& thePnt) const;
76
77   //! SAT intersection test between defined volume and given segment
78   Standard_Boolean hasSegmentOverlap (const gp_Pnt& thePnt1,
79                                       const gp_Pnt& thePnt2) const;
80
81   //! SAT intersection test between frustum given and planar convex polygon represented as ordered point set
82   Standard_Boolean hasPolygonOverlap (const TColgp_Array1OfPnt& theArrayOfPnts,
83                                       gp_Vec& theNormal) const;
84
85   //! SAT intersection test between defined volume and given triangle
86   Standard_Boolean hasTriangleOverlap (const gp_Pnt& thePnt1,
87                                        const gp_Pnt& thePnt2,
88                                        const gp_Pnt& thePnt3,
89                                        gp_Vec& theNormal) const;
90
91   //! Intersection test between defined volume and given sphere
92   Standard_Boolean hasSphereOverlap (const gp_Pnt& thePnt1,
93                                      const Standard_Real theRadius,
94                                      Standard_Boolean* theInside = NULL) const;
95
96   //! Intersection test between defined volume and given cylinder (or cone).
97   Standard_Boolean hasCylinderOverlap (const Standard_Real theBottomRad,
98                                        const Standard_Real theTopRad,
99                                        const Standard_Real theHeight,
100                                        const gp_Trsf& theTrsf,
101                                        Standard_Boolean* theInside = NULL) const;
102
103   //! Checking whether the point thePnt is inside the shape with borders theVertices.
104   //! thePnt and theVertices lie in the same plane.
105   Standard_Boolean IsDotInside (const gp_Pnt& thePnt,
106                                 const TColgp_Array1OfPnt& theVertices) const;
107
108 private:
109
110   //! Return true if one segment enclosed between the points thePnt1Seg1 and thePnt2Seg1
111   //! intersects another segment that enclosed between thePnt1Seg2 and thePnt2Seg2.
112   Standard_Boolean isSegmentsIntersect (const gp_Pnt& thePnt1Seg1,
113                                         const gp_Pnt& thePnt2Seg1,
114                                         const gp_Pnt& thePnt1Seg2,
115                                         const gp_Pnt& thePnt2Seg2) const;
116
117   //! Checking whether the borders theVertices of the shape intersect
118   //! the cylinder (or cone) end face with the center theCenter and radius theRadius
119   Standard_Boolean isIntersectCylinderEndFace (const Standard_Real theRad,
120                                                const gp_Pnt& theCenter,
121                                                const gp_Trsf& theTrsf,
122                                                const TColgp_Array1OfPnt& theVertices) const;
123
124   //! Checks if AABB and frustum are separated along the given axis
125   Standard_Boolean isSeparated (const SelectMgr_Vec3& theBoxMin,
126                                 const SelectMgr_Vec3& theBoxMax,
127                                 const gp_XYZ&         theDirect,
128                                 Standard_Boolean*     theInside) const;
129
130   //! Checks if triangle and frustum are separated along the given axis
131   Standard_Boolean isSeparated (const gp_Pnt& thePnt1,
132                                 const gp_Pnt& thePnt2,
133                                 const gp_Pnt& thePnt3,
134                                 const gp_XYZ& theAxis) const;
135
136 protected:
137
138   gp_Vec myPlanes[N + 2];        //!< Plane equations
139   gp_Pnt myVertices[N * 2];      //!< Vertices coordinates
140
141   Standard_Real myMaxVertsProjections[N + 2];      //!< Cached projections of vertices onto frustum plane directions
142   Standard_Real myMinVertsProjections[N + 2];      //!< Cached projections of vertices onto frustum plane directions
143   Standard_Real myMaxOrthoVertsProjections[3];     //!< Cached projections of vertices onto directions of ortho unit vectors
144   Standard_Real myMinOrthoVertsProjections[3];     //!< Cached projections of vertices onto directions of ortho unit vectors
145
146   gp_Vec myEdgeDirs[6];                    //!< Cached edge directions
147 };
148
149 #include <SelectMgr_Frustum.lxx>
150
151 #endif // _SelectMgr_Frustum_HeaderFile