Loading...
Searching...
No Matches
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_SPLINE_DATA_H
9#define PXR_BASE_TS_SPLINE_DATA_H
10
11#include "pxr/pxr.h"
12#include "pxr/base/ts/api.h"
13#include "pxr/base/ts/knotData.h"
14#include "pxr/base/ts/types.h"
15#include "pxr/base/ts/typeHelpers.h"
18#include "pxr/base/tf/type.h"
19#include "pxr/base/tf/stl.h"
20
21#include <vector>
22#include <unordered_map>
23#include <algorithm>
24#include <iterator>
25#include <utility>
26#include <cmath>
27
28PXR_NAMESPACE_OPEN_SCOPE
29
30class TsSpline;
31
32
33// Primary data structure for splines. Abstract; subclasses store knot data,
34// which is flexibly typed (double/float/half). This is the unit of data that
35// is managed by shared_ptr, and forms the basis of copy-on-write data sharing.
36//
37struct Ts_SplineData
38{
39public:
40 // If valueType is known, create a TypedSplineData of the specified type.
41 // If valueType is unknown, create a TypedSplineData<double> to store
42 // overall spline parameters in the absence of a value type; this assumes
43 // that when knots arrive, they are most likely to be double-typed. If
44 // overallParamSource is provided, it is a previous overall-only struct, and
45 // our guess about double was wrong, so we are transferring the overall
46 // parameters.
47 static Ts_SplineData* Create(
48 TfType valueType,
49 const Ts_SplineData *overallParamSource = nullptr);
50
51 TS_API virtual ~Ts_SplineData();
52
53public:
54 // Virtual interface for typed data.
55
56 virtual TfType GetValueType() const = 0;
57 virtual size_t GetKnotStructSize() const = 0;
58 virtual Ts_SplineData* Clone() const = 0;
59
60 virtual bool operator==(const Ts_SplineData &other) const = 0;
61
62 virtual void ReserveForKnotCount(size_t count) = 0;
63 virtual void PushKnot(
64 const Ts_KnotData *knotData,
65 const VtDictionary &customData) = 0;
66 // // Overload of PushKnot that offsets the time by timeOffset and values by
67 // // valueOffset. This allows us to unroll knots in loops without having to
68 // // dispatch based on TfType comparisons.
69 // virtual void PushKnot(
70 // const Ts_KnotData *knotData,
71 // const VtDictionary &customData,
72 // const double timeOffset,
73 // const double valueOffset) = 0;
74 virtual size_t SetKnot(
75 const Ts_KnotData *knotData,
76 const VtDictionary &customData) = 0;
77
78 // For ease of use by breakdown, double knot data to be set into any type of
79 // spline.
80 virtual size_t SetKnotFromDouble(
81 const Ts_TypedKnotData<double>* knotData,
82 const VtDictionary &customData) = 0;
83
84 virtual Ts_KnotData* CloneKnotAtIndex(size_t index) const = 0;
85 virtual Ts_KnotData* CloneKnotAtTime(TsTime time) const = 0;
86 virtual Ts_KnotData* GetKnotPtrAtIndex(size_t index) = 0;
87 virtual const Ts_KnotData* GetKnotPtrAtIndex(size_t index) const = 0;
88 virtual Ts_TypedKnotData<double>
89 GetKnotDataAsDouble(size_t index) const = 0;
90 virtual double GetKnotValueAsDouble(size_t index) const = 0;
91 virtual double GetKnotPreValueAsDouble(size_t index) const = 0;
92
93 virtual void ClearKnots() = 0;
94 virtual void RemoveKnotAtTime(TsTime time) = 0;
95
96 virtual void ApplyOffsetAndScale(
97 TsTime offset,
98 double scale) = 0;
99
100 virtual bool HasValueBlocks() const = 0;
101 virtual bool HasValueBlockAtTime(TsTime time) const = 0;
102
103 virtual bool UpdateKnotTangentsAtIndex(size_t index) = 0;
104
105public:
106 // Returns whether there is a valid inner-loop configuration. If
107 // firstProtoIndexOut is provided, it receives the index of the first knot
108 // in the prototype.
109 TS_API
110 bool HasInnerLoops(
111 size_t *firstProtoIndexOut = nullptr) const;
112
113 // Return the time at which pre-extrapolation ends and knot interpolation
114 // begins. Returns 0.0 if there are no knots. It is the caller's
115 // responsibility to ensure that there are knots before relying on the
116 // answer.
117 TsTime GetPreExtrapTime() const;
118
119 // Return the time at which knot interpolation ends and post-extrapolation
120 // begins. Returns 0.0 if there are no knots. It is the caller's
121 // responsibility to ensure that there are knots before relying on the
122 // answer.
123 TsTime GetPostExtrapTime() const;
124
125 // Return the value from which pre-extrapolation extrapolates (as a double).
126 // This accounts for dual valued knots and inner looping. Returns 0.0 if
127 // there are no knots. It is the caller's responsibility to ensure that
128 // there are knots before relying on the answer.
129 double GetPreExtrapValue() const;
130
131 // Return the value from which post-extrapolation extrapolates (as a
132 // double). This accounts for inner looping. Returns 0.0 if there are no
133 // knots. It is the caller's responsibility to ensure that there are knots
134 // before relying on the answer.
135 double GetPostExtrapValue() const;
136
137public:
138 // BITFIELDS - note: for enum-typed bitfields, we declare one bit more than
139 // is minimally needed to represent all declared enum values. For example,
140 // TsCurveType has only two values, so it should be representable in one
141 // bit. However, compilers are free to choose the underlying representation
142 // of enums, and some platforms choose signed values, meaning that we
143 // actually need one bit more, so that we can hold the sign bit. We could
144 // declare the enums with unsigned underlying types, but that runs into a
145 // gcc 9.2 bug. We can spare the extra bit; alignment means there is no
146 // difference in struct size.
147
148 // If true, our subtype is authoritative; we know our value type. If false,
149 // then no value type was provided at initialization, and no knots have been
150 // set. In the latter case, we exist only to store overall parameters, and
151 // we have been presumptively created as TypedSplineData<double>.
152 bool isTyped : 1;
153
154 // Overall spline parameters.
155 TsCurveType curveType : 2;
156 TfType valueType;
157 TsExtrapolation preExtrapolation;
158 TsExtrapolation postExtrapolation;
159 Ts_LoopParams loopParams;
160
161 // A duplicate of the knot times, so that we can maximize locality while
162 // performing binary searches for knots. This is part of the evaluation hot
163 // path; given an eval time, we must find either the knot at that time, or
164 // the knots before and after that time. The entries in this vector
165 // correspond exactly to the entries in the 'knots' vector in
166 // Ts_TypedSplineData. Times are unique and sorted in ascending order.
167 std::vector<TsTime> times;
168
169 // Custom data for knots, sparsely allocated, keyed by time.
170 std::unordered_map<TsTime, VtDictionary> customData;
171};
172
173
174// Concrete subclass of Ts_SplineData. Templated on T, the value type.
175//
176template <typename T>
177struct Ts_TypedSplineData final :
178 public Ts_SplineData
179{
180public:
181 TfType GetValueType() const override;
182 size_t GetKnotStructSize() const override;
183 Ts_SplineData* Clone() const override;
184
185 bool operator==(const Ts_SplineData &other) const override;
186
187 void ReserveForKnotCount(size_t count) override;
188 void PushKnot(
189 const Ts_KnotData *knotData,
190 const VtDictionary &customData) override;
191 // void PushKnot(
192 // const Ts_KnotData *knotData,
193 // const VtDictionary &customData,
194 // const double timeOffset,
195 // const double valueOffset) override;
196 size_t SetKnot(
197 const Ts_KnotData *knotData,
198 const VtDictionary &customData) override;
199
200 // For ease of use while splitting, double knot data to be set
201 // into any type of spline.
202 size_t SetKnotFromDouble(
203 const Ts_TypedKnotData<double>* knotData,
204 const VtDictionary &customData) override;
205
206 Ts_KnotData* CloneKnotAtIndex(size_t index) const override;
207 Ts_KnotData* CloneKnotAtTime(TsTime time) const override;
208 Ts_KnotData* GetKnotPtrAtIndex(size_t index) override;
209 const Ts_KnotData* GetKnotPtrAtIndex(size_t index) const override;
210 Ts_TypedKnotData<double>
211 GetKnotDataAsDouble(size_t index) const override;
212 double GetKnotValueAsDouble(size_t index) const override;
213 double GetKnotPreValueAsDouble(size_t index) const override;
214
215 void ClearKnots() override;
216 void RemoveKnotAtTime(TsTime time) override;
217
218 // Apply offset and scale to all spline data.
219 //
220 // If \p scale is negative, a coding error is generated. This is because
221 // the spline is not only scaled, but also time-reversed. Doing so can
222 // lead to incorrect evaluation results with any scenario where direction
223 // of time is assumed, like dual-value knots, inner looping,
224 // segment interpolation mode assignment, etc.
225 void ApplyOffsetAndScale(
226 TsTime offset,
227 double scale) override;
228
229 bool HasValueBlocks() const override;
230 bool HasValueBlockAtTime(TsTime time) const override;
231
232 bool UpdateKnotTangentsAtIndex(size_t index) override;
233
234public:
235 // Per-knot data.
236 std::vector<Ts_TypedKnotData<T>> knots;
237};
238
239
240// Data-access helpers for the Ts implementation. The untyped functions are
241// friends of TsSpline, and retrieve private data pointers.
242
243Ts_SplineData*
244Ts_GetSplineData(TsSpline &spline);
245
246const Ts_SplineData*
247Ts_GetSplineData(const TsSpline &spline);
248
249template <typename T>
250Ts_TypedSplineData<T>*
251Ts_GetTypedSplineData(TsSpline &spline);
252
253template <typename T>
254const Ts_TypedSplineData<T>*
255Ts_GetTypedSplineData(const TsSpline &spline);
256
257
259// TEMPLATE IMPLEMENTATIONS
260
261template <typename T>
262TfType Ts_TypedSplineData<T>::GetValueType() const
263{
264 if (!isTyped)
265 {
266 return TfType();
267 }
268
269 return valueType;
270}
271
272template <typename T>
273size_t Ts_TypedSplineData<T>::GetKnotStructSize() const
274{
275 return sizeof(Ts_TypedKnotData<T>);
276}
277
278template <typename T>
279Ts_SplineData*
280Ts_TypedSplineData<T>::Clone() const
281{
282 return new Ts_TypedSplineData<T>(*this);
283}
284
285template <typename T>
286bool Ts_TypedSplineData<T>::operator==(
287 const Ts_SplineData &other) const
288{
289 // Compare non-templated data.
290 if (isTyped != other.isTyped
291 || curveType != other.curveType
292 || preExtrapolation != other.preExtrapolation
293 || postExtrapolation != other.postExtrapolation
294 || loopParams != other.loopParams
295 || customData != other.customData)
296 {
297 return false;
298 }
299
300 // Downcast to our value type. If other is not of the same type, we're not
301 // equal.
302 const Ts_TypedSplineData<T>* const typedOther =
303 dynamic_cast<const Ts_TypedSplineData<T>*>(&other);
304 if (!typedOther)
305 {
306 return false;
307 }
308
309 // Compare all knots.
310 return knots == typedOther->knots;
311}
312
313template <typename T>
314void Ts_TypedSplineData<T>::ReserveForKnotCount(
315 const size_t count)
316{
317 times.reserve(count);
318 knots.reserve(count);
319}
320
321template <typename T>
322void Ts_TypedSplineData<T>::PushKnot(
323 const Ts_KnotData* const knotData,
324 const VtDictionary &customDataIn)
325{
326 const Ts_TypedKnotData<T>* const typedKnotData =
327 static_cast<const Ts_TypedKnotData<T>*>(knotData);
328
329 times.push_back(knotData->time);
330 knots.push_back(*typedKnotData);
331
332 if (!customDataIn.empty())
333 {
334 customData[knotData->time] = customDataIn;
335 }
336}
337
338// template <typename T>
339// void Ts_TypedSplineData<T>::PushKnot(
340// const Ts_KnotData *knotData,
341// const VtDictionary &customDataIn,
342// const double timeOffset,
343// const double valueOffset)
344// {
345// Ts_TypedKnotData<T> typedKnotData(
346// *static_cast<const Ts_TypedKnotData<T>*>(knotData));
347
348// typedKnotData.time += timeOffset;
349// typedKnotData.value += valueOffset;
350// typedKnotData.preValue += valueOffset;
351
352// // Clamp to prevent infinities in types smaller than double (especially
353// // GfHalf).
354// if constexpr(!std::is_same_v<T, double>) {
355// if (typedKnotData.value > std::numeric_limits<T>::max()) {
356// typedKnotData.value = std::numeric_limits<T>::max();
357// } else if (typedKnotData.value < std::numeric_limits<T>::lowest()) {
358// typedKnotData.value = std::numeric_limits<T>::lowest();
359// }
360
361// if (typedKnotData.preValue > std::numeric_limits<T>::max()) {
362// typedKnotData.preValue = std::numeric_limits<T>::max();
363// } else if (typedKnotData.preValue < std::numeric_limits<T>::lowest()) {
364// typedKnotData.preValue = std::numeric_limits<T>::lowest();
365// }
366// }
367
368// times.push_back(typedKnotData.time);
369// knots.push_back(typedKnotData);
370
371// if (!customDataIn.empty())
372// {
373// customData[knotData->time] = customDataIn;
374// }
375// }
376
377template <typename T>
378size_t Ts_TypedSplineData<T>::SetKnot(
379 const Ts_KnotData* const knotData,
380 const VtDictionary &customDataIn)
381{
382 const Ts_TypedKnotData<T>* const typedKnotData =
383 static_cast<const Ts_TypedKnotData<T>*>(knotData);
384
385 // Use binary search to find insert-or-overwrite position.
386 const auto it =
387 std::lower_bound(times.begin(), times.end(), knotData->time);
388 const size_t idx =
389 it - times.begin();
390 const bool overwrite =
391 (it != times.end() && *it == knotData->time);
392
393 // Insert or overwrite new time and knot data.
394 if (overwrite)
395 {
396 times[idx] = knotData->time;
397 knots[idx] = *typedKnotData;
398 }
399 else
400 {
401 times.insert(it, knotData->time);
402 knots.insert(knots.begin() + idx, *typedKnotData);
403 }
404
405 // Store customData, if any.
406 if (!customDataIn.empty())
407 {
408 customData[knotData->time] = customDataIn;
409 }
410
411 return idx;
412}
413
414template <typename T>
415size_t Ts_TypedSplineData<T>::SetKnotFromDouble(
416 const Ts_TypedKnotData<double>* knotData,
417 const VtDictionary &customDataIn)
418{
419 // If we have double data, just set it directly.
420 if constexpr(std::is_same_v<T, double>) {
421 return SetKnot(knotData, customDataIn);
422 }
423
424 Ts_TypedKnotData<T> typedData;
425
426 // Use operator= to copy base-class members. This is admittedly weird, but
427 // it will continue working if members are added to the base class.
428 static_cast<Ts_KnotData&>(typedData) =
429 static_cast<const Ts_KnotData&>(*knotData);
430
431 // We need to copy and convert the data from double to T. We don't want
432 // infinite values, so clamp to the largest possible finite value.
433 auto _Clamp_cast =
434 [](double v) -> T
435 {
436 if (v >= 0) {
437 return std::min(T(v), std::numeric_limits<T>::max());
438 } else {
439 return std::max(T(v), std::numeric_limits<T>::lowest());
440 }
441 };
442
443 // Convert and clamp the value fields.
444 typedData.value = _Clamp_cast(knotData->value);
445 typedData.preValue = _Clamp_cast(knotData->preValue);
446
447 // Slopes are tricky. If they overflow, we need to compute a new slope and a
448 // new width such that the new tangent end-point is as close as possible to
449 // the original tangent end point.
450
451 auto _ConvertTangent =
452 [&_Clamp_cast](double slope, double* width) -> T
453 {
454 T typedSlope = T(slope);
455 // std::isfinite<GfHalf>() is missing so use the helper from
456 // typeHelpers.h instead.
457 if (Ts_IsFinite(typedSlope)) {
458 return typedSlope;
459 }
460
461 // Convert both the slope and width to values that preserve the
462 // endpoint of the tangent as much as possible.
463 double height = *width * slope;
464
465 // typedSlope is infinite, clamp it to a finite value.
466 typedSlope = _Clamp_cast(slope);
467
468 // modify width to preserve the tangent's height.
469 *width = height / typedSlope;
470
471 return typedSlope;
472 };
473
474 typedData.preTanSlope = _ConvertTangent(knotData->preTanSlope,
475 &typedData.preTanWidth);
476 typedData.postTanSlope = _ConvertTangent(knotData->postTanSlope,
477 &typedData.postTanWidth);
478
479 return SetKnot(&typedData, customDataIn);
480}
481
482template <typename T>
483Ts_KnotData*
484Ts_TypedSplineData<T>::CloneKnotAtIndex(
485 const size_t index) const
486{
487 return new Ts_TypedKnotData<T>(knots[index]);
488}
489
490template <typename T>
491Ts_KnotData*
492Ts_TypedSplineData<T>::CloneKnotAtTime(
493 const TsTime time) const
494{
495 const auto it = std::lower_bound(times.begin(), times.end(), time);
496 if (it == times.end() || *it != time)
497 {
498 return nullptr;
499 }
500
501 const auto knotIt = knots.begin() + (it - times.begin());
502 return new Ts_TypedKnotData<T>(*knotIt);
503}
504
505template <typename T>
506Ts_KnotData*
507Ts_TypedSplineData<T>::GetKnotPtrAtIndex(
508 const size_t index)
509{
510 return &(knots[index]);
511}
512
513template <typename T>
514const Ts_KnotData*
515Ts_TypedSplineData<T>::GetKnotPtrAtIndex(
516 const size_t index) const
517{
518 return &(knots[index]);
519}
520
521// Depending on T, this is either a verbatim copy or an increase in precision.
522template <typename T>
523Ts_TypedKnotData<double>
524Ts_TypedSplineData<T>::GetKnotDataAsDouble(
525 const size_t index) const
526{
527 const Ts_TypedKnotData<T> &in = knots[index];
528 Ts_TypedKnotData<double> out;
529
530 // Use operator= to copy base-class members. This is admittedly weird, but
531 // it will continue working if members are added to the base class.
532 static_cast<Ts_KnotData&>(out) = static_cast<const Ts_KnotData&>(in);
533
534 // Copy derived members individually.
535 out.value = in.value;
536 out.preValue = in.preValue;
537 out.preTanSlope = in.preTanSlope;
538 out.postTanSlope = in.postTanSlope;
539
540 return out;
541}
542
543// Depending on T, this is either a verbatim copy or an increase in precision.
544template <typename T>
545double
546Ts_TypedSplineData<T>::GetKnotValueAsDouble(
547 const size_t index) const
548{
549 const Ts_TypedKnotData<T> &typedData = knots[index];
550 return typedData.value;
551}
552
553// Depending on T, this is either a verbatim copy or an increase in precision.
554template <typename T>
555double
556Ts_TypedSplineData<T>::GetKnotPreValueAsDouble(
557 const size_t index) const
558{
559 const Ts_TypedKnotData<T> &typedData = knots[index];
560 return typedData.GetPreValue();
561}
562
563template <typename T>
564void Ts_TypedSplineData<T>::ClearKnots()
565{
566 times.clear();
567 customData.clear();
568 knots.clear();
569}
570
571template <typename T>
572void Ts_TypedSplineData<T>::RemoveKnotAtTime(
573 const TsTime time)
574{
575 const auto it = std::lower_bound(times.begin(), times.end(), time);
576 if (it == times.end() || *it != time)
577 {
578 TF_CODING_ERROR("Cannot remove nonexistent knot from SplineData");
579 return;
580 }
581
582 const size_t idx = it - times.begin();
583 times.erase(it);
584 customData.erase(time);
585 knots.erase(knots.begin() + idx);
586
587 // Update the tangents on the knots either side of the one removed
588 if (idx > 0) {
589 UpdateKnotTangentsAtIndex(idx - 1);
590 }
591 if (idx < times.size()) {
592 UpdateKnotTangentsAtIndex(idx);
593 }
594}
595
596// Apply offset and scale to knot for knot fields that need to be
597// transformed regardless of whether the spline is time valued.
598template <typename T>
599static void _ApplyOffsetAndScaleToKnot(
600 Ts_TypedKnotData<T>* const knotData,
601 const TsTime offset,
602 const double scale)
603{
604 // Process knot time (absolute).
605 knotData->time = knotData->time * scale + offset;
606
607 // Process tangent widths (relative, strictly positive).
608 knotData->preTanWidth *= fabs(scale);
609 knotData->postTanWidth *= fabs(scale);
610}
611
612template <typename T>
613void Ts_TypedSplineData<T>::ApplyOffsetAndScale(
614 const TsTime offset,
615 const double scale)
616{
617 TF_VERIFY(scale != 0);
618
619 // XXX: Negative scaling doesn't perfectly invert splines. Some
620 // asymmetrical behavior persists. However, inverting a spline
621 // twice will recover the original spline's shape.
622 //
623 // - Evaluating in a held segment always produces the values from
624 // the preceding knot. After negative scaling, the value will
625 // be taken from the originally following knot instead.
626 // - Evaluating exactly at a dual-valued knot produces the ordinary
627 // value, not the pre-value. After negative scaling, the value
628 // will be taken from the original pre-value instead.
629
630 if (scale < 0 && HasInnerLoops())
631 {
632 TF_CODING_ERROR("Negative time scale factor is not compatible "
633 "inner loops. Please first bake inner loops.");
634 return;
635 }
636
637 if (scale < 0) {
638 // Flip pre and post extrapolation.
639 std::swap(preExtrapolation, postExtrapolation);
640 }
641
642 // The spline is changed in the time dimension only.
643 // Different parameters are affected in different ways:
644 // - Absolute times (e.g. knot times): apply scale and offset.
645 // - Relative times (e.g. tan widths): apply scale only.
646 // - Inverse relative (slopes): slope = height/width, so we apply 1/scale.
647 //
648 // Note: Time valued splines don't need slopes scaled, because their
649 // slopes are in units of time value / time value; the scale reduces to 1.
650
651 // Process loop boundary times if they exist.
652 if (preExtrapolation.IsLooping()
653 && preExtrapolation.loopBoundaryTime.has_value())
654 {
655 double& lbt = preExtrapolation.loopBoundaryTime.value();
656 lbt = lbt * scale + offset;
657 }
658 if (postExtrapolation.IsLooping()
659 && postExtrapolation.loopBoundaryTime.has_value())
660 {
661 double& lbt = postExtrapolation.loopBoundaryTime.value();
662 lbt = lbt * scale + offset;
663 }
664
665 // Process inner-loop params.
666 if (loopParams.protoEnd > loopParams.protoStart)
667 {
668 // Process start and end times (absolute).
669 loopParams.protoStart = loopParams.protoStart * scale + offset;
670 loopParams.protoEnd = loopParams.protoEnd * scale + offset;
671 }
672
673 // Process knot-times vector (absolute).
674 for (TsTime &time : times) {
675 time = time * scale + offset;
676 }
677
678 // Reverse knot order if scale is negative. This flips the spline about
679 // time 0. It swaps knots' pre and post values, changes interpolation
680 // knot sources accordingly, and reverses the items in the spline data
681 // `knots` and `times` vectors. This section does *not* scale any knot
682 // fields, including knot time.
683 if (scale < 0) {
684 std::reverse(times.begin(), times.end());
685
686 for (size_t i = 0; i < knots.size(); i++) {
687 size_t idx = knots.size() - 1 - i;
688 Ts_TypedKnotData<T> reversedKnot;
689 const Ts_TypedKnotData<T> knot = knots[idx];
690 if (knot.dualValued) {
691 reversedKnot.value = knot.preValue;
692 reversedKnot.dualValued = true;
693 reversedKnot.preValue = knot.value;
694 } else {
695 reversedKnot.value = knot.value;
696 }
697
698 reversedKnot.time = knot.time;
699 reversedKnot.preTanWidth = knot.postTanWidth;
700 reversedKnot.postTanWidth = knot.preTanWidth;
701 reversedKnot.preTanSlope = knot.postTanSlope;
702 reversedKnot.postTanSlope = knot.preTanSlope;
703 reversedKnot.preTanAlgorithm = knot.postTanAlgorithm;
704 reversedKnot.postTanAlgorithm = knot.preTanAlgorithm;
705
706 if (idx > 0) {
707 reversedKnot.nextInterp = knots[idx - 1].nextInterp;
708 }
709
710 knots[idx] = reversedKnot;
711 }
712 std::reverse(knots.begin(), knots.end());
713 }
714
715 // Scale and offset knot fields. Duplicate the logic that is applied
716 // unconditionally, so that we can rip through the entire vector just
717 // once, and we don't have to do the if-check on each iteration.
718 if (valueType == Ts_GetType<GfTimeCode>()) {
719 for (Ts_TypedKnotData<T> &knotData : knots)
720 {
721 _ApplyOffsetAndScaleToKnot(&knotData, offset, scale);
722
723 // Process time values (absolute).
724 knotData.value =
725 static_cast<T>(knotData.value * scale + offset);
726 knotData.preValue =
727 static_cast<T>(knotData.preValue * scale + offset);
728 }
729 } else if (valueType == Ts_GetType<GfDuration>()) {
730 for (Ts_TypedKnotData<T> &knotData : knots)
731 {
732 _ApplyOffsetAndScaleToKnot(&knotData, offset, scale);
733
734 // Process duration values (relative).
735 knotData.value =
736 static_cast<T>(knotData.value * scale);
737 knotData.preValue =
738 static_cast<T>(knotData.preValue * scale);
739 }
740 } else {
741 // Note that we scale slopes only for value types that are not
742 // time valued, because the units for time valued slopes are
743 // GfTimeCode over GfTimeCode, which reduces to 1 for any valid scale.
744 for (Ts_TypedKnotData<T> &knotData : knots) {
745 _ApplyOffsetAndScaleToKnot(&knotData, offset, scale);
746
747 // Process slopes (inverse relative).
748 knotData.preTanSlope /= scale;
749 knotData.postTanSlope /= scale;
750 }
751
752 // Scale extrapolation slopes if applicable (inverse relative).
753 if (preExtrapolation.mode == TsExtrapSloped)
754 {
755 preExtrapolation.slope /= scale;
756 }
757 if (postExtrapolation.mode == TsExtrapSloped)
758 {
759 postExtrapolation.slope /= scale;
760 }
761 }
762
763 // Re-index custom data. Times are adjusted absolutely.
764 if (!customData.empty())
765 {
766 std::unordered_map<TsTime, VtDictionary> newCustomData;
767 for (const auto &mapPair : customData) {
768 newCustomData[mapPair.first * scale + offset] = mapPair.second;
769 }
770 customData.swap(newCustomData);
771 }
772}
773
774template <typename T>
775bool Ts_TypedSplineData<T>::HasValueBlocks() const
776{
777 if (knots.empty())
778 {
779 return false;
780 }
781
782 if (preExtrapolation.mode == TsExtrapValueBlock
783 || postExtrapolation.mode == TsExtrapValueBlock)
784 {
785 return true;
786 }
787
788 for (const Ts_TypedKnotData<T> &knotData : knots)
789 {
790 if (knotData.nextInterp == TsInterpValueBlock)
791 {
792 return true;
793 }
794 }
795
796 return false;
797}
798
799template <typename T>
800bool Ts_TypedSplineData<T>::HasValueBlockAtTime(
801 const TsTime time) const
802{
803 // If no knots, no blocks.
804 if (knots.empty())
805 {
806 return false;
807 }
808
809 // Find first knot at or after time.
810 const auto lbIt =
811 std::lower_bound(times.begin(), times.end(), time);
812
813 // If time is after all knots, return whether we have blocked
814 // post-extrapolation.
815 if (lbIt == times.end())
816 {
817 return postExtrapolation.mode == TsExtrapValueBlock;
818 }
819
820 // If there is a knot at this time, return whether its segment has blocked
821 // interpolation.
822 if (*lbIt == time)
823 {
824 const auto knotIt = knots.begin() + (lbIt - times.begin());
825 return knotIt->nextInterp == TsInterpValueBlock;
826 }
827
828 // If time is before all knots, return whether we have blocked
829 // pre-extrapolation.
830 if (lbIt == times.begin())
831 {
832 return preExtrapolation.mode == TsExtrapValueBlock;
833 }
834
835 // Between knots. Return whether the segment that we're in has blocked
836 // interpolation.
837 const auto knotIt = knots.begin() + (lbIt - times.begin());
838 return (knotIt - 1)->nextInterp == TsInterpValueBlock;
839}
840
841template <typename T>
842bool Ts_TypedSplineData<T>::UpdateKnotTangentsAtIndex(size_t index)
843{
844 // XXX: Should we use PXR_PREFER_SAFETY_OVER_SPEED around this test?
845 if (!TF_VERIFY(index < knots.size(),
846 "Knot index (%zd) out of range [0 .. %zd)",
847 index, knots.size()))
848 {
849 return false;
850 }
851
852 Ts_TypedKnotData<T>* prevKnot = (index > 0 ? &knots[index - 1] : nullptr);
853 Ts_TypedKnotData<T>* knot = &knots[index];
854 Ts_TypedKnotData<T>* nextKnot = (index < knots.size() - 1
855 ? &knots[index + 1]
856 : nullptr);
857
858 return knot->UpdateTangents(prevKnot, nextKnot, curveType);
859}
860
861template <typename T>
862Ts_TypedSplineData<T>*
863Ts_GetTypedSplineData(TsSpline &spline)
864{
865 return static_cast<Ts_TypedSplineData<T>*>(
866 Ts_GetSplineData(spline));
867}
868
869template <typename T>
870const Ts_TypedSplineData<T>*
871Ts_GetTypedSplineData(const TsSpline &spline)
872{
873 return static_cast<Ts_TypedSplineData<T>*>(
874 Ts_GetSplineData(spline));
875}
876
877
878PXR_NAMESPACE_CLOSE_SCOPE
879
880#endif
Low-level utilities for informing users of various internal and external diagnostic conditions.
TfType represents a dynamic runtime type.
Definition type.h:48
Extrapolation parameters for the ends of a spline beyond the knots.
Definition types.h:249
A mathematical description of a curved function from time to value.
Definition spline.h:60
A map with string keys and VtValue values.
Definition dictionary.h:52
VT_API bool empty() const
true if the VtDictionary's size is 0.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition diagnostic.h:69
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition diagnostic.h:267