source: ntrip/trunk/BNC/qwt/qwt_interval.cpp@ 9045

Last change on this file since 9045 was 8127, checked in by stoecker, 8 years ago

update qwt and qwtpolar, many QT5 fixes (unfinished)

File size: 8.2 KB
Line 
1/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_interval.h"
11#include "qwt_math.h"
12#include <qalgorithms.h>
13
14/*!
15 \brief Normalize the limits of the interval
16
17 If maxValue() < minValue() the limits will be inverted.
18 \return Normalized interval
19
20 \sa isValid(), inverted()
21*/
22QwtInterval QwtInterval::normalized() const
23{
24 if ( d_minValue > d_maxValue )
25 {
26 return inverted();
27 }
28 if ( d_minValue == d_maxValue && d_borderFlags == ExcludeMinimum )
29 {
30 return inverted();
31 }
32
33 return *this;
34}
35
36/*!
37 Invert the limits of the interval
38 \return Inverted interval
39 \sa normalized()
40*/
41QwtInterval QwtInterval::inverted() const
42{
43 BorderFlags borderFlags = IncludeBorders;
44 if ( d_borderFlags & ExcludeMinimum )
45 borderFlags |= ExcludeMaximum;
46 if ( d_borderFlags & ExcludeMaximum )
47 borderFlags |= ExcludeMinimum;
48
49 return QwtInterval( d_maxValue, d_minValue, borderFlags );
50}
51
52/*!
53 Test if a value is inside an interval
54
55 \param value Value
56 \return true, if value >= minValue() && value <= maxValue()
57*/
58bool QwtInterval::contains( double value ) const
59{
60 if ( !isValid() )
61 return false;
62
63 if ( value < d_minValue || value > d_maxValue )
64 return false;
65
66 if ( value == d_minValue && d_borderFlags & ExcludeMinimum )
67 return false;
68
69 if ( value == d_maxValue && d_borderFlags & ExcludeMaximum )
70 return false;
71
72 return true;
73}
74
75//! Unite 2 intervals
76QwtInterval QwtInterval::unite( const QwtInterval &other ) const
77{
78 /*
79 If one of the intervals is invalid return the other one.
80 If both are invalid return an invalid default interval
81 */
82 if ( !isValid() )
83 {
84 if ( !other.isValid() )
85 return QwtInterval();
86 else
87 return other;
88 }
89 if ( !other.isValid() )
90 return *this;
91
92 QwtInterval united;
93 BorderFlags flags = IncludeBorders;
94
95 // minimum
96 if ( d_minValue < other.minValue() )
97 {
98 united.setMinValue( d_minValue );
99 flags &= d_borderFlags & ExcludeMinimum;
100 }
101 else if ( other.minValue() < d_minValue )
102 {
103 united.setMinValue( other.minValue() );
104 flags &= other.borderFlags() & ExcludeMinimum;
105 }
106 else // d_minValue == other.minValue()
107 {
108 united.setMinValue( d_minValue );
109 flags &= ( d_borderFlags & other.borderFlags() ) & ExcludeMinimum;
110 }
111
112 // maximum
113 if ( d_maxValue > other.maxValue() )
114 {
115 united.setMaxValue( d_maxValue );
116 flags &= d_borderFlags & ExcludeMaximum;
117 }
118 else if ( other.maxValue() > d_maxValue )
119 {
120 united.setMaxValue( other.maxValue() );
121 flags &= other.borderFlags() & ExcludeMaximum;
122 }
123 else // d_maxValue == other.maxValue() )
124 {
125 united.setMaxValue( d_maxValue );
126 flags &= d_borderFlags & other.borderFlags() & ExcludeMaximum;
127 }
128
129 united.setBorderFlags( flags );
130 return united;
131}
132
133/*!
134 \brief Intersect 2 intervals
135
136 \param other Interval to be intersect with
137 \return Intersection
138 */
139QwtInterval QwtInterval::intersect( const QwtInterval &other ) const
140{
141 if ( !other.isValid() || !isValid() )
142 return QwtInterval();
143
144 QwtInterval i1 = *this;
145 QwtInterval i2 = other;
146
147 // swap i1/i2, so that the minimum of i1
148 // is smaller then the minimum of i2
149
150 if ( i1.minValue() > i2.minValue() )
151 {
152 qSwap( i1, i2 );
153 }
154 else if ( i1.minValue() == i2.minValue() )
155 {
156 if ( i1.borderFlags() & ExcludeMinimum )
157 qSwap( i1, i2 );
158 }
159
160 if ( i1.maxValue() < i2.minValue() )
161 {
162 return QwtInterval();
163 }
164
165 if ( i1.maxValue() == i2.minValue() )
166 {
167 if ( i1.borderFlags() & ExcludeMaximum ||
168 i2.borderFlags() & ExcludeMinimum )
169 {
170 return QwtInterval();
171 }
172 }
173
174 QwtInterval intersected;
175 BorderFlags flags = IncludeBorders;
176
177 intersected.setMinValue( i2.minValue() );
178 flags |= i2.borderFlags() & ExcludeMinimum;
179
180 if ( i1.maxValue() < i2.maxValue() )
181 {
182 intersected.setMaxValue( i1.maxValue() );
183 flags |= i1.borderFlags() & ExcludeMaximum;
184 }
185 else if ( i2.maxValue() < i1.maxValue() )
186 {
187 intersected.setMaxValue( i2.maxValue() );
188 flags |= i2.borderFlags() & ExcludeMaximum;
189 }
190 else // i1.maxValue() == i2.maxValue()
191 {
192 intersected.setMaxValue( i1.maxValue() );
193 flags |= i1.borderFlags() & i2.borderFlags() & ExcludeMaximum;
194 }
195
196 intersected.setBorderFlags( flags );
197 return intersected;
198}
199
200/*!
201 \brief Unite this interval with the given interval.
202
203 \param other Interval to be united with
204 \return This interval
205 */
206QwtInterval& QwtInterval::operator|=( const QwtInterval &other )
207{
208 *this = *this | other;
209 return *this;
210}
211
212/*!
213 \brief Intersect this interval with the given interval.
214
215 \param other Interval to be intersected with
216 \return This interval
217 */
218QwtInterval& QwtInterval::operator&=( const QwtInterval &other )
219{
220 *this = *this & other;
221 return *this;
222}
223
224/*!
225 \brief Test if two intervals overlap
226
227 \param other Interval
228 \return True, when the intervals are intersecting
229*/
230bool QwtInterval::intersects( const QwtInterval &other ) const
231{
232 if ( !isValid() || !other.isValid() )
233 return false;
234
235 QwtInterval i1 = *this;
236 QwtInterval i2 = other;
237
238 // swap i1/i2, so that the minimum of i1
239 // is smaller then the minimum of i2
240
241 if ( i1.minValue() > i2.minValue() )
242 {
243 qSwap( i1, i2 );
244 }
245 else if ( i1.minValue() == i2.minValue() &&
246 i1.borderFlags() & ExcludeMinimum )
247 {
248 qSwap( i1, i2 );
249 }
250
251 if ( i1.maxValue() > i2.minValue() )
252 {
253 return true;
254 }
255 if ( i1.maxValue() == i2.minValue() )
256 {
257 return !( ( i1.borderFlags() & ExcludeMaximum ) ||
258 ( i2.borderFlags() & ExcludeMinimum ) );
259 }
260 return false;
261}
262
263/*!
264 Adjust the limit that is closer to value, so that value becomes
265 the center of the interval.
266
267 \param value Center
268 \return Interval with value as center
269*/
270QwtInterval QwtInterval::symmetrize( double value ) const
271{
272 if ( !isValid() )
273 return *this;
274
275 const double delta =
276 qMax( qAbs( value - d_maxValue ), qAbs( value - d_minValue ) );
277
278 return QwtInterval( value - delta, value + delta );
279}
280
281/*!
282 Limit the interval, keeping the border modes
283
284 \param lowerBound Lower limit
285 \param upperBound Upper limit
286
287 \return Limited interval
288*/
289QwtInterval QwtInterval::limited( double lowerBound, double upperBound ) const
290{
291 if ( !isValid() || lowerBound > upperBound )
292 return QwtInterval();
293
294 double minValue = qMax( d_minValue, lowerBound );
295 minValue = qMin( minValue, upperBound );
296
297 double maxValue = qMax( d_maxValue, lowerBound );
298 maxValue = qMin( maxValue, upperBound );
299
300 return QwtInterval( minValue, maxValue, d_borderFlags );
301}
302
303/*!
304 \brief Extend the interval
305
306 If value is below minValue(), value becomes the lower limit.
307 If value is above maxValue(), value becomes the upper limit.
308
309 extend() has no effect for invalid intervals
310
311 \param value Value
312 \return extended interval
313
314 \sa isValid()
315*/
316QwtInterval QwtInterval::extend( double value ) const
317{
318 if ( !isValid() )
319 return *this;
320
321 return QwtInterval( qMin( value, d_minValue ),
322 qMax( value, d_maxValue ), d_borderFlags );
323}
324
325/*!
326 Extend an interval
327
328 \param value Value
329 \return Reference of the extended interval
330
331 \sa extend()
332*/
333QwtInterval& QwtInterval::operator|=( double value )
334{
335 *this = *this | value;
336 return *this;
337}
338
339#ifndef QT_NO_DEBUG_STREAM
340
341QDebug operator<<( QDebug debug, const QwtInterval &interval )
342{
343 const int flags = interval.borderFlags();
344
345 debug.nospace() << "QwtInterval("
346 << ( ( flags & QwtInterval::ExcludeMinimum ) ? "]" : "[" )
347 << interval.minValue() << "," << interval.maxValue()
348 << ( ( flags & QwtInterval::ExcludeMaximum ) ? "[" : "]" )
349 << ")";
350
351 return debug.space();
352}
353
354#endif
Note: See TracBrowser for help on using the repository browser.