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 GF_INTERVAL_SET_H
25 #define GF_INTERVAL_SET_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.
57  /// Constructs an multi-interval by copying the given set.
58  GF_API GfMultiInterval(const GfMultiInterval &s) : _set(s._set) {}
59  /// Constructs an multi-interval with the single given interval.
60  GF_API explicit GfMultiInterval(const GfInterval &i);
61  /// Constructs an multi-interval containing the given input intervals.
62  GF_API explicit GfMultiInterval(const std::vector<GfInterval> &intervals);
63  /// @}
64 
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 _set < that._set; }
68  GF_API bool operator>=(const GfMultiInterval &that) const { return !(*this < that); }
69  GF_API bool operator>(const GfMultiInterval &that) const { return (that < *this); }
70  GF_API bool operator<=(const GfMultiInterval &that) const { return !(that < *this); }
71 
72 
73  /// Hash value.
74  /// Just a basic hash function, not particularly high quality.
75  GF_API size_t Hash() const;
76 
77  friend inline size_t hash_value(const GfMultiInterval &mi) {
78  return mi.Hash();
79  }
80 
81  /// \name Accessors
82  /// @{
83 
84  /// Returns true if the multi-interval is empty.
85  GF_API bool IsEmpty() const { return _set.empty(); }
86 
87  /// Returns the number of intervals in the set.
88  GF_API size_t GetSize() const { return _set.size(); }
89 
90  /// Returns an interval bounding the entire multi-interval.
91  /// Returns an empty interval if the multi-interval is empty.
92  GF_API GfInterval GetBounds() const;
93 
94  /// Returns true if the multi-interval contains the given value.
95  GF_API bool Contains(double d) const;
96 
97  /// Returns true if the multi-interval contains the given interval.
98  GF_API bool Contains(const GfInterval & i) const;
99 
100  /// Returns true if the multi-interval contains all the intervals in the
101  /// given multi-interval.
102  GF_API bool Contains(const GfMultiInterval & s) const;
103 
104  /// @}
105 
106  /// \name Mutation
107  /// @{
108 
109  /// Clear the multi-interval.
110  GF_API void Clear() { _set.clear(); }
111 
112  /// Add the given interval to the multi-interval.
113  GF_API void Add( const GfInterval & i );
114  /// Add the given multi-interval to the multi-interval.
115  /// Sets this object to the union of the two sets.
116  GF_API void Add( const GfMultiInterval &s );
117 
118  /// Uses the given interval to extend the multi-interval in
119  /// the interval arithmetic sense.
120  GF_API void ArithmeticAdd( const GfInterval &i );
121 
122  /// Remove the given interval from this multi-interval.
123  GF_API void Remove( const GfInterval & i );
124  /// Remove the given multi-interval from this multi-interval.
125  GF_API void Remove( const GfMultiInterval &s );
126 
127  GF_API void Intersect( const GfInterval & i );
128  GF_API void Intersect( const GfMultiInterval &s );
129 
130  /// Return the complement of this set.
132 
133  /// @}
134 
135  /// \name Iteration
136  /// Only const iterators are returned. To maintain the invariants of
137  /// the multi-interval, changes must be made via the public mutation API.
138  /// @{
139 
140  GF_API const_iterator begin() const { return _set.begin(); }
141  GF_API const_iterator end() const { return _set.end(); }
142 
143  /// Returns an iterator identifying the first (lowest) interval whose
144  /// minimum value is >= x. If no such interval exists, returns end().
145  GF_API const_iterator lower_bound( double x ) const;
146 
147  /// Returns an iterator identifying the first (lowest) interval whose
148  /// minimum value is > x. If no such interval exists, returns end().
149  GF_API const_iterator upper_bound( double x ) const;
150 
151  /// Returns an iterator identifying the first (loest) interval whose
152  /// minimum value is > x. If no such interval exists, returns end().
154 
155  /// Returns an iterator identifying the last (highest) interval whose
156  /// maximum value is < x. If no such interval exists, returns end().
158 
159  /// Returns an iterator identifying the interval that contains x. If
160  /// no interval contains x, then it returns end()
162 
163  /// @}
164 
165  /// Returns the full interval (-inf, inf).
168  }
169 
170  /// Swap two multi-intervals
171  void swap(GfMultiInterval &other) { _set.swap(other._set); }
172 
173 private:
174  void _AssertInvariants() const;
175 
176  Set _set;
177 };
178 
179 /// Output a GfMultiInterval
180 /// \ingroup group_gf_DebuggingOutput
181 GF_API std::ostream & operator<<(std::ostream &out, const GfMultiInterval &s);
182 
184 
185 #endif // GF_INTERVAL_SET_H
GLdouble s
Definition: glew.h:1390
GF_API bool IsEmpty() const
Returns true if the multi-interval is empty.
Definition: multiInterval.h:85
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:65
GF_API bool operator<(const GfMultiInterval &that) const
Definition: multiInterval.h:67
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:70
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:68
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:66
static GfMultiInterval GetFullInterval()
Returns the full interval (-inf, inf).
GF_API const_iterator GetNextNonContainingInterval(double x) const
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
GF_API size_t GetSize() const
Returns the number of intervals in the set.
Definition: multiInterval.h:88
Set::const_iterator const_iterator
Definition: multiInterval.h:50
GF_API GfMultiInterval()
Constructs an multi-interval by copying the given set.
Definition: multiInterval.h:56
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:77
Set::const_iterator iterator
Definition: multiInterval.h:51
GF_API bool operator>(const GfMultiInterval &that) const
Definition: multiInterval.h:69
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
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:83
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 GfMultiInterval(const GfMultiInterval &s)
Constructs an multi-interval by copying the given set.
Definition: multiInterval.h:58
GF_API size_t Hash() const
#define GF_API
Definition: api.h:40
GF_API const_iterator begin() const