interval.h 14.8 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
// 
// ================================================================================================

25
26
27
28
#ifndef INTERVAL_H__
#define INTERVAL_H__

#include "tgt/tgt_math.h"
29
#include "tgt/vector.h"
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
#include <limits>

namespace campvis {

    /**
     * Templated class for handling intervals of numeric types.
     * Offers support for common interval arithmetic.
     * \tparam  T   Numeric type of the interval.
     * \todo    Thread-safety - use atomics!
     */
    template<typename T>
    class Interval {
    public:
        /**
         * Default constructor, creates the empty interval (0,0).
         */
        Interval();

        /**
         * Copy constructor.
         * \param   src     Source interval
         */
        Interval(const Interval<T>& src);

        /**
         * Constructor, creates a new Interval<T>.
         * \param   left        Left interval border
         * \param   right       Right interval border
         * \param   leftOpen    Flag whether left interval border is open, defaults to false
         * \param   rightOpen   Flag whether right interval border is open, defaults to false
         */
        Interval(T left, T right, bool leftOpen=false, bool rightOpen=false);

63
64
65
66
67
68
        /**
         * Creates a new closed Interval<T> with the given vec2 as values
         * \param   vec     Vector with interval borders
         */
        explicit Interval(const tgt::Vector2<T>& vec);

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
        /**
         * Destructor
         */
        virtual ~Interval();


        /**
         * Returns left interval border.
         * \return  _left
         */
        inline const T& getLeft() const;

        /**
         * Returns right interval border.
         * \return  _right
         */
        inline const T& getRight() const;

        /**
         * Returns whether left interval border is open.
         * \return  _leftOpen
         */
        inline bool getLeftOpen() const;

        /**
         * Returns whether right interval border is open.
         * \return  _rightOpen
         */
        inline bool getRightOpen() const;


        /**
         * Returns whether the interval is empty.
         * \return  True if the interval is empty.
         */
        bool empty() const;

        /**
         * Returns the size of the interval 
         * \return  (_right - _left)
         */
        T size() const;


        /**
         * Checks whether the interval contains the value \a value.
         * \param   value   Value to check.
         * \return  True if \a value is inside this interval.
         */
        bool contains(const T& value) const;

        /**
         * Checks whether the interval contains the entire interval \a interval.
         * \param   value   Interval to check.
         * \return  True if \a interval is entirely inside this interval.
         */
        bool contains(const Interval<T>& interval) const;


        /**
         * Intersects this interval with \a rhs.
         * \param   rhs The other interval
         */
        void intersectWith(const Interval<T>& rhs);

        /**
         * Builds the union of this interval and \a rhs.
         * \param   rhs The other interval
         */
        void unionWith(const Interval<T>& rhs);

        /**
         * Sets all infinite, NaN or std::numeric_limits<T>::max() values to according values in rhs
         * \param   rhs The other interval
         */
        void clampInfinitesTo(const Interval<T>& rhs);


        /**
         * Enlarges the current interval by factor \a factor.
         * \param   factor  Enlargement factor
         */
        void enlarge(const T& factor);

        /**
         * Enlarges current interval as much as necessary until it contains \a value.
         * \param   value   Value the interval shall contain
         */
        void nibble(const T& value);


        /**
         * Clamps \a value so that it fits into this interval.
         * \param   value   Value to clamp.
         * \return  \a value clamped to this interval.
         */
        T clampValue(const T& value) const;


        /**
         * Returns a string representation of this interval.
         * \return  (_left, _right) or [_left, _right]
         */
        std::string toString() const;

        
        /**
         * Returns an interval whose borders are incremented by \a rhs.
         * \note    The interval size does not change!
         * \param   rhs The amount to add to the borders
         */
        Interval<T> operator+(const T& rhs) const;

        /**
         * Returns an interval whose borders are decremented by \a rhs.
         * \note    The interval size does not change!
         * \param   rhs The amount to substract from the borders
         */
        Interval<T> operator-(const T& rhs) const;

        /**
         * Returns an interval whose borders are multiplied by \a rhs.
         * \param   rhs The factor to multiply to the borders
         */
        Interval<T> operator*(const T& rhs) const;

