All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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/base/arch/hints.h"
15#include "pxr/base/tf/hash.h"
16
17#include <limits>
18#include <iosfwd>
19#include <cmath>
20
21
22PXR_NAMESPACE_OPEN_SCOPE
23
24
25#define USD_TIME_CODE_TOKENS \
26 (DEFAULT) \
27 (EARLIEST) \
28 (PRE_TIME)
29
30TF_DECLARE_PUBLIC_TOKENS(UsdTimeCodeTokens, USD_API, USD_TIME_CODE_TOKENS);
31
32
73public:
75 constexpr UsdTimeCode(double t = 0.0) noexcept : _value(t) {}
76
78 constexpr UsdTimeCode(const SdfTimeCode &sdfTimeCode) noexcept
79 : _value(sdfTimeCode.GetValue()) {}
80
84 static constexpr UsdTimeCode PreTime(double t) noexcept {
85 return UsdTimeCode(t, /*isPreTime=*/true);
86 }
87
90 static constexpr UsdTimeCode PreTime(const SdfTimeCode& timeCode) noexcept {
91 return UsdTimeCode(timeCode.GetValue(), /*isPreTime=*/true);
92 }
93
102 static constexpr UsdTimeCode EarliestTime() {
103 return UsdTimeCode(std::numeric_limits<double>::lowest());
104 }
105
113 static constexpr UsdTimeCode Default() {
114 return UsdTimeCode(std::numeric_limits<double>::quiet_NaN());
115 }
116
126 static constexpr double
127 SafeStep(double maxValue=1e6, double maxCompression=10.0) {
128 return std::numeric_limits<double>::epsilon() *
129 maxValue * maxCompression * 2.0;
130 }
131
133 bool IsPreTime() const {
134 return _isPreTime;
135 }
136
139 bool IsEarliestTime() const {
140 return IsNumeric() && (_value == std::numeric_limits<double>::lowest());
141 }
142
145 bool IsDefault() const {
146 return std::isnan(_value);
147 }
148
151 bool IsNumeric() const {
152 return !IsDefault();
153 }
154
157 double GetValue() const {
158 if (ARCH_UNLIKELY(IsDefault()))
159 _IssueGetValueOnDefaultError();
160 return _value;
161 }
162
164 friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
165 if (lhs.IsDefault() && rhs.IsDefault()) {
166 return true;
167 }
168 return lhs._value == rhs._value &&
169 lhs._isPreTime == rhs._isPreTime;
170 }
171
173 friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
174 return !(lhs == rhs);
175 }
176
184 friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
185 if (lhs.IsDefault() || rhs.IsDefault()) {
186 return lhs.IsDefault() && !rhs.IsDefault();
187 }
188 return lhs._value < rhs._value ||
189 (lhs._value == rhs._value &&
190 lhs._isPreTime && !rhs._isPreTime);
191 }
192
195 friend bool operator>=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
196 return !(lhs < rhs);
197 }
198
200 friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
201 return !(rhs < lhs);
202 }
203
206 friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
207 return !(lhs <= rhs);
208 }
209
211 friend size_t hash_value(const UsdTimeCode &time) {
212 return TfHash::Combine(time._value, time._isPreTime);
213 }
214
215private:
216 constexpr UsdTimeCode(double t, bool isPreTime) noexcept
217 : _value(t), _isPreTime(isPreTime) {}
218
219 USD_API
220 void _IssueGetValueOnDefaultError() const;
221
222 double _value;
223 bool _isPreTime = false;
224};
225
226// Stream I/O operators.
227USD_API
228std::ostream& operator<<(std::ostream& os, const UsdTimeCode& time);
229
230USD_API
231std::istream& operator>>(std::istream& is, UsdTimeCode& time);
232
233
234PXR_NAMESPACE_CLOSE_SCOPE
235
236#endif // PXR_USD_USD_TIME_CODE_H
Value type that represents a time code.
Definition: timeCode.h:28
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:72
friend size_t hash_value(const UsdTimeCode &time)
Hash function.
Definition: timeCode.h:211
static constexpr UsdTimeCode PreTime(const SdfTimeCode &timeCode) noexcept
Produces a UsdTimeCode representing a pre-time using SdfTimeCode timeCode.
Definition: timeCode.h:90
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:127
friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-equal.
Definition: timeCode.h:200
static constexpr UsdTimeCode EarliestTime()
Produce a UsdTimeCode representing the lowest/earliest possible timeCode.
Definition: timeCode.h:102
friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Equality comparison.
Definition: timeCode.h:164
bool IsPreTime() const
Return true if this timeCode represents a pre-value, false otherwise.
Definition: timeCode.h:133
friend bool operator>=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Greater-equal.
Definition: timeCode.h:195
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for 'default'.
Definition: timeCode.h:113
friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-than.
Definition: timeCode.h:184
friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Greater-than.
Definition: timeCode.h:206
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:157
bool IsDefault() const
Return true if this time represents the 'default' sentinel value, false otherwise.
Definition: timeCode.h:145
bool IsNumeric() const
Return true if this time represents a numeric value, false otherwise.
Definition: timeCode.h:151
static constexpr UsdTimeCode PreTime(double t) noexcept
Produces a UsdTimeCode representing a pre-time at t.
Definition: timeCode.h:84
constexpr UsdTimeCode(double t=0.0) noexcept
Construct with optional time value. Impilicitly convert from double.
Definition: timeCode.h:75
constexpr UsdTimeCode(const SdfTimeCode &sdfTimeCode) noexcept
Construct and implicitly cast from SdfTimeCode.
Definition: timeCode.h:78
bool IsEarliestTime() const
Return true if this time represents the lowest/earliest possible timeCode, false otherwise.
Definition: timeCode.h:139
friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Inequality comparison.
Definition: timeCode.h:173
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
Compiler hints.
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:81