HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
layerOffset.h
Go to the documentation of this file.
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_SDF_LAYER_OFFSET_H
8 #define PXR_USD_SDF_LAYER_OFFSET_H
9 
10 /// \file sdf/layerOffset.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/sdf/api.h"
14 
15 #include <iosfwd>
16 #include <vector>
17 
19 
20 class SdfTimeCode;
21 
22 /// \class SdfLayerOffset
23 ///
24 /// Represents a time offset and scale between layers.
25 ///
26 /// The SdfLayerOffset class is an affine transform, providing both a scale and
27 /// a translate. It supports vector algebra semantics for composing
28 /// SdfLayerOffsets together via multiplication. The SdfLayerOffset class is
29 /// unitless: it does not refer to seconds or frames.
30 ///
31 /// For example, suppose layer A uses layer B, with an offset of X:
32 /// when bringing animation from B into A, you first apply the scale of X, and
33 /// then the offset. Suppose you have a scale of 2 and an offset of 24:
34 /// first multiply B's frame numbers by 2, and then add 24. The animation from
35 /// B as seen in A will take twice as long and start 24 frames later.
36 ///
37 /// Offsets are typically used in either sublayers or prim references. For more
38 /// information, see the SetSubLayerOffset() method of the SdfLayer class (the
39 /// subLayerOffsets property in Python), as well as the SetReference() and
40 /// GetReferenceLayerOffset() methods (the latter is the referenceLayerOffset
41 /// property in Python) of the SdfPrimSpec class.
42 ///
44 {
45 public:
46  /// \name Constructors
47  /// @{
48 
49  /// Constructs a new SdfLayerOffset instance.
50  explicit SdfLayerOffset(double offset = 0.0, double scale = 1.0)
51  : _offset(offset), _scale(scale) {}
52 
53  /// @}
54 
55  /// \name Accessors
56  /// @{
57 
58  /// Returns the time offset.
59  double GetOffset() const { return _offset; }
60 
61  /// Returns the time scale factor.
62  double GetScale() const { return _scale; }
63 
64  /// Sets the time offset.
65  void SetOffset(double newOffset) { _offset = newOffset; }
66 
67  /// Sets the time scale factor.
68  void SetScale(double newScale) { _scale = newScale; }
69 
70  /// Returns \c true if this is an identity transformation, with an offset of
71  /// 0.0 and a scale of 1.0. Note that for historical reasons this uses
72  /// operator==() against a default-constructed instance, which, as noted in
73  /// the documentation for equality comparison, is a "fuzzy" equality.
74  bool IsIdentity() const {
75  // Check for the common case of exact identity.
76  if (_offset == 0.0 && _scale == 1.0) {
77  return true;
78  }
79  return *this == SdfLayerOffset {};
80  }
81 
82  /// Returns \c true if this offset is valid, i.e. both the offset and
83  /// scale are finite (not infinite or NaN). Note that a valid layer
84  /// offset's inverse may be invalid.
85  SDF_API
86  bool IsValid() const;
87 
88  /// Gets the inverse offset, which performs the opposite transformation.
89  SDF_API
90  SdfLayerOffset GetInverse() const;
91 
92  /// \name Hashing
93  /// @{
94 
95  /// Returns hash for this offset.
96  SDF_API
97  size_t GetHash() const;
98 
99  /// Hash functor for hash maps and sets.
100  struct Hash {
101  size_t operator()(const SdfLayerOffset &offset) const {
102  return offset.GetHash();
103  }
104  };
105 
106  friend inline size_t hash_value(const SdfLayerOffset &offset) {
107  return offset.GetHash();
108  }
109 
110  /// @}
111 
112  /// \name Operators
113  /// @{
114 
115  /// Returns whether the offsets are equal. For historical reasons, this
116  /// performs a "fuzzy" equality comparison. If neither `*this` nor `rhs`
117  /// are valid by IsValid(), return true. If both are valid and their scales
118  /// and offsets are within an implementation-defined epsilon, return true.
119  /// Otherwise return false.
120  SDF_API
121  bool operator==(const SdfLayerOffset &rhs) const;
122 
123  /// \sa SdfLayerOffset::operator==
124  bool operator!=(const SdfLayerOffset &rhs) const {
125  return !(*this == rhs);
126  }
127 
128  /// Returns whether this offset is less than another. The meaning
129  /// of less than is somewhat arbitrary.
130  SDF_API
131  bool operator<(const SdfLayerOffset &rhs) const;
132 
133  /// \sa SdfLayerOffset::operator<
134  bool operator>(const SdfLayerOffset& rhs) const {
135  return rhs < *this;
136  }
137 
138  /// \sa SdfLayerOffset::operator<
139  bool operator>=(const SdfLayerOffset& rhs) const {
140  return !(*this < rhs);
141  }
142 
143  /// \sa SdfLayerOffset::operator<
144  bool operator<=(const SdfLayerOffset& rhs) const {
145  return !(*this > rhs);
146  }
147 
148  /// Composes this with the offset \e rhs, such that the resulting
149  /// offset is equivalent to first applying \e rhs and then \e *this.
150  SDF_API
151  SdfLayerOffset operator*(const SdfLayerOffset &rhs) const;
152 
153  /// Applies the offset to the given value.
154  SDF_API
155  double operator*(double rhs) const;
156 
157  /// Applies the offset to the given value.
158  SDF_API
159  SdfTimeCode operator*(const SdfTimeCode &rhs) const;
160 
161  /// @}
162 
163 private:
164  double _offset;
165  double _scale;
166 };
167 
168 typedef std::vector<SdfLayerOffset> SdfLayerOffsetVector;
169 
170 ///
171 /// Writes the string representation of \a SdfLayerOffset to \a out.
172 SDF_API
173 std::ostream & operator<<( std::ostream &out,
174  const SdfLayerOffset &layerOffset );
175 
177 
178 #endif // PXR_USD_SDF_LAYER_OFFSET_H
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
SDF_API SdfLayerOffset GetInverse() const
Gets the inverse offset, which performs the opposite transformation.
bool IsIdentity() const
Definition: layerOffset.h:74
SDF_API bool operator<(const SdfLayerOffset &rhs) const
bool operator>=(const SdfLayerOffset &rhs) const
Definition: layerOffset.h:139
SDF_API size_t GetHash() const
Returns hash for this offset.
std::vector< SdfLayerOffset > SdfLayerOffsetVector
Definition: layerOffset.h:168
GA_API const UT_StringHolder scale
bool operator!=(const SdfLayerOffset &rhs) const
Definition: layerOffset.h:124
void SetScale(double newScale)
Sets the time scale factor.
Definition: layerOffset.h:68
GLintptr offset
Definition: glcorearb.h:665
SDF_API SdfLayerOffset operator*(const SdfLayerOffset &rhs) const
bool operator>(const SdfLayerOffset &rhs) const
Definition: layerOffset.h:134
double GetScale() const
Returns the time scale factor.
Definition: layerOffset.h:62
size_t operator()(const SdfLayerOffset &offset) const
Definition: layerOffset.h:101
Hash functor for hash maps and sets.
Definition: layerOffset.h:100
SDF_API bool operator==(const SdfLayerOffset &rhs) const
SDF_API std::ostream & operator<<(std::ostream &out, const SdfLayerOffset &layerOffset)
Writes the string representation of SdfLayerOffset to out.
friend size_t hash_value(const SdfLayerOffset &offset)
Returns hash for this offset.
Definition: layerOffset.h:106
bool operator<=(const SdfLayerOffset &rhs) const
Definition: layerOffset.h:144
SDF_API bool IsValid() const
#define SDF_API
Definition: api.h:23
double GetOffset() const
Returns the time offset.
Definition: layerOffset.h:59
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void SetOffset(double newOffset)
Sets the time offset.
Definition: layerOffset.h:65
SdfLayerOffset(double offset=0.0, double scale=1.0)
Constructs a new SdfLayerOffset instance.
Definition: layerOffset.h:50