Rgl: draw a cube with colored faces, vertices, and lines

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

I can’t understand how to use different colors for faces, and how to make it more general, so I don’t need to repeat all the steps for the result when converting.

what i tried:

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

This gives the image below. But I do not understand how faces are painted. And I seem to have to be used points3dand lines3dto form components c3dand not have a single object that I can convert.

enter image description here

A , ,

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)

:

enter image description here

?

+4
3

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

Screenshot of a cube displayed

alpha; alpha = 0.5.

, , ; , . , :

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)

:

sphere

:

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

transformed sphere

, , .

+6

(: rgl version 0.96.0. , wire3d() dot3d() )

mesh3d, shade3d(), wire3d() dot3d() . .

;
A <- matrix(c( 1, 0, 1, 0, 2, 0,  1, 0, 2), 3, 3)
c3d2 <- cube3d()
c3d_trans2 <- cube3d(A)
colv <- rep(2:7, each=4)

shade3d(c3d2, col = colv, alpha = 0.8)
wire3d(c3d2); dot3d(c3d2, size = 5)
shade3d(c3d_trans2, col = colv, alpha=0.5)
dot3d(c3d_trans2, size = 5)

enter image description here


[, mesh3d.obj]

, , ( , . ). ib mesh3d.obj$vb .

cube3d()$ib
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    1    3    2    1    1    5
# [2,]    3    7    4    5    2    6
# [3,]    4    8    8    7    6    8
# [4,]    2    4    6    3    5    7
shade3d() ( )
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, ...

enter image description here

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.

ib col (, ib[,1], Index3 1-3, 3-4. , . ( ), wire3d(). , lines3d() segments3d()

enter image description here

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")[unique(c(object$ib))] , index1 col1, index2 - col2.

enter image description here

+4

The second part of my question - how to generalize this a bit - remained unanswered. Here is a simple function that I am currently using to make steps reusable.

# 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],])
}
+2
source

Source: https://habr.com/ru/post/1655993/


All Articles