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
70 {
71 p0 += delta;
72 t0 += delta;
73 t1 += delta;
74 p1 += delta;
75
76 return *this;
77 }
78
80 Ts_Segment& operator +=(const double timeDelta)
81 {
82 p0[0] += timeDelta;
83 t0[0] += timeDelta;
84 t1[0] += timeDelta;
85 p1[0] += timeDelta;
86
87 return *this;
88 }
89
93 {
94 p0 -= delta;
95 t0 -= delta;
96 t1 -= delta;
97 p1 -= delta;
98
99 return *this;
100 }
101
103 Ts_Segment& operator -=(const double timeDelta)
104 {
105 p0[0] -= timeDelta;
106 t0[0] -= timeDelta;
107 t1[0] -= timeDelta;
108 p1[0] -= timeDelta;
109
110 return *this;
111 }
112
114 template <typename T>
115 Ts_Segment operator +(const T& delta) const
116 {
117 Ts_Segment result = *this;
118 result += delta;
119 return result;
120 }
121
122 // Subtraction operator
123 template <typename T>
124 Ts_Segment operator -(const T& delta) const
125 {
126 Ts_Segment result = *this;
127 result -= delta;
128 return result;
129 }
130
131 // Unary negation operator - negates the times, not the values.
132 Ts_Segment operator -() const
133 {
134 Ts_Segment result = *this;
135 result.p0[0] = -result.p0[0];
136 result.t0[0] = -result.t0[0];
137 result.t1[0] = -result.t1[0];
138 result.p1[0] = -result.p1[0];
139
140 // Now swap the points back into the correct order (p0 always has the
141 // smallest time value).
142 using std::swap;
143 swap(result.p0, result.p1);
144 swap(result.t0, result.t1);
145
146 return result;
147 }
148
150 bool operator ==(const Ts_Segment& rhs) const
151 {
152 return (p0 == rhs.p0 &&
153 t0 == rhs.t0 &&
154 t1 == rhs.t1 &&
155 p1 == rhs.p1 &&
156 interp == rhs.interp);
157 }
158
160 bool operator !=(const Ts_Segment& rhs) const
161 {
162 return !(*this == rhs);
163 }
164
165 bool IsClose(const Ts_Segment& other, const double tolerance) const
166 {
167 return (interp == other.interp &&
168 GfIsClose(p0, other.p0, tolerance) &&
169 GfIsClose(t0, other.t0, tolerance) &&
170 GfIsClose(t1, other.t1, tolerance) &&
171 GfIsClose(p1, other.p1, tolerance));
172 }
173
176 TS_API
177 double _ComputeDerivative(double u) const;
178};
179
180// For testing purposes.
181TS_API
182std::ostream& operator <<(std::ostream&, const Ts_SegmentInterp);
183TS_API
184std::ostream& operator <<(std::ostream&, const Ts_Segment&);
185
186PXR_NAMESPACE_CLOSE_SCOPE
187
188#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:92
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:69
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:150
bool operator!=(const Ts_Segment &rhs) const
Not equivalent.
Definition: segment.h:160
Ts_Segment operator+(const T &delta) const
Addition operator.
Definition: segment.h:115