HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
types.h
Go to the documentation of this file.
1 //
2 // Copyright 2024 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_BASE_TS_TYPES_H
9 #define PXR_BASE_TS_TYPES_H
10 
11 #include "pxr/pxr.h"
12 #include "pxr/base/ts/api.h"
13 
14 #include "pxr/base/gf/interval.h"
15 #include "pxr/base/gf/vec2d.h"
17 
18 #include <cstdint>
19 #include <vector>
20 
22 
23 /// \anchor TS_SPLINE_SUPPORTED_VALUE_TYPES
24 /// Sequence of value types that are supported by the spline system.
25 /// \li <b>double</b>
26 /// \li <b>float</b>
27 /// \li <b>GfHalf</b>
28 /// \hideinitializer
29 #define TS_SPLINE_SUPPORTED_VALUE_TYPES \
30  ((Double, double)) \
31  ((Float, float)) \
32  ((Half, GfHalf))
33 
34 #define TS_SPLINE_SAMPLE_VERTEX_TYPES \
35  ((Vec2d, GfVec2d)) \
36  ((Vec2f, GfVec2f)) \
37  ((Vec2h, GfVec2h))
38 
39 #define TS_SPLINE_VALUE_TYPE_NAME(x) TF_PP_TUPLE_ELEM(0, x)
40 #define TS_SPLINE_VALUE_CPP_TYPE(x) TF_PP_TUPLE_ELEM(1, x)
41 
42 /// \brief True if template parameter T is a supported spline data type.
43 template <class T>
44 inline constexpr bool
46 
47 #define _TS_SUPPORT_DATA_TYPE(unused, tuple) \
48  template <> \
49  inline constexpr bool \
50  TsSplineIsValidDataType< TS_SPLINE_VALUE_CPP_TYPE(tuple) > = true;
52  ~,
54 #undef _TS_SUPPORT_DATA_TYPE
55 
56 /// \brief True if template parameter T is a supported spline sampling vertex
57 /// type.
58 template <class T>
59 inline constexpr bool
60 TsSplineIsValidSampleType = false;
61 
62 #define _TS_SUPPORT_SAMPLE_TYPE(unused, tuple) \
63  template <> \
64  inline constexpr bool \
65  TsSplineIsValidSampleType< TS_SPLINE_VALUE_CPP_TYPE(tuple) > = true;
67  ~,
69 #undef _TS_SUPPORT_SAMPLE_TYPE
70 
71 // Times are encoded as double.
72 using TsTime = double;
73 
74 //////////////////////////////
75 // ** NOTE TO MAINTAINERS **
76 //
77 // The following enum values are used in the binary crate format.
78 // Do not change them; only add.
79 
80 /// Interpolation mode for a spline segment (region between two knots).
81 ///
83 {
84  TsInterpValueBlock = 0, //< No value in this segment.
85  TsInterpHeld = 1, //< Constant value in this segment.
86  TsInterpLinear = 2, //< Linear interpolation.
87  TsInterpCurve = 3 //< Bezier or Hermite, depends on curve type.
88 };
89 
90 /// Type of interpolation for a spline's \c Curve segments.
91 ///
93 {
94  TsCurveTypeBezier = 0, //< Bezier curve, free tangent widths.
95  TsCurveTypeHermite = 1 //< Hermite curve, like Bezier but fixed tan width.
96 };
97 
98 /// Curve-shaping mode for one of a spline's extrapolation regions (before all
99 /// knots and after all knots).
100 ///
102 {
103  TsExtrapValueBlock = 0, //< No value in this region.
104  TsExtrapHeld = 1, //< Constant value in this region.
105  TsExtrapLinear = 2, //< Linear interpolation based on edge knots.
106  TsExtrapSloped = 3, //< Linear interpolation with specified slope.
107  TsExtrapLoopRepeat = 4, //< Knot curve repeated, offset so ends meet.
108  TsExtrapLoopReset = 5, //< Curve repeated exactly, discontinuous joins.
109  TsExtrapLoopOscillate = 6 //< Like Reset, but every other copy reversed.
110 };
111 
112 /// The source for a particular part of a sampled spline. A \c TsSpline can have
113 /// a number of different regions. The source is not important to the values
114 /// that vary over time, but if the spline is sampled and displayed in a user
115 /// interface, the source can be used to highlight different regions of the
116 /// displayed spline.
117 ///
119 {
120  TsSourcePreExtrap, //< Extrapolation before the first knot
121  TsSourcePreExtrapLoop, //< Looped extrapolation before the first knot
122  TsSourceInnerLoopPreEcho, //< Echoed copy of an inner loop prototype
123  TsSourceInnerLoopProto, //< This is the inner loop prototype
124  TsSourceInnerLoopPostEcho, //< Echoed copy of an inner loop prototype
125  TsSourceKnotInterp, //< "Normal" knot interpolation
126  TsSourcePostExtrap, //< Extrapolation after the last knot
127  TsSourcePostExtrapLoop, //< Looped extrapolation after the last knot
128 };
129 
130 /// Automatic tangent calculation algorithms.
131 ///
132 /// Which automatic tangent algorithm to use.
133 ///
134 /// \li None - Tangents are not automatically calculated, the provided values
135 /// are used. Note that the tangent values are still subject to modification
136 /// by the spline's anti-regression setting.
137 ///
138 /// \li Custom - The tangent algorithm is determined by the "preTanAlgorithm"
139 /// and "postTanAlgorithm" keys in the knot's custom data. These custom
140 /// data keys are reserved for this purpose. If the custom data values do not
141 /// exist or if their value cannot be understood, then Custom behaves as if
142 /// the None algorithm was used. Note that the Custom algorithm is not yet
143 /// implemented so it currently always behaves like None.
144 ///
145 /// \li AutoEase - Use the "Auto Ease" algorithm from Maya/animX. This is a
146 /// cubic controlled blending algorithm that computes a slope between the slopes
147 /// to the knots on either side of this knot. If there is a discontinuity in the
148 /// spline at this knot (this knot has no previous or next knot, is dual valued,
149 /// or is adjacent to a value blocked segment of the spline) then the slope will
150 /// be 0 (flat).
151 ///
152 /// \see \ref TsKnot::SetPreTanAlgorithm, \ref TsKnot::SetPostTanAlgorithm
154 {
158 };
159 
160 /// Inner-loop parameters.
161 ///
162 /// At most one inner-loop region can be specified per spline. Only whole
163 /// numbers of pre- and post-iterations are supported.
164 ///
165 /// The value offset specifies the difference between the values at the starts
166 /// of consecutive iterations.
167 ///
168 /// There must always be a knot at the protoStart time; otherwise the loop
169 /// parameters are invalid and will be ignored.
170 ///
171 /// A copy of the start knot is always made at the end of the prototype region.
172 /// This is true even if there is no post-looping; it ensures that all
173 /// iterations (including pre-loops) match the prototype region exactly.
174 ///
175 /// Enabling inner looping will generally change the shape of the prototype
176 /// interval (and thus all looped copies), because the first knot is echoed as
177 /// the last. Inner looping does not aim to make copies of an existing shape;
178 /// it aims to set up for continuity at loop joins.
179 ///
180 /// When inner looping is applied, any knots specified in the pre-looped or
181 /// post-looped intervals are removed from consideration, though they remain in
182 /// the spline parameters. A knot exactly at the end of the prototype interval
183 /// is not part of the prototype; it will be ignored, and overwritten by the
184 /// start-knot copy.
185 ///
186 /// When protoEnd <= protoStart, inner looping is disabled.
187 ///
188 /// Negative numbers of loops are not meaningful; they are treated the same as
189 /// zero counts. These quantities are signed only so that accidental underflow
190 /// does not result in huge loop counts.
191 ///
193 {
194 public:
195  TsTime protoStart = 0.0;
196  TsTime protoEnd = 0.0;
197  int32_t numPreLoops = 0;
198  int32_t numPostLoops = 0;
199  double valueOffset = 0.0;
200 
201 public:
202  TS_API
203  bool operator==(const TsLoopParams &other) const;
204 
205  TS_API
206  bool operator!=(const TsLoopParams &other) const;
207 
208  /// Returns the prototype region, [protoStart, protoEnd).
209  TS_API
211 
212  /// Returns the union of the prototype region and the echo region(s).
213  TS_API
215 };
216 
217 /// Extrapolation parameters for the ends of a spline beyond the knots.
218 ///
220 {
221 public:
223  double slope = 0.0;
224 
225 public:
226  TS_API
227  TsExtrapolation();
228 
229  TS_API
231 
232  TS_API
234 
235  TS_API
236  bool operator==(const TsExtrapolation &other) const;
237 
238  TS_API
239  bool operator!=(const TsExtrapolation &other) const;
240 
241  /// Returns whether our mode is one of the looping extrapolation modes.
242  TS_API
243  bool IsLooping() const;
244 };
245 
246 /// \brief \c TsSplineSamples<Vertex> holds a collection of piecewise linear
247 /// polylines that approximate a \c TsSpline.
248 ///
249 /// The vertex must be one of \c GfVec2d, \c GfVec2f, or \c GfVec2h. Note that
250 /// you may have precision or overflow issues if you use \c GfVec2h.
251 ///
252 /// \sa \ref TsSplineSamplesWithSources and \ref TsSpline::Sample
253 template <typename Vertex>
255 {
256 public:
257  static_assert(TsSplineIsValidSampleType<Vertex>,
258  "The Vertex template parameter to TsSplineSamples must be one"
259  " of GfVec2d, GfVec2f, or GfVec2h.");
260 
261  using Polyline = std::vector<Vertex>;
262 
263  std::vector<Polyline> polylines;
264 };
265 
266 /// \brief \c TsSplineSamplesWithSources<Vertex> is a \c TsSplineSamples<Vertex>
267 /// that also includes source information for each polyline.
268 ///
269 /// The vertex must be one of \c GfVec2d, \c GfVec2f, or \c GfVec2h. Note that
270 /// you may have precision or overflow issues if you use \c GfVec2h.
271 ///
272 /// The \c polylines and \c sources vectors are parallel arrays. In other words,
273 /// the source for the \c Polyline in \c polylines[i] is in \c sources[i] and
274 /// the two vectors have the same size.
275 /// \sa \ref TsSplineSamples and \ref TsSpline::SampleWithSources
276 template <typename Vertex>
278 {
279 public:
280  static_assert(TsSplineIsValidSampleType<Vertex>,
281  "The Vertex template parameter to TsSplineSamplesWithSources"
282  " must be one of GfVec2d, GfVec2f, or GfVec2h.");
283 
284  using Polyline = std::vector<Vertex>;
285 
286  std::vector<Polyline> polylines;
287  std::vector<TsSplineSampleSource> sources;
288 };
289 
290 /// Modes for enforcing non-regression in splines.
291 ///
292 /// See \ref page_ts_regression for a general introduction to regression and
293 /// anti-regression.
294 ///
296 {
297  /// Do not enforce. If there is regression, runtime evaluation will use
298  /// KeepRatio.
300 
301  /// Prevent tangents from crossing neighboring knots. This guarantees
302  /// non-regression, but is slightly over-conservative, preventing the
303  /// authoring of some extreme curves that cannot be created without
304  /// non-contained tangents.
306 
307  /// If there is regression in a segment, shorten both of its tangents until
308  /// the regression is just barely prevented (the curve comes to a
309  /// near-standstill at some time). Preserve the ratio of the tangent
310  /// lengths.
312 
313  /// If there is regression in a segment, leave its start tangent alone, and
314  /// shorten its end tangent until the regression is just barely prevented.
315  /// This matches Maya behavior.
317 };
318 
319 
321 
322 #endif
TsAntiRegressionMode
Definition: types.h:295
TsSplineSamplesWithSources<Vertex> is a TsSplineSamples<Vertex> that also includes source information...
Definition: types.h:277
TS_API GfInterval GetLoopedInterval() const
Returns the union of the prototype region and the echo region(s).
std::vector< Vertex > Polyline
Definition: types.h:284
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
TsTime protoStart
Definition: types.h:195
TsSplineSampleSource
Definition: types.h:118
double slope
Definition: types.h:223
TF_PP_SEQ_FOR_EACH(_TS_SUPPORT_DATA_TYPE,~, TS_SPLINE_SUPPORTED_VALUE_TYPES) template< class T > inline const expr bool TsSplineIsValidSampleType
True if template parameter T is a supported spline sampling vertex type.
TS_API bool operator==(const TsExtrapolation &other) const
TS_API bool operator!=(const TsLoopParams &other) const
std::vector< Polyline > polylines
Definition: types.h:263
#define TS_SPLINE_SAMPLE_VERTEX_TYPES
Definition: types.h:34
TsCurveType
Definition: types.h:92
TS_API GfInterval GetPrototypeInterval() const
Returns the prototype region, [protoStart, protoEnd).
TS_API bool operator!=(const TsExtrapolation &other) const
TS_API bool IsLooping() const
Returns whether our mode is one of the looping extrapolation modes.
TS_API TsExtrapolation()
TsExtrapMode
Definition: types.h:101
#define TS_API
Definition: api.h:24
TsTime protoEnd
Definition: types.h:196
GLenum mode
Definition: glcorearb.h:99
int32_t numPreLoops
Definition: types.h:197
std::vector< TsSplineSampleSource > sources
Definition: types.h:287
int32_t numPostLoops
Definition: types.h:198
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
double valueOffset
Definition: types.h:199
TS_API bool operator==(const TsLoopParams &other) const
std::vector< Polyline > polylines
Definition: types.h:286
#define TS_SPLINE_SUPPORTED_VALUE_TYPES
Definition: types.h:29
TsSplineSamples<Vertex> holds a collection of piecewise linear polylines that approximate a TsSpline...
Definition: types.h:254
#define _TS_SUPPORT_DATA_TYPE(unused, tuple)
Definition: types.h:47
std::vector< Vertex > Polyline
Definition: types.h:261
TsTangentAlgorithm
Definition: types.h:153
TsInterpMode
Definition: types.h:82
constexpr bool TsSplineIsValidDataType
True if template parameter T is a supported spline data type.
Definition: types.h:45
#define _TS_SUPPORT_SAMPLE_TYPE(unused, tuple)
Definition: types.h:62