matlab 在R中打印三维对象

4zcjmb1e  于 2022-11-15  发布在  Matlab
关注(0)|答案(1)|浏览(190)

我正在尝试创建3D形状绘图,就像在MatLab中使用patch命令所做的那样,如下所示:

我已经使用expand.grid创建了我的坐标数据:

library(pracma)
n1=60
n2=60
n3=60
data = expand.grid(x=linspace(-3,3,n1),y=linspace(-3,3,n2),z=linspace(-3,3,n3))

我已经使用这些数据填充3D矩阵以获得3D矩阵V,它定义了形状:

someData <- rep(0, 60*60*60)  
V <- array(someData, c(60, 60, 60))
count = 1
lambda = c(2,1,1)
for (ii in 1:n1){
    for (jj in 1:n2){
        for (kk in 1:n3){
            V[ii, jj, kk] = t(as.matrix(lambda))%*%as.matrix(c(data$x[count],data$y[count],data$z[count]))
            count = count+1
        }
    }
}

然而,我正在努力用R来描绘这一点。

可重现的MatLab示例:

n1 = 60;
n2 = 60;
n3 = 60;
[y,x,z] = ndgrid(linspace(-3,3,n1),linspace(-3,3,n2),linspace(-3,3,n3));
V = zeros(n1, n2, n3);
lambda = [2;1;1];
for ii = 1: n1
    for jj = 1:n2
        for kk =1:n3
            V(ii, jj, kk) =  lambda'*[abs(y(ii, jj, kk)); abs(x(ii, jj, kk)); abs(z(ii, jj, kk))];
        end
    end
end
p = patch(isosurface(x,y,z,V,1));
p.FaceColor = 'cyan';
p.EdgeColor = 'none';
view(3);
camlight
axis equal
lighting gouraud
box on

我在R中的尝试:

n1=60
n2=60
n3=60
someData <- rep(0, 60*60*60)  
V <- array(someData, c(60, 60, 60))
data = expand.grid(x=linspace(-3,3,n1),y=linspace(-3,3,n2),z=linspace(-3,3,n3))
lambda = c(2,1,1)
count = 1
for (ii in 1:n1){
    for (jj in 1:n2){
        for (kk in 1:n3){
            V[ii, jj, kk] =  t(as.matrix(lambda))%*%as.matrix(c(data$x[count],data$y[count],data$z[count]))
                 count = count+1
        }
    }
}
M = melt(V)
plot_ly(type = "mesh3d",
        x = data$x,
        y = data$y,
        z = data$z,
        i = M$Var1,
        j = M$Var2,
        k = M$Var3,
        facecolor = rep(toRGB(colorRampPalette(c("navy", "blue"))(6)), each = 2)
)

然而,我只得到了一个空的坐标轴,没有形状

r7s23pms

r7s23pms1#

您可以使用rmarchingcube程序包计算等值面,并使用rgl程序包在3D中绘制。我用下面的代码得到的形状看起来像一个钻石。我并没有试图改进您的代码(有可能的改进)。

library(pracma)
library(rmarchingcubes) # to compute isosurface
library(rgl) # to plot in 3D

# your code with a couple of modifications
n1 <- 60
n2 <- 60
n3 <- 60
x <- linspace(-3, 3, n1)
y <- linspace(-3, 3, n2)
z <- linspace(-3, 3, n3)
data <- expand.grid(x = x, y = y, z = z)
someData <- rep(0, 60 * 60 * 60)
V <- array(someData, c(60, 60, 60))
count <- 1
lambda <- c(2, 1, 1)
for(ii in 1:n1) {
  for(jj in 1:n2) {
    for(kk in 1:n3) {
      V[ii, jj, kk] <- t(as.matrix(lambda)) %*% # you forgot the absolute value:
        abs(as.matrix(c(data$x[count], data$y[count], data$z[count])))
      count <- count + 1
    }
  }
}

# compute isosurface
isosurface <- contour3d(
  griddata = V, level = 1, x = x, y = y, z = z
)

# make rgl mesh
mesh <- tmesh3d(
  vertices = t(isosurface[["vertices"]]),
  indices = t(isosurface[["triangles"]]),
  normals = isosurface[["normals"]],
  homogeneous = FALSE
)

# plot
shade3d(mesh, color = "turquoise")

编辑

计算V的一大改进:

Mat <- abs(t(as.matrix(data)))
V <- array(t(lambda) %*% Mat, dim = c(n1, n2, n3))

编辑

如果您真的想使用情节

library(plotly)
vs = isosurface[["vertices"]]
idx = isosurface[["triangles"]] - 1
plot_ly(
  x = vs[,1], y = vs[,2], z = vs[,3],
  i = idx[,1], j = idx[,2], k = idx[,3],
  type = "mesh3d"
) %>% layout(scene = list(aspectmode = "data"))

相关问题