在 3D 空间中绘制立方体

Posted

技术标签:

【中文标题】在 3D 空间中绘制立方体【英文标题】:Drawing cube in 3D space 【发布时间】:2021-12-25 02:38:47 【问题描述】:

我需要画一个大立方体,然后在 R 中将它分成 8 个较小的立方体。我需要在 3D 空间中绘制它,每个较小的立方体都有不同的颜色。我有大小立方体的顶点。 以下是顶点:

type_cube <- c("type_8","type_8","type_8","type_8",
               "type_8","type_8","type_8","type_8",
               "type_7","type_7","type_7","type_7", 
                "type_7","type_7","type_7","type_7",
               "type_6","type_6","type_6","type_6",
               "type_6","type_6","type_6","type_6",
                "type_5","type_5","type_5","type_5",
               "type_5","type_5","type_5","type_5","
               type_4", "typ3_4","type_4","type_4",
               "type_4","typ3_4","type_4","type_4",
               "type_3","type_3","type_3","type_3",
               "type_3","type_3","type_3","type_3",
                "type_2","type_2","type_2","type_2",
               "type_2","type_2","type_2","type_2",
               "type_1","type_1","type_1","type_1",
                "type_1","type_1","type_1","type_1",
               "big_cube", "big_cube", "big_cube", 
               "big_cube", "big_cube", "big_cube", 
               "big_cube", "big_cube")


x <- c(0.0,0.0,0.5,0.5,0.5,0.0,0.0,0.5,0.5,1.0,1.0,0.5,0.5,0.5,
       1.0,1.0,0.0,0.5,0.5,0.0,0.0,0.0,0.5,0.5,0.0,0.5,0.5,0.0,0.0,0.5,
       0.0,0.5,1.0,0.5,0.5,1.0,0.5,0.5,1.0,1.0,1.0,0.5,0.5,1.0,
        0.5,0.5,1.0,1.0,0.0,0.5,0.5,0.0,0.0,0.5,0.5,0.0,1.0,1.0,
       0.5,0.5,0.5,0.5,1.0,1.0,0.0,0.0,1.0,1.0,1.0,0.0,0.0,1.0)

y <- c(0.0,0.5,0.5,0.0,0.0,0.0,0.5,0.5,0.0,0.0,0.5,0.5,0.0,0.5,0.5,
       0.0,0.5,0.5,1.0,1.0,0.5,1.0,1.0,0.5,0.0,0.0,0.5,0.5,0.0,0.0,
       0.5,0.5,0.5,0.5,1.0,1.0,0.5,1.0,1.0,0.5,0.0,0.0,0.5,0.5,0.0,
       0.5,0.5,0.0,0.5,0.5,1.0,1.0,0.5,0.5,1.0,1.0,1.0,0.5,0.5,1.0,
       0.5,1.0,1.0,0.5,0.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0)


z <- c(0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.0,0.0,0.0,
       0.0,0.5,0.5,0.5,0.5,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.5,0.5,
       0.5,0.5,0.5,0.5,0.5,0.5,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.5,
      0.5,0.5,0.5,1.0,1.0,1.0,1.0,0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0,
      0.5,0.5,0.5,0.5,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0)

data <- data.frame(type_cube,x,y,z)

【问题讨论】:

【参考方案1】:

这就是我的做法。感谢 Jon Spring 为我指明了正确的方向。

    type_cube <- c("type_8","type_8","type_8","type_8",
               "type_8","type_8","type_8","type_8",
               "type_7","type_7","type_7","type_7", 
               "type_7","type_7","type_7","type_7",
               "type_6","type_6","type_6","type_6",
               "type_6","type_6","type_6","type_6",
               "type_5","type_5","type_5","type_5",
               "type_5","type_5","type_5","type_5","
               type_4", "typ3_4","type_4","type_4",
               "type_4","typ3_4","type_4","type_4",
               "type_3","type_3","type_3","type_3",
               "type_3","type_3","type_3","type_3",
               "type_2","type_2","type_2","type_2",
               "type_2","type_2","type_2","type_2",
               "type_1","type_1","type_1","type_1",
               "type_1","type_1","type_1","type_1",
               "big_cube", "big_cube", "big_cube", 
               "big_cube", "big_cube", "big_cube", 
               "big_cube", "big_cube")


x <- c(0.0,0.0,0.5,0.5,0.5,0.0,0.0,0.5,0.5,1.0,1.0,0.5,0.5,0.5,
       1.0,1.0,0.0,0.5,0.5,0.0,0.0,0.0,0.5,0.5,0.0,0.5,0.5,0.0,0.0,0.5,
       0.0,0.5,1.0,0.5,0.5,1.0,0.5,0.5,1.0,1.0,1.0,0.5,0.5,1.0,
       0.5,0.5,1.0,1.0,0.0,0.5,0.5,0.0,0.0,0.5,0.5,0.0,1.0,1.0,
       0.5,0.5,0.5,0.5,1.0,1.0,0.0,0.0,1.0,1.0,1.0,0.0,0.0,1.0)

