Loading...
Searching...
No Matches
tsTest_SplineData.h
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_TS_TEST_SPLINE_DATA_H
9#define PXR_BASE_TS_TS_TEST_SPLINE_DATA_H
10
11#include "pxr/pxr.h"
12#include "pxr/base/ts/api.h"
13
14#include <string>
15#include <set>
16
17PXR_NAMESPACE_OPEN_SCOPE
18
19// A generic way of encoding spline control parameters. Allows us to pass the
20// same data to different backends (Ts, mayapy, etc) for evaluation.
21//
22class TsTest_SplineData
23{
24public:
25 // Interpolation method for a spline segment.
26 enum InterpMethod
27 {
28 InterpHeld,
29 InterpLinear,
30 InterpCurve
31 };
32
33 // Extrapolation method for the ends of a spline beyond the knots.
34 enum ExtrapMethod
35 {
36 ExtrapHeld,
37 ExtrapLinear,
38 ExtrapSloped,
39 ExtrapLoop
40 };
41
42 // Looping modes.
43 enum LoopMode
44 {
45 LoopNone,
46 LoopContinue, // Used by inner loops. Copy whole knots.
47 LoopRepeat, // Used by extrap loops. Repeat with offset.
48 LoopReset, // Used by extrap loops. Repeat identically.
49 LoopOscillate // Used by extrap loops. Alternate forward / reverse.
50 };
51
52 // Features that may be required by splines.
53 enum Feature
54 {
55 FeatureHeldSegments = 1 << 0,
56 FeatureLinearSegments = 1 << 1,
57 FeatureBezierSegments = 1 << 2,
58 FeatureHermiteSegments = 1 << 3,
59 FeatureAutoTangents = 1 << 4,
60 FeatureDualValuedKnots = 1 << 5,
61 FeatureInnerLoops = 1 << 6,
62 FeatureExtrapolatingLoops = 1 << 7,
63 FeatureExtrapolatingSlopes = 1 << 8
64 };
65 using Features = unsigned int;
66
67 // One knot in a spline.
68 struct Knot
69 {
70 double time = 0;
71 InterpMethod nextSegInterpMethod = InterpHeld;
72 double value = 0;
73 bool isDualValued = false;
74 double preValue = 0;
75 double preSlope = 0;
76 double postSlope = 0;
77 double preLen = 0;
78 double postLen = 0;
79 bool preAuto = false;
80 bool postAuto = false;
81
82 public:
83 TS_API
84 bool operator==(
85 const Knot &other) const;
86
87 TS_API
88 bool operator!=(
89 const Knot &other) const;
90
91 TS_API
92 bool operator<(
93 const Knot &other) const;
94 };
95 using KnotSet = std::set<Knot>;
96
97 // Inner-loop parameters.
98 //
99 // The prototype interval [protoStart, protoEnd) is duplicated before and/or
100 // after where it occurs.
101 //
102 // There must always be a knot exactly at protoStart. The start knot is
103 // copied to the end of the prototype, and to the end of every loop
104 // iteration.
105 //
106 // A knot exactly at the end of the prototype interval is not part of the
107 // prototype. If there is post-looping, a knot at the end of the prototype
108 // interval is overwritten by a copy of the knot from the start of the
109 // prototype interval.
110 //
111 // Enabling inner looping can change the shape of the prototype interval
112 // (and thus all looped copies), because the first knot is echoed as the
113 // last. Inner looping does not aim to make copies of an existing shape; it
114 // aims to set up for continuity at loop joins.
115 //
116 // The value offset specifies the difference between the value at the starts
117 // of consecutive iterations.
118 //
119 struct InnerLoopParams
120 {
121 bool enabled = false;
122 double protoStart = 0;
123 double protoEnd = 0;
124 int numPreLoops = 0;
125 int numPostLoops = 0;
126 double valueOffset = 0;
127
128 public:
129 TS_API
130 bool operator==(
131 const InnerLoopParams &other) const;
132
133 TS_API
134 bool operator!=(
135 const InnerLoopParams &other) const;
136
137 TS_API
138 bool IsValid() const;
139 };
140
141 // Extrapolation parameters for the ends of a spline beyond the knots.
142 struct Extrapolation
143 {
144 ExtrapMethod method = ExtrapHeld;
145 double slope = 0;
146 LoopMode loopMode = LoopNone;
147
148 public:
149 TS_API
150 Extrapolation();
151
152 TS_API
153 Extrapolation(ExtrapMethod method);
154
155 TS_API
156 bool operator==(
157 const Extrapolation &other) const;
158
159 TS_API
160 bool operator!=(
161 const Extrapolation &other) const;
162 };
163
164public:
165 TS_API
166 bool operator==(
167 const TsTest_SplineData &other) const;
168
169 TS_API
170 bool operator!=(
171 const TsTest_SplineData &other) const;
172
173 TS_API
174 void SetIsHermite(bool hermite);
175
176 TS_API
177 void AddKnot(
178 const Knot &knot);
179
180 TS_API
181 void SetKnots(
182 const KnotSet &knots);
183
184 TS_API
185 void SetPreExtrapolation(
186 const Extrapolation &preExtrap);
187
188 TS_API
189 void SetPostExtrapolation(
190 const Extrapolation &postExtrap);
191
192 TS_API
193 void SetInnerLoopParams(
194 const InnerLoopParams &params);
195
196 TS_API
197 bool GetIsHermite() const;
198
199 TS_API
200 const KnotSet&
201 GetKnots() const;
202
203 TS_API
204 const Extrapolation&
205 GetPreExtrapolation() const;
206
207 TS_API
208 const Extrapolation&
209 GetPostExtrapolation() const;
210
211 TS_API
212 const InnerLoopParams&
213 GetInnerLoopParams() const;
214
215 TS_API
216 Features GetRequiredFeatures() const;
217
218 TS_API
219 std::string GetDebugDescription(int precision = 6) const;
220
221private:
222 bool _isHermite = false;
223 KnotSet _knots;
224 Extrapolation _preExtrap;
225 Extrapolation _postExtrap;
226 InnerLoopParams _innerLoopParams;
227};
228
229PXR_NAMESPACE_CLOSE_SCOPE
230
231#endif