Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-08 09:15:36

0001 // Created on: 2016-06-07
0002 // Created by: Nikolai BUKHALOV
0003 // Copyright (c) 2016 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 _Bnd_Range_HeaderFile
0017 #define _Bnd_Range_HeaderFile
0018 
0019 #include <Standard_ConstructionError.hxx>
0020 
0021 #include <NCollection_List.hxx>
0022 
0023 //! This class describes a range in 1D space restricted
0024 //! by two real values.
0025 //! A range can be void indicating there is no point included in the range.
0026 class Bnd_Range
0027 {
0028 public:
0029   //! Default constructor. Creates VOID range.
0030   Bnd_Range()
0031       : myFirst(0.0),
0032         myLast(-1.0)
0033   {
0034   }
0035 
0036   //! Constructor. Never creates VOID range.
0037   Bnd_Range(const Standard_Real theMin, const Standard_Real theMax)
0038       : myFirst(theMin),
0039         myLast(theMax)
0040   {
0041     if (myLast < myFirst)
0042       throw Standard_ConstructionError("Last < First");
0043   }
0044 
0045   //! Replaces <this> with common-part of <this> and theOther
0046   Standard_EXPORT void Common(const Bnd_Range& theOther);
0047 
0048   //! Joins *this and theOther to one interval.
0049   //! Replaces *this to the result.
0050   //! Returns false if the operation cannot be done (e.g.
0051   //! input arguments are empty or separated).
0052   //! @sa use method ::Add() to merge two ranges unconditionally
0053   Standard_EXPORT Standard_Boolean Union(const Bnd_Range& theOther);
0054 
0055   //! Splits <this> to several sub-ranges by theVal value
0056   //! (e.g. range [3, 15] will be split by theVal==5 to the two
0057   //! ranges: [3, 5] and [5, 15]). New ranges will be pushed to
0058   //! theList (theList must be initialized correctly before
0059   //! calling this method).
0060   //! If thePeriod != 0.0 then at least one boundary of
0061   //! new ranges (if <*this> intersects theVal+k*thePeriod) will be equal to
0062   //! theVal+thePeriod*k, where k is an integer number (k = 0, +/-1, +/-2, ...).
0063   //! (let thePeriod in above example be 4 ==> we will obtain
0064   //! four ranges: [3, 5], [5, 9], [9, 13] and [13, 15].
0065   Standard_EXPORT void Split(const Standard_Real          theVal,
0066                              NCollection_List<Bnd_Range>& theList,
0067                              const Standard_Real          thePeriod = 0.0) const;
0068 
0069   //! Checks if <this> intersects values like
0070   //!   theVal+k*thePeriod, where k is an integer number (k = 0, +/-1, +/-2, ...).
0071   //! Returns:
0072   //!     0 - if <this> does not intersect the theVal+k*thePeriod.
0073   //!     1 - if <this> intersects theVal+k*thePeriod.
0074   //!     2 - if myFirst or/and myLast are equal to theVal+k*thePeriod.
0075   //!
0076   //! ATTENTION!!!
0077   //!  If (myFirst == myLast) then this function will return only either 0 or 2.
0078   Standard_EXPORT Standard_Integer IsIntersected(const Standard_Real theVal,
0079                                                  const Standard_Real thePeriod = 0.0) const;
0080 
0081   //! Extends <this> to include theParameter
0082   void Add(const Standard_Real theParameter)
0083   {
0084     if (IsVoid())
0085     {
0086       myFirst = myLast = theParameter;
0087       return;
0088     }
0089 
0090     myFirst = Min(myFirst, theParameter);
0091     myLast  = Max(myLast, theParameter);
0092   }
0093 
0094   //! Extends this range to include both ranges.
0095   //! @sa use method ::Union() to check if two ranges overlap method merging
0096   void Add(const Bnd_Range& theRange)
0097   {
0098     if (theRange.IsVoid())
0099     {
0100       return;
0101     }
0102     else if (IsVoid())
0103     {
0104       *this = theRange;
0105     }
0106     myFirst = Min(myFirst, theRange.myFirst);
0107     myLast  = Max(myLast, theRange.myLast);
0108   }
0109 
0110   //! Obtain MIN boundary of <this>.
0111   //! If <this> is VOID the method returns false.
0112   Standard_Boolean GetMin(Standard_Real& thePar) const
0113   {
0114     if (IsVoid())
0115     {
0116       return Standard_False;
0117     }
0118 
0119     thePar = myFirst;
0120     return Standard_True;
0121   }
0122 
0123   //! Obtain MAX boundary of <this>.
0124   //! If <this> is VOID the method returns false.
0125   Standard_Boolean GetMax(Standard_Real& thePar) const
0126   {
0127     if (IsVoid())
0128     {
0129       return Standard_False;
0130     }
0131 
0132     thePar = myLast;
0133     return Standard_True;
0134   }
0135 
0136   //! Obtain first and last boundary of <this>.
0137   //! If <this> is VOID the method returns false.
0138   Standard_Boolean GetBounds(Standard_Real& theFirstPar, Standard_Real& theLastPar) const
0139   {
0140     if (IsVoid())
0141     {
0142       return Standard_False;
0143     }
0144 
0145     theFirstPar = myFirst;
0146     theLastPar  = myLast;
0147     return Standard_True;
0148   }
0149 
0150   //! Obtain theParameter satisfied to the equation
0151   //!     (theParameter-MIN)/(MAX-MIN) == theLambda.
0152   //!   *  theLambda == 0 --> MIN boundary will be returned;
0153   //!   *  theLambda == 0.5 --> Middle point will be returned;
0154   //!   *  theLambda == 1 --> MAX boundary will be returned;
0155   //!   *  theLambda < 0 --> the value less than MIN will be returned;
0156   //!   *  theLambda > 1 --> the value greater than MAX will be returned.
0157   //! If <this> is VOID the method returns false.
0158   Standard_Boolean GetIntermediatePoint(const Standard_Real theLambda,
0159                                         Standard_Real&      theParameter) const
0160   {
0161     if (IsVoid())
0162     {
0163       return Standard_False;
0164     }
0165 
0166     theParameter = myFirst + theLambda * (myLast - myFirst);
0167     return Standard_True;
0168   }
0169 
0170   //! Returns range value (MAX-MIN). Returns negative value for VOID range.
0171   Standard_Real Delta() const { return (myLast - myFirst); }
0172 
0173   //! Is <this> initialized.
0174   Standard_Boolean IsVoid() const { return (myLast < myFirst); }
0175 
0176   //! Initializes <this> by default parameters. Makes <this> VOID.
0177   void SetVoid()
0178   {
0179     myLast  = -1.0;
0180     myFirst = 0.0;
0181   }
0182 
0183   //! Extends this to the given value (in both side)
0184   void Enlarge(const Standard_Real theDelta)
0185   {
0186     if (IsVoid())
0187     {
0188       return;
0189     }
0190 
0191     myFirst -= theDelta;
0192     myLast += theDelta;
0193   }
0194 
0195   //! Returns the copy of <*this> shifted by theVal
0196   Bnd_Range Shifted(const Standard_Real theVal) const
0197   {
0198     return !IsVoid() ? Bnd_Range(myFirst + theVal, myLast + theVal) : Bnd_Range();
0199   }
0200 
0201   //! Shifts <*this> by theVal
0202   void Shift(const Standard_Real theVal)
0203   {
0204     if (!IsVoid())
0205     {
0206       myFirst += theVal;
0207       myLast += theVal;
0208     }
0209   }
0210 
0211   //! Trims the First value in range by the given lower limit.
0212   //! Marks range as Void if the given Lower value is greater than range Max.
0213   void TrimFrom(const Standard_Real theValLower)
0214   {
0215     if (!IsVoid())
0216     {
0217       myFirst = Max(myFirst, theValLower);
0218     }
0219   }
0220 
0221   //! Trim the Last value in range by the given Upper limit.
0222   //! Marks range as Void if the given Upper value is smaller than range Max.
0223   void TrimTo(const Standard_Real theValUpper)
0224   {
0225     if (!IsVoid())
0226     {
0227       myLast = Min(myLast, theValUpper);
0228     }
0229   }
0230 
0231   //! Returns True if the value is out of this range.
0232   Standard_Boolean IsOut(Standard_Real theValue) const
0233   {
0234     return IsVoid() || theValue < myFirst || theValue > myLast;
0235   }
0236 
0237   //! Returns True if the given range is out of this range.
0238   Standard_Boolean IsOut(const Bnd_Range& theRange) const
0239   {
0240     return IsVoid() || theRange.IsVoid() || theRange.myLast < myFirst || theRange.myFirst > myLast;
0241   }
0242 
0243   //! Returns TRUE if theOther is equal to <*this>
0244   Standard_Boolean operator==(const Bnd_Range& theOther) const
0245   {
0246     return ((myFirst == theOther.myFirst) && (myLast == theOther.myLast));
0247   }
0248 
0249   //! Dumps the content of me into the stream
0250   Standard_EXPORT void DumpJson(Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0251 
0252 private:
0253   Standard_Real myFirst; //!< Start of range
0254   Standard_Real myLast;  //!< End   of range
0255 };
0256 
0257 #endif