y <- c(0.0,0.5,0.5,0.0,0.0,0.0,0.5,0.5,0.0,0.0,0.5,0.5,0.0,0.5,0.5,
       0.0,0.5,0.5,1.0,1.0,0.5,1.0,1.0,0.5,0.0,0.0,0.5,0.5,0.0,0.0,
       0.5,0.5,0.5,0.5,1.0,1.0,0.5,1.0,1.0,0.5,0.0,0.0,0.5,0.5,0.0,
       0.5,0.5,0.0,0.5,0.5,1.0,1.0,0.5,0.5,1.0,1.0,1.0,0.5,0.5,1.0,
       0.5,1.0,1.0,0.5,0.0,1.0,1.0,0.0,0.0,0.0,1.0,1.0)


z <- c(0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.0,0.0,0.0,
       0.0,0.5,0.5,0.5,0.5,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.5,0.5,
       0.5,0.5,0.5,0.5,0.5,0.5,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.5,
       0.5,0.5,0.5,1.0,1.0,1.0,1.0,0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0,
       0.5,0.5,0.5,0.5,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0)



##############################################

library(rgl)

c3d <- cube3d(color="#ffffff", alpha=0.5)  



#########################
# creating the cubes
# The original cube
c3d2 <- c3d %>% 
   translate3d(1, 1, 1)%>%scale3d(0.5, 0.5, 0.5)

shade3d(c3d2)

#type 1
c3d1 <- cube3d(color = "green", alpha=0.2) %>% 
   translate3d(3, 3, 3)%>%
   scale3d(0.25, 0.25, 0.25)
shade3d(c3d1)

#type 2 
c3d2 <- cube3d(color =  "#91682c", alpha=0.5) %>% 
   translate3d(1, 3, 3)%>%
   scale3d(0.25, 0.25, 0.25)
shade3d(c3d2)

#type 3 
c3d3 <- cube3d(color = "orange", alpha=0.5) %>% 
   translate3d(3, 1, 3)%>%
   scale3d(0.25, 0.25, 0.25)

shade3d(c3d3)


#type 4 
c3d4 <- cube3d(color = "cyan", alpha=0.5) %>% 
   translate3d(3, 3, 1)%>%
   scale3d(0.25, 0.25, 0.25)

shade3d(c3d4)


#type 5 
c3d5 <- cube3d(color = "#425e5b", alpha=0.6) %>% 
   translate3d(1, 1, 3)%>%
   scale3d(0.25, 0.25, 0.25)

shade3d(c3d5)

# type 6 
c3d6 <- cube3d(color = "yellow", alpha=0.7) %>% 
   translate3d(1, 3, 1)%>%
   scale3d(0.25, 0.25, 0.25)

shade3d(c3d6)

# type 7 
c3d7 <- cube3d(color = "blue", alpha=0.1) %>% 
   translate3d(3, 1, 1)%>%
   scale3d(0.25, 0.25, 0.25)

shade3d(c3d7)

# type 8
c3d8 <- cube3d(color = "#a4211c", alpha=0.9) %>% 
   translate3d(1, 1, 1) %>% 
   scale3d(0.25, 0.25, 0.25)
shade3d(c3d8)


#########################
# Text showing cube types
text3d(x=.75, y = .75, z = .75, "type 1", adj = 0.5, pos = NULL, offset = 0.5, col='black') 

text3d(x=.25, y = .75, z = .75, "type 2", adj = 0.5, pos = NULL, offset = 0.5, col='purple') 

text3d(x=.75, y = .15, z = .75, "type 3", adj = 0.5, pos = NULL, offset = 0.5, col='black') 

text3d(x=.75, y = .75, z = .1, "type 4", adj = 0.5, pos = NULL, offset = 0.5, col='black') 

text3d(x=.25, y = .15, z = .75,"type 5", adj = 0.5, pos = NULL, offset = 0.5, col='yellow') 

text3d(x=.25, y = .75, z = .1, "type 6", adj = 0.5, pos = NULL, offset = 0.5, col='black') 

text3d(x=.75, y = .15, z = .1, "type 7", adj = 0.5, pos = NULL, offset = 0.5, col='black') 

text3d(x=.25, y = .15, z = .1, "type 8", adj = 0.5, pos = NULL, offset = 0.5, col='black') 

#########################
# adding the axes

axes3d('x+-',  col='purple',pos = c(0, 1.1, 0),labels = c("0","","","","","Full Score"))

axes3d('y+-', col='green',pos = c(1.1, 0, 0),labels = c("0","","","","","Full Score"))

axes3d('z-+', col='blue',pos = c(0, 1.1, 0),labels = c("0","","","","","Full Score"))

enter image description here

【讨论】:

【参考方案2】:
library(plotly)
plotly::plot_ly(data, x = ~x, y = ~y, z = ~z) %>%
  add_markers()

这会绘制顶点,但不会按照立方体的方式执行您想要的操作。

我怀疑rgl 可能是个好方法,就像在这个相关答案中一样:

https://***.com/a/47718466/6851825

【讨论】:

谢谢,乔恩·斯普林。我能够破解它。

以上是关于在 3D 空间中绘制立方体的主要内容,如果未能解决你的问题,请参考以下文章

在 Matplotlib 中绘制一个 3d 立方体、一个球体和一个向量

OpenCV中的姿势估计及3D效果(3D坐标轴,3D立方体)绘制

Panda3D绘制立方体

Panda3D绘制立方体

在 3D 空间中查找所有相邻的单元格/立方体

使用 Opengl 绘制 3D 立方体