HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
multiInterval.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_GF_MULTI_INTERVAL_H
25 #define PXR_BASE_GF_MULTI_INTERVAL_H
26 
27 /// \file gf/multiInterval.h
28 /// \ingroup group_gf_BasicMath
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/gf/interval.h"
32 #include "pxr/base/gf/api.h"
33 
34 #include <iosfwd>
35 #include <set>
36 #include <vector>
37 
39 
40 /// \class GfMultiInterval
41 /// \ingroup group_gf_BasicMath
42 ///
43 /// GfMultiInterval represents a subset of the real number line as an
44 /// ordered set of non-intersecting GfIntervals.
45 ///
47 {
48 public:
49  typedef std::set<GfInterval> Set;
50  typedef Set::const_iterator const_iterator;
51  typedef Set::const_iterator iterator;
52 
53  /// \name Constructors
54  /// @{
55  /// Constructs an empty multi-interval.
56  GfMultiInterval() = default;
57  /// Constructs an multi-interval with the single given interval.
58  GF_API explicit GfMultiInterval(const GfInterval &i);
59  /// Constructs an multi-interval containing the given input intervals.
60  GF_API explicit GfMultiInterval(const std::vector<GfInterval> &intervals);
61  /// @}
62 
63  GF_API bool operator==(const GfMultiInterval &that) const { return _set == that._set; }
64  GF_API bool operator!=(const GfMultiInterval &that) const { return !(*this == that); }
65  GF_API bool operator<(const GfMultiInterval &that) const { return _set < that._set; }
66  GF_API bool operator>=(const GfMultiInterval &that) const { return !(*this < that); }
67  GF_API bool operator>(const GfMultiInterval &that) const { return (that < *this); }
68  GF_API bool operator<=(const GfMultiInterval &that) const { return !(that < *this); }
69 
70 
71  /// Hash value.
72  /// Just a basic hash function, not particularly high quality.
73  GF_API size_t Hash() const;
74 
75  friend inline size_t hash_value(const GfMultiInterval &mi) {
76  return mi.Hash();
77  }
78 
79  /// \name Accessors
80  /// @{
81 
82  /// Returns true if the multi-interval is empty.
83  GF_API bool IsEmpty() const { return _set.empty(); }
84 
85  /// Returns the number of intervals in the set.
86  GF_API size_t GetSize() const { return _set.size(); }
87 
88  /// Returns an interval bounding the entire multi-interval.
89  /// Returns an empty interval if the multi-interval is empty.
90  GF_API GfInterval GetBounds() const;
91 
92  /// Returns true if the multi-interval contains the given value.
93  GF_API bool Contains(double d) const;
94 
95  /// Returns true if the multi-interval contains the given interval.
96  GF_API bool Contains(const GfInterval & i) const;
97 
98  /// Returns true if the multi-interval contains all the intervals in the
99  /// given multi-interval.
100  GF_API bool Contains(const GfMultiInterval & s) const;
101 
102  /// @}
103 
104  /// \name Mutation
105  /// @{
106 
107  /// Clear the multi-interval.
108  GF_API void Clear() { _set.clear(); }
109 
110  /// Add the given interval to the multi-interval.
111  GF_API void Add( const GfInterval & i );
112  /// Add the given multi-interval to the multi-interval.
113  /// Sets this object to the union of the two sets.
114  GF_API void Add( const GfMultiInterval &s );
115 
116  /// Uses the given interval to extend the multi-interval in
117  /// the interval arithmetic sense.
118  GF_API void ArithmeticAdd( const GfInterval &i );
119 
120  /// Remove the given interval from this multi-interval.
121  GF_API void Remove( const GfInterval & i );
122  /// Remove the given multi-interval from this multi-interval.
123  GF_API void Remove( const GfMultiInterval &s );
124 
125  GF_API void Intersect( const GfInterval & i );
126  GF_API void Intersect( const GfMultiInterval &s );
127 
128  /// Return the complement of this set.
130 
131  /// @}
132 
133  /// \name Iteration
134  /// Only const iterators are returned. To maintain the invariants of
135  /// the multi-interval, changes must be made via the public mutation API.
136  /// @{
137 
138  GF_API const_iterator begin() const { return _set.begin(); }
139  GF_API const_iterator end() const { return _set.end(); }
140 
141  /// Returns an iterator identifying the first (lowest) interval whose
142  /// minimum value is >= x. If no such interval exists, returns end().
143  GF_API const_iterator lower_bound( double x ) const;
144 
145  /// Returns an iterator identifying the first (lowest) interval whose
146  /// minimum value is > x. If no such interval exists, returns end().
147  GF_API const_iterator upper_bound( double x ) const;
148 
149  /// Returns an iterator identifying the first (loest) interval whose
150  /// minimum value is > x. If no such interval exists, returns end().
152 
153  /// Returns an iterator identifying the last (highest) interval whose
154  /// maximum value is < x. If no such interval exists, returns end().
156 
157  /// Returns an iterator identifying the interval that contains x. If
158  /// no interval contains x, then it returns end()
160 
161  /// @}
162 
163  /// Returns the full interval (-inf, inf).
166  }
167 
168  /// Swap two multi-intervals
169  void swap(GfMultiInterval &other) { _set.swap(other._set); }
170 
171 private:
172  void _AssertInvariants() const;
173 
174  Set _set;
175 };
176 
177 /// Output a GfMultiInterval
178 /// \ingroup group_gf_DebuggingOutput
179 GF_API std::ostream & operator<<(std::ostream &out, const GfMultiInterval &s);
180 
182 
183 #endif // PXR_BASE_GF_MULTI_INTERVAL_H
GF_API bool IsEmpty() const
Returns true if the multi-interval is empty.
Definition: multiInterval.h:83
GF_API bool Contains(double d) const
Returns true if the multi-interval contains the given value.
GF_API const_iterator lower_bound(double x) const
void swap(GfMultiInterval &other)
Swap two multi-intervals.
GF_API bool operator==(const GfMultiInterval &that) const
Definition: multiInterval.h:63
GF_API bool operator<(const GfMultiInterval &that) const
Definition: multiInterval.h:65
GF_API const_iterator GetPriorNonContainingInterval(double x) const
static GfInterval GetFullInterval()
Returns the full interval (-inf, inf).
Definition: interval.h:346
GF_API bool operator<=(const GfMultiInterval &that) const
Definition: multiInterval.h:68
GF_API void Add(const GfInterval &i)
Add the given interval to the multi-interval.
GF_API bool operator>=(const GfMultiInterval &that) const
Definition: multiInterval.h:66
GF_API const_iterator upper_bound(double x) const
GF_API GfMultiInterval GetComplement() const
Return the complement of this set.
GF_API bool operator!=(const GfMultiInterval &that) const
Definition: multiInterval.h:64
GLint GLenum GLint x
Definition: glcorearb.h:408
static GfMultiInterval GetFullInterval()
Returns the full interval (-inf, inf).
GF_API const_iterator GetNextNonContainingInterval(double x) const
GF_API size_t GetSize() const
Returns the number of intervals in the set.
Definition: multiInterval.h:86
GfMultiInterval()=default
Constructs an multi-interval with the single given interval.
Set::const_iterator const_iterator
Definition: multiInterval.h:50
GF_API void ArithmeticAdd(const GfInterval &i)
GF_API GfInterval GetBounds() const
GF_API std::ostream & operator<<(std::ostream &out, const GfMultiInterval &s)
GF_API const_iterator end() const
friend size_t hash_value(const GfMultiInterval &mi)
Definition: multiInterval.h:75
Set::const_iterator iterator
Definition: multiInterval.h:51
GF_API bool operator>(const GfMultiInterval &that) const
Definition: multiInterval.h:67
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
GF_API const_iterator GetContainingInterval(double x) const
GF_API void Clear()
Clear the multi-interval.
std::set< GfInterval > Set
Definition: multiInterval.h:49
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GF_API void Intersect(const GfInterval &i)
Clear the multi-interval.
GF_API void Remove(const GfInterval &i)
Remove the given interval from this multi-interval.
GF_API size_t Hash() const
GLdouble s
Definition: glew.h:1395
#define GF_API
Definition: api.h:40
GF_API const_iterator begin() const