Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:03

0001 // Created on: 1991-01-28
0002 // Created by: Remi Lequette
0003 // Copyright (c) 1991-1999 Matra Datavision
0004 // Copyright (c) 1999-2012 OPEN CASCADE SAS
0005 //
0006 // This file is part of Open CASCADE Technology software library.
0007 //
0008 // This library is free software; you can redistribute it and/or modify it under
0009 // the terms of the GNU Lesser General Public License version 2.1 as published
0010 // by the Free Software Foundation, with special exception defined in the file
0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0012 // distribution for complete text of the license and disclaimer of any warranty.
0013 //
0014 // Alternatively, this file may be used under the terms of Open CASCADE
0015 // commercial license or contractual agreement.
0016 
0017 #ifndef _Bnd_Box2d_HeaderFile
0018 #define _Bnd_Box2d_HeaderFile
0019 
0020 #include <gp_Lin2d.hxx>
0021 #include <gp_Pnt2d.hxx>
0022 #include <Standard.hxx>
0023 #include <Standard_DefineAlloc.hxx>
0024 #include <Standard_Handle.hxx>
0025 
0026 #include <Standard_Real.hxx>
0027 #include <Standard_Integer.hxx>
0028 #include <Standard_Boolean.hxx>
0029 class gp_Dir2d;
0030 class gp_Trsf2d;
0031 
0032 
0033 //! Describes a bounding box in 2D space.
0034 //! A bounding box is parallel to the axes of the coordinates
0035 //! system. If it is finite, it is defined by the two intervals:
0036 //! -   [ Xmin,Xmax ], and
0037 //! -   [ Ymin,Ymax ].
0038 //! A bounding box may be infinite (i.e. open) in one or more
0039 //! directions. It is said to be:
0040 //! -   OpenXmin if it is infinite on the negative side of the   "X Direction";
0041 //! -   OpenXmax if it is infinite on the positive side of the   "X Direction";
0042 //! -   OpenYmin if it is infinite on the negative side of the   "Y Direction";
0043 //! -   OpenYmax if it is infinite on the positive side of the   "Y Direction";
0044 //! -   WholeSpace if it is infinite in all four directions. In
0045 //! this case, any point of the space is inside the box;
0046 //! -   Void if it is empty. In this case, there is no point included in the box.
0047 //! A bounding box is defined by four bounds (Xmin, Xmax, Ymin and Ymax) which
0048 //! limit the bounding box if it is finite, six flags (OpenXmin, OpenXmax, OpenYmin,
0049 //! OpenYmax, WholeSpace and Void) which describe the bounding box if it is infinite or empty, and
0050 //! -   a gap, which is included on both sides in any direction when consulting the finite bounds of the box.
0051 class Bnd_Box2d 
0052 {
0053 public:
0054 
0055   DEFINE_STANDARD_ALLOC
0056 
0057   //! Creates an empty 2D bounding box.
0058   //! The constructed box is qualified Void. Its gap is null.
0059   Bnd_Box2d() : Xmin(0.), Xmax(0.), Ymin(0.), Ymax(0.), Gap(0.), Flags (VoidMask) {}
0060 
0061   //! Sets this bounding box so that it covers the whole 2D
0062   //! space, i.e. it is infinite in all directions.
0063   void SetWhole() { Flags = WholeMask; }
0064 
0065   //! Sets this 2D bounding box so that it is empty. All points are outside a void box.
0066   void SetVoid()
0067   {
0068     Flags = VoidMask;
0069     Gap   = 0.0;
0070   }
0071 
0072   //! Sets this 2D bounding box so that it bounds
0073   //! the point P. This involves first setting this bounding box
0074   //! to be void and then adding the point PThe rectangle bounds   the  point <P>.
0075   void Set (const gp_Pnt2d& thePnt)
0076   {
0077     Flags = VoidMask;
0078     Gap   = 0.0;
0079     Add (thePnt);
0080   }
0081 
0082   //! Sets this 2D bounding box so that it bounds
0083   //! the half-line defined by point P and direction D, i.e. all
0084   //! points M defined by M=P+u*D, where u is greater than
0085   //! or equal to 0, are inside the bounding area. This involves
0086   //! first setting this 2D box to be void and then adding the   half-line.
0087   void Set (const gp_Pnt2d& thePnt, const gp_Dir2d& theDir)
0088   {
0089     Flags = VoidMask;
0090     Gap   = 0.0;
0091     Add (thePnt, theDir);
0092   }
0093 
0094   //! Enlarges this 2D bounding box, if required, so that it
0095   //! contains at least:
0096   //! -   interval [ aXmin,aXmax ] in the "X Direction",
0097   //! -   interval [ aYmin,aYmax ] in the "Y Direction"
0098   Standard_EXPORT void Update (const Standard_Real aXmin, const Standard_Real aYmin, const Standard_Real aXmax, const Standard_Real aYmax);
0099   
0100   //! Adds a point of coordinates (X,Y) to this bounding box.
0101   Standard_EXPORT void Update (const Standard_Real X, const Standard_Real Y);
0102   
0103   //! Returns the gap of this 2D bounding box.
0104   Standard_Real GetGap() const { return Gap; }
0105 
0106   //! Set the gap of this 2D bounding box to abs(Tol).
0107   void SetGap (const Standard_Real Tol) { Gap = Tol; }
0108 
0109   //! Enlarges     the  box  with    a  tolerance  value.
0110   //! This means that the minimum values of its X and Y
0111   //! intervals of definition, when they are finite, are reduced by
0112   //! the absolute value of Tol, while the maximum values are
0113   //! increased by the same amount.
0114   void Enlarge (const Standard_Real theTol)
0115   {
0116     Standard_Real aTol = theTol < 0.0 ? -theTol : theTol;
0117     if (Gap < aTol) Gap = aTol;
0118   }
0119 
0120   //! Returns the bounds of this 2D bounding box.
0121   //! The gap is included. If this bounding box is infinite (i.e. "open"), returned values
0122   //! may be equal to +/- Precision::Infinite().
0123   //! if IsVoid()
0124   Standard_EXPORT void Get (Standard_Real& aXmin, Standard_Real& aYmin, Standard_Real& aXmax, Standard_Real& aYmax) const;
0125 
0126   //! The Box will be infinitely long in the Xmin direction.
0127   void OpenXmin() { Flags |= XminMask; }
0128 
0129   //! The Box will be infinitely long in the Xmax direction.
0130   void OpenXmax() { Flags |= XmaxMask; }
0131 
0132   //! The Box will be infinitely long in the Ymin direction.
0133   void OpenYmin() { Flags |= YminMask; }
0134 
0135   //! The Box will be infinitely long in the Ymax direction.
0136   void OpenYmax() { Flags |= YmaxMask; }
0137 
0138   //! Returns true if this bounding box is open in the Xmin direction.
0139   Standard_Boolean IsOpenXmin() const { return (Flags & XminMask) != 0; }
0140 
0141   //! Returns true if this bounding box is open in the Xmax direction.
0142   Standard_Boolean IsOpenXmax() const { return (Flags & XmaxMask) != 0; }
0143 
0144   //! Returns true if this bounding box is open in the Ymin direction.
0145   Standard_Boolean IsOpenYmin() const { return (Flags & YminMask) != 0; }
0146 
0147   //! Returns true if this bounding box is open in the Ymax direction.
0148   Standard_Boolean IsOpenYmax() const { return (Flags & YmaxMask) != 0; }
0149 
0150   //! Returns true if this bounding box is infinite in all 4
0151   //! directions (Whole Space flag).
0152   Standard_Boolean IsWhole() const { return (Flags & WholeMask) == WholeMask; }
0153 
0154   //! Returns true if this 2D bounding box is empty (Void flag).
0155   Standard_Boolean IsVoid() const { return (Flags & VoidMask) != 0; }
0156 
0157   //! Returns a bounding box which is the result of applying the
0158   //! transformation T to this bounding box.
0159   //! Warning
0160   //! Applying a geometric transformation (for example, a
0161   //! rotation) to a bounding box generally increases its
0162   //! dimensions. This is not optimal for algorithms which use it.
0163   Standard_NODISCARD Standard_EXPORT Bnd_Box2d Transformed (const gp_Trsf2d& T) const;
0164   
0165   //! Adds the 2d box <Other> to <me>.
0166   Standard_EXPORT void Add (const Bnd_Box2d& Other);
0167   
0168   //! Adds the 2d point.
0169   void Add (const gp_Pnt2d& thePnt) { Update (thePnt.X(), thePnt.Y()); }
0170 
0171   //! Extends bounding box from thePnt in the direction theDir.
0172   void Add (const gp_Pnt2d& thePnt, const gp_Dir2d& theDir)
0173   {
0174     Add (thePnt);
0175     Add (theDir);
0176   }
0177   
0178   //! Extends the Box  in the given Direction, i.e. adds
0179   //! a half-line. The box may become infinite in 1 or 2
0180   //! directions.
0181   Standard_EXPORT void Add (const gp_Dir2d& D);
0182   
0183   //! Returns True if the 2d pnt <P> is out <me>.
0184   Standard_EXPORT Standard_Boolean IsOut (const gp_Pnt2d& P) const;
0185 
0186   //! Returns True if the line doesn't intersect the box.
0187   Standard_EXPORT Standard_Boolean IsOut(const gp_Lin2d& theL) const;
0188   
0189   //! Returns True if the segment doesn't intersect the box.
0190   Standard_EXPORT Standard_Boolean IsOut(const gp_Pnt2d& theP0, const gp_Pnt2d& theP1) const;
0191 
0192   //! Returns True if <Box2d> is out <me>.
0193   Standard_EXPORT Standard_Boolean IsOut (const Bnd_Box2d& Other) const;
0194   
0195   //! Returns True if transformed <Box2d> is out <me>.
0196   Standard_Boolean IsOut (const Bnd_Box2d& theOther, const gp_Trsf2d& theTrsf) const
0197   {
0198     return IsOut (theOther.Transformed (theTrsf));
0199   }
0200 
0201   //! Compares  a transformed  bounding with  a    transformed
0202   //! bounding. The default implementation is  to make a copy
0203   //! of <me> and <Other>, to transform them and to test.
0204   Standard_Boolean IsOut (const gp_Trsf2d& T1, const Bnd_Box2d& Other, const gp_Trsf2d& T2) const
0205   {
0206     return Transformed(T1).IsOut (Other.Transformed(T2));
0207   }
0208 
0209   Standard_EXPORT void Dump() const;
0210   
0211   //! Computes the squared diagonal of me.
0212   Standard_Real SquareExtent() const
0213   {
0214     if (IsVoid()) return 0.0;
0215     const Standard_Real aDx = Xmax - Xmin + Gap + Gap;
0216     const Standard_Real aDy = Ymax - Ymin + Gap + Gap;
0217     return aDx*aDx + aDy*aDy;
0218   }
0219 
0220 protected:
0221 
0222   //! Bit flags.
0223   enum MaskFlags
0224   {
0225     VoidMask  = 0x01,
0226     XminMask  = 0x02,
0227     XmaxMask  = 0x04,
0228     YminMask  = 0x08,
0229     YmaxMask  = 0x10,
0230     WholeMask = 0x1e
0231   };
0232 
0233 private:
0234 
0235   Standard_Real Xmin;
0236   Standard_Real Xmax;
0237   Standard_Real Ymin;
0238   Standard_Real Ymax;
0239   Standard_Real Gap;
0240   Standard_Integer Flags;
0241 
0242 };
0243 
0244 #endif // _Bnd_Box2d_HeaderFile