        /**
         * Returns an interval whose borders are divided by \a rhs.
         * \param   rhs The divider to apply to the borders
         */
        Interval<T> operator/(const T& rhs) const;


        /**
         * Returns whether this interval is equal to \a rhs.
         * \param   rhs Interval to compare
         * \return  True if borders and open flags match.
         */
        bool operator==(const Interval<T>& rhs) const;

        /**
         * Returns whether this interval is not equal to \a rhs.
         * \param   rhs Interval to compare
         * \return  True if at least one of the borders or open flags does not match.
         */
        bool operator!=(const Interval<T>& rhs) const;

    private:
        T _left;                ///< left interval border
        T _right;               ///< right interval border

        bool _leftOpen;         ///< flag whether left interval border is open
        bool _rightOpen;        ///< flag whether right interval border is open

    };


    // ---------------------------------------- implementation ----------------------------------------

    template<typename T>
    Interval<T>::Interval()
        : _left(0)
        , _right(0)
        , _leftOpen(true)
        , _rightOpen(true)
    {}

    template<typename T>
    Interval<T>::Interval(const Interval<T>& src)
        : _left(src._left)
        , _right(src._right)
        , _leftOpen(src._leftOpen)
        , _rightOpen(src._rightOpen)
    {}


    template<typename T>
    Interval<T>::Interval(T left, T right, bool leftOpen, bool rightOpen)
        : _left(left)
        , _right(right)
        , _leftOpen(leftOpen)
        , _rightOpen(rightOpen)
    {}

253
254
255
256
257
258
259
260
261
262
263
    template<typename T>
    Interval<T>::Interval(const tgt::Vector2<T>& vec)
        : _left(vec.x)
        , _right(vec.y)
        , _leftOpen(false)
        , _rightOpen(false)
    {
        if (_left > _right)
            std::swap(_left, _right);
    }

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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
    template<typename T>
    Interval<T>::~Interval() {
    }

    template<typename T>
    const T& Interval<T>::getLeft() const {
        return _left;
    }

    template<typename T>
    const T& Interval<T>::getRight() const {
        return _right;
    }

    template<typename T>
    bool Interval<T>::getLeftOpen() const {
        return _leftOpen;
    }

    template<typename T>
    bool Interval<T>::getRightOpen() const {
        return _rightOpen;
    }

    template<typename T>
    T Interval<T>::size() const {
        return _right - _left;
    }

    template<typename T>
    bool Interval<T>::empty() const {
        if (_left > _right)
            return true;
        if (_left == _right)
            return (_leftOpen || _rightOpen);
        return false;
    }

    template<typename T>
    bool Interval<T>::contains(const T& value) const {
        if (tgt::isNaN(value))
            return false;
        if ((value < _left) || (_leftOpen && value == _left))
            return false;
        if ((value > _right) || (_rightOpen && value == _right))
            return false;
        return true;
    }

    template<typename T>
    bool Interval<T>::contains(const Interval<T>& rhs) const {
        if ((_left > rhs._left) || ((_leftOpen || rhs._leftOpen) && _left == rhs._left))
            return false;
        if ((_right < rhs._right) || ((_rightOpen || rhs._rightOpen) && _right == rhs._right))
            return false;
        return true;
    }

    template<typename T>
    void Interval<T>::intersectWith(const Interval<T>& rhs) {
        if (rhs.empty())
            return;
        if (empty()) {
            _left = rhs._left;
            _leftOpen = rhs._leftOpen;
            _right = rhs._right;
            _rightOpen = rhs._rightOpen;
            return;
        }

        if (_left < rhs._left) {
            _left = rhs._left;
            _leftOpen = rhs._leftOpen;
        }
        else if (_left == rhs._left) {
            _leftOpen = _leftOpen || rhs._leftOpen;
        }
        else {
            // do nothing, keep values
        }

        if (_right > rhs._right) {
            _right = rhs._right;
            _rightOpen = rhs._rightOpen;
        }
        else if (_right == rhs._right) {
            _rightOpen = _rightOpen || rhs._rightOpen;
        }
        else {
            // do nothing, keep values
        }
    }

