|
|
|||
File indexing completed on 2026-12-04 10:58:22
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 <algorithm> 0027 #include <cmath> 0028 #include <optional> 0029 0030 class gp_Dir2d; 0031 class gp_Trsf2d; 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 0051 //! the box. 0052 class Bnd_Box2d 0053 { 0054 public: 0055 DEFINE_STANDARD_ALLOC 0056 0057 //! Structure containing the 2D box limits (Xmin, Xmax, Ymin, Ymax). 0058 //! The values include the gap and account for open directions. 0059 struct Limits 0060 { 0061 double Xmin; //!< Minimum X coordinate 0062 double Xmax; //!< Maximum X coordinate 0063 double Ymin; //!< Minimum Y coordinate 0064 double Ymax; //!< Maximum Y coordinate 0065 }; 0066 0067 //! Sets this bounding box so that it covers the whole 2D 0068 //! space, i.e. it is infinite in all directions. 0069 void SetWhole() noexcept { Flags = WholeMask; } 0070 0071 //! Sets this 2D bounding box so that it is empty. All points are outside a void box. 0072 void SetVoid() noexcept 0073 { 0074 Xmin = RealLast(); 0075 Xmax = -RealLast(); 0076 Ymin = RealLast(); 0077 Ymax = -RealLast(); 0078 Gap = 0.0; 0079 Flags = VoidMask; 0080 } 0081 0082 //! Sets this 2D bounding box so that it bounds 0083 //! the point P. This involves first setting this bounding box 0084 //! to be void and then adding the point PThe rectangle bounds the point <P>. 0085 void Set(const gp_Pnt2d& thePnt) 0086 { 0087 Flags = VoidMask; 0088 Gap = 0.0; 0089 Add(thePnt); 0090 } 0091 0092 //! Sets this 2D bounding box so that it bounds 0093 //! the half-line defined by point P and direction D, i.e. all 0094 //! points M defined by M=P+u*D, where u is greater than 0095 //! or equal to 0, are inside the bounding area. This involves 0096 //! first setting this 2D box to be void and then adding the half-line. 0097 void Set(const gp_Pnt2d& thePnt, const gp_Dir2d& theDir) 0098 { 0099 Flags = VoidMask; 0100 Gap = 0.0; 0101 Add(thePnt, theDir); 0102 } 0103 0104 //! Enlarges this 2D bounding box, if required, so that it 0105 //! contains at least: 0106 //! - interval [ aXmin,aXmax ] in the "X Direction", 0107 //! - interval [ aYmin,aYmax ] in the "Y Direction" 0108 Standard_EXPORT void Update(const double aXmin, 0109 const double aYmin, 0110 const double aXmax, 0111 const double aYmax); 0112 0113 //! Adds a point of coordinates (X,Y) to this bounding box. 0114 Standard_EXPORT void Update(const double X, const double Y); 0115 0116 //! Returns the gap of this 2D bounding box. 0117 [[nodiscard]] constexpr double GetGap() const noexcept { return Gap; } 0118 0119 //! Set the gap of this 2D bounding box to abs(Tol). 0120 void SetGap(const double Tol) noexcept { Gap = std::abs(Tol); } 0121 0122 //! Enlarges the box with a tolerance value. 0123 //! This means that the minimum values of its X and Y 0124 //! intervals of definition, when they are finite, are reduced by 0125 //! the absolute value of Tol, while the maximum values are 0126 //! increased by the same amount. 0127 void Enlarge(const double theTol) noexcept { Gap = (std::max)(Gap, std::abs(theTol)); } 0128 0129 //! Returns the bounds of this 2D bounding box. 0130 //! The gap is included. If this bounding box is infinite (i.e. "open"), returned values 0131 //! may be equal to +/- Precision::Infinite(). 0132 //! if IsVoid() 0133 Standard_EXPORT void Get(double& aXmin, double& aYmin, double& aXmax, double& aYmax) const; 0134 0135 //! Returns the bounds of this 2D bounding box as a Limits structure. 0136 //! The gap is included. If this bounding box is infinite (i.e. "open"), 0137 //! returned values may be equal to +/- Precision::Infinite(). 0138 //! If the box is void, returns raw internal values. 0139 //! Can be used with C++17 structured bindings: 0140 //! @code 0141 //! auto [xmin, xmax, ymin, ymax] = aBox.Get(); 0142 //! @endcode 0143 [[nodiscard]] Standard_EXPORT Limits Get() const; 0144 0145 //! Returns the Xmin value (IsOpenXmin() ? -Precision::Infinite() : Xmin - GetGap()). 0146 [[nodiscard]] Standard_EXPORT double GetXMin() const; 0147 0148 //! Returns the Xmax value (IsOpenXmax() ? Precision::Infinite() : Xmax + GetGap()). 0149 [[nodiscard]] Standard_EXPORT double GetXMax() const; 0150 0151 //! Returns the Ymin value (IsOpenYmin() ? -Precision::Infinite() : Ymin - GetGap()). 0152 [[nodiscard]] Standard_EXPORT double GetYMin() const; 0153 0154 //! Returns the Ymax value (IsOpenYmax() ? Precision::Infinite() : Ymax + GetGap()). 0155 [[nodiscard]] Standard_EXPORT double GetYMax() const; 0156 0157 //! Returns the center of this 2D bounding box. The gap is included. 0158 //! If this bounding box is infinite (i.e. "open"), returned values 0159 //! may be equal to +/- Precision::Infinite(). 0160 //! Returns std::nullopt if the box is void. 0161 [[nodiscard]] Standard_EXPORT std::optional<gp_Pnt2d> Center() const; 0162 0163 //! The Box will be infinitely long in the Xmin direction. 0164 void OpenXmin() noexcept { Flags |= XminMask; } 0165 0166 //! The Box will be infinitely long in the Xmax direction. 0167 void OpenXmax() noexcept { Flags |= XmaxMask; } 0168 0169 //! The Box will be infinitely long in the Ymin direction. 0170 void OpenYmin() noexcept { Flags |= YminMask; } 0171 0172 //! The Box will be infinitely long in the Ymax direction. 0173 void OpenYmax() noexcept { Flags |= YmaxMask; } 0174 0175 //! Returns true if this bounding box is open in the Xmin direction. 0176 [[nodiscard]] bool IsOpenXmin() const noexcept { return (Flags & XminMask) != 0; } 0177 0178 //! Returns true if this bounding box is open in the Xmax direction. 0179 [[nodiscard]] bool IsOpenXmax() const noexcept { return (Flags & XmaxMask) != 0; } 0180 0181 //! Returns true if this bounding box is open in the Ymin direction. 0182 [[nodiscard]] bool IsOpenYmin() const noexcept { return (Flags & YminMask) != 0; } 0183 0184 //! Returns true if this bounding box is open in the Ymax direction. 0185 [[nodiscard]] bool IsOpenYmax() const noexcept { return (Flags & YmaxMask) != 0; } 0186 0187 //! Returns true if this bounding box is infinite in all 4 0188 //! directions (Whole Space flag). 0189 [[nodiscard]] bool IsWhole() const noexcept { return (Flags & WholeMask) == WholeMask; } 0190 0191 //! Returns true if this 2D bounding box is empty (Void flag). 0192 [[nodiscard]] bool IsVoid() const noexcept { return (Flags & VoidMask) != 0; } 0193 0194 //! Returns a bounding box which is the result of applying the 0195 //! transformation T to this bounding box. 0196 //! Warning 0197 //! Applying a geometric transformation (for example, a 0198 //! rotation) to a bounding box generally increases its 0199 //! dimensions. This is not optimal for algorithms which use it. 0200 [[nodiscard]] Standard_EXPORT Bnd_Box2d Transformed(const gp_Trsf2d& T) const; 0201 0202 //! Adds the 2d box <Other> to <me>. 0203 Standard_EXPORT void Add(const Bnd_Box2d& Other); 0204 0205 //! Adds the 2d point. 0206 void Add(const gp_Pnt2d& thePnt) noexcept { Update(thePnt.X(), thePnt.Y()); } 0207 0208 //! Extends bounding box from thePnt in the direction theDir. 0209 void Add(const gp_Pnt2d& thePnt, const gp_Dir2d& theDir) noexcept 0210 { 0211 Add(thePnt); 0212 Add(theDir); 0213 } 0214 0215 //! Extends the Box in the given Direction, i.e. adds 0216 //! a half-line. The box may become infinite in 1 or 2 0217 //! directions. 0218 Standard_EXPORT void Add(const gp_Dir2d& D); 0219 0220 //! Returns True if the 2d pnt <P> is out <me>. 0221 [[nodiscard]] Standard_EXPORT bool IsOut(const gp_Pnt2d& P) const; 0222 0223 //! Returns True if the line doesn't intersect the box. 0224 [[nodiscard]] Standard_EXPORT bool IsOut(const gp_Lin2d& theL) const; 0225 0226 //! Returns True if the segment doesn't intersect the box. 0227 [[nodiscard]] Standard_EXPORT bool IsOut(const gp_Pnt2d& theP0, const gp_Pnt2d& theP1) const; 0228 0229 //! Returns True if <Box2d> is out <me>. 0230 [[nodiscard]] Standard_EXPORT bool IsOut(const Bnd_Box2d& Other) const; 0231 0232 //! Returns True if the 2d point is inside or on the boundary of this box. 0233 [[nodiscard]] bool Contains(const gp_Pnt2d& theP) const { return !IsOut(theP); } 0234 0235 //! Returns True if the other 2d box intersects or is inside this box. 0236 [[nodiscard]] bool Intersects(const Bnd_Box2d& theOther) const { return !IsOut(theOther); } 0237 0238 //! Computes the minimum distance between two 2D boxes. 0239 [[nodiscard]] Standard_EXPORT double Distance(const Bnd_Box2d& theOther) const; 0240 0241 //! Returns True if transformed <Box2d> is out <me>. 0242 [[nodiscard]] bool IsOut(const Bnd_Box2d& theOther, const gp_Trsf2d& theTrsf) const noexcept 0243 { 0244 return IsOut(theOther.Transformed(theTrsf)); 0245 } 0246 0247 //! Compares a transformed bounding with a transformed 0248 //! bounding. The default implementation is to make a copy 0249 //! of <me> and <Other>, to transform them and to test. 0250 [[nodiscard]] bool IsOut(const gp_Trsf2d& T1, 0251 const Bnd_Box2d& Other, 0252 const gp_Trsf2d& T2) const noexcept 0253 { 0254 return Transformed(T1).IsOut(Other.Transformed(T2)); 0255 } 0256 0257 Standard_EXPORT void Dump() const; 0258 0259 //! Computes the squared diagonal of me. 0260 [[nodiscard]] double SquareExtent() const noexcept 0261 { 0262 if (IsVoid()) 0263 return 0.0; 0264 const double aDx = Xmax - Xmin + Gap + Gap; 0265 const double aDy = Ymax - Ymin + Gap + Gap; 0266 return aDx * aDx + aDy * aDy; 0267 } 0268 0269 protected: 0270 //! Bit flags. 0271 enum MaskFlags 0272 { 0273 VoidMask = 0x01, 0274 XminMask = 0x02, 0275 XmaxMask = 0x04, 0276 YminMask = 0x08, 0277 YmaxMask = 0x10, 0278 WholeMask = 0x1e 0279 }; 0280 0281 private: 0282 double Xmin = RealLast(); 0283 double Xmax = -RealLast(); 0284 double Ymin = RealLast(); 0285 double Ymax = -RealLast(); 0286 double Gap = 0.0; 0287 int Flags = VoidMask; 0288 }; 0289 0290 #endif // _Bnd_Box2d_HeaderFile
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|