HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timeInterval.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_EF_TIME_INTERVAL_H
8 #define PXR_EXEC_EF_TIME_INTERVAL_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/ef/api.h"
15 #include "pxr/exec/ef/time.h"
16 
17 #include "pxr/base/gf/interval.h"
19 
20 #include <initializer_list>
21 #include <iosfwd>
22 #include <string>
23 #include <vector>
24 
26 
27 ///////////////////////////////////////////////////////////////////////////////
28 ///
29 /// A class that represents an interval in EfTime.
30 ///
31 /// This class holds a GfMultiInterval that represents intervals on the frame
32 /// timeline and a bool that represents a single point, not on the timeline,
33 /// for the "default" time.
34 ///
36 {
37 public:
38 
39  explicit EfTimeInterval(
40  const GfMultiInterval &timeMultiInterval = GfMultiInterval(),
41  bool defaultTime = false) :
42  _timeMultiInterval(timeMultiInterval),
43  _defaultTime(defaultTime)
44  {}
45 
46  explicit EfTimeInterval(
47  const GfInterval &timeInterval,
48  bool defaultTime = false) :
49  _timeMultiInterval(timeInterval),
50  _defaultTime(defaultTime)
51  {}
52 
53  /// For convenience, constructs a multiInterval from the discrete \p times.
54  ///
55  explicit EfTimeInterval(
56  const std::vector<double> &times,
57  bool defaultTime = false) :
58  _defaultTime(defaultTime)
59  {
60  for(double t : times)
61  _timeMultiInterval.Add(GfInterval(t));
62  }
63 
64  /// For convenience, constructs a multiInterval from the discrete \p times.
65  ///
66  explicit EfTimeInterval(
67  const std::initializer_list<double> &times,
68  bool defaultTime = false) :
69  _defaultTime(defaultTime)
70  {
71  for(double t : times)
72  _timeMultiInterval.Add(GfInterval(t));
73  }
74 
75  /// Clears the time interval to an empty interval.
76  ///
77  void Clear() {
78  _timeMultiInterval.Clear();
79  _defaultTime = false;
80  }
81 
82  /// Returns true if the interval is empty.
83  ///
84  bool IsEmpty() const {
85  return !_defaultTime && _timeMultiInterval.IsEmpty();
86  }
87 
88  /// Returns the multi interval that represents intervals on the frame
89  /// timeline.
90  ///
92  return _timeMultiInterval;
93  }
94 
95  /// Returns true if the interval contains the default time.
96  ///
97  bool IsDefaultTimeSet() const {
98  return _defaultTime;
99  }
100 
101  /// Returns true if this time interval contains \p time, with special
102  /// treatment for the default time and for left- and right-side time
103  /// values.
104  ///
105  /// The default time is treated as a separate time outside of the frame
106  /// timeline.
107  ///
108  /// See EfTime::IsContainedIn() for details on how side is handled.
109  ///
110  bool Contains(const EfTime &time) const {
111  return time.GetTimeCode().IsDefault()
112  ? _defaultTime
113  : _ContainsTime(time);
114  }
115 
116  /// Returns true if this time interval fully contains the time interval
117  /// \p rhs.
118  ///
119  /// The default time is treated as a separate time outside of the frame
120  /// timeline.
121  ///
122  bool Contains(const EfTimeInterval &rhs) const {
123  if (!_defaultTime && rhs.IsDefaultTimeSet()) {
124  return false;
125  }
126  return _timeMultiInterval.Contains(rhs.GetTimeMultiInterval());
127  }
128 
129  /// Returns true if the time interval is the full interval.
130  ///
131  /// I.e., this returns true if the time interval contains the full frame
132  /// timeline **and** the default time.
133  ///
134  bool IsFullInterval() const {
135  return _defaultTime &&
136  _timeMultiInterval == GfMultiInterval::GetFullInterval();
137  }
138 
139  /// Returns the full time interval: (-inf, inf) with the default time.
140  ///
143  }
144 
145  bool operator==(const EfTimeInterval &rhs) const {
146  return _timeMultiInterval == rhs._timeMultiInterval &&
147  _defaultTime == rhs._defaultTime;
148  }
149 
150  bool operator!=(const EfTimeInterval &rhs) const {
151  return !(*this == rhs);
152  }
153 
154  /// Unions this time interval and the EfTimeInterval \p rhs.
155  ///
156  void operator|=(const EfTimeInterval &rhs) {
157  _timeMultiInterval.Add(rhs._timeMultiInterval);
158  _defaultTime |= rhs._defaultTime;
159  }
160 
161  /// Unions this time interval and the GfInterval \p interval.
162  ///
163  void operator|=(const GfInterval &interval) {
164  _timeMultiInterval.Add(interval);
165  }
166 
167  /// Unions this time interval and the EfTime \p time.
168  ///
169  void operator|=(const EfTime &time) {
170  const UsdTimeCode timeCode = time.GetTimeCode();
171  if (timeCode.IsDefault()) {
172  _defaultTime = true;
173  } else {
174  _timeMultiInterval.Add(GfInterval(timeCode.GetValue()));
175  }
176  }
177 
178  /// Computes the intersection of this and the EfTimeInterval \p rhs.
179  ///
180  void operator&=(const EfTimeInterval &rhs) {
181  _timeMultiInterval.Intersect(rhs._timeMultiInterval);
182  _defaultTime &= rhs._defaultTime;
183  }
184 
185  /// Less-than operator.
186  bool operator<(const EfTimeInterval &rhs) const {
187  if (_timeMultiInterval < rhs._timeMultiInterval) return true;
188  if (_timeMultiInterval != rhs._timeMultiInterval) return false;
189  if (_defaultTime != rhs._defaultTime) return _defaultTime;
190 
191  // Equal
192  return false;
193  }
194 
195  /// Extends the interval by the specified number of frames in each
196  /// direction.
197  ///
198  /// E.g., extends (-100, 100) to (-110, 105) when leftFrames = 10 and
199  /// rightFrames = 5; or the multi interval (-100, 100), (200, 300) becomes
200  /// (-110, 105), (190, 305).
201  ///
202  EfTimeInterval &Extend(double leftFrames, double rightFrames) {
203  const GfInterval extension( -leftFrames, +rightFrames, true, true);
204  _timeMultiInterval.ArithmeticAdd(extension);
205  return *this;
206  }
207 
208  /// Get time interval as string, for debugging.
209  EF_API
210  std::string GetAsString() const;
211 
212 private:
213 
214  // Returns true if this time is contained in _timeMultiInterval. If frame
215  // is "default" this returns false.
216  EF_API
217  bool _ContainsTime(const EfTime &time) const;
218 
219  // The time multi interval.
220  GfMultiInterval _timeMultiInterval;
221 
222  // Whether or not the default time is set.
223  bool _defaultTime;
224 };
225 
226 /// Output an EfTimeInterval.
227 ///
228 EF_API
229 std::ostream &operator<<(std::ostream &os, const EfTimeInterval &timeInterval);
230 
232 
233 #endif
EfTimeInterval(const GfInterval &timeInterval, bool defaultTime=false)
Definition: timeInterval.h:46
EfTimeInterval(const GfMultiInterval &timeMultiInterval=GfMultiInterval(), bool defaultTime=false)
Definition: timeInterval.h:39
EF_API std::ostream & operator<<(std::ostream &os, const EfTimeInterval &timeInterval)
void operator|=(const EfTimeInterval &rhs)
Definition: timeInterval.h:156
GF_API bool IsEmpty() const
Returns true if the multi-interval is empty.
Definition: multiInterval.h:66
void operator&=(const EfTimeInterval &rhs)
Definition: timeInterval.h:180
GF_API bool Contains(double d) const
Returns true if the multi-interval contains the given value.
double GetValue() const
Definition: timeCode.h:157
GT_API const UT_StringHolder time
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
EF_API std::string GetAsString() const
Get time interval as string, for debugging.
GF_API void Add(const GfInterval &i)
Add the given interval to the multi-interval.
void operator|=(const GfInterval &interval)
Definition: timeInterval.h:163
bool Contains(const EfTime &time) const
Definition: timeInterval.h:110
Definition: time.h:36
bool IsDefaultTimeSet() const
Definition: timeInterval.h:97
static GfMultiInterval GetFullInterval()
Returns the full interval (-inf, inf).
bool operator!=(const EfTimeInterval &rhs) const
Definition: timeInterval.h:150
EfTimeInterval(const std::initializer_list< double > &times, bool defaultTime=false)
Definition: timeInterval.h:66
bool IsDefault() const
Definition: timeCode.h:145
const UsdTimeCode GetTimeCode() const
Definition: time.h:66
GF_API void ArithmeticAdd(const GfInterval &i)
bool Contains(const EfTimeInterval &rhs) const
Definition: timeInterval.h:122
static EfTimeInterval GetFullInterval()
Definition: timeInterval.h:141
const GfMultiInterval & GetTimeMultiInterval() const
Definition: timeInterval.h:91
GLdouble t
Definition: glad.h:2397
bool IsEmpty() const
Definition: timeInterval.h:84
GF_API void Clear()
Clear the multi-interval.
Definition: multiInterval.h:91
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
EfTimeInterval(const std::vector< double > &times, bool defaultTime=false)
Definition: timeInterval.h:55
bool operator<(const EfTimeInterval &rhs) const
Less-than operator.
Definition: timeInterval.h:186
bool operator==(const EfTimeInterval &rhs) const
Definition: timeInterval.h:145
GF_API void Intersect(const GfInterval &i)
Clear the multi-interval.
void operator|=(const EfTime &time)
Definition: timeInterval.h:169
EfTimeInterval & Extend(double leftFrames, double rightFrames)
Definition: timeInterval.h:202
OIIO_UTIL_API std::string extension(string_view filepath, bool include_dot=true) noexcept
bool IsFullInterval() const
Definition: timeInterval.h:134
#define EF_API
Definition: api.h:25