Loading...
Searching...
No Matches
spline.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_H
9#define PXR_BASE_TS_SPLINE_H
10
11#include "pxr/pxr.h"
12#include "pxr/base/ts/api.h"
13#include "pxr/base/ts/splineData.h"
14#include "pxr/base/ts/knotMap.h"
15#include "pxr/base/ts/knot.h"
16#include "pxr/base/ts/types.h"
17#include "pxr/base/ts/typeHelpers.h"
18#include "pxr/base/ts/eval.h"
19#include "pxr/base/vt/traits.h"
20#include "pxr/base/vt/value.h"
22#include "pxr/base/tf/type.h"
23
24#include <string>
25#include <memory>
26#include <iosfwd>
27
28PXR_NAMESPACE_OPEN_SCOPE
29
30class VtDictionary;
31
32
60{
61public:
69
72 TS_API
74
76 TS_API
77 TsSpline(TfType valueType);
78
79 TS_API
80 TsSpline(const TsSpline &other);
81
82 TS_API
83 TsSpline& operator=(const TsSpline &other);
84
85 TS_API
86 bool operator==(const TsSpline &other) const;
87
88 TS_API
89 bool operator!=(const TsSpline &other) const;
90
94
95 TS_API
96 static bool IsSupportedValueType(TfType valueType);
97
98 TS_API
99 TfType GetValueType() const;
100
101 template <typename T>
102 bool IsHolding() const;
103
106 TS_API
107 bool IsTimeValued() const;
108
112
113 TS_API
114 void SetCurveType(TsCurveType curveType);
115
116 TS_API
117 TsCurveType GetCurveType() const;
118
122
128 TS_API
130 const TsExtrapolation &extrap);
131
132 TS_API
133 TsExtrapolation GetPreExtrapolation() const;
134
140 TS_API
142 const TsExtrapolation &extrap);
143
144 TS_API
145 TsExtrapolation GetPostExtrapolation() const;
146
147 // Returns whether pre extrapolation is valid.
148 //
149 // Pre extrapolation is invalid if it is looping and loopBoundaryTime
150 // is set to a value for which the spline does not have an associated
151 // knot time.
152 //
153 // Invalid extrapolation regions evaluate to value-block.
154 TS_API
155 bool IsPreExtrapolationValid() const;
156
157 // Returns whether post extrapolation is valid.
158 //
159 // Post extrapolation is invalid if it is looping and loopBoundaryTime
160 // is set to a value for which the spline does not have an associated
161 // knot time.
162 //
163 // Invalid extrapolation regions evaluate to value-block.
164 TS_API
165 bool IsPostExtrapolationValid() const;
166
185
186 TS_API
187 void SetInnerLoopParams(
188 const TsLoopParams &params);
189
190 TS_API
191 TsLoopParams GetInnerLoopParams() const;
192
196
198 TS_API
200 const TsKnotMap &knots);
201
202 TS_API
203 bool CanSetKnot(
204 const TsKnot &knot,
205 std::string *reasonOut = nullptr) const;
206
209 TS_API
211 const TsKnot &knot,
212 GfInterval *affectedIntervalOut = nullptr);
213
217 TS_API
219
230 TS_API
231 TsKnotMap GetKnots(const GfInterval& timeInterval) const;
232
236 TS_API
238 TsTime time,
239 TsKnot *knotOut) const;
240
244
245 TS_API
246 void ClearKnots();
247
250 TS_API
252 TsTime time,
253 GfInterval *affectedIntervalOut = nullptr);
254
256 TS_API
258 VtValue defaultValue = VtValue(),
259 const GfInterval &interval = GfInterval::GetFullInterval());
260
264
269 TS_API
271
280 TS_API
282
302
303 TS_API
305 const GfInterval &interval) const;
306
315
332 TS_API
334 TsTime time,
335 GfInterval *affectedIntervalOut = nullptr);
336
344 TS_API
346 TsTime time,
347 std::string* reason = nullptr);
348
358
363 TS_API
364 static TsAntiRegressionMode GetAntiRegressionAuthoringMode();
365
369 TS_API
371
375 TS_API
377
384
385 template <typename T>
386 bool Eval(
387 TsTime time,
388 T *valueOut) const;
389
390 template <typename T>
391 bool EvalPreValue(
392 TsTime time,
393 T *valueOut) const;
394
395 template <typename T>
396 bool EvalDerivative(
397 TsTime time,
398 T *valueOut) const;
399
400 template <typename T>
401 bool EvalPreDerivative(
402 TsTime time,
403 T *valueOut) const;
404
405 template <typename T>
406 bool EvalHeld(
407 TsTime time,
408 T *valueOut) const;
409
410 template <typename T>
411 bool EvalPreValueHeld(
412 TsTime time,
413 T *valueOut) const;
414
415 TS_API
416 bool DoSidesDiffer(
417 TsTime time) const;
418
438 template <typename Vertex>
439 bool
441 const GfInterval& timeInterval,
442 double timeScale,
443 double valueScale,
444 double tolerance,
445 TsSplineSamples<Vertex>* splineSamples) const
446 {
447 return _Sample(timeInterval, timeScale, valueScale, tolerance,
448 splineSamples);
449 }
450
457 template <typename Vertex>
458 bool
460 const GfInterval& timeInterval,
461 double timeScale,
462 double valueScale,
463 double tolerance,
464 TsSplineSamplesWithSources<Vertex>* splineSamples) const
465 {
466 return _Sample(timeInterval, timeScale, valueScale, tolerance,
467 splineSamples);
468 }
469
471
477
509 TS_API
511 const GfInterval& interval,
512 TsExtrapolation preFallback = TsExtrapolation(TsExtrapValueBlock),
513 TsExtrapolation postFallback = TsExtrapolation(TsExtrapValueBlock)
514 ) const;
515
529 TS_API
530 TsSpline GetTimeScaled(double timeScale, double timeOffset) const;
531
561 TS_API
562 static
563 TsSpline Concatenate(const std::vector<TsSpline>& splines);
564
566
567
570
603 TS_API
604 GfInterval Diff(const TsSpline& other) const;
605
613 TS_API
615 const GfInterval& compareInterval) const;
616
620
621 TS_API
622 bool IsEmpty() const;
623
624 TS_API
625 bool HasValueBlocks() const;
626
628 TS_API
629 bool IsVarying() const;
630
632 TS_API
633 bool HasLoops() const;
634
635 TS_API
636 bool HasInnerLoops() const;
637
638 TS_API
639 bool HasExtrapolatingLoops() const;
640
642 TS_API
643 bool IsLinear() const;
644
646 TS_API
647 bool IsC0Continuous() const;
648
650 TS_API
651 bool IsG1Continuous() const;
652
654 TS_API
655 bool IsC1Continuous() const;
656
658 TS_API
660 const GfInterval &timeSpan,
661 std::pair<VtValue, VtValue> *rangeOut) const;
662
664 template <typename T>
666 const GfInterval &timeSpan,
667 std::pair<T, T> *rangeOut) const;
668
672
673 TS_API
674 bool HasValueBlockAtTime(
675 TsTime time) const;
676
678 TS_API
680 TsTime startTime) const;
681
683 TS_API
685 TsTime startTime) const;
686
688 TS_API
690 TsTime time,
691 VtValue defaultValue = VtValue()) const;
692
694
695public:
696 // Hash function. For now this is cheap, and only hashes by data pointer.
697 // If there are two identical but independent splines, they will hash
698 // unequal.
699 template <typename HashState>
700 friend void TfHashAppend(
701 HashState &h,
702 const TsSpline &spline)
703 {
704 h.Append(spline._data.get());
705 }
706
707private:
708 friend class TsRegressionPreventer;
709
710 // Direct access method used by TsRegressionPreventer.
711 void _SetKnotUnchecked(const TsKnot & knot);
712
713 template <typename SampleHolder>
714 bool _Sample(
715 const GfInterval& timeInterval,
716 double timeScale,
717 double valueScale,
718 double tolerance,
719 SampleHolder* splineSamples) const;
720
721 // External helpers provide direct data access for Ts implementation.
722 friend Ts_SplineData* Ts_GetSplineData(TsSpline &spline);
723 friend const Ts_SplineData* Ts_GetSplineData(const TsSpline &spline);
724
725 friend struct Ts_BinaryDataAccess;
726 friend struct Ts_SplineOffsetAccess;
727
728private:
729 // TsSpline takes ownership of `data`. The caller should not retain or
730 // delete `data` after this constructor is called.
731 TS_API
732 TsSpline(Ts_SplineData* data);
733
734 // Get data to read from. Will be either actual data or default data.
735 TS_API
736 const Ts_SplineData* _GetData() const;
737
738 // Ensure we have our own independent data, in preparation for writing. If
739 // a value type is passed, and we don't yet have typed data, ensure we have
740 // data of the specified type.
741 void _PrepareForWrite(TfType valueType = TfType());
742
743 template <typename T>
744 bool _Eval(
745 TsTime time,
746 T *valueOut,
747 Ts_EvalAspect aspect,
748 Ts_EvalLocation location) const;
749
750 // Update all the tangents based on the tangent algorithms in the knots and
751 // follow that with a call to AdjustRegressiveTangents() to remove any
752 // remaining regressive spline segments. Return true if any changes were
753 // made.
754 TS_API
755 bool _UpdateAllTangents();
756
757 // Update the tangents of a single knot based on its tangent algorithms and
758 // the regression prevention settings.
759 TS_API
760 bool _UpdateKnotTangents(const size_t knotIndex);
761
762private:
763 // Our parameter data. Copy-on-write. Null only if we are in the default
764 // state, with no knots, and all overall parameters set to defaults. To
765 // deal with the possibility of null data, call _GetData for reading, and
766 // _PrepareForWrite before writing.
767 std::shared_ptr<Ts_SplineData> _data;
768};
769
770// TsSpline supports value transforms.
772
774TS_API
775std::ostream& operator<<(std::ostream& out, const TsSpline &spline);
776
777// XXX: This should not be necessary. All it does is call std::swap. This is
778// here as a workaround for a downstream library that tries to call swap on
779// splines, with a "using namespace std" that doesn't appear to work when pxr
780// namespaces are in use.
781TS_API
782void swap(TsSpline &lhs, TsSpline &rhs);
783
784// For applying layer offsets.
785struct Ts_SplineOffsetAccess
786{
787 TS_API
788 static void ApplyOffsetAndScale(
789 TsSpline *spline,
790 const TsTime offset,
791 const double scale);
792};
793
794
796// TEMPLATE IMPLEMENTATIONS
797
798template <typename T>
799bool TsSpline::IsHolding() const
800{
801 if constexpr (!Ts_IsSupportedValueType<T>::value)
802 {
803 return false;
804 }
805
806 return GetValueType() == Ts_GetType<T>();
807}
808
809template <typename T>
810bool TsSpline::_Eval(
811 const TsTime time,
812 T* const valueOut,
813 const Ts_EvalAspect aspect,
814 const Ts_EvalLocation location) const
815{
816 const std::optional<double> result =
817 Ts_Eval(_GetData(), time, aspect, location);
818
819 if (!result)
820 {
821 return false;
822 }
823
824 *valueOut = T(*result);
825 return true;
826}
827
828// Implement a special case that will ensure the contents of the VtValue output
829// variable contain a value of the same type (double, float, GfHalf,
830// GfTimeCode, or GfDuration) as the spline.
831template <>
832TS_API
833bool TsSpline::_Eval(
834 const TsTime time,
835 VtValue* const valueOut,
836 const Ts_EvalAspect aspect,
837 const Ts_EvalLocation location) const;
838
839template <typename T>
840bool TsSpline::Eval(const TsTime time, T* const valueOut) const
841{
842 return _Eval(time, valueOut, Ts_EvalValue, Ts_EvalAtTime);
843}
844
845template <typename T>
846bool TsSpline::EvalPreValue(const TsTime time, T* const valueOut) const
847{
848 return _Eval(time, valueOut, Ts_EvalValue, Ts_EvalPre);
849}
850
851template <typename T>
852bool TsSpline::EvalDerivative(const TsTime time, T* const valueOut) const
853{
854 return _Eval(time, valueOut, Ts_EvalDerivative, Ts_EvalAtTime);
855}
856
857template <typename T>
858bool TsSpline::EvalPreDerivative(const TsTime time, T* const valueOut) const
859{
860 return _Eval(time, valueOut, Ts_EvalDerivative, Ts_EvalPre);
861}
862
863template <typename T>
864bool TsSpline::EvalHeld(const TsTime time, T* const valueOut) const
865{
866 return _Eval(time, valueOut, Ts_EvalHeldValue, Ts_EvalAtTime);
867}
868
869template <typename T>
870bool TsSpline::EvalPreValueHeld(const TsTime time, T* const valueOut) const
871{
872 return _Eval(time, valueOut, Ts_EvalHeldValue, Ts_EvalPre);
873}
874
875
876PXR_NAMESPACE_CLOSE_SCOPE
877
878#endif
#define VT_VALUE_TYPE_CAN_TRANSFORM(T)
A helper for specializing the above trait.
Definition traits.h:150
A basic mathematical interval class.
Definition interval.h:33
static GfInterval GetFullInterval()
Returns the full interval (-inf, inf).
Definition interval.h:325
TfType represents a dynamic runtime type.
Definition type.h:48
Extrapolation parameters for the ends of a spline beyond the knots.
Definition types.h:234
A knot belonging to a TsSpline.
Definition knot.h:43
The knots in a spline.
Definition knotMap.h:37
Inner-loop parameters.
Definition types.h:207
An authoring helper class that enforces non-regression in splines.
A mathematical description of a curved function from time to value.
Definition spline.h:60
TS_API bool CanBreakdown(TsTime time, std::string *reason=nullptr)
Test if a knot could be inserted by Breakdown at time.
TS_API TsSpline GetTruncated(const GfInterval &interval, TsExtrapolation preFallback=TsExtrapolation(TsExtrapValueBlock), TsExtrapolation postFallback=TsExtrapolation(TsExtrapValueBlock)) const
Returns a new spline resulting from truncation of the given spline to the given interval.
TS_API bool IsG1Continuous() const
Not yet implemented.
TS_API TsKnotMap GetKnots() const
Returns the spline's knots.
TS_API GfInterval Diff(const TsSpline &other, const GfInterval &compareInterval) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
TS_API bool IsLinear() const
Not yet implemented.
TS_API bool SetKnot(const TsKnot &knot, GfInterval *affectedIntervalOut=nullptr)
Incompletely implemented; affectedIntervalOut is not yet populated.
TS_API TsKnotMap GetKnotsWithInnerLoopsBaked() const
Return all the knots on the spline, including knots generated by inner looping.
bool GetValueRange(const GfInterval &timeSpan, std::pair< T, T > *rangeOut) const
Not yet implemented.
static TS_API TsSpline Concatenate(const std::vector< TsSpline > &splines)
Returns a spline resulting from the concatenation of the given splines.
TS_API TsKnotMap GetKnots(const GfInterval &timeInterval) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
TS_API bool GetValueRange(const GfInterval &timeSpan, std::pair< VtValue, VtValue > *rangeOut) const
Not yet implemented.
TS_API bool IsC1Continuous() const
Not yet implemented.
TS_API TsSpline(TfType valueType)
Creates a spline with a specified value type.
TS_API GfInterval Diff(const TsSpline &other) const
Compare two splines.
TS_API TsSpline GetTimeScaled(double timeScale, double timeOffset) const
Returns a new spline resulting from time scaling the knots of a given spline, applying first the scal...
TS_API void SetPreExtrapolation(const TsExtrapolation &extrap)
Sets pre extrapolation.
TS_API bool ClearRedundantKnots(VtValue defaultValue=VtValue(), const GfInterval &interval=GfInterval::GetFullInterval())
Not yet implemented.
bool Sample(const GfInterval &timeInterval, double timeScale, double valueScale, double tolerance, TsSplineSamples< Vertex > *splineSamples) const
Evaluates the value of the TsSpline over the given time interval, typically for drawing.
Definition spline.h:440
TS_API bool IsSegmentMonotonic(TsTime startTime) const
Not yet implemented.
TS_API TsKnotMap GetKnotsWithLoopsBaked(const GfInterval &interval) const
Return baked knots that will replicate this spline over the given time interval without any looping.
TS_API void RemoveKnot(TsTime time, GfInterval *affectedIntervalOut=nullptr)
Incompletely implemented; affectedIntervalOut is not yet populated.
TS_API bool IsTimeValued() const
Convenience function that returns true if the value type of the spline is GfTimeCode or GfDuration.
bool Sample(const GfInterval &timeInterval, double timeScale, double valueScale, double tolerance, TsSplineSamplesWithSources< Vertex > *splineSamples) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition spline.h:459
TS_API TsSpline()
Default constructor creates a spline without a value type.
TS_API bool AdjustRegressiveTangents()
Shorten any regressive tangents; or, if the current authoring mode is Contain, any tangents that exce...
TS_API bool Breakdown(TsTime time, GfInterval *affectedIntervalOut=nullptr)
Add a knot at the specified time.
TS_API bool HasLoops() const
Convenience for HasInnerLoops() || HasExtrapolatingLoops().
TS_API void SetKnots(const TsKnotMap &knots)
Clears existing knots and sets the given knots.
TS_API bool HasRegressiveTangents() const
Returns whether this spline has any tangents long enough to cause regression; or, if the current auth...
TS_API void SetPostExtrapolation(const TsExtrapolation &extrap)
Sets post extrapolation.
TS_API bool IsSegmentFlat(TsTime startTime) const
Not yet implemented.
TS_API bool IsVarying() const
Not yet implemented.
TS_API bool GetKnot(TsTime time, TsKnot *knotOut) const
Retrieves a copy of the knot at the specified time, if one exists.
TS_API bool IsKnotRedundant(TsTime time, VtValue defaultValue=VtValue()) const
Not yet implemented.
static TS_API TsAntiRegressionMode GetAntiRegressionAuthoringMode()
Returns the current effective anti-regression authoring mode.
TS_API bool BakeInnerLoops()
Bake inner loops in the spline.
TS_API bool IsC0Continuous() const
Not yet implemented.
TsSplineSamples<Vertex> holds a collection of piecewise linear polylines that approximate a TsSpline.
Definition types.h:298
TsSplineSamplesWithSources<Vertex> is a TsSplineSamples<Vertex> that also includes source information...
Definition types.h:321
A map with string keys and VtValue values.
Definition dictionary.h:52
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition value.h:90