Catmull-Rom Splines

Catmull-Rom Splines

http://algorithmist.net/docs/catmullrom.pdf

 

TechNote TN-06-001Catmull-Rom SplinesJim ArmstrongSingularityJanuary 2006This is the fifthin a series of TechNotes on the subject of applied curve mathematics in Adobe FlashTM. Each TechNote provides themathematical foundation for a set of Actionscript examples. Interpolating Splines and AnimationAn important consideration in computer-generated tweening is smooth motion between irregularly spaced keyframes. Cubic splines are often an ideal solution. Anatural cubic spline produces a 2C-continuous interpolation, although the parametric version is computationally expensive. Often, a 1C-continuous curve is adequate. Catmull-Rom splines, as often referred to in both online and printed literature, are actually a specific instance of a family of splines derived by Catmull and Rom [1]. These splines exhibit 1Ccontinuity and have a simple piecewise construction. Relationship to Hermite InterpolationIt seems as if almost every cubic curve construction bears some resemblance to Hermite curves. Consider the case of fitting a cubic curve between two points. In addition to passing through the two points, two additional constraints are required to define the curve. Suppose derivative values are specified at each endpoint, as illustrated in the following diagram.This situation is very similar to the Hermite curveexcept that the above curve does not extend beyond the control points. As the tangents influence the shape of the curve, tangents may either be explicitly provided or inferredfrom additional control points.Single-Curve ConstructionConsider the derivation of a single curve,P(t), that interpolates the points0Pand 1Pabove. Suppose that additional control points, 1−Pand 2Pare specified as shown below.P(t)is a cubic curve with the conditions that t= 0 yields 0Pand t= 1 yields 1P. The auxiliary points 1−Pand 2Pare used to adjust the shape of the curve by implicitly defining tangents,)()(022110PPDPPD−=−=−ααwhere αis between 0 and 1. This definition makes the tangent at each endpoint parallel to the chord between adjacent control points. The general equation of the curve isdctbtattP+++=23)([1]The coefficients can be determined from the geometric constraints provided by endpoint and tangent specifications.The process is similar to the derivation of Hermite interpolation,)()1(')()0(')1()0(021110PPPPPPPPPP−=−===−αα[2]Since cbtattP++=23)('2cbaPcPdcbaPdP++==+++==23)1(')0(')1()0([3]If 0)0('SP=and 1)1('SP=, equation[3] yields, ⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡dcbaSSPP01230100111110001010[4a]From equation [2],⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−21011010000001000010PPPPSSPPαααα[4b]Equations [4a] and [4b] together imply⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⇒⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−αααααααα0000010000100123010011111000000001000010012301001111100012101dcbaPPPPdcba[4c]Every now and then, it is useful to work one of these out in full. If⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡=0123010011111000Mthen IMM=−1. Let ijcCM==−1from which⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡1000010000100001012301001111100044434241343332312423222114131211cccccccccccccccc[5a]Although 16 equations in 16 unknowns seemintimidating by hand,halfthe equationsare trivial.10003334323144434241========ccccccccThe remaining equations are123023023023001034241433231332221231211144342414433323134232221241312111=++=++=++=++=+++=+++=+++=+++ccccccccccccccccccccccccccccCranking through the algebra yields⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−−−=−00010100123311221M⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−−−−−−=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−−−=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⇒0010002332220000010000100001010012331122ααααααααααααααdcba[5b]Equation [5b] represents the basis matrix for the so-called cardinal splines. The parameter, α, represents the spline’s ‘tension’. As the tension parameter approaches 1, the bend at each knot is less, as if the spline was a rope thatwas being tightened while still passing through all the knots. A tension value of ½ is commonly used to represent the Catmull-Rom spline.Substituting into [5b] yields the Catmull-Rom basis⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−−−−=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−−−−=00200101145213312/1002/2002/102/12/12/42/52/22/12/32/32/1cB[6]Substituting [6] into the standard matrix equation for a cubic curve yields⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−−−−=−2101230020010114521331]1[)(PPPPttttP[7]Single Curve DemoThis demo illustrates the use of the CRBasicclass(Flash source no longer online). This class generates a single cubic curve from 0Pto 1P. The auxiliary control points, 1−Pand 2Pare interactively generated using tangent handles. The demo allows two control points to be added to the drawing area. Tangent handles are automatically generated. Drag the handles to new locations to study the effect on the curve. Load the CRBasicclass and study the __coef()method to see the direct application of equation [7]. InterpolatingMultiple Points and ParameterizationOnce the generation of a single cubic curve is understood, the next step is to apply the process in a piecewise manner to fit a number of datapoints. The i-th curve segment is generated between points iPand 1+iPusing the sequence of points, 1−iP, iP, 1+iP, and 2+iP, as illustrated below.As each new segment is generated, the slopes match at the interior control points, providing 1Ccontinuity. There are two new issues, however, in generating the complete curve.Auxiliary Control PointsIf the curve is to interpolate all user-supplied knots, additional control points must be supplied at each end of the curve. There are several approaches for resolving this issue. The user may wish interactive control over setting the points. If the spline is to be used in animation, it is valuable to have the control points automatically generated. Some implementations duplicate the end knots. This can cause kinks in the generated curve. Another method is reflection. For example, the segment 01PP−is reflected about 0Pto generate 1−P. While this ensures the curve is ‘moving’ in an intuitive direction at each endpoint, it can sometimes result in ‘pinching’, as will be seen in the examples.The CatmullRomclass allows the user to either explicitly specify auxiliary control points or have them automatically computed. For illustrative purposes, the latter option is performed with a simple reflection. Either mode can be selected through a setter function, i.e.var __myCurve:CatmullRom = new CatmullRom();__myCurve.tangent = CatmullRom.EXPLICIT;or__myCurve.tangent = CatmullRom.AUTO;The default tangent mode is AUTO.ParameterizationThe curve is piecewise cubic. Global parameter values of tbetween 0 and 1 must be mapped to the appropriate segment and a local parameter value between 0 and 1.For purposes of this discussion, tis used for the normal (global) curve parameter and *tis used for the local parameter. If there are a total of Nknots, then1*0*)(,11)(,00−===>===⇒=NPtPttPtPttare the trivial mappings. As the user varies tbetween 0 and 1, the appropriate segment index, i, and local parameter value, *tmust be computed. The specific computation determines how the curve is parameterized. A common parameterization is chord-length. The cumulative distance between each knot is divided by the total length to produce a parameter that varies from 0 to 1. The user-specified value of tis compared segment-by-segment to determine the appropriate curve segment. The parameter value is mapped into a local range of [0,1] in order to be evaluated by equation [7].A different parameterization is theuniform formula,))1(()1()1,1)1((min(*tNfloortNtNtNfloori−−−=−+−=The latter parameterization is the default (__myCurve.parameterize = UNIFORM). For comparative purposes, the chord-length parameterization can be selected with __myCurve.parameterize = CHORD_LENGTH. The two parameterizations apply to the same set of geometric constraints, so the same curve plot is generated.While this parameterization is usefulfor drawing the spline, it has a drawback in terms of animation. Ideally, an object moving along the curve should do so with constant velocity. Equal increments in parameter, t, should result in equal distance along the curve. This is not achieved withchord-length or similar parameterization, as illustrated in the following example. Consider the control points,(-10,0) (0,200) (100,200) and (110,300). The Catmull-Rom spline fitting these control points is shown below (in the Flash Stage coordinate space) with markers placed at a distance of 0.1 in t.Notice that the distance between markers is not constant near the bends at the second and third control points, even though they appear pretty close to constant elsewhere. As tis varied uniformly between 0 and 1, an object moving along the curve will move faster and then slower around those bends than it would at other points on the curve.The cure to this situation is an arc-length parameterization, which is a topic that will have to wait for a future TechNote.Piecewise Cubic ImplementationThe cubic coefficients for each segment are precomputed, based on the specified method for endpoint tangents. For the i-th control point, the direct application of equation [7] for the x-coordinate yieldsixiixiiiixiiiixxdxxcxxxxbxxxxa24523311211211=−=−+−=+−+−=−+++−++−An example is illustrated below.Notice how the curve ‘pinches’ slightly against the chord at each endpoint as a result of the pure reflection of the first and last segments about the endpoints.The curve moves very smoothly through each knot. The Catmull-Rom spline has the additional benefit that modification of a single knot only affects the curve near that knot (local control). Sincethe Catmull-Rom blending functions do not sum to unity, the convex hull property is lost.An arc-length parameterization and a little more flexibility with the auxiliary control points will provide the basis for a general path-tweening class.Closed LoopsThe auxiliary control points at thebeginning and end of the Catmull-Rom spline provide flexibility for a variety of curve shapes, including closed loops. A common misconception among beginning students is that these endpoints somehow form ‘tangent handles’. Recall that the tangent at anyknot is parallel to the chord between previous and successive knots. One of the simplest strategies to create a smooth, closed loop is to place the outermost control points along the chords into and out of the first knot. This is illustrated in the following diagram.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值