Moving Along Curve Specified Speed
Moving Along Curve Specified Speed
Contents
1 Introduction 2
1
1 Introduction
A frequently asked question in computer graphics is how to move an object (or camera eyepoint) along a
parameterized curve with constant speed. The method to do this requires the curve to be reparameterized
by arc length. A more general problem is to specify the speed of the object at every point of the curve. This
also requires the concept of reparameterization of the curve. I cover both topics in this document, including
discussion of the theory, implementations of the numerical algorithms and performance issues. The concepts
apply to curves in any dimension. The curves may be defined as a collection of contiguous curves such as
piecewise Bézier curves.
Consider a parametric curve, X(t), for t ∈ [tmin , tmax ]. The variable t is referred to as the curve parameter.
The curve may be thought of as the path of a particle whose position is X(t) at time t. The velocity V(t)
of the particle is the rate of change of position with respect to time, a quantity measured by the derivative
dX
V(t) = (1)
dt
The velocity vector V(t) is tangent to the curve at the position X(t). The speed σ(t) of the particle is the
length of the velocity vector
dX
σ(t) = |V(t)| =
(2)
dt
The requirement that the speed of the particle be constant for all time is stated mathematically as σ(t) = c
for all t, where c is a specified positive constant. The particle is said to travel with constant speed along the
curve. If c = 1, the particle is said to travel with unit speed along the curve. When the velocity vector has
unit length for all time, the curve is said to be parameterized by arc length. In this case, the curve parameter
is typically named s, and the parameter is referred to as the arc length parameter. The value s is a measure
of distance along the curve. The arc-length parameterization of the curve is written as X(s), where s ∈ [0, L]
and L is the total length of the curve.
For example, a circular path in the plane that has center (0, 0) and radius 1 is parameterized by X(s) =
(cos(s), sin(s)) for s ∈ [0, 2π). The velocity is V(s) = (− sin(s), cos(s)) and the speed is σ(s) = |dV/ds| =
|(− sin(s), cos(s))| = 1, which is constant for all s. The particle travels with unit speed around the circle, so
X(s) is an arc-length parameterization of the circle.
√
A different parameterization of the circle is Y(t) = (cos(t2 ), sin(t
2
)) for t ∈ [0, 2π). The velocity is
V(t) = (−2t sin(t2 ), 2t cos(t2 )) and the speed is σ(t) = |dV/dt| = (−2t sin(t2 ), 2t cos(t2 ) = 2t. The speed
increases with t, so the particle does not move with constant speed around the circle.
Suppose you were given Y(t), the parameterization of the circle for which the particle does not travel with
constant speed, and you want to change the parameterization so that the particle does travel with constant
speed. That is, you want to relate the parameter t√to the arc-length parameter s, say by a function t = f (s),
so that X(s) = Y(t). In our circle example, t = s. The process of determining the relationship t = f (s)
is referred to as reparameterization by arc length. How do you actually find this relationship between t and
s? Generally, there is no closed-form expression for the function f . Numerical methods must be used to
compute t for each specified s.
2
3 Reparameterization by Arc Length
Let X(s) be an arc-length parameterization of a curve. Let Y(t) be another parameterization of the same
curve, in which case Y(t) = X(s) implies a relationship between t and s. We may apply the chain rule from
Calculus to obtain
dY dX ds
= (3)
dt ds dt
Computing lengths and using the convention that speeds are nonnegative, we have
dY dX ds dX ds ds
dt ds dt ds dt = dt
= = (4)
When time t = tmin , the arc length is s = g(tmin ) = 0. This makes sense because the particle has not yet
moved—the distance traveled is zero. When time t = tmax , the arc length is s = g(tmax ) = L, which is the
total distance L traveled along the curve.
Given the time t, we can determine the corresponding arc length s from the integration. However, what is
needed is the inverse problem. Given an arc length s, we want to know the time t at which this arc length
occurs; that is, you first decide the distance the object should move along the curve and then figure out the
time t at which that occurs. The position on the curve an arc length of s is Y(t). To compute t, we must
invert the function g to obtain
t = g −1 (s) (6)
Inverting g in terms of elementary functions is usually not possible, so we have to rely on numerical methods
to compute t ∈ [tmin , tmax ] given a value of s ∈ [0, L].
Define F (t) = g(t) − s. Given a value s, the problem is now to find a value t so that F (t) = 0. This is
a root-finding problem that you might try solving using Newton’s method. If t0 ∈ [tmin , tmax ] is an initial
guess for t, Newton’s method produces a sequence
F (ti )
ti+1 = ti − , i≥0 (7)
F 0 (ti )
where
0 dF dg dY
F (t) = = = (8)
dt dt dt
The iterate t1 is determined by t0 , F (t0 ), and F 0 (t0 ). Evaluation of F 0 (t0 ) is straightforward because you
already have a formula for Y(t) and can compute dY/dt from it. Evaluation of F (t0 ) requires computing
g(t0 ), an integration that can be approximated using standard numerical integrators.
3
A reasonable choice for the initial iterate is
s
t0 = tmin + (tmax − tmin ) (9)
L
where the value s/L is the fraction of total arc length at which the particle should be located. The initial
iterate uses that same fraction applied to the parameter interval [tmin , tmax ]. The subsequent iterates are
computed until either F (ti ) is sufficiently close to zero or until a maximum number of iterates has been
computed.
There is a potential problem when using only Newton’s method. The derivative F (t) is a nondecreasing
function because its derivative F 0 (t) = |dY/dt| is nonnegative. The second derivative is F 00 (t). If F 00 (t) ≥ 0
for all t ∈ [tmin , tmax ], then the function is said to be convex and the Newton iterates are guaranteed to
converge to the root. However, F 00 (t) can be negative, which might lead to Newton iterates outside the
domain [tmin , tmax ]. To avoid this problem, a hybrid of Newton’s method and bisection should be used. A
root-bounding interval is maintained along with the iterates. A candidate Newton’s iterate is computed. If
it is inside the current root-bounding interval, it is accepted as the next time estimate. If it is outside the
interval, the midpoint of the interval is used instead. Regardless of whether a Newton iterate or bisection
is used, the root-bounding interval is updated, so the interval length strictly decreases over time. Listing 1
contains pseudocode for the hybrid algorithm.
Listing 1. The pseudocode listed here inverts the integral of equation (5) to obtain the time t for a
specified arc length s. The algorithm is a hybrid of Newton’s method and bisection.
// The t-domain for the curve is [tmin , tmax ].
R e a l tmin , tmax ;
4
Real F = ArcLength ( t ) − s ;
// Update the root-bounding interval and test for containment of the candidate.
i f (F > 0)
{
// F > 0 implies tCandidate < t ≤ upper.
upper = t ;
i f ( t C a n d i d a t e <= l o w e r )
{
// The candidate is outside the root-bounding interval, so use bisection instead.
t = ( upper + lower ) / 2;
}
else
{
// The candidate is in [lower,upper].
t = tCandidate ;
}
}
else // F < 0
{
// F < 0 implies lower ≤ t < tCandidate.
lower = t ;
i f ( t C a n d i d a t e >= u p p e r )
{
// The candidate is outside the root-bounding interval, so use bisection instead.
t = ( upper + lower ) / 2;
}
else
{
// The candidate is in [lower,upper].
t = tCandidate ;
}
}
}
// A root was not found according to the specified number of iterations and tolerance. You might
// want to increase imax or epsilon or the integration accuracy. However, in this application it is
// likely that the time values are oscillating because of the limited numerical precision of floating-point
// numbers. It is safe to report the last computed time as the t-value corresponding to the s-value.
return t ;
}
The function Integral(tmin, t, f()) is any numerical integrator that computes the integral of f (τ ) over the
interval τ ∈ [tmin , t]. A good integrator will choose an optimal set of τ -samples in the interval to obtain an
accurate estimate. This gives the Newton’s-method approach a global flavor. In comparison, setting up the
problem to use a numerical solver for differential equations has a local flavor—you have to apply the solver
for many consecutive τ samples before reaching a final estimate for t given s. I prefer the Newton’s-method
approach because generally t can be computed faster than with differential equation solvers.
5
3.2 Numerical Solution: Solving a Differential Equation
where the last equality defines the function F (t). The independent variable is s and the dependent variable is
t. The differential equation is referred to as autonomous, because the independent variable does not appear
in the right-hand function.
Equation (10) may be solved numerically with any standard differential equation solver; I prefer Runge–
Kutta methods. For the sake of illustration only, Euler’s method may be used. If s is the user-specified arc
length and if n steps of the solver are desired, then the step size is h = s/n. Setting t0 = tmin , the iterates
for Euler’s method are
h
ti+1 = ti + hF (ti ) = ti + , i≥0 (11)
|dY(ti )/dt|
The final iteration gives you t = tn , the t-value that approximates s. In Newton’s method, you iterate until
convergence, a process that you hope will happen quickly. In Euler’s method, you iterate n times to compute
a sequence of ordered t-values. For a reasonable numerical approximation to t, n might have to be quite
large.
Pseudocode for computing t for a specified s that uses a 4th-order Runge–Kutta method is shown in Listing
2.
Listing 2. Pseudocode for a 4th-order Runge–Kutta method to solve numerically the equation (10).
// The input is s ∈ [0, L] and the output is t ∈ [tmin , tmax ].
Real GetCurveParameter ( Real s )
{
R e a l t = tmin , h = s / imax ;
f o r ( i n t i = 1 ; i <= imax ; ++i )
{
R e a l k1 = h / Speed ( t ) ;
R e a l k2 = h / Speed ( t + k1 / 2 ) ;
R e a l k3 = h / Speed ( t + k2 / 2 ) ;
R e a l k4 = h / Speed ( t + k3 ) ;
t += ( k1 + 2 ∗ ( k2 + k3 ) + k4 ) / 6 ;
}
return t ;
}
In the previous section, we had a parameterized curve, Y(t), and asked how to relate the time t to the arc
length s in order to obtain a parameterization by arc length, X(s) = Y(t). Equivalently, the idea may be
thought of as choosing the time t so that a particle traveling along the curve arrives at a specifed distance s
along the curve at the given time.
In this section, we start with a parameterized curve, Y(u) for u ∈ [umin , umax ], and determine a parameteri-
zation by time t, say, X(t) = Y(u) for t ∈ [tmin , tmax ], so that the speed at time t is a specified function σ(t).
6
Notice that in the previous problem, the time variable t is already that of the given curve. In this problem,
the curve parameter u is not about time—it is solely whatever parameter was convenient for parameterizing
the curve. We need to relate the time variable t to the curve parameter u to obtain the desired speed.
Let L be the arc length of the curve; that is,
Z umax
dY
L= du du
(12)
umin
This may be computed using a numerical integrator. A particle travels along this curve over time t ∈
[tmin , tmax ], where the minimum and maximum times are user-specified values.
The speed, as a function of time, is also user-specified. Let the function be named σ(t). From calculus and
physics, the distance traveled by the particle along a path is the integral of the speed over that path. Thus,
we have the constraint Z tmax
L= σ(t) dt (13)
tmin
In practice, it may be quite tedious to choose σ(t) to exactly meet the constraint of Equation (13). Moreover,
when moving a camera along a path, the choice of speed might be stated in words rather than as equations,
say, “Make the camera travel slowly at the beginning of the curve, speed up in the middle of the curve, and
slow down at the end of the curve.” In this scenario, most likely the function σ(t) is chosen so that its graph
in the (t, σ) plane has a certain shape. Once you commit to a mathematical representation, you can apply
a scaling factor to satisfy the constraint of Equation (13). Specifically, let σ̄(t) be the function you have
chosen based on obtaining a desired shape for its graph. The actual speed function you use is
Lσ̄(t)
σ(t) = R tmax (14)
tmin
σ̄(t) dt
By the construction, the integral of σ(t) over the time interval is the length L.
The direct method of solution is to choose a time t ∈ [tmin , tmax ] and compute the distance ` traveled by the
particle for that time, Z t
`= σ(τ ) dτ ∈ [0, L] (15)
tmin
Now use the method described earlier for choosing the curve parameter u that gets you to this arc length.
That is, you must numerically invert the integral equation
Z u
dY(µ)
`= du dµ
(16)
umin
Here is where you need not to get confused by the notation. In the section on reparameterization by arc
length, the arc length parameter was named s and the curve parameter was named t. Now we have the
arc length parameter named ` and the curve parameter named u. The technical difficulty in discussing
simultaneously reparameterizing by arc length and reparameterizing to obtain a desired speed is that time
t comes into play in two different ways, so you will just have to bear with the notation that accommodates
all variables involved.
7
An equivalent formulation that leads to the same numerical solution is the following. Because X(t) = Y(u),
we may apply the chain rule from calculus to obtain
dX dY du
= (17)
dt du dt
Now compute the lengths to obtain
dX dY du
σ(t) = = (18)
dt du dt
Once again we assume that u and t increase jointly—the particle can never retrace portions of the curve,
in which case du/dt ≥ 0 and the absolute value signs on that term are not necessary. We may separate the
variables and rewrite this equation as
Z u Z t
dY(µ)
dµ = σ(τ ) dτ = ` (19)
du
umin tmin
The right-most equality is just our definition of the length traveled by the particle along the curve through
time t. This last equation is the same as Equation (16). Pseudocode for computing u given t is contained in
Listing 3.
Listing 3. Pseudocode for computing t from u so that at position Y)(u) the speed is the user-specified
function σ(t).
// The u-domain for the curve is [umin , umax ].
R e a l umin , umax ;
8
The function Integral is a numerical integrator. The function GetCurveParameter was discussed previously in
this document, which may be implemented using either Newton’s method for root finding or a Runge–Kutta
method for solving a differential equation.
Equation (17) may be solved directly using a differential equation solver. The equation may be written as
du σ(t)
= = F (t, u) (20)
dt |dY(u)/du|
where the right-most equality defines the function F . The independent variable is t and the dependent
variable is u. This is a nonautonomous differential equation, since the right-hand side depends on both the
independent and dependent variables.
Pseudocode for computing t for a specified u that uses a 4th-order Runge–Kutta method is shown in Listing
4.
Listing 4. Pseudocode for a 4th-order Runge–Kutta method to solve numerically the equation (20).
// The input is t ∈ [tmin , tmax ] and the output is u ∈ [umin , umax ].
R e a l GetU ( R e a l t )
{
R e a l h = ( t − t m i n ) / imax , u = umin , t = t m i n ;
f o r ( i n t i = 1 ; i <= imax ; ++i )
{
R e a l k1 = h ∗ Sigma ( t ) / LengthDY ( u ) ;
R e a l k2 = h ∗ Sigma ( t + h / 2 ) / LengthDY ( u + k1 / 2 ) ;
R e a l k3 = h ∗ Sigma ( t + h / 2 ) / LengthDY ( u + k2 / 2 ) ;
R e a l k4 = h ∗ Sigma ( t + h ) / LengthDY ( u + k3 ) ;
t += h ;
u += ( k1 + 2 ∗ ( k2 + k3 ) + k4 ) / 6 ;
}
return u ;
}
This section describes how to implement the function GetCurveParameter(s) for a continuous curve defined as
a collection of p curve segments,
Y1 (t), t ∈ [t0 , t1 ]
Y2 (t), t ∈ [t1 , t2 ]
.. ..
Y(t) = . . (21)
Yp−1 (t), t ∈ [tp−2 , tp−1 ]
Y (t),
p t ∈ [tp−1 , tp ]
9
where the ti are monotonic increasing and specified by the user. The curve domain is [tmin , tmax ] where
tmin = t0 and tmax = tp . For continuity at the user-specified times, the curve segments match at their
endpoints: Yi (ti ) = Yi+1 (ti ) for all relevant i. Let L be the total length of the curve Y(t).
Given an arc length s ∈ [0, L], we wish to compute t ∈ [tmin , tmax ] such that Y(t) is the position that is a
distance s measured along the curve from the initial point Y(tmin ) = Y1 (t0 ). The pseudocode of Listing 2 is
applicable here, but the implementations of the functions Y(t), DYDT(t), Speed(t) and ArcLength(t) hide the
details of selecting the index i for which t ∈ [ti , ti+1 ]. In a naive implementation, index i is computed by each
function, which is inefficient. Moreover, the function ArcLength(t) is called multiple times in the pseudocode,
which amounts to computing the lookup indices multiple times. To avoid the redundant calculations, we
can precompute the arc lengths for each curve segment and the partial sums of the arc lengths. The hybrid
Newton-bisection method always computes t-iterates for the same curve segment, so the computation of
index i is performed only once.
Let Li denote the length of the curve segment Yi (t); that is
Z ti
dYi
Li = dt dt
(22)
ti−1
For compact summation indexing, define L0 = 0. If the user-specified arc length is s = 0, the returned t-value
is t = tmin . If the user-specified arc length is s = L, the returned
Pi−1 t-value is P
tmax . For the user-specified arc
i
length s ∈ (0, L), search for the index i ∈ {1, . . . , p} so that j=0 Lj ≤ s < j=0 Lj .
Now we can restrict our attention to the curve segment Yi (t). We need to know how far along this segment
to choose the the position so that it is located s − Li−1 units of distance from the initial curve point Yi (ti−1 ).
That is, we must solve for t in the integral equation
i−1 Z t
X dYi (τ )
s− Lj = dt dτ
(24)
j=0 ti−1
Listing 5. The pseudocode listed here inverts the integral of equation (5) to obtain the time t for a
specified arc length s when the curve Y(t) is defined as a collection of curve segments.
// The user-specified number of curve segments, p ≥ 1.
int p;
// The user-specified increasing sequence of times t0 through tp for the curve segment endpoints.
Real time [ p +1];
10
// The velocity is dYi (t)/dt for t ∈ [ti−1 , ti ].
P o i n t DYDT( i n t i , R e a l t ) ;
// Precompute the lengths of the curve segments and the partial sums of the lengths.
R e a l l e n g t h S e g m e n t [ p +1] , l e n g t h S u m [ p + 1 ] ;
lengthSegment [ 0 ] = 0;
lengthSum [ 0 ] = 0 ;
f o r ( i n t i = 1 ; i <= p ; ++i )
{
lengthSegment [ i ] = ArcLength ( i , time [ i ] ) ;
l e n g t h S u m [ i ] = l e n g t h S u m [ i −1] + l e n g t h S e g m e n t [ i ] ;
}
Real l e n g t h T o t a l = lengthSum [ p ] ;
i f ( s >= l e n g t h T o t a l )
{
r e t u r n tmax ;
}
// Choose the initial guess for Newton’s method using s and t relative to the segment i.
Real segS = s − lengthSum [ i −1];
Real segL = lengthSegment [ i ] ;
R e a l segTMin = t i m e [ i − 1 ] ;
R e a l segTMax = t i m e [ i ] ;
R e a l segT = segTMin + ( segTMax − segTMin ) ∗ ( s e g S / s e g L ) ;
11
i f ( Abs ( F ) <= e p s i l o n )
{
// |F (tseg )| is close enough to zero. Report tseg as the time at which length s is attained.
r e t u r n segT ;
}
// Update the root-bounding interval and test for containment of the candidate.
i f (F > 0)
{
// F > 0 implies segTCandidate < segT ≤ upper.
u p p e r = segT ;
i f ( s e g T C a n d i d a t e <= l o w e r )
{
// The candidate is outside the root-bounding interval, so use bisection instead.
segT = ( u p p e r + l o w e r ) / 2 ;
}
else
{
// The candidate is in [lower,upper].
segT = s e g T C a n d i d a t e ;
}
}
else // F < 0
{
// F < 0 implies lower ≤ segT < segTCandidate.
l o w e r = segT ;
i f ( s e g T C a n d i d a t e >= u p p e r )
{
// The candidate is outside the root-bounding interval, so use bisection instead.
segT = ( u p p e r + l o w e r ) / 2 ;
}
else
{
// The candidate is in [lower,upper].
segT = s e g T C a n d i d a t e ;
}
}
}
// A root was not found according to the specified number of iterations and tolerance. You might
// want to increase imax or epsilon or the integration accuracy. However, in this application it is
// likely that the time values are oscillating because of the limited numerical precision of floating-point
// numbers. It is safe to report the last computed time as the t-value corresponding to the s-value.
r e t u r n segT ;
}
In practice, calling GetCurveParameter(s) many times in a real-time application can be costly. An alternative
is to compute a set of pairs (s, t) and fit this set with a polynomial-based function. This function is evaluated
with fewer cycles but in exchange for less accurate t-values.
As a simple example, suppose you are willing to use a piecewise linear polynomial to approximate the
relationship between s and t. You select n + 1 samples and compute ti for si = Li/n for 0 ≤ i ≤ n.
Compute the index i ≥ 1 for which si−1 ≤ s < si , and then compute t for which (t − ti−1 )/(ti − ti−1 ) =
(s − si−1 )/(si − si−1 ).
Naturally, you can use higher-degree fits, say, with piecewise Bézier polynomial curves. Listing 6 contains
pseudocode for the fitting and t-from-s computations.
12
Listing 6. Pseudocode for reparameterization by arc length of a curve using fitting of (s, t) samples.
i n t n = <u s e r −s p e c i f i e d q u a n t i t y >;
R e a l s S a m p l e [ n +1] , t S a m p l e [ n +1] , t s S l o p e [ n + 1 ] ;
sSample [ 0 ] = 0 ;
t s S l o p e [ 0 ] = tmin ;
s t S l o p e [ 0 ] = 0 ; // u n u s e d i n t h e c o d e
f o r ( i = 1 ; i < n ; i ++)
{
sSample [ i ] = L ∗ i / n ;
tSample [ i ] = GetCurveParameter ( sSample [ i ] ) ;
t s S l o p e [ i ] = ( t S a m p l e [ i ] − t S a m p l e [ i −1]) / ( s S a m p l e [ i ] − s S a m p l e [ i − 1 ] ) ;
}
sSample [ n ] = L ;
t S a m p l e [ n ] = tmax ;
t s S l o p e [ n ] = ( t S a m p l e [ n ] − t S a m p l e [ n −1]) / ( s S a m p l e [ n ] − s S a m p l e [ n − 1 ] ) ;
int i ;
f o r ( i = 1 ; i < n ; i ++)
{
i f ( s < sSample [ i ] )
{
break ;
}
}
// We know t h a t s S a m p l e [ i −1] <= s < s S a m p l e [ i ] .
R e a l t = t S a m p l e [ i −1] + t s S l o p e [ i ] ∗ ( s − s S a m p l e [ i − 1 ] ) ;
return t ;
}
If instead you used a polynomial fit, say t = f (s), then the line of code before the return statement would be
Real t = PolynomialFit(s); where Real PolynomialFit (Real s) is the implementation of f (s) and uses the samples
tSample[] and sSample[] in its evaluation.
13