Loading...
Searching...
No Matches
segment.h
1//
2// Copyright 2025 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_SEGMENT_H
9#define PXR_BASE_TS_SEGMENT_H
10
11#include "pxr/pxr.h"
12
13#include "pxr/base/ts/api.h"
14#include "pxr/base/ts/types.h"
15
16#include "pxr/base/gf/vec2d.h"
17
18PXR_NAMESPACE_OPEN_SCOPE
19
22//
24enum class Ts_SegmentInterp
25{
26 ValueBlock, //< This segment explicitly has no value
27 Held, //< The value is always the starting value
28 Linear, //< Interpolate linearly from start to end
29 Bezier, //< The segment uses Bezier interpolation
30 Hermite, //< The segment uses Hermite interpolation
31 PreExtrap, //< Linear extrapolation from -infinity to
32 //< the end value with a fixed slope
33 PostExtrap //< Linear extrapolation to +infinity from
34 //< the start value with a fixed slope
35};
36
57{
58 GfVec2d p0 = GfVec2d(0);
59 GfVec2d t0 = GfVec2d(0);
60 GfVec2d t1 = GfVec2d(0);
61 GfVec2d p1 = GfVec2d(0);
62 Ts_SegmentInterp interp = Ts_SegmentInterp::ValueBlock;
63
65 void SetInterp(TsInterpMode interpMode, TsCurveType curveType);
66
68 TsInterpMode GetInterpMode() const;
69
73 {
74 p0 += delta;
75 t0 += delta;
76 t1 += delta;
77 p1 += delta;
78
79 return *this;
80 }
81
83 Ts_Segment& operator +=(const double timeDelta)
84 {
85 p0[0] += timeDelta;
86 t0[0] += timeDelta;
87 t1[0] += timeDelta;
88 p1[0] += timeDelta;
89
90 return *this;
91 }
92
96 {
97 p0 -= delta;
98 t0 -= delta;
99 t1 -= delta;
100 p1 -= delta;
101
102 return *this;
103 }
104
106 Ts_Segment& operator -=(const double timeDelta)
107 {
108 p0[0] -= timeDelta;
109 t0[0] -= timeDelta;
110 t1[0] -= timeDelta;
111 p1[0] -= timeDelta;
112
113 return *this;
114 }
115
117 template <typename T>
118 Ts_Segment operator +(const T& delta) const
119 {
120 Ts_Segment result = *this;
121 result += delta;
122 return result;
123 }
124
125 // Subtraction operator
126 template <typename T>
127 Ts_Segment operator -(const T& delta) const
128 {
129 Ts_Segment result = *this;
130 result -= delta;
131 return result;
132 }
133
134 // Unary negation operator - negates the times, not the values.
135 Ts_Segment operator -() const
136 {
137 Ts_Segment result = *this;
138 result.p0[0] = -result.p0[0];
139 result.t0[0] = -result.t0[0];
140 result.t1[0] = -result.t1[0];
141 result.p1[0] = -result.p1[0];
142
143 // Now swap the points back into the correct order (p0 always has the
144 // smallest time value).
145 using std::swap;
146 swap(result.p0, result.p1);
147 swap(result.t0, result.t1);
148
149 return result;
150 }
151
153 bool operator ==(const Ts_Segment& rhs) const
154 {
155 return (p0 == rhs.p0 &&
156 t0 == rhs.t0 &&
157 t1 == rhs.t1 &&
158 p1 == rhs.p1 &&
159 interp == rhs.interp);
160 }
161
163 bool operator !=(const Ts_Segment& rhs) const
164 {
165 return !(*this == rhs);
166 }
167
168 bool IsClose(const Ts_Segment& other, const double tolerance) const
169 {
170 return (interp == other.interp &&
171 GfIsClose(p0, other.p0, tolerance) &&
172 GfIsClose(t0, other.t0, tolerance) &&
173 GfIsClose(t1, other.t1, tolerance) &&
174 GfIsClose(p1, other.p1, tolerance));
175 }
176
179 TS_API
180 double _ComputeDerivative(double u) const;
181};
182
183// For testing purposes.
184TS_API
185std::ostream& operator <<(std::ostream&, const Ts_SegmentInterp);
186TS_API
187std::ostream& operator <<(std::ostream&, const Ts_Segment&);
188
189PXR_NAMESPACE_CLOSE_SCOPE
190
191#endif
Basic type for a vector of 2 double components.
Definition vec2d.h:46
bool GfIsClose(GfColor const &c1, GfColor const &c2, double tolerance)
Tests for equality of the RGB tuple in a color with a given tolerance, returning true if the length o...
Definition color.h:114
Ts_Segment represents one section of a spline.
Definition segment.h:57
void SetInterp(TsInterpMode interpMode, TsCurveType curveType)
Set interp from a TsInterpMode and TsCurveType.
Ts_Segment & operator-=(const GfVec2d &delta)
Subtract the (timeDelta, valueDelta) contained in the GfVec2d argument from the segment's time and va...
Definition segment.h:95
Ts_Segment & operator+=(const GfVec2d &delta)
Add the (timeDelta, valueDelta) contained in the GfVec2d argument to the segment's time and value.
Definition segment.h:72
TS_API double _ComputeDerivative(double u) const
Compute dv/dt at the value u in the interval [0..1].
bool operator==(const Ts_Segment &rhs) const
Compare for equivalence.
Definition segment.h:153
bool operator!=(const Ts_Segment &rhs) const
Not equivalent.
Definition segment.h:163
TsInterpMode GetInterpMode() const
Get TsInterpMode from this segment's interp.
Ts_Segment operator+(const T &delta) const
Addition operator.
Definition segment.h:118