    template<typename T>
    void Interval<T>::unionWith(const Interval<T>& rhs) {
        // check if one of the intervals is empty
        if (empty()) {
            _left = rhs._left;
            _leftOpen = rhs._leftOpen;
            _right = rhs._right;
            _rightOpen = rhs._rightOpen;
            return;
        }
        if (rhs.empty())
            return;

        if (_left < rhs._left) {
            // do nothing, keep values
        }
        else if (_left == rhs._left) {
            _leftOpen = _leftOpen && rhs._leftOpen;
        }
        else {
            _left = rhs._left;
            _leftOpen = rhs._leftOpen;
        }

        if (_right > rhs._right) {
            // do nothing, keep values
        }
        else if (_right == rhs._right) {
            _rightOpen = _rightOpen && rhs._rightOpen;
        }
        else {
            _right = rhs._right;
            _rightOpen = rhs._rightOpen;
        }
    }

    template<typename T>
    void Interval<T>::clampInfinitesTo(const Interval<T>& rhs) {
        if (rhs.empty())
            return;
        if (empty()) {
            _left = rhs._left;
            _leftOpen = rhs._leftOpen;
            _right = rhs._right;
            _rightOpen = rhs._rightOpen;
            return;
        }

        if (tgt::isNaN(_left) || _left == std::numeric_limits<T>::infinity() || _left == -std::numeric_limits<T>::max()) {
            _left = rhs._left;
            _leftOpen = rhs._leftOpen;
        }
        if (tgt::isNaN(_right) || _right == std::numeric_limits<T>::infinity() || _right == std::numeric_limits<T>::max()) {
            _right = rhs._right;
            _rightOpen = rhs._rightOpen;
        }
    }

    template<typename T>
    Interval<T> Interval<T>::operator+(const T& rhs) const {
        return Interval<T>(_left+rhs, _right+rhs, _leftOpen, _rightOpen);
    }

    template<typename T>
    Interval<T> Interval<T>::operator-(const T& rhs) const {
        return Interval<T>(_left-rhs, _right-rhs, _leftOpen, _rightOpen);
    }

    template<typename T>
    Interval<T> Interval<T>::operator*(const T& rhs) const {
        return Interval<T>(_left*rhs, _right*rhs, _leftOpen, _rightOpen);
    }

    template<typename T>
    Interval<T> Interval<T>::operator/(const T& rhs) const {
        return Interval<T>(_left/rhs, _right/rhs, _leftOpen, _rightOpen);
    }

    template<typename T>
    void Interval<T>::enlarge(const T& factor) {
        T length = _right-_left;
        T offset = length*(factor-1.0)/2.0;
        _left -= offset;
        _right += offset;
    }

    template<typename T>
    void Interval<T>::nibble(const T& value) {
        if (empty()) {
            _left = value;
            _right = value;
            _leftOpen = false;
            _rightOpen = false;
        }
        else if (_leftOpen && _left == value)
            _leftOpen = false;
        else if (_rightOpen && _right == value)
            _rightOpen = false;
        else if (_left > value) {
            _left = value;
            _leftOpen = false;
        }
        else if (_right < value) {
            _right = value;
            _rightOpen = false;
        }
    }

    template<typename T>
    T Interval<T>::clampValue(const T& value) const {
        if (value < _left)
            return _left;
        else if (value > _right)
            return _right;
        else // contains(value)
            return value;
    }

    template<typename T>
    std::string Interval<T>::toString() const {
        std::stringstream result;
        result << (_leftOpen ? "(" : "[") << _left << ", " << _right << (_rightOpen ? ")" : "]");
        return result.str();
    }

    template<typename T>
    bool Interval<T>::operator==(const Interval<T>& rhs) const {
        return (_left == rhs._left
             && _right == rhs._right
             && _leftOpen == rhs._leftOpen
             && _rightOpen == rhs._rightOpen);
    }

    template<typename T>
    bool Interval<T>::operator!=(const Interval<T>& rhs) const {
        return (_left != rhs._left
             || _right != rhs._right
             || _leftOpen != rhs._leftOpen
             || _rightOpen != rhs._rightOpen);
    }

}

#endif // INTERVAL_H__