All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timeCode.h
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 PXR_USD_USD_TIME_CODE_H
25 #define PXR_USD_USD_TIME_CODE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/api.h"
29 #include "pxr/usd/sdf/timeCode.h"
30 #include "pxr/base/arch/hints.h"
32 
33 #include <boost/functional/hash.hpp>
34 
35 #include <limits>
36 #include <iosfwd>
37 #include <cmath>
38 
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 
43 #define USD_TIME_CODE_TOKENS \
44  (DEFAULT) \
45  (EARLIEST)
46 
47 TF_DECLARE_PUBLIC_TOKENS(UsdTimeCodeTokens, USD_API, USD_TIME_CODE_TOKENS);
48 
49 
85 class UsdTimeCode {
86 public:
88  constexpr UsdTimeCode(double t = 0.0) noexcept : _value(t) {}
89 
91  constexpr UsdTimeCode(const SdfTimeCode &timeCode) noexcept
92  : _value(timeCode.GetValue()) {}
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 
134  bool IsEarliestTime() const {
135  return IsNumeric() && (_value == std::numeric_limits<double>::lowest());
136  }
137 
140  bool IsDefault() const {
141  return std::isnan(_value);
142  }
143 
146  bool IsNumeric() const {
147  return !IsDefault();
148  }
149 
152  double GetValue() const {
153  if (ARCH_UNLIKELY(IsDefault()))
154  _IssueGetValueOnDefaultError();
155  return _value;
156  }
157 
159  friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
160  return lhs.IsDefault() == rhs.IsDefault() &&
161  (lhs.IsDefault() || (lhs.GetValue() == rhs.GetValue()));
162  }
163 
165  friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
166  return !(lhs == rhs);
167  }
168 
171  friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
172  return (lhs.IsDefault() && rhs.IsNumeric()) ||
173  (lhs.IsNumeric() && rhs.IsNumeric() &&
174  lhs.GetValue() < rhs.GetValue());
175  }
176 
179  friend bool operator>=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
180  return !(lhs < rhs);
181  }
182 
185  friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
186  return lhs.IsDefault() ||
187  (rhs.IsNumeric() && lhs.GetValue() <= rhs.GetValue());
188  }
189 
192  friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
193  return !(lhs <= rhs);
194  }
195 
197  friend size_t hash_value(const UsdTimeCode &time) {
198  return boost::hash_value(time._value);
199  }
200 
201 private:
202  USD_API
203  void _IssueGetValueOnDefaultError() const;
204 
205  double _value;
206 };
207 
208 // Stream I/O operators.
209 USD_API
210 std::ostream& operator<<(std::ostream& os, const UsdTimeCode& time);
211 
212 USD_API
213 std::istream& operator>>(std::istream& is, UsdTimeCode& time);
214 
215 
216 PXR_NAMESPACE_CLOSE_SCOPE
217 
218 #endif // PXR_USD_USD_TIME_CODE_H
friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Equality comparison.
Definition: timeCode.h:159
bool IsNumeric() const
Return true if this time represents a numeric value, false otherwise.
Definition: timeCode.h:146
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)
Greater-equal.
Definition: timeCode.h:179
friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-equal.
Definition: timeCode.h:185
Compiler hints.
bool IsDefault() const
Return true if this time represents the &#39;default&#39; sentinel value, false otherwise.
Definition: timeCode.h:140
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:152
constexpr UsdTimeCode(double t=0.0) noexcept
Construct with optional time value. Impilicitly convert from double.
Definition: timeCode.h:88
Value type that represents a time code.
Definition: timeCode.h:44
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:118
bool IsEarliestTime() const
Return true if this time represents the lowest/earliest possible timeCode, false otherwise.
Definition: timeCode.h:134
friend size_t hash_value(const UsdTimeCode &time)
Hash function.
Definition: timeCode.h:197
USDUTILS_API std::istream & operator>>(std::istream &is, UsdUtilsTimeCodeRange &timeCodeRange)
Stream extraction operator.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
constexpr UsdTimeCode(const SdfTimeCode &timeCode) noexcept
Construct and implicitly cast from SdfTimeCode.
Definition: timeCode.h:91
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 &#39;default&#39;.
Definition: timeCode.h:113
friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Inequality comparison.
Definition: timeCode.h:165
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], t +/- (step / maxCompression) != t with a safety factor of 2.
Definition: timeCode.h:127
friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-than.
Definition: timeCode.h:171
friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Greater-than.
Definition: timeCode.h:192