rgl:使用彩色面、顶点和线绘制立方体
为了演示3D中线性变换的效果,rgl:使用彩色面、顶点和线绘制立方体,r,linear-algebra,rgl,R,Linear Algebra,Rgl,为了演示3D中线性变换的效果,x->A x,我想画一个立方体,并在A下显示其变换。为此,我需要分别为每个面着色,并显示每个面的顶点和轮廓线 我不知道如何为面使用不同的颜色,以及如何使其更通用,这样我就不必为变换下的结果重复所有步骤 我尝试的是: library(rgl) c3d <- cube3d(color=rainbow(6), alpha=0.5) open3d() shade3d(c3d) points3d(t(c3d$vb), size=5) for (i in 1:6)
x->A x
,我想画一个立方体,并在A
下显示其变换。为此,我需要分别为每个面着色,并显示每个面的顶点和轮廓线
我不知道如何为面使用不同的颜色,以及如何使其更通用,这样我就不必为变换下的结果重复所有步骤
我尝试的是:
library(rgl)
c3d <- cube3d(color=rainbow(6), alpha=0.5)
open3d()
shade3d(c3d)
points3d(t(c3d$vb), size=5)
for (i in 1:6)
lines3d(t(c3d$vb)[c3d$ib[,i],])
这使得:
是否有一些方法可以简化此过程并使其更为普遍有用?在
rgl
中,在绘制基本体形状时,将颜色应用于顶点,而不是面。面通过在顶点处插值颜色来着色
但是,cube3d()
不是一个基本形状,它是一个“网格”。它被绘制为6个独立的四边形。每个顶点使用3次
它并没有真正的文档记录,但颜色的使用顺序是前4个用于一张脸,然后下4个用于下一张脸,以此类推。如果您希望您的颜色是rainbow(6)
,您需要将每种颜色复制4次:
library(rgl)
c3d <- cube3d(color=rep(rainbow(6), each = 4), alpha = 0.5)
open3d()
shade3d(c3d)
points3d(t(c3d$vb), size = 5)
for (i in 1:6)
lines3d(t(c3d$vb)[c3d$ib[,i],])
这就形成了这个形状:
以下哪一项转化为:
A <- matrix(c( 1, 0, 1, 0, 2, 0, 1, 0, 2), 3, 3)
trans <- transform3d(sphere, A)
open3d()
shade3d(trans)
A我问题的第二部分——如何将其概括一点——没有得到回答。下面是一个简单的函数,我现在使用它使这些步骤可重用
# draw a mesh3d object with vertex points and lines
draw3d <- function(object, ...) {
shade3d(object, ...)
vertices <- t(object$vb)
indices <- object$ib
points3d(vertices, size=5)
for (i in 1:ncol(indices))
lines3d(vertices[indices[,i],])
}
#使用顶点和线绘制mesh3d对象
draw3d(注意:我使用的是rgl版本0.96.0
。如果我的内存正确,wire3d()
和dot3d()
规则已更改)
在创建类mesh3d
对象时提供颜色信息不是一个好主意,因为shade3d()
、wire3d()
和dot3d()
以不同的方式使用它。在绘制对象时,最好为plot函数提供颜色信息
例如
shade3d()规则(很简单)
plot3d(cube3d(scaleMatrix(1.2,1.2,1.2)), alpha=0)
text3d(t(cube3d()$vb[1:3,]*1.05), texts=1:8) # indices
dot3d(cube3d(), col=1:8, size=8)
unique(c(cube3d()$ib))
# [1] 1 3 4 2 7 8 6 5
wire3d规则(复杂而可怕……;编辑)
text3d(t(cube3d()$vb[1:3,]*1.05), texts=1:8, font=2) # indices
wire3d(cube3d(), col=c(rep(2,6), rep(3,6), rep(4,6), rep(5,6), rep(6,6), rep(7,6)))
# I gave each color 6 times.
index 1 3 4 2 1
ib$[1,1] - [2,1] - [3,1] - [4,1] - [1,1] - NA
col 2 2 2 2 2 2 # Why NA uses a color!!??
index 3 7 8 4 (skipped) 3 # the line already has been drawn, skipped.
ib$[1,2] - [2,2] - [3,2] - [4,2] - [1,2] - NA;
col 3 3 3 3 (skipped) 3 3
index 2 (sk) 4 (sk) 8 6 2
ib$[1,3] - [2,3] - [3,3] - [4,3] - [1,3] - NA;
col 4 (sk) 4 (sk) 4 4 4 4, and so on.
plot3d(cube3d(scaleMatrix(1.2,1.2,1.2)), alpha=0)
text3d(t(cube3d()$vb[1:3,]*1.05), texts=1:8) # indices
dot3d(cube3d(), col=1:8, size=8)
unique(c(cube3d()$ib))
# [1] 1 3 4 2 7 8 6 5
在一个ib
的col
中,一个顶点只有一种颜色(例如,在ib[,1]
,Index3的颜色信息同时使用1-3和3-4。当已经绘制线时,将跳过该线。使用wire3d()绘制不同颜色的线太难了(某些图案是不可能的)
。如果您想这样做,最好使用lines3d()
或segments3d()
dot3d规则
plot3d(cube3d(scaleMatrix(1.2,1.2,1.2)), alpha=0)
text3d(t(cube3d()$vb[1:3,]*1.05), texts=1:8) # indices
dot3d(cube3d(), col=1:8, size=8)
unique(c(cube3d()$ib))
# [1] 1 3 4 2 7 8 6 5
如果给出col=1:8
,则不是index2而是index3变成col=2
(red
)。
因此,col=c(“col1”,“col2”,“col8”)[唯一(c(object$ib))
意味着index1是col1
,index2是col2
如果能将这种颜色行为记录下来,那就太好了。如何画点和线也不明显。也许我的例子可以用来说明这些事情。为什么不在文档中提交一个补丁呢?
plot3d(cube3d(scaleMatrix(1.2,1.2,1.2)), alpha=0)
text3d(t(cube3d()$vb[1:3,]*1.05), texts=1:8) # indices
shade3d(cube3d(), col=c(rep(2,4), rep(3,4), rep(4,4), rep(5,4), rep(6,4), rep(7,4)), alpha=0.8)
# ib[1:4, 1] [1:4, 2] [1:4, 3] [1:4, 4] [1:4, 5] [1:4, 6]
# index 1,3,4,2 3,7,8,4 2,4,8,6, ...
text3d(t(cube3d()$vb[1:3,]*1.05), texts=1:8, font=2) # indices
wire3d(cube3d(), col=c(rep(2,6), rep(3,6), rep(4,6), rep(5,6), rep(6,6), rep(7,6)))
# I gave each color 6 times.
index 1 3 4 2 1
ib$[1,1] - [2,1] - [3,1] - [4,1] - [1,1] - NA
col 2 2 2 2 2 2 # Why NA uses a color!!??
index 3 7 8 4 (skipped) 3 # the line already has been drawn, skipped.
ib$[1,2] - [2,2] - [3,2] - [4,2] - [1,2] - NA;
col 3 3 3 3 (skipped) 3 3
index 2 (sk) 4 (sk) 8 6 2
ib$[1,3] - [2,3] - [3,3] - [4,3] - [1,3] - NA;
col 4 (sk) 4 (sk) 4 4 4 4, and so on.
plot3d(cube3d(scaleMatrix(1.2,1.2,1.2)), alpha=0)
text3d(t(cube3d()$vb[1:3,]*1.05), texts=1:8) # indices
dot3d(cube3d(), col=1:8, size=8)
unique(c(cube3d()$ib))
# [1] 1 3 4 2 7 8 6 5