【问题标题】:Drawing cuboid with given coordinates in rgl在rgl中绘制具有给定坐标的长方体
【发布时间】:2020-04-11 22:55:10
【问题描述】:

我正在尝试绘制一个给定顶点坐标的长方体:(-3,-2,-5), (-3,-2,6), (-3,3,-5), (-3 ,3,6), (7,-2,-5), (7,-2,6), (7,3,-5), (7,3,6) 使用rgl 包。我已经通过以下方式完成了:

library(rgl)
vertices1 <- c( 
  -3, -2, -5, 1,
  7, -2, -5, 1,
  7,  3, -5, 1,
  -3,  3, -5, 1
)
vertices2 <- c( 
  -3, -2, 6, 1,
  7, -2, 6, 1,
  7,  3, 6, 1,
  -3,  3, 6, 1
)
vertices3 <- c( 
  -3, -2, -5, 1,
  -3, -2, 6, 1,
  -3, 3, 6, 1,
  -3,  3, -5, 1
)
vertices4 <- c(
  7, -2, -5, 1,
  7, -2, 6, 1,
  7,  3, 6, 1,
  7,  3, -5, 1
)
vertices5 <- c(
  -3, 3, -5, 1,
  7, 3, -5, 1,
  7, 3, 6, 1,
  -3, 3, 6, 1
)
vertices6 <- c(
  -3, -2, -5, 1,
  7, -2, -5, 1,
  7, -2, 6, 1,
  -3, -2, 6, 1

)
indices <- c( 1, 2, 3, 4 )
open3d()  
wire3d( qmesh3d(vertices1, indices) , col = "blue")
wire3d( qmesh3d(vertices2, indices) , col = "blue" )
wire3d( qmesh3d(vertices3, indices) , col = "blue")
wire3d( qmesh3d(vertices4, indices) , col = "blue")
shade3d(qmesh3d(vertices1, indices) , col = "blue", alpha = 0.1)
shade3d(qmesh3d(vertices2, indices) , col = "blue", alpha = 0.1)
shade3d(qmesh3d(vertices3, indices) , col = "blue", alpha = 0.1)
shade3d(qmesh3d(vertices4, indices) , col = "blue", alpha = 0.1)
shade3d(qmesh3d(vertices5, indices) , col = "blue", alpha = 0.1)
shade3d(qmesh3d(vertices6, indices) , col = "blue", alpha = 0.1)

有没有更好的方法来使用rgl

【问题讨论】:

    标签: r rgl


    【解决方案1】:

    更简洁,避免重复一些顶点:

    library(rgl)
    
    vertices <- cbind(
      c(-3,-2,-5), 
      c(-3,-2, 6), 
      c(-3, 3,-5), 
      c(-3, 3, 6), 
      c( 7,-2,-5), 
      c( 7,-2, 6), 
      c( 7, 3,-5), 
      c( 7, 3, 6)
    )
    
    indices <- cbind(
      c(1, 5, 7, 3),
      c(2, 6, 8, 4),
      c(1, 2, 4, 3),
      c(5, 6, 8, 7),
      c(3, 7, 8, 4)
      c(1, 5, 6, 2)
    )
    
    cuboid <- qmesh3d(
      vertices = vertices,
      indices = indices,
      homogeneous = FALSE
    )
    
    shade3d(cuboid, color = "blue", alpha = 0.1)
    wire3d(cuboid, color = "blue")
    

    【讨论】:

    • 看来如果我改变vertices的顺序,那么我也需要改变indices。是否有任何通用方法可以根据任意vertices 集合选择indices?感谢您的帮助!
    • @CatDistribution 每个索引对应一个顶点,按照它们给出的顺序。您必须选择构成网格面的索引。
    猜你喜欢
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2017-02-05
    相关资源
    最近更新 更多