-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathinterval.py
More file actions
399 lines (319 loc) · 12.2 KB
/
interval.py
File metadata and controls
399 lines (319 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the Qwt License
# Copyright (c) 2002 Uwe Rathmann, for the original C++ code
# Copyright (c) 2015 Pierre Raybaut, for the Python translation/optimization
# (see LICENSE file for more details)
"""
QwtInterval
-----------
.. autoclass:: QwtInterval
:members:
"""
class QwtInterval(object):
"""
A class representing an interval
The interval is represented by 2 doubles, the lower and the upper limit.
.. py:class:: QwtInterval(minValue=0., maxValue=-1., borderFlags=None)
Build an interval with from min/max values
:param float minValue: Minimum value
:param float maxValue: Maximum value
:param int borderFlags: Include/Exclude borders
"""
# enum BorderFlag
IncludeBorders = 0x00
ExcludeMinimum = 0x01
ExcludeMaximum = 0x02
ExcludeBorders = ExcludeMinimum | ExcludeMaximum
def __init__(self, minValue=0.0, maxValue=-1.0, borderFlags=None):
assert not isinstance(minValue, QwtInterval)
assert not isinstance(maxValue, QwtInterval)
self.__minValue = None
self.__maxValue = None
self.__borderFlags = None
self.setInterval(minValue, maxValue, borderFlags)
def setInterval(self, minValue, maxValue, borderFlags=None):
"""
Assign the limits of the interval
:param float minValue: Minimum value
:param float maxValue: Maximum value
:param int borderFlags: Include/Exclude borders
"""
self.__minValue = float(minValue) # avoid overflows with NumPy scalars
self.__maxValue = float(maxValue) # avoid overflows with NumPy scalars
if borderFlags is None:
self.__borderFlags = self.IncludeBorders
else:
self.__borderFlags = borderFlags
def setBorderFlags(self, borderFlags):
"""
Change the border flags
:param int borderFlags: Include/Exclude borders
.. seealso::
:py:meth:`borderFlags()`
"""
self.__borderFlags = borderFlags
def borderFlags(self):
"""
:return: Border flags
.. seealso::
:py:meth:`setBorderFlags()`
"""
return self.__borderFlags
def setMinValue(self, minValue):
"""
Assign the lower limit of the interval
:param float minValue: Minimum value
"""
self.__minValue = float(minValue) # avoid overflows with NumPy scalars
def setMaxValue(self, maxValue):
"""
Assign the upper limit of the interval
:param float maxValue: Maximum value
"""
self.__maxValue = float(maxValue) # avoid overflows with NumPy scalars
def minValue(self):
"""
:return: Lower limit of the interval
"""
return self.__minValue
def maxValue(self):
"""
:return: Upper limit of the interval
"""
return self.__maxValue
def isValid(self):
"""
A interval is valid when minValue() <= maxValue().
In case of `QwtInterval.ExcludeBorders` it is true
when minValue() < maxValue()
:return: True, when the interval is valid
"""
if (self.__borderFlags & self.ExcludeBorders) == 0:
return self.__minValue <= self.__maxValue
else:
return self.__minValue < self.__maxValue
def width(self):
"""
The width of invalid intervals is 0.0, otherwise the result is
maxValue() - minValue().
:return: the width of an interval
"""
if self.isValid():
return self.__maxValue - self.__minValue
else:
return 0.0
def __and__(self, other):
return self.intersect(other)
def __iand__(self, other):
self = self & other
return self
def __or__(self, other):
if isinstance(other, QwtInterval):
return self.unite(other)
else:
return self.extend(other)
def __ior__(self, other):
self = self | other
return self
def __eq__(self, other):
return (
self.__minValue == other.__minValue
and self.__maxValue == other.__maxValue
and self.__borderFlags == other.__borderFlags
)
def __ne__(self, other):
return not self.__eq__(other)
def isNull(self):
"""
:return: true, if isValid() && (minValue() >= maxValue())
"""
return self.isValid() and self.__minValue >= self.__maxValue
def invalidate(self):
"""
The limits are set to interval [0.0, -1.0]
.. seealso::
:py:meth:`isValid()`
"""
self.__minValue = 0.0
self.__maxValue = -1.0
def normalized(self):
"""
Normalize the limits of the interval
If maxValue() < minValue() the limits will be inverted.
:return: Normalized interval
.. seealso::
:py:meth:`isValid()`, :py:meth:`inverted()`
"""
if self.__minValue > self.__maxValue:
return self.inverted()
elif (
self.__minValue == self.__maxValue
and self.__borderFlags == self.ExcludeMinimum
):
return self.inverted()
else:
return self
def inverted(self):
"""
Invert the limits of the interval
:return: Inverted interval
.. seealso::
:py:meth:`normalized()`
"""
borderFlags = self.IncludeBorders
if self.__borderFlags & self.ExcludeMinimum:
borderFlags |= self.ExcludeMaximum
if self.__borderFlags & self.ExcludeMaximum:
borderFlags |= self.ExcludeMinimum
return QwtInterval(self.__maxValue, self.__minValue, borderFlags)
def contains(self, value):
"""
Test if a value is inside an interval
:param float value: Value
:return: true, if value >= minValue() && value <= maxValue()
"""
if not self.isValid():
return False
elif value < self.__minValue or value > self.__maxValue:
return False
elif value == self.__minValue and self.__borderFlags & self.ExcludeMinimum:
return False
elif value == self.__maxValue and self.__borderFlags & self.ExcludeMaximum:
return False
else:
return True
def unite(self, other):
"""
Unite two intervals
:param qwt.interval.QwtInterval other: other interval to united with
:return: united interval
"""
if not self.isValid():
if not other.isValid():
return QwtInterval()
else:
return other
elif not other.isValid():
return self
united = QwtInterval()
flags = self.IncludeBorders
# minimum
if self.__minValue < other.minValue():
united.setMinValue(self.__minValue)
flags &= self.__borderFlags & self.ExcludeMinimum
elif other.minValue() < self.__minValue:
united.setMinValue(other.minValue())
flags &= other.borderFlags() & self.ExcludeMinimum
else:
united.setMinValue(self.__minValue)
flags &= (self.__borderFlags & other.borderFlags()) & self.ExcludeMinimum
# maximum
if self.__maxValue > other.maxValue():
united.setMaxValue(self.__maxValue)
flags &= self.__borderFlags & self.ExcludeMaximum
elif other.maxValue() > self.__maxValue:
united.setMaxValue(other.maxValue())
flags &= other.borderFlags() & self.ExcludeMaximum
else:
united.setMaxValue(self.__maxValue)
flags &= self.__borderFlags & other.borderFlags() & self.ExcludeMaximum
united.setBorderFlags(flags)
return united
def intersect(self, other):
"""
Intersect two intervals
:param qwt.interval.QwtInterval other: other interval to intersect with
:return: intersected interval
"""
if not other.isValid() or not self.isValid():
return QwtInterval()
i1 = self
i2 = other
if i1.minValue() > i2.minValue():
i1, i2 = i2, i1
elif i1.minValue() == i2.minValue():
if i1.borderFlags() & self.ExcludeMinimum:
i1, i2 = i2, i1
if i1.maxValue() < i2.maxValue():
return QwtInterval()
if i1.maxValue() == i2.minValue():
if (
i1.borderFlags() & self.ExcludeMaximum
or i2.borderFlags() & self.ExcludeMinimum
):
return QwtInterval()
intersected = QwtInterval()
flags = self.IncludeBorders
intersected.setMinValue(i2.minValue())
flags |= i2.borderFlags() & self.ExcludeMinimum
if i1.maxValue() < i2.maxValue():
intersected.setMaxValue(i1.maxValue())
flags |= i1.borderFlags() & self.ExcludeMaximum
elif i2.maxValue() < i1.maxValue():
intersected.setMaxValue(i2.maxValue())
flags |= i2.borderFlags() & self.ExcludeMaximum
else: # i1.maxValue() == i2.maxValue()
intersected.setMaxValue(i1.maxValue())
flags |= i1.borderFlags() & i2.borderFlags() & self.ExcludeMaximum
intersected.setBorderFlags(flags)
return intersected
def intersects(self, other):
"""
Test if two intervals overlap
:param qwt.interval.QwtInterval other: other interval
:return: True, when the intervals are intersecting
"""
if not other.isValid() or not self.isValid():
return False
i1 = self
i2 = other
if i1.minValue() > i2.minValue():
i1, i2 = i2, i1
elif i1.minValue() == i2.minValue() and i1.borderFlags() & self.ExcludeMinimum:
i1, i2 = i2, i1
if i1.maxValue() > i2.minValue():
return True
elif i1.maxValue() == i2.minValue():
return (
i1.borderFlags() & self.ExcludeMaximum
and i2.borderFlags() & self.ExcludeMinimum
)
return False
def symmetrize(self, value):
"""
Adjust the limit that is closer to value, so that value becomes
the center of the interval.
:param float value: Center
:return: Interval with value as center
"""
if not self.isValid():
return self
delta = max([abs(value - self.__maxValue), abs(value - self.__minValue)])
return QwtInterval(value - delta, value + delta)
def limited(self, lowerBound, upperBound):
"""
Limit the interval, keeping the border modes
:param float lowerBound: Lower limit
:param float upperBound: Upper limit
:return: Limited interval
"""
if not self.isValid() or lowerBound > upperBound:
return QwtInterval()
minValue = max([self.__minValue, lowerBound])
minValue = min([minValue, upperBound])
maxValue = max([self.__maxValue, lowerBound])
maxValue = min([maxValue, upperBound])
return QwtInterval(minValue, maxValue, self.__borderFlags)
def extend(self, value):
"""
Extend the interval
If value is below minValue(), value becomes the lower limit.
If value is above maxValue(), value becomes the upper limit.
extend() has no effect for invalid intervals
:param float value: Value
:return: extended interval
"""
if not self.isValid():
return self
return QwtInterval(min([value, self.__minValue]), max([value, self.__maxValue]))