Line data Source code
1 :
2 : /* Copyright (c) 2006-2017, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #ifndef EQFABRIC_RANGE_H
19 : #define EQFABRIC_RANGE_H
20 :
21 : #include <algorithm> // min,max
22 : #include <eq/fabric/api.h>
23 : #include <iostream>
24 : #include <lunchbox/bitOperation.h>
25 :
26 : namespace eq
27 : {
28 : namespace fabric
29 : {
30 : /** A fractional database range with methods for manipulation. */
31 : class Range
32 : {
33 : public:
34 : /** @name Constructors */
35 : //@{
36 : /** Construct a new range covering all data. @version 1.0 */
37 20224 : Range()
38 20224 : : start(0.f)
39 20224 : , end(1.f)
40 : {
41 20224 : }
42 :
43 : /** Construct a new range with default values. @version 1.0 */
44 723 : Range(const float start_, const float end_)
45 723 : : start(start_)
46 723 : , end(end_)
47 : {
48 723 : }
49 : //@}
50 :
51 : /** @name Data Access */
52 : //@{
53 : /** @return true if the two ranges are identical. @version 1.0 */
54 18 : bool operator==(const Range& rhs) const
55 : {
56 18 : return start == rhs.start && end == rhs.end;
57 : }
58 :
59 : /** @return true if the two ranges are not identical. @version 1.0 */
60 2305 : bool operator!=(const Range& rhs) const
61 : {
62 2305 : return start != rhs.start || end != rhs.end;
63 : }
64 :
65 : /** @return the interval spanned by this range. @version 1.3 */
66 0 : float getSize() const { return end - start; }
67 : /** @internal Invalidate the database range. */
68 : void invalidate()
69 : {
70 : start = 0.f;
71 : end = 0.f;
72 : }
73 :
74 : /** @internal @return true if the database range is valid. */
75 2316 : bool isValid() const
76 : {
77 2316 : return (start >= 0.f && end <= 1.f && (end - start) >= 0.f);
78 : }
79 :
80 : /** @internal @return true if the database range covers some data. */
81 610 : bool hasData() const { return (end - start) > 0.f; }
82 : /** @internal Apply (accumulate) another database range. */
83 590 : void apply(const Range& rhs)
84 : {
85 590 : const float w = end - start;
86 590 : end = start + rhs.end * w;
87 590 : start += rhs.start * w;
88 590 : }
89 :
90 : /** Merge the two ranges (form the union). @version 1.11 */
91 4 : void merge(const Range& rhs)
92 : {
93 4 : start = std::min(start, rhs.start);
94 4 : end = std::max(end, rhs.end);
95 4 : }
96 :
97 : float start; //!< The start position
98 : float end; //!< The end position
99 :
100 : EQFABRIC_API static const Range ALL; //!< A full database range
101 : };
102 :
103 370 : inline std::ostream& operator<<(std::ostream& os, const Range& range)
104 : {
105 370 : os << "range [ " << range.start << " " << range.end << " ]";
106 370 : return os;
107 : }
108 : }
109 : }
110 :
111 : #endif // EQFABRIC_RANGE_H
|