0032751: Coding - get rid of unused headers [AppStd to BndLib]
[occt.git] / src / Bnd / Bnd_Range.hxx
CommitLineData
d30895f5 1// Created on: 2016-06-07
2// Created by: Nikolai BUKHALOV
3// Copyright (c) 2016 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
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.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#ifndef _Bnd_Range_HeaderFile
17#define _Bnd_Range_HeaderFile
18
d30895f5 19#include <Standard_ConstructionError.hxx>
20
261b7d9e 21#include <NCollection_List.hxx>
22
d30895f5 23//! This class describes a range in 1D space restricted
24//! by two real values.
25//! A range can be void indicating there is no point included in the range.
d30895f5 26class Bnd_Range
27{
28public:
29
30 //! Default constructor. Creates VOID range.
8662560e 31 Bnd_Range() : myFirst(0.0), myLast(-1.0) {}
d30895f5 32
33 //! Constructor. Never creates VOID range.
34 Bnd_Range(const Standard_Real theMin, const Standard_Real theMax) :
35 myFirst(theMin), myLast(theMax)
36 {
37 if(myLast < myFirst)
9775fa61 38 throw Standard_ConstructionError("Last < First");
8662560e 39 }
d30895f5 40
41 //! Replaces <this> with common-part of <this> and theOther
42 Standard_EXPORT void Common(const Bnd_Range& theOther);
261b7d9e 43
44 //! Joins *this and theOther to one interval.
45 //! Replaces *this to the result.
46 //! Returns false if the operation cannot be done (e.g.
47 //! input arguments are empty or separated).
8662560e 48 //! @sa use method ::Add() to merge two ranges unconditionally
261b7d9e 49 Standard_EXPORT Standard_Boolean Union(const Bnd_Range& theOther);
50
51 //! Splits <this> to several sub-ranges by theVal value
52 //! (e.g. range [3, 15] will be split by theVal==5 to the two
53 //! ranges: [3, 5] and [5, 15]). New ranges will be pushed to
54 //! theList (theList must be initialized correctly before
55 //! calling this method).
56 //! If thePeriod != 0.0 then at least one boundary of
57 //! new ranges (if <*this> intersects theVal+k*thePeriod) will be equal to
58 //! theVal+thePeriod*k, where k is an integer number (k = 0, +/-1, +/-2, ...).
59 //! (let thePeriod in above example be 4 ==> we will obtain
60 //! four ranges: [3, 5], [5, 9], [9, 13] and [13, 15].
61 Standard_EXPORT void Split(const Standard_Real theVal,
62 NCollection_List<Bnd_Range>& theList,
63 const Standard_Real thePeriod = 0.0) const;
64
65 //! Checks if <this> intersects values like
66 //! theVal+k*thePeriod, where k is an integer number (k = 0, +/-1, +/-2, ...).
67 //! Returns:
68 //! 0 - if <this> does not intersect the theVal+k*thePeriod.
69 //! 1 - if <this> intersects theVal+k*thePeriod.
70 //! 2 - if myFirst or/and myLast are equal to theVal+k*thePeriod.
71 //!
72 //! ATTENTION!!!
73 //! If (myFirst == myLast) then this function will return only either 0 or 2.
74 Standard_EXPORT Standard_Integer
75 IsIntersected(const Standard_Real theVal,
76 const Standard_Real thePeriod = 0.0) const;
d30895f5 77
78 //! Extends <this> to include theParameter
79 void Add(const Standard_Real theParameter)
80 {
81 if(IsVoid())
82 {
83 myFirst = myLast = theParameter;
84 return;
85 }
86
87 myFirst = Min(myFirst, theParameter);
88 myLast = Max(myLast, theParameter);
89 }
90
8662560e 91 //! Extends this range to include both ranges.
92 //! @sa use method ::Union() to check if two ranges overlap method merging
93 void Add (const Bnd_Range& theRange)
94 {
95 if (theRange.IsVoid())
96 {
97 return;
98 }
99 else if (IsVoid())
100 {
101 *this = theRange;
102 }
103 myFirst = Min(myFirst, theRange.myFirst);
104 myLast = Max(myLast, theRange.myLast);
105 }
106
d30895f5 107 //! Obtain MIN boundary of <this>.
108 //! If <this> is VOID the method returns false.
109 Standard_Boolean GetMin(Standard_Real& thePar) const
110 {
111 if(IsVoid())
112 {
113 return Standard_False;
114 }
115
116 thePar = myFirst;
117 return Standard_True;
118 }
119
120 //! Obtain MAX boundary of <this>.
121 //! If <this> is VOID the method returns false.
8662560e 122 Standard_Boolean GetMax(Standard_Real& thePar) const
d30895f5 123 {
124 if(IsVoid())
125 {
126 return Standard_False;
127 }
128
129 thePar = myLast;
130 return Standard_True;
131 }
132
261b7d9e 133 //! Obtain first and last boundary of <this>.
134 //! If <this> is VOID the method returns false.
135 Standard_Boolean GetBounds(Standard_Real& theFirstPar,
136 Standard_Real& theLastPar) const
137 {
138 if(IsVoid())
139 {
140 return Standard_False;
141 }
142
143 theFirstPar = myFirst;
144 theLastPar = myLast;
145 return Standard_True;
146 }
638ad7f3 147
148 //! Obtain theParameter satisfied to the equation
149 //! (theParameter-MIN)/(MAX-MIN) == theLambda.
150 //! * theLambda == 0 --> MIN boundary will be returned;
151 //! * theLambda == 0.5 --> Middle point will be returned;
152 //! * theLambda == 1 --> MAX boundary will be returned;
153 //! * theLambda < 0 --> the value less than MIN will be returned;
154 //! * theLambda > 1 --> the value greater than MAX will be returned.
155 //! If <this> is VOID the method returns false.
156 Standard_Boolean GetIntermediatePoint(const Standard_Real theLambda,
157 Standard_Real& theParameter) const
158 {
159 if (IsVoid())
160 {
161 return Standard_False;
162 }
163
164 theParameter = myFirst + theLambda*(myLast - myFirst);
165 return Standard_True;
166 }
261b7d9e 167
d30895f5 168 //! Returns range value (MAX-MIN). Returns negative value for VOID range.
169 Standard_Real Delta() const
170 {
171 return (myLast - myFirst);
172 }
173
174 //! Is <this> initialized.
175 Standard_Boolean IsVoid() const
176 {
177 return (myLast < myFirst);
178 }
179
180 //! Initializes <this> by default parameters. Makes <this> VOID.
181 void SetVoid()
182 {
183 myLast = -1.0;
184 myFirst = 0.0;
185 }
186
187 //! Extends this to the given value (in both side)
188 void Enlarge(const Standard_Real theDelta)
189 {
190 if (IsVoid())
191 {
192 return;
193 }
194
195 myFirst -= theDelta;
196 myLast += theDelta;
197 }
198
261b7d9e 199 //! Returns the copy of <*this> shifted by theVal
200 Bnd_Range Shifted(const Standard_Real theVal) const
201 {
8662560e 202 return !IsVoid() ? Bnd_Range(myFirst + theVal, myLast + theVal) : Bnd_Range();
261b7d9e 203 }
204
205 //! Shifts <*this> by theVal
206 void Shift(const Standard_Real theVal)
207 {
8662560e 208 if (!IsVoid())
209 {
210 myFirst += theVal;
211 myLast += theVal;
212 }
213 }
214
25c35042 215 //! Trims the First value in range by the given lower limit.
216 //! Marks range as Void if the given Lower value is greater than range Max.
217 void TrimFrom (const Standard_Real theValLower)
218 {
219 if (!IsVoid())
220 {
221 myFirst = Max (myFirst, theValLower);
222 }
223 }
224
225 //! Trim the Last value in range by the given Upper limit.
226 //! Marks range as Void if the given Upper value is smaller than range Max.
227 void TrimTo (const Standard_Real theValUpper)
228 {
229 if (!IsVoid())
230 {
231 myLast = Min (myLast, theValUpper);
232 }
233 }
234
8662560e 235 //! Returns True if the value is out of this range.
236 Standard_Boolean IsOut (Standard_Real theValue) const
237 {
238 return IsVoid()
239 || theValue < myFirst
240 || theValue > myLast;
241 }
242
243 //! Returns True if the given range is out of this range.
244 Standard_Boolean IsOut (const Bnd_Range& theRange) const
245 {
246 return IsVoid()
247 || theRange.IsVoid()
248 || theRange.myLast < myFirst
249 || theRange.myFirst > myLast;
261b7d9e 250 }
251
252 //! Returns TRUE if theOther is equal to <*this>
253 Standard_Boolean operator==(const Bnd_Range& theOther) const
254 {
255 return ((myFirst == theOther.myFirst) && (myLast == theOther.myLast));
256 }
257
0904aa63 258 //! Dumps the content of me into the stream
bc73b006 259 Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0904aa63 260
d30895f5 261private:
d30895f5 262
8662560e 263 Standard_Real myFirst; //!< Start of range
264 Standard_Real myLast; //!< End of range
265
d30895f5 266};
267
8662560e 268#endif