timeCode.h
1 //
2 // Copyright 2016 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_USD_USD_TIME_CODE_H
8 #define PXR_USD_USD_TIME_CODE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usd/api.h"
13 #include "pxr/usd/sdf/timeCode.h"
14 #include "pxr/base/arch/hints.h"
16 #include "pxr/base/tf/hash.h"
17 
18 #include <limits>
19 #include <iosfwd>
20 #include <cmath>
21 
22 
23 PXR_NAMESPACE_OPEN_SCOPE
24 
25 
26 #define USD_TIME_CODE_TOKENS \
27  (DEFAULT) \
28  (EARLIEST) \
29  (PRE_TIME)
30 
31 TF_DECLARE_PUBLIC_TOKENS(UsdTimeCodeTokens, USD_API, USD_TIME_CODE_TOKENS);
32 
33 
73 class UsdTimeCode {
74 public:
76  constexpr UsdTimeCode(double t = 0.0) noexcept : _value(t) {}
77 
79  constexpr UsdTimeCode(const GfTimeCode &timeCode) noexcept
80  : _value(timeCode.GetValue()) {}
81 
85  static constexpr UsdTimeCode PreTime(double t) noexcept {
86  return UsdTimeCode(t, /*isPreTime=*/true);
87  }
88 
91  static constexpr UsdTimeCode PreTime(const GfTimeCode& timeCode) noexcept {
92  return UsdTimeCode(timeCode.GetValue(), /*isPreTime=*/true);
93  }
94 
103  static constexpr UsdTimeCode EarliestTime() {
104  return UsdTimeCode(std::numeric_limits<double>::lowest());
105  }
106 
114  static constexpr UsdTimeCode Default() {
115  return UsdTimeCode(std::numeric_limits<double>::quiet_NaN());
116  }
117 
127  static constexpr double
128  SafeStep(double maxValue=1e6, double maxCompression=10.0) {
129  return std::numeric_limits<double>::epsilon() *
130  maxValue * maxCompression * 2.0;
131  }
132 
134  bool IsPreTime() const {
135  return _isPreTime;
136  }
137 
140  bool IsEarliestTime() const {
141  return IsNumeric() && (_value == std::numeric_limits<double>::lowest());
142  }
143 
146  bool IsDefault() const {
147  return std::isnan(_value);
148  }
149 
152  bool IsNumeric() const {
153  return !IsDefault();
154  }
155 
158  double GetValue() const {
159  if (ARCH_UNLIKELY(IsDefault()))
160  _IssueGetValueOnDefaultError();
161  return _value;
162  }
163 
165  friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
166  if (lhs.IsDefault() && rhs.IsDefault()) {
167  return true;
168  }
169  return lhs._value == rhs._value &&
170  lhs._isPreTime == rhs._isPreTime;
171  }
172 
174  friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
175  return !(lhs == rhs);
176  }
177 
185  friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
186  if (lhs.IsDefault() || rhs.IsDefault()) {
187  return lhs.IsDefault() && !rhs.IsDefault();
188  }
189  return lhs._value < rhs._value ||
190  (lhs._value == rhs._value &&
191  lhs._isPreTime && !rhs._isPreTime);
192  }
193 
196  friend bool operator>=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
197  return !(lhs < rhs);
198  }
199 
201  friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
202  return !(rhs < lhs);
203  }
204 
207  friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
208  return !(lhs <= rhs);
209  }
210 
212  friend size_t hash_value(const UsdTimeCode &time) {
213  return TfHash::Combine(time._value, time._isPreTime);
214  }
215 
216 private:
217  constexpr UsdTimeCode(double t, bool isPreTime) noexcept
218  : _value(t), _isPreTime(isPreTime) {}
219 
220  USD_API
221  void _IssueGetValueOnDefaultError() const;
222 
223  double _value;
224  bool _isPreTime = false;
225 };
226 
227 // Stream I/O operators.
228 USD_API
229 std::ostream& operator<<(std::ostream& os, const UsdTimeCode& time);
230 
231 USD_API
232 std::istream& operator>>(std::istream& is, UsdTimeCode& time);
233 
238 USD_API
239 UsdTimeCode operator*(const SdfLayerOffset& offset, const UsdTimeCode& time);
240 
241 PXR_NAMESPACE_CLOSE_SCOPE
242 
243 #endif // PXR_USD_USD_TIME_CODE_H
friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Equality comparison.
Definition: timeCode.h:165
static constexpr UsdTimeCode EarliestTime()
Produce a UsdTimeCode representing the lowest/earliest possible timeCode.
Definition: timeCode.h:103
friend bool operator>=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Greater-equal.
Definition: timeCode.h:196
friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-equal.
Definition: timeCode.h:201
Compiler hints.
bool IsNumeric() const
Return true if this time represents a numeric value, false otherwise.
Definition: timeCode.h:152
bool IsPreTime() const
Return true if this timeCode represents a pre-value, false otherwise.
Definition: timeCode.h:134
constexpr UsdTimeCode(double t=0.0) noexcept
Construct with optional time value. Implicitly convert from double.
Definition: timeCode.h:76
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:73
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:92
friend size_t hash_value(const UsdTimeCode &time)
Hash function.
Definition: timeCode.h:212
static constexpr UsdTimeCode PreTime(double t) noexcept
Produces a UsdTimeCode representing a pre-time at t.
Definition: timeCode.h:85
USDUTILS_API std::istream & operator>>(std::istream &is, UsdUtilsTimeCodeRange &timeCodeRange)
Stream extraction operator.
GF_API std::ostream & operator<<(std::ostream &out, const GfTimeCode &ap)
Stream insertion operator for the string representation of this time code.
This file defines some macros that are useful for declaring and using static TfTokens.
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for 'default'.
Definition: timeCode.h:114
bool IsEarliestTime() const
Return true if this time represents the lowest/earliest possible timeCode, false otherwise.
Definition: timeCode.h:140
friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Inequality comparison.
Definition: timeCode.h:174
static constexpr double SafeStep(double maxValue=1e6, double maxCompression=10.0)
Produce a safe step value such that for any numeric UsdTimeCode t in [-maxValue, maxValue],...
Definition: timeCode.h:128
Represents a time offset and scale between layers.
Definition: layerOffset.h:43
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:158
Value type that represents a time code.
Definition: timeCode.h:28
bool IsDefault() const
Return true if this time represents the 'default' sentinel value, false otherwise.
Definition: timeCode.h:146
friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-than.
Definition: timeCode.h:185
friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Greater-than.
Definition: timeCode.h:207
constexpr UsdTimeCode(const GfTimeCode &timeCode) noexcept
Construct and implicitly cast from GfTimeCode.
Definition: timeCode.h:79
static constexpr UsdTimeCode PreTime(const GfTimeCode &timeCode) noexcept
Produces a UsdTimeCode representing a pre-time using GfTimeCode timeCode.
Definition: timeCode.h:91