计算R中的2D样条曲线

时间:2015-01-22 16:33:05

标签: r plot bezier cubic-spline catmull-rom-curve

我试图计算一个类似Bezier的样条曲线,该曲线通过一系列x-y坐标。一个例子就像Matlab中cscvn函数的以下输出(example link):

enter image description here

我相信(不再维护)grid包用于执行此操作(grid.xspline功能?),但我还无法安装该软件包的存档版本,并且没有找到任何与我想要的完全一致的例子。

bezier包也看起来很有希望,但速度非常慢,我也无法做到这一点:

library(bezier)

set.seed(1)
n <- 10
x <- runif(n)
y <- runif(n)
p <- cbind(x,y)
xlim <- c(min(x) - 0.1*diff(range(x)), c(max(x) + 0.1*diff(range(x))))
ylim <- c(min(y) - 0.1*diff(range(y)), c(max(y) + 0.1*diff(range(y))))
plot(p, xlim=xlim, ylim=ylim)
text(p, labels=seq(n), pos=3)

bp <- pointsOnBezier(cbind(x,y), n=100)
lines(bp$points)
arrows(bp$points[nrow(bp$points)-1,1], bp$points[nrow(bp$points)-1,2],
  bp$points[nrow(bp$points),1], bp$points[nrow(bp$points),2]
)

enter image description here

正如您所看到的,它不会通过除结束值之外的任何点。

我非常感谢这里的一些指导!

3 个答案:

答案 0 :(得分:12)

没有必要真正使用grid。您可以从xspline包中访问graphics

从您的代码和@mrflick的shape开始:

set.seed(1)
n <- 10
x <- runif(n)
y <- runif(n)
p <- cbind(x,y)
xlim <- c(min(x) - 0.1*diff(range(x)), c(max(x) + 0.1*diff(range(x))))
ylim <- c(min(y) - 0.1*diff(range(y)), c(max(y) + 0.1*diff(range(y))))
plot(p, xlim=xlim, ylim=ylim)
text(p, labels=seq(n), pos=3)

你只需要一个额外的行:

xspline(x, y, shape = c(0,rep(-1, 10-2),0), border="red")

enter image description here

答案 1 :(得分:10)

这可能不是最好的方法,位grid肯定不是非活动的。它包含在R安装的默认包中。它是用于绘制像lattice和ggplot这样的库的底层图形引擎。您不需要安装它,您应该只能加载它。以下是我将您的代码翻译为使用grid.xpline

的方式
set.seed(1)
n <- 10
x <- runif(n)
y <- runif(n)
xlim <- c(min(x) - 0.1*diff(range(x)), c(max(x) + 0.1*diff(range(x))))
ylim <- c(min(y) - 0.1*diff(range(y)), c(max(y) + 0.1*diff(range(y))))

library(grid)
grid.newpage()
pushViewport(viewport(xscale=xlim, yscale=ylim))
grid.points(x, y, pch=16, size=unit(2, "mm"), 
    default.units="native")
grid.text(seq(n), x,y, just=c("center","bottom"), 
    default.units="native")
grid.xspline(x, y, shape=c(0,rep(-1, 10-2),0), open=TRUE, 
    default.units="native")
popViewport()

导致

enter image description here

请注意,网格非常低级,因此使用它并不是一件容易的事,但它确实可以让您更好地控制绘制的内容和位置。

如果您想沿曲线提取点而不是绘制点,请查看?xsplinePoints帮助页面。

答案 2 :(得分:3)

感谢所有有帮助的人。我总结了经验教训以及其他一些方面。

Catmull-Rom样条与立方B样条

xspline函数中的负形状值返回Catmull-Rom类型样条曲线,样条曲线穿过x-y点。正值返回三次B型样条曲线。零值返回一个尖角。如果给出单个形状值,则将其用于所有点。端点的形状始终被视为尖角(shape = 0),其他值不会影响端点处的最终样条:

# Catmull-Rom spline vs. cubic B-spline
plot(p, xlim=extendrange(x, f=0.2), ylim=extendrange(y, f=0.2))
text(p, labels=seq(n), pos=3)
# Catmull-Rom spline (-1)
xspline(p, shape = -1, border="red", lwd=2) 
# Catmull-Rom spline (-0.5)
xspline(p, shape = -0.5, border="orange", lwd=2) 
# cubic B-spline (0.5)
xspline(p, shape = 0.5, border="green", lwd=2) 
# cubic B-spline (1)
xspline(p, shape = 1, border="blue", lwd=2)
legend("bottomright", ncol=2, legend=c(-1,-0.5), title="Catmull-Rom spline", col=c("red", "orange"), lty=1)
legend("topleft", ncol=2, legend=c(1, 0.5), title="cubic B-spline", col=c("blue", "green"), lty=1)

enter image description here

xspline中提取外部绘图的结果

这需要一些搜索,但诀窍是将参数draw=FALSE应用于xspline

# Extract xy values
plot(p, xlim=extendrange(x, f=0.1), ylim=extendrange(y, f=0.1))
text(p, labels=seq(n), pos=3)
spl <- xspline(x, y, shape = -0.5, draw=FALSE) 
lines(spl)
arrows(x0=(spl$x[length(spl$x)-0.01*length(spl$x)]), y0=(spl$y[length(spl$y)-0.01*length(spl$y)]),
       x1=(spl$x[length(spl$x)]), y1=(spl$y[length(spl$y)])
)

enter image description here

相关问题