将ashape3d类转换为mesh3d

时间:2016-04-06 06:55:07

标签: r rgl

有人可以帮我转换一个' ashape3d' class class to class' mesh3d&#39 ;?

在ashape3d中,三角形四面体面存储在不同的字段中。由于我不认为有一个函数可以同时从三角形和四面体创建一个mesh3d对象,我尝试了以下(伪代码):

model <- ashape3d(rtorus(1000, 0.5, 2),alpha=0.25)

vert <- model$x[model$vert[,2]==1,]
vert <- cbind(vert,rep(1,nrow(vert)))
tria <- model$triang[model$triang[,4]==1,1:3]
tetr <- model$tetra[model$tetra[,6]==1,1:4]

m3dTria <- tmesh3d(vertices=vert , indices=tria)
m3dTetr <- qmesh3d(vertices=vert , indices=tetr)
m3d     <- mergeMeshes(m3dTria,m3dTetr)

plot.ashape3d(model) # works fine
plot3d(m3d)          # Error in x$vb[1, x$it] : subscript out of bounds

有人有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

我最近需要这样做,并发现了这个悬而未决的问题。弄清楚发生了什么的最简单方法是查看plot.ashape3d并阅读ashape3d的文档。 plot.ashape3d只绘制三角形。

rgl包具有通用as.mesh3d功能。这为该泛型函数定义了一种方法。

as.mesh3d.ashape3d <- function(x, ...) {
  if (length(x$alpha) > 1)
    stop("I don't know how to handle ashape3d objects with >1 alpha value")
  iAlpha = 1

  # from help for ashape3d
  # for each alpha, a value (0, 1, 2 or 3) indicating, respectively, that the 
  # triangle is not in the alpha-shape or it is interior, regular or singular 
  # (columns 9 to last)

  # Pick the rows for which the triangle is regular or singular
  selrows = x$triang[, 8 + iAlpha] >= 2
  tr <- x$triang[selrows, c("tr1", "tr2", "tr3")]

  rgl::tmesh3d(
    vertices = t(x$x),
    indices = t(tr),
    homogeneous = FALSE
  )
}

您可以尝试上面的数据

model <- ashape3d(rtorus(1000, 0.5, 2),alpha=0.25)
plot(model, edges=F, vertices=F)

library(rgl)
model2=as.mesh3d(model)
open3d()
shade3d(model2, col='red')