1 // Created on: 2016-06-07
2 // Created by: Nikolai BUKHALOV
3 // Copyright (c) 2016 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _Bnd_Range_HeaderFile
17 #define _Bnd_Range_HeaderFile
19 #include <Standard_Real.hxx>
20 #include <Standard_ConstructionError.hxx>
22 #include <NCollection_List.hxx>
24 //! This class describes a range in 1D space restricted
25 //! by two real values.
26 //! A range can be void indicating there is no point included in the range.
31 //! Default constructor. Creates VOID range.
32 Bnd_Range() : myFirst(0.0), myLast(-1.0) {}
34 //! Constructor. Never creates VOID range.
35 Bnd_Range(const Standard_Real theMin, const Standard_Real theMax) :
36 myFirst(theMin), myLast(theMax)
39 throw Standard_ConstructionError("Last < First");
42 //! Replaces <this> with common-part of <this> and theOther
43 Standard_EXPORT void Common(const Bnd_Range& theOther);
45 //! Joins *this and theOther to one interval.
46 //! Replaces *this to the result.
47 //! Returns false if the operation cannot be done (e.g.
48 //! input arguments are empty or separated).
49 //! @sa use method ::Add() to merge two ranges unconditionally
50 Standard_EXPORT Standard_Boolean Union(const Bnd_Range& theOther);
52 //! Splits <this> to several sub-ranges by theVal value
53 //! (e.g. range [3, 15] will be split by theVal==5 to the two
54 //! ranges: [3, 5] and [5, 15]). New ranges will be pushed to
55 //! theList (theList must be initialized correctly before
56 //! calling this method).
57 //! If thePeriod != 0.0 then at least one boundary of
58 //! new ranges (if <*this> intersects theVal+k*thePeriod) will be equal to
59 //! theVal+thePeriod*k, where k is an integer number (k = 0, +/-1, +/-2, ...).
60 //! (let thePeriod in above example be 4 ==> we will obtain
61 //! four ranges: [3, 5], [5, 9], [9, 13] and [13, 15].
62 Standard_EXPORT void Split(const Standard_Real theVal,
63 NCollection_List<Bnd_Range>& theList,
64 const Standard_Real thePeriod = 0.0) const;
66 //! Checks if <this> intersects values like
67 //! theVal+k*thePeriod, where k is an integer number (k = 0, +/-1, +/-2, ...).
69 //! 0 - if <this> does not intersect the theVal+k*thePeriod.
70 //! 1 - if <this> intersects theVal+k*thePeriod.
71 //! 2 - if myFirst or/and myLast are equal to theVal+k*thePeriod.
74 //! If (myFirst == myLast) then this function will return only either 0 or 2.
75 Standard_EXPORT Standard_Integer
76 IsIntersected(const Standard_Real theVal,
77 const Standard_Real thePeriod = 0.0) const;
79 //! Extends <this> to include theParameter
80 void Add(const Standard_Real theParameter)
84 myFirst = myLast = theParameter;
88 myFirst = Min(myFirst, theParameter);
89 myLast = Max(myLast, theParameter);
92 //! Extends this range to include both ranges.
93 //! @sa use method ::Union() to check if two ranges overlap method merging
94 void Add (const Bnd_Range& theRange)
96 if (theRange.IsVoid())
104 myFirst = Min(myFirst, theRange.myFirst);
105 myLast = Max(myLast, theRange.myLast);
108 //! Obtain MIN boundary of <this>.
109 //! If <this> is VOID the method returns false.
110 Standard_Boolean GetMin(Standard_Real& thePar) const
114 return Standard_False;
118 return Standard_True;
121 //! Obtain MAX boundary of <this>.
122 //! If <this> is VOID the method returns false.
123 Standard_Boolean GetMax(Standard_Real& thePar) const
127 return Standard_False;
131 return Standard_True;
134 //! Obtain first and last boundary of <this>.
135 //! If <this> is VOID the method returns false.
136 Standard_Boolean GetBounds(Standard_Real& theFirstPar,
137 Standard_Real& theLastPar) const
141 return Standard_False;
144 theFirstPar = myFirst;
146 return Standard_True;
149 //! Returns range value (MAX-MIN). Returns negative value for VOID range.
150 Standard_Real Delta() const
152 return (myLast - myFirst);
155 //! Is <this> initialized.
156 Standard_Boolean IsVoid() const
158 return (myLast < myFirst);
161 //! Initializes <this> by default parameters. Makes <this> VOID.
168 //! Extends this to the given value (in both side)
169 void Enlarge(const Standard_Real theDelta)
180 //! Returns the copy of <*this> shifted by theVal
181 Bnd_Range Shifted(const Standard_Real theVal) const
183 return !IsVoid() ? Bnd_Range(myFirst + theVal, myLast + theVal) : Bnd_Range();
186 //! Shifts <*this> by theVal
187 void Shift(const Standard_Real theVal)
196 //! Trims the First value in range by the given lower limit.
197 //! Marks range as Void if the given Lower value is greater than range Max.
198 void TrimFrom (const Standard_Real theValLower)
202 myFirst = Max (myFirst, theValLower);
206 //! Trim the Last value in range by the given Upper limit.
207 //! Marks range as Void if the given Upper value is smaller than range Max.
208 void TrimTo (const Standard_Real theValUpper)
212 myLast = Min (myLast, theValUpper);
216 //! Returns True if the value is out of this range.
217 Standard_Boolean IsOut (Standard_Real theValue) const
220 || theValue < myFirst
221 || theValue > myLast;
224 //! Returns True if the given range is out of this range.
225 Standard_Boolean IsOut (const Bnd_Range& theRange) const
229 || theRange.myLast < myFirst
230 || theRange.myFirst > myLast;
233 //! Returns TRUE if theOther is equal to <*this>
234 Standard_Boolean operator==(const Bnd_Range& theOther) const
236 return ((myFirst == theOther.myFirst) && (myLast == theOther.myLast));
241 Standard_Real myFirst; //!< Start of range
242 Standard_Real myLast; //!< End of range