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
29TF_DECLARE_PUBLIC_TOKENS(UsdTimeCodeTokens, USD_API, USD_TIME_CODE_TOKENS);
30
31
68public:
70 constexpr UsdTimeCode(double t = 0.0) noexcept : _value(t) {}
71
73 constexpr UsdTimeCode(const SdfTimeCode &timeCode) noexcept
74 : _value(timeCode.GetValue()) {}
75
84 static constexpr UsdTimeCode EarliestTime() {
85 return UsdTimeCode(std::numeric_limits<double>::lowest());
86 }
87
95 static constexpr UsdTimeCode Default() {
96 return UsdTimeCode(std::numeric_limits<double>::quiet_NaN());
97 }
98
108 static constexpr double
109 SafeStep(double maxValue=1e6, double maxCompression=10.0) {
110 return std::numeric_limits<double>::epsilon() *
111 maxValue * maxCompression * 2.0;
112 }
113
116 bool IsEarliestTime() const {
117 return IsNumeric() && (_value == std::numeric_limits<double>::lowest());
118 }
119
122 bool IsDefault() const {
123 return std::isnan(_value);
124 }
125
128 bool IsNumeric() const {
129 return !IsDefault();
130 }
131
134 double GetValue() const {
135 if (ARCH_UNLIKELY(IsDefault()))
136 _IssueGetValueOnDefaultError();
137 return _value;
138 }
139
141 friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
142 return lhs.IsDefault() == rhs.IsDefault() &&
143 (lhs.IsDefault() || (lhs.GetValue() == rhs.GetValue()));
144 }
145
147 friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
148 return !(lhs == rhs);
149 }
150
153 friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
154 return (lhs.IsDefault() && rhs.IsNumeric()) ||
155 (lhs.IsNumeric() && rhs.IsNumeric() &&
156 lhs.GetValue() < rhs.GetValue());
157 }
158
161 friend bool operator>=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
162 return !(lhs < rhs);
163 }
164
167 friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
168 return lhs.IsDefault() ||
169 (rhs.IsNumeric() && lhs.GetValue() <= rhs.GetValue());
170 }
171
174 friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
175 return !(lhs <= rhs);
176 }
177
179 friend size_t hash_value(const UsdTimeCode &time) {
180 return TfHash()(time._value);
181 }
182
183private:
184 USD_API
185 void _IssueGetValueOnDefaultError() const;
186
187 double _value;
188};
189
190// Stream I/O operators.
191USD_API
192std::ostream& operator<<(std::ostream& os, const UsdTimeCode& time);
193
194USD_API
195std::istream& operator>>(std::istream& is, UsdTimeCode& time);
196
197
198PXR_NAMESPACE_CLOSE_SCOPE
199
200#endif // PXR_USD_USD_TIME_CODE_H
Value type that represents a time code.
Definition: timeCode.h:28
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:460
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:67
friend size_t hash_value(const UsdTimeCode &time)
Hash function.
Definition: timeCode.h:179
constexpr UsdTimeCode(const SdfTimeCode &timeCode) noexcept
Construct and implicitly cast from SdfTimeCode.
Definition: timeCode.h:73
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:109
friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-equal.
Definition: timeCode.h:167
static constexpr UsdTimeCode EarliestTime()
Produce a UsdTimeCode representing the lowest/earliest possible timeCode.
Definition: timeCode.h:84
friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Equality comparison.
Definition: timeCode.h:141
friend bool operator>=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Greater-equal.
Definition: timeCode.h:161
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for 'default'.
Definition: timeCode.h:95
friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-than.
Definition: timeCode.h:153
friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Greater-than.
Definition: timeCode.h:174
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:134
bool IsDefault() const
Return true if this time represents the 'default' sentinel value, false otherwise.
Definition: timeCode.h:122
bool IsNumeric() const
Return true if this time represents a numeric value, false otherwise.
Definition: timeCode.h:128
constexpr UsdTimeCode(double t=0.0) noexcept
Construct with optional time value. Impilicitly convert from double.
Definition: timeCode.h:70
bool IsEarliestTime() const
Return true if this time represents the lowest/earliest possible timeCode, false otherwise.
Definition: timeCode.h:116
friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Inequality comparison.
Definition: timeCode.h:147
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