rgl:绘制带有彩色面,顶点和线的立方体 [英] rgl: drawing a cube with colored faces, vertex points and lines

查看:181
本文介绍了rgl:绘制带有彩色面,顶点和线的立方体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为演示3D x -> A x中线性变换的效果,我想绘制一个立方体并在A下显示其变换.为此,我需要分别为每个面着色,并显示顶点和轮廓每个面的线.

To demonstrate the effect of linear transformations in 3D, x -> A x, I want to draw a cube and show its transformation under A. For this, I need to color each face separately, and also show the vertex points and the lines that outline each face.

我无法弄清楚如何为面部使用不同的颜色,以及如何使颜色更加笼统,因此我不必为变换下的结果重复所有步骤.

I can't figure out how to use distinct colors for the faces, and how to make this more general so I don't have to repeat all the steps for the result under the transformation.

我尝试过的事情:

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],])

这给出了下面的图像.但是我不明白这些脸是怎么变色的.而且,我似乎必须在c3d形状的组件上使用points3dlines3d,并且没有可以变换的单个对象.

This gives the image below. But I don't understand how the faces are colored. And, I seem to have to use points3d and lines3d on the components of the c3d shape, and don't have a single object I can transform.

下面的矩阵A给出了一种特殊的变换,这是我将其添加到场景中的方式,

A particular transformation is given by the matrix A below, and here is how I add that to the scene,

A <- matrix(c( 1, 0, 1, 0, 2, 0,  1, 0, 2), 3, 3)
c3d_trans <- transform3d(c3d, A) 
shade3d( c3d_trans )
points3d(t(c3d_trans$vb), size=5)

这给出了:

是否有某种方法可以简化此过程并使其更普遍地有用?

Is there some way to simplify this and make it more generally useful?

推荐答案

rgl中,绘制基本形状时,将颜色应用于顶点,而不是面.通过在顶点处插入颜色来对这些面着色.

In rgl, when drawing primitive shapes, you apply colours to vertices, not faces. The faces are coloured by interpolating the colors at the vertices.

但是,cube3d()不是原始形状,而是网格".它被绘制为6个独立的四边形.每个顶点使用3次.

However, cube3d() is not a primitive shape, it's a "mesh". It is drawn as 6 separate quadrilaterals. Each vertex is used 3 times.

它没有真正记载,但是使用颜色的顺序是前4个用于一个面,然后下4个用于下一个面,依此类推.如果要使颜色为rainbow(6),则需要将每种颜色复制4次:

It's not really documented, but the order the colours are used is that the first 4 are used for one face, then the next 4 for the next face, etc. If you want your colours to be rainbow(6), you need to replicate each colour 4 times:

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],])

我建议使用更高的alpha值;我在alpha = 0.5上发现透明度有些混乱.

I'd recommend a higher alpha value; I find the transparency a little confusing at alpha = 0.5.

顺便说一句,出于相同的目的,我通常使用看起来更像球形的形状作为基线.我认为它为转换提供了更好的直觉.这是我使用的代码:

By the way, for the same purpose, I generally use a shape that looks more spherical as the baseline; I think it gives better intuition about the transformation. Here's code I have used:

sphere <- subdivision3d(cube3d(color=rep(rainbow(6),rep(4*4^4,6)), alpha=0.9),
    depth=4)
sphere$vb[4,] <- apply(sphere$vb[1:3,], 2, function(x) sqrt(sum(x^2)))
open3d()
shade3d(sphere)

这就是这样的形状:

将转换为此:

A <- matrix(c( 1, 0, 1, 0, 2, 0,  1, 0, 2), 3, 3)
trans <- transform3d(sphere, A)
open3d()
shade3d(trans)

当然,如果您可以旋转它,一切看起来会更好.

Of course, it all looks better if you can rotate it.

这篇关于rgl:绘制带有彩色面,顶点和线的立方体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