简体   繁体   English

使用 RGL 在 R 中绘制 3d 线段

[英]Using RGL to plot 3d line segments in R

I having some problems with an application of the rgl 3d graphing package.我在使用 rgl 3d 图形包时遇到了一些问题。

I'm trying to draw some line segments.我正在尝试绘制一些线段。 And my data is arranged in a dataframe called 'markers' with six columns, one for each of the starting x, y, and z values, and one for each of the ending x, y, and z values.我的数据排列在一个名为“标记”的数据框中,它有六列,一列用于起始 x、y 和 z 值,一列用于结束 x、y 和 z 值。

startX  startY startZ endX   endY    endZ
69.345  45.732     20  115 39.072 1.92413
80.270  38.480     30  175 44.548 0.36777
99.590  33.596     20  175 35.224 0.06929
32.120  41.218     20  115 39.294 2.81424
11.775  37.000     30  175 35.890 1.38047
76.820  44.104     22  115 44.992 4.14674
85.790  23.384     18  115 36.112 0.40508
80.040  17.464     20  175 31.080 2.59038
103.615 38.850     22  115 39.220 3.18201
41.200  31.006     30  175 36.260 3.48049
88.665  43.956     30  115 39.738 0.50635
109.365 23.976     20  175 33.374 3.99750

This should be a piece of cake.这应该是小菜一碟。 Just feed those values to the segment3d() command and I should get the plot I want.只需将这些值提供给 segment3d() 命令,我就会得到我想要的图。 Only I can't figure out how to correctly pass the respective starting and ending pairs into segment3d().只有我不知道如何正确地将各自的开始和结束对传递到 segment3d() 中。

I've tried just about everything possible ($ notation, indexing, concatenating, using a loop, apply and sapply, etc.), including reading the documentation.我已经尝试了几乎所有可能的方法($ 符号、索引、连接、使用循环、应用和应用等),包括阅读文档。 It's great, it says for the arguments x, y, and z: "Any reasonable way of defining the coordinates is acceptable."很好,它对参数 x、y 和 z 说:“定义坐标的任何合理方式都是可以接受的。” Ugh... it does refer you to the xyz.coords utility.呃...它确实将您推荐给 xyz.coords 实用程序。

So I went over that documentation.所以我查看了那个文档。 And I think I understand what it does;我想我明白它的作用; I can even use it to standardize my data eg我什至可以用它来标准化我的数据,例如

starts <- xyz.coords(markers$startX, markers$startY, markers$startZ)

ends <- xyz.coords(markers$endX, markers$endY, markers$endZ)

But then I'm still not sure what to do with those two lists.但是我仍然不确定如何处理这两个列表。

segments3d(starts, ends)
segments3d(starts + ends) 
segments3d((starts, ends), (starts, ends), (starts, ends))
segments3d(c(starts, ends), c(starts, ends), c(starts, ends))
segments3d(c(starts$x, ends$x), c(starts$y, ends$y), c(starts$z, ends$z))

I mean I know why the above don't work.我的意思是我知道为什么上述方法不起作用。 I'm basically just trying things at this point as this is making me feel incredibly stupid, like there is something obvious—I mean facepalm level obvious—I'm missing.我现在基本上只是在尝试一些事情,因为这让我感到非常愚蠢,就像有一些明显的东西——我的意思是面部表情很明显——我错过了。

I went through the rgl documentation itself looking for an example, and the only place I found them using segment3d() in any manner resembling what I'm trying to do, they used the '+' notation I tried above.我浏览了 rgl 文档本身寻找一个例子,我发现它们以任何类似于我想要做的方式使用 segment3d() 的唯一地方,他们使用了我在上面尝试过的“+”符号。 Basically they built 2 matrices and added the second to the first.基本上,他们构建了 2 个矩阵并将第二个添加到第一个矩阵。

Something like this should work.像这样的事情应该有效。

library(rgl)
open3d(scale=c(1/5,1,1))
segments3d(x=as.vector(t(markers[,c(1,4)])),
           y=as.vector(t(markers[,c(2,5)])),
           z=as.vector(t(markers[,c(3,6)])))
axes3d()
title3d(xlab="X",ylab="Y",zlab="Z")

在此处输入图片说明

The problem is that segments3d(...) takes the x (and y and z) values in pairs .问题是segments3d(...)成对形式获取 x(以及 y 和 z)值。 So rows 1-2 are the first segment, rows 3-4 are the second segment, etc. You need to interleave , eg $startx and $endx , etc. The code above does that.所以第 1-2 行是第一段,第 3-4 行是第二段,等等。你需要交错,例如$startx$endx等。上面的代码就是这样做的。

创建数据集的代码:

markers <- data.frame(startX = c(69.345, 80.270, 99.590, 32.120, 11.775, 76.820, 85.790, 80.040, 103.615, 41.200, 88.665, 109.365), startY = c(45.732, 38.480, 33.596, 41.218, 37.000, 44.104, 23.384, 17.464, 38.850, 31.006, 43.956, 23.976), startZ = c(20, 30, 20, 20, 30, 22, 18, 20, 22, 30, 30, 20), endX = c(115, 175, 175, 115, 175, 115, 115, 175, 115, 175, 115, 175), endY = c(39.072, 44.548, 35.224, 39.294, 35.890, 44.992, 36.112, 31.080, 39.220, 36.260, 39.738, 33.374), endZ = c(1.92413, 0.36777, 0.06929, 2.81424, 1.38047, 4.14674, 0.40508, 2.59038, 3.18201, 3.48049, 0.50635, 3.99750))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM