Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:53

0001 // Created on: 2014-05-22
0002 // Created by: Varvara POSKONINA
0003 // Copyright (c) 2005-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _SelectMgr_RectangularFrustum_HeaderFile
0017 #define _SelectMgr_RectangularFrustum_HeaderFile
0018 
0019 #include <SelectMgr_Frustum.hxx>
0020 
0021 //! This class contains representation of rectangular selecting frustum, created in case
0022 //! of point and box selection, and algorithms for overlap detection between selecting
0023 //! frustum and sensitive entities. The principle of frustum calculation:
0024 //! - for point selection: on a near view frustum plane rectangular neighborhood of
0025 //!                        user-picked point is created according to the pixel tolerance
0026 //!                        given and then this rectangle is projected onto far view frustum
0027 //!                        plane. This rectangles define the parallel bases of selecting frustum;
0028 //! - for box selection: box points are projected onto near and far view frustum planes.
0029 //!                      These 2 projected rectangles define parallel bases of selecting frustum.
0030 //! Overlap detection tests are implemented according to the terms of separating axis
0031 //! theorem (SAT).
0032 class SelectMgr_RectangularFrustum : public SelectMgr_Frustum<4>
0033 {
0034 public:
0035 
0036   //! Auxiliary structure to define selection primitive (point or box)
0037   //! In case of point selection min and max points are identical.
0038   struct SelectionRectangle
0039   {
0040     SelectionRectangle()
0041     : myMinPnt(gp_Pnt2d(RealLast(), RealLast())),
0042       myMaxPnt(gp_Pnt2d(RealLast(), RealLast())) {}
0043 
0044     const gp_Pnt2d& MousePos() const { return myMinPnt; }
0045     void SetMousePos (const gp_Pnt2d& thePos) { myMinPnt = thePos; myMaxPnt = thePos; }
0046 
0047     const gp_Pnt2d& MinPnt() const { return myMinPnt; }
0048     void SetMinPnt (const gp_Pnt2d& theMinPnt) { myMinPnt = theMinPnt; }
0049 
0050     const gp_Pnt2d& MaxPnt() const { return myMaxPnt; }
0051     void SetMaxPnt (const gp_Pnt2d& theMaxPnt) { myMaxPnt = theMaxPnt; }
0052 
0053   private:
0054 
0055     gp_Pnt2d myMinPnt;
0056     gp_Pnt2d myMaxPnt;
0057   };
0058 
0059   //! Creates rectangular selecting frustum.
0060   Standard_EXPORT SelectMgr_RectangularFrustum();
0061 
0062   //! Initializes volume according to the point and given pixel tolerance
0063   Standard_EXPORT void Init (const gp_Pnt2d& thePoint);
0064 
0065   //! Initializes volume according to the selected rectangle
0066   Standard_EXPORT void Init (const gp_Pnt2d& theMinPnt,
0067                              const gp_Pnt2d& theMaxPnt);
0068 
0069   //! Returns True if Frustum (theVertices) intersects the circle.
0070   Standard_EXPORT Standard_Boolean isIntersectCircle (const Standard_Real theRadius,
0071                                                       const gp_Pnt& theCenter,
0072                                                       const gp_Trsf& theTrsf,
0073                                                       const TColgp_Array1OfPnt& theVertices) const;
0074 
0075   //! Returns True if Seg1 (thePnt1Seg1, thePnt2Seg1) and Seg2 (thePnt1Seg2, thePnt2Seg2) intersect.
0076   Standard_EXPORT Standard_Boolean isSegmentsIntersect (const gp_Pnt& thePnt1Seg1,
0077                                                         const gp_Pnt& thePnt2Seg1,
0078                                                         const gp_Pnt& thePnt1Seg2,
0079                                                         const gp_Pnt& thePnt2Seg2) const;
0080 
0081   //! Builds volume according to internal parameters.
0082   //! NOTE: it should be called after Init() method
0083   Standard_EXPORT virtual void Build() Standard_OVERRIDE;
0084 
0085   //! Checks if it is possible to scale this frustum.
0086   //! It is true for frustum built on a single point.
0087   Standard_EXPORT virtual Standard_Boolean IsScalable() const Standard_OVERRIDE;
0088 
0089   //! IMPORTANT: Scaling makes sense only for frustum built on a single point!
0090   //!            Note that this method does not perform any checks on type of the frustum.
0091   //! Returns a copy of the frustum resized according to the scale factor given
0092   //! and transforms it using the matrix given.
0093   //! There are no default parameters, but in case if:
0094   //!    - transformation only is needed: @theScaleFactor must be initialized as any negative value;
0095   //!    - scale only is needed: @theTrsf must be set to gp_Identity.
0096   //! Builder is an optional argument that represents corresponding settings for re-constructing transformed
0097   //! frustum from scratch. Can be null if reconstruction is not expected furthermore.
0098   Standard_EXPORT virtual Handle(SelectMgr_BaseIntersector) ScaleAndTransform (const Standard_Integer theScaleFactor,
0099                                                                                const gp_GTrsf& theTrsf,
0100                                                                                const Handle(SelectMgr_FrustumBuilder)& theBuilder) const Standard_OVERRIDE;
0101 
0102   // SAT Tests for different objects
0103 
0104   //! SAT intersection test between defined volume and given axis-aligned box
0105   Standard_EXPORT virtual Standard_Boolean OverlapsBox (const SelectMgr_Vec3& theBoxMin,
0106                                                         const SelectMgr_Vec3& theBoxMax,
0107                                                         const SelectMgr_ViewClipRange& theClipRange,
0108                                                         SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
0109 
0110   //! Returns true if selecting volume is overlapped by axis-aligned bounding box
0111   //! with minimum corner at point theMinPt and maximum at point theMaxPt
0112   Standard_EXPORT virtual Standard_Boolean OverlapsBox (const SelectMgr_Vec3& theBoxMin,
0113                                                         const SelectMgr_Vec3& theBoxMax,
0114                                                         Standard_Boolean*     theInside) const Standard_OVERRIDE;
0115 
0116   //! Intersection test between defined volume and given point
0117   Standard_EXPORT virtual Standard_Boolean OverlapsPoint (const gp_Pnt& thePnt,
0118                                                      const SelectMgr_ViewClipRange& theClipRange,
0119                                                      SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
0120 
0121   //! Intersection test between defined volume and given point
0122   Standard_EXPORT virtual Standard_Boolean OverlapsPoint (const gp_Pnt& thePnt) const Standard_OVERRIDE;
0123 
0124   //! SAT intersection test between defined volume and given ordered set of points,
0125   //! representing line segments. The test may be considered of interior part or
0126   //! boundary line defined by segments depending on given sensitivity type
0127   Standard_EXPORT virtual Standard_Boolean OverlapsPolygon (const TColgp_Array1OfPnt& theArrayOfPnts,
0128                                                             Select3D_TypeOfSensitivity theSensType,
0129                                                             const SelectMgr_ViewClipRange& theClipRange,
0130                                                             SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
0131 
0132   //! Checks if line segment overlaps selecting frustum
0133   Standard_EXPORT virtual Standard_Boolean OverlapsSegment (const gp_Pnt& thePnt1,
0134                                                             const gp_Pnt& thePnt2,
0135                                                             const SelectMgr_ViewClipRange& theClipRange,
0136                                                             SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
0137 
0138   //! SAT intersection test between defined volume and given triangle. The test may
0139   //! be considered of interior part or boundary line defined by triangle vertices
0140   //! depending on given sensitivity type
0141   Standard_EXPORT virtual Standard_Boolean OverlapsTriangle (const gp_Pnt& thePnt1,
0142                                                              const gp_Pnt& thePnt2,
0143                                                              const gp_Pnt& thePnt3,
0144                                                              Select3D_TypeOfSensitivity theSensType,
0145                                                              const SelectMgr_ViewClipRange& theClipRange,
0146                                                              SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
0147 
0148   //! Intersection test between defined volume and given sphere
0149   Standard_EXPORT virtual Standard_Boolean OverlapsSphere (const gp_Pnt& theCenter,
0150                                                            const Standard_Real theRadius,
0151                                                            const SelectMgr_ViewClipRange& theClipRange,
0152                                                            SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
0153 
0154   //! Intersection test between defined volume and given sphere
0155   Standard_EXPORT virtual Standard_Boolean OverlapsSphere (const gp_Pnt& theCenter,
0156                                                            const Standard_Real theRadius,
0157                                                            Standard_Boolean* theInside) const Standard_OVERRIDE;
0158 
0159   //! Returns true if selecting volume is overlapped by cylinder (or cone) with radiuses theBottomRad
0160   //! and theTopRad, height theHeight and transformation to apply theTrsf.
0161   Standard_EXPORT virtual Standard_Boolean OverlapsCylinder (const Standard_Real theBottomRad,
0162                                                              const Standard_Real theTopRad,
0163                                                              const Standard_Real theHeight,
0164                                                              const gp_Trsf& theTrsf,
0165                                                              const Standard_Boolean theIsHollow,
0166                                                              const SelectMgr_ViewClipRange& theClipRange,
0167                                                              SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
0168 
0169   //! Returns true if selecting volume is overlapped by cylinder (or cone) with radiuses theBottomRad
0170   //! and theTopRad, height theHeight and transformation to apply theTrsf.
0171   Standard_EXPORT virtual Standard_Boolean OverlapsCylinder (const Standard_Real theBottomRad,
0172                                                              const Standard_Real theTopRad,
0173                                                              const Standard_Real theHeight,
0174                                                              const gp_Trsf& theTrsf,
0175                                                              const Standard_Boolean theIsHollow,
0176                                                              Standard_Boolean* theInside = NULL) const Standard_OVERRIDE;
0177 
0178   //! Returns true if selecting volume is overlapped by circle with radius theRadius,
0179   //! boolean theIsFilled and transformation to apply theTrsf.
0180   //! The position and orientation of the circle are specified
0181   //! via theTrsf transformation for gp::XOY() with center in gp::Origin().
0182   Standard_EXPORT virtual Standard_Boolean OverlapsCircle (const Standard_Real theBottomRad,
0183                                                            const gp_Trsf& theTrsf,
0184                                                            const Standard_Boolean theIsFilled,
0185                                                            const SelectMgr_ViewClipRange& theClipRange,
0186                                                            SelectBasics_PickResult& thePickResult) const Standard_OVERRIDE;
0187 
0188   //! Returns true if selecting volume is overlapped by circle with radius theRadius,
0189   //! boolean theIsFilled and transformation to apply theTrsf.
0190   //! The position and orientation of the circle are specified
0191   //! via theTrsf transformation for gp::XOY() with center in gp::Origin().
0192   Standard_EXPORT virtual Standard_Boolean OverlapsCircle (const Standard_Real theBottomRad,
0193                                                            const gp_Trsf& theTrsf,
0194                                                            const Standard_Boolean theIsFilled,
0195                                                            Standard_Boolean* theInside = NULL) const Standard_OVERRIDE;
0196 
0197   //! Measures distance between 3d projection of user-picked
0198   //! screen point and given point theCOG.
0199   //! It makes sense only for frustums built on a single point.
0200   Standard_EXPORT virtual Standard_Real DistToGeometryCenter (const gp_Pnt& theCOG) const Standard_OVERRIDE;
0201 
0202   //! Calculates the point on a view ray that was detected during the run of selection algo by given depth
0203   Standard_EXPORT virtual gp_Pnt DetectedPoint (const Standard_Real theDepth) const Standard_OVERRIDE;
0204 
0205   //! A set of helper functions that return rectangular selecting frustum data
0206   const gp_Pnt* GetVertices() const { return myVertices; }
0207 
0208   //! Returns projection of 2d mouse picked point or projection
0209   //! of center of 2d rectangle (for point and rectangular selection
0210   //! correspondingly) onto near view frustum plane
0211   virtual const gp_Pnt& GetNearPnt() const Standard_OVERRIDE { return myNearPickedPnt; }
0212 
0213   //! Returns projection of 2d mouse picked point or projection
0214   //! of center of 2d rectangle (for point and rectangular selection
0215   //! correspondingly) onto far view frustum plane
0216   virtual const gp_Pnt& GetFarPnt() const Standard_OVERRIDE { return myFarPickedPnt; }
0217 
0218   //! Returns view ray direction.
0219   virtual const gp_Dir& GetViewRayDirection() const Standard_OVERRIDE { return myViewRayDir; }
0220 
0221   //! Returns current mouse coordinates.
0222   Standard_EXPORT virtual const gp_Pnt2d& GetMousePosition() const Standard_OVERRIDE;
0223 
0224   //! Stores plane equation coefficients (in the following form:
0225   //! Ax + By + Cz + D = 0) to the given vector
0226   Standard_EXPORT virtual void GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const Standard_OVERRIDE;
0227 
0228   //! Dumps the content of me into the stream
0229   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
0230 
0231 protected:
0232 
0233   Standard_EXPORT void segmentSegmentDistance (const gp_Pnt& theSegPnt1,
0234                                                const gp_Pnt& theSegPnt2,
0235                                                SelectBasics_PickResult& thePickResult) const;
0236 
0237   Standard_EXPORT bool segmentPlaneIntersection (const gp_Vec& thePlane,
0238                                                  const gp_Pnt& thePntOnPlane,
0239                                                  SelectBasics_PickResult& thePickResult) const;
0240 
0241 private:
0242 
0243   void cacheVertexProjections (SelectMgr_RectangularFrustum* theFrustum) const;
0244 
0245 private:
0246   enum { LeftTopNear, LeftTopFar,
0247          LeftBottomNear, LeftBottomFar,
0248          RightTopNear, RightTopFar,
0249          RightBottomNear, RightBottomFar };
0250 
0251 private:
0252 
0253   SelectionRectangle      mySelRectangle;              //!< parameters for selection by point or box (it is used to build frustum)
0254   gp_Pnt                  myNearPickedPnt;             //!< 3d projection of user-picked selection point onto near view plane
0255   gp_Pnt                  myFarPickedPnt;              //!< 3d projection of user-picked selection point onto far view plane
0256   gp_Dir                  myViewRayDir;                //!< view ray direction
0257   Standard_Real           myScale;                     //!< Scale factor of applied transformation, if there was any
0258 
0259 };
0260 
0261 #endif // _SelectMgr_RectangularFrustum_HeaderFile