C++ 向心 Catmull-Rom 样条如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1085617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does the centripetal Catmull–Rom spline work?
提问by tweetypi
From this site, which seems to have the most detailed information about Catmull-Rom splines, it seems that four points are needed to create the spline. However, it does not mention how the points p0 and p3 affect the values between p1 and p2.
从这个似乎有关于 Catmull-Rom 样条曲线的最详细信息的站点来看,似乎需要四个点来创建样条曲线。但是,它没有提到点 p0 和 p3 如何影响 p1 和 p2 之间的值。
Another question I have is how would you create continuous splines? Would it be as easy as defining the points p1, p2 to be continuous with p4, p5 by making p4 = p2 (that is, assuming we have p0, p1, p2, p3, p4, p5, p6, ..., pN).
我的另一个问题是如何创建连续样条曲线?通过使 p4 = p2(即,假设我们有 p0、p1、p2、p3、p4、p5、p6、...、pN,将点 p1、p2 定义为与 p4、p5 连续是否一样简单)。
A more general question is how would one calculate tangents on Catmull-Rom splines? Would it have to involve taking two points on the spline (say at 0.01, 0.011) and getting the tangent based on Pythagoras, given the position coordinates those input values give?
一个更普遍的问题是如何计算 Catmull-Rom 样条上的切线?考虑到这些输入值给出的位置坐标,它是否必须涉及在样条上取两个点(比如在 0.01、0.011)并根据毕达哥拉斯获得切线?
采纳答案by George Phillips
Take a look at equation 2 -- it describes how the control points affect the line. You can see points P0
and P3
go into the equation for plotting points along the curve from P1
to P2
. You'll also see that the equation gives P1
when t == 0
and P2
when t == 1
.
看看等式 2——它描述了控制点如何影响线。您可以看到点P0
并P3
进入方程,以便沿着从P1
到的曲线绘制点P2
。您还将看到该等式给出了P1
whent == 0
和P2
when t == 1
。
This example equation can be generalized. If you have points R0
, R1
, … RN
then you can plot the points between RK
and RK + 1
by using equation 2 with P0 = RK - 1
, P1 = RK
, P2 = RK + 1
and P3 = RK + 2
.
这个示例方程可以推广。如果你点R0
,R1
...RN
那么你就可以绘制的点RK
,并RK + 1
利用公式2 P0 = RK - 1
,P1 = RK
,P2 = RK + 1
和P3 = RK + 2
。
You can't plot from R0
to R1
or from RN - 1
to RN
unless you add extra control points to stand in for R - 1
and RN + 1
. The general idea is that you can pick whatever points you want to add to the head and tail of a sequence to give yourself all the parameters to calculate the spline.
除非您添加额外的控制点来代替and ,否则您无法绘制 from R0
toR1
或 from RN - 1
to 。一般的想法是,您可以选择要添加到序列的头部和尾部的任何点,以便为自己提供计算样条的所有参数。RN
R - 1
RN + 1
You can join two splines together by dropping one of the control points between them. Say you have R0
, R1
, …, RN
and S0
, S1
, … SM
they can be joined into R0
, R1
, …, RN - 1
, S1
, S2
, … SM
.
您可以通过删除它们之间的控制点之一来将两条样条线连接在一起。假设您有R0
, R1
, ..., RN
and S0
, S1
, ...SM
它们可以连接成R0
, R1
, ..., RN - 1
, S1
, S2
, ... SM
。
To compute the tangent at any point just take the derivative of equation 2.
要计算任意点的切线,只需对等式 2 求导即可。
回答by Eric
The Wikipedia articlegoes into a little bit more depth. The general form of the spline takes as input 2 control points with associated tangent vectors. Additional spline segments can then be added provided that the tangent vectors at the common control points are equal, which preserves the C1 continuity.
在维基百科的文章进入多一点点深度。样条的一般形式将 2 个控制点和相关的切向量作为输入。如果公共控制点处的切向量相等,则可以添加额外的样条线段,从而保持 C1 连续性。
In the specific Catmull-Rom form, the tangent vector at intermediate points is determined by the locations of neighboring control points. Thus, to create a C1 continuous spline through multiple points, it is sufficient to supply the set of control points and the tangent vectors at the first and last control point. I think the standard behavior is to use P1 - P0 for the tangent vector at P0 and PN - PN-1 at PN.
在特定的 Catmull-Rom 形式中,中间点的切向量由相邻控制点的位置决定。因此,要通过多个点创建 C1 连续样条,提供控制点集和第一个和最后一个控制点的切向量就足够了。我认为标准行为是使用 P1 - P0 作为 P0 处的切线向量和 PN - PN-1 处的 PN。
According to the Wikipedia article, to calculate the tangent at control point Pn, you use this equation:
根据维基百科文章,要计算控制点 Pn 处的切线,您可以使用以下等式:
T(n) = (P(n - 1) + P(n + 1)) / 2
This also answers your first question. For a set of 4 control points, P1, P2, P3, P4, interpolating values between P2 and P3 requires information form all 4 control points. P2 and P3 themselves define the endpoints through which the interpolating segment must pass. P1 and P3 determine the tangent vector the interpolating segment will have at point P2. P4 and P2 determine the tangent vector the segment will have at point P3. The tangent vectors at control points P2 and P3 influence the shape of the interpolating segment between them.
这也回答了你的第一个问题。对于一组 4 个控制点 P1、P2、P3、P4,在 P2 和 P3 之间插入值需要来自所有 4 个控制点的信息。P2 和 P3 本身定义了插值段必须通过的端点。P1 和 P3 确定插值段在点 P2 处的切向量。P4 和 P2 确定线段在点 P3 处的切向量。控制点 P2 和 P3 处的切向量影响它们之间的插值段的形状。