Back to home page

EIC code displayed by LXR

 
 

    


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

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