R rgl 库 planes3d() 函数不在 rgl 小部件中显示平面 window
R rgl library planes3d() function not displaying planes in rgl widget window
我一直在尝试绘制一组简单的平面,在我闪亮的应用程序 window 的 rgl window 中使用 planes3d() 函数,但没有任何东西可以显示平面。
library("shiny")
library("rgl")
# helper method for making edges
make_edge3d <- function(u, v) { return(sort(c(u,v))) }
rad2deg <- function(rad) {(rad * 180) / (pi)}
deg2rad <- function(deg) {(deg * pi) / (180)}
get_direction_vector <- function(theta, phi){
#Helper function that converts theta and phi (in degrees) into a direction vector
theta <- deg2rad(theta)
phi <- deg2rad(phi)
Z <- sin(phi) * cos(theta)
X <- sin(phi) * sin(theta)
Y <- cos(phi)
return( c(X, Y, Z))
}
#This function will plot the inputted simplicial complex
plot_shape3d <- function(verts, edges, ...){
vert_x <- verts[,1]
vert_y <- verts[,2]
vert_z <- verts[,3]
#Draws the Points
for (i in 1:length(vert_x)){
points3d(vert_x[i], vert_y[i], vert_z[i])
}
#Constructs the edges
for(e in edges){
e_vert <- verts$vert_lab %in% e
p1x = verts$vert_x[e_vert][1]
p2x = verts$vert_x[e_vert][2]
p1y = verts$vert_y[e_vert][1]
p2y = verts$vert_y[e_vert][2]
p1z = verts$vert_z[e_vert][1]
p2z = verts$vert_z[e_vert][2]
lines3d(c(p1x, p2x), c(p1y, p2y), c(p1z, p2z))
}
}
#This graphs filtration planes orthogonal to viewpoint vector going through each point
plot_filtration_planes <- function(verts, edges, direction){
for (i in range(1:nrow(verts))){
vert = verts[i,]
a = direction[1]
b = direction[2]
c = direction[3]
d = -vert[1] * a - vert[2] * b - vert[3] * c
planes3d(a,b,c,d)
}
}
#Main function that plots out the persistence diagram of the 3d simplicial complex
plot3d <- function(phi=0, theta=0, graph3d){
#List of vertex coordinates
vert_x <- c(0, 1, 2, 3, 4)
vert_y <- c(2, 0, 2, 0, 2)
vert_z <- c(0, 0, 0, 0, 0)
#Vertex Labels
vert_lab <- c("v1", "v2", "v3", "v4", "v5")
verts <- data.frame(vert_x, vert_y,vert_z, vert_lab, stringsAsFactors = F)
#Makes the edges between vertices
edges <- list(
make_edge3d("v1", "v2"),
make_edge3d("v2", "v3"),
make_edge3d("v3", "v4"),
make_edge3d("v4", "v5")
)
#offset <- -vert_x[i] * direction[1] - vert_y[i] * direction[2] - vert_z[i] * direction[3]
#Opens up the rgl plot
open3d(useNULL = TRUE)
axes3d()
#Plots out the W shape
plot_shape3d(verts, edges)
#Retrieves a direction vector from the camera position to the origin
direction <- get_direction_vector(theta, phi)
#Plots filtration planes
plot_filtration_planes(verts, edges, direction)
highlevel(integer())
rgl.viewpoint(theta, phi - 90, zoom=.4)
rglwidget()
}
ui <- fluidPage(
titlePanel("PD visualizer"),
tabsetPanel(
tabPanel("3D", fluid=TRUE,
sidebarLayout(
sidebarPanel(
sliderInput(inputId="theta", label="Choose a number between 0 and pi for theta",
value = 0, min=0, max=360, step=1, round=-2),
sliderInput(inputId="phi", label="Choose a number between 0 and two pi for phi",
value = 0, min=0, max=180, step=1, round=-2)
),
mainPanel(fluidRow(column(1, offset=0, rglwidgetOutput("ThreePD", width = 400, height = 150))))
)
)
)
)
server <- function(input, output){
output$PD <- renderCachedPlot({
createPlot(input$num, input$diagonals, input$filtlines, input$graphfile)
},
cacheKeyExpr = {paste(toString(input$num),"Diagonals: ", input$diagonals, "filtlines: ", input$filtlines, "file: ", input$graphfile$datapath)}
)
#output$plot1 <- renderPlot({plotPD3D(input$theta, input$phi, input$graphfile3d, input$diagonals3d)})
output$ThreePD <- renderRglwidget({
plot3d(input$phi, input$theta, input$graphfile3d)
}
)
}
shinyApp(ui = ui, server = server)
plot_filtration_planes 函数出现问题。我正在尝试绘制 5 个平面,每个顶点一个,这些平面与当前视点正交,如 phi 和 theta 所定义。该代码似乎可以很好地计算平面方程,但是当我使用 planes3d() 调用它们时,平面不会出现在 rgl 设备中的任何位置。任何建议将不胜感激!
一个平面是无限的。
rgl
根据场景中所有 非无限对象 的最大限制计算可视化立方体限制。
如果飞机不属于最大限制立方体,你将看不到它。
相反,如果场景包含一个被平面穿过的立方体,则出现平面:
library(rgl)
library(magrittr)
axes3d()
cube3d(color="black", alpha=0.1) %>% scale3d(4,4,4) %>% wire3d
planes3d(1, 0, 0, 3, col='red',alpha=1)
在此示例中,显示了与上述 plane3d
调用相对应的平面 x + 3 = 0
。
我一直在尝试绘制一组简单的平面,在我闪亮的应用程序 window 的 rgl window 中使用 planes3d() 函数,但没有任何东西可以显示平面。
library("shiny")
library("rgl")
# helper method for making edges
make_edge3d <- function(u, v) { return(sort(c(u,v))) }
rad2deg <- function(rad) {(rad * 180) / (pi)}
deg2rad <- function(deg) {(deg * pi) / (180)}
get_direction_vector <- function(theta, phi){
#Helper function that converts theta and phi (in degrees) into a direction vector
theta <- deg2rad(theta)
phi <- deg2rad(phi)
Z <- sin(phi) * cos(theta)
X <- sin(phi) * sin(theta)
Y <- cos(phi)
return( c(X, Y, Z))
}
#This function will plot the inputted simplicial complex
plot_shape3d <- function(verts, edges, ...){
vert_x <- verts[,1]
vert_y <- verts[,2]
vert_z <- verts[,3]
#Draws the Points
for (i in 1:length(vert_x)){
points3d(vert_x[i], vert_y[i], vert_z[i])
}
#Constructs the edges
for(e in edges){
e_vert <- verts$vert_lab %in% e
p1x = verts$vert_x[e_vert][1]
p2x = verts$vert_x[e_vert][2]
p1y = verts$vert_y[e_vert][1]
p2y = verts$vert_y[e_vert][2]
p1z = verts$vert_z[e_vert][1]
p2z = verts$vert_z[e_vert][2]
lines3d(c(p1x, p2x), c(p1y, p2y), c(p1z, p2z))
}
}
#This graphs filtration planes orthogonal to viewpoint vector going through each point
plot_filtration_planes <- function(verts, edges, direction){
for (i in range(1:nrow(verts))){
vert = verts[i,]
a = direction[1]
b = direction[2]
c = direction[3]
d = -vert[1] * a - vert[2] * b - vert[3] * c
planes3d(a,b,c,d)
}
}
#Main function that plots out the persistence diagram of the 3d simplicial complex
plot3d <- function(phi=0, theta=0, graph3d){
#List of vertex coordinates
vert_x <- c(0, 1, 2, 3, 4)
vert_y <- c(2, 0, 2, 0, 2)
vert_z <- c(0, 0, 0, 0, 0)
#Vertex Labels
vert_lab <- c("v1", "v2", "v3", "v4", "v5")
verts <- data.frame(vert_x, vert_y,vert_z, vert_lab, stringsAsFactors = F)
#Makes the edges between vertices
edges <- list(
make_edge3d("v1", "v2"),
make_edge3d("v2", "v3"),
make_edge3d("v3", "v4"),
make_edge3d("v4", "v5")
)
#offset <- -vert_x[i] * direction[1] - vert_y[i] * direction[2] - vert_z[i] * direction[3]
#Opens up the rgl plot
open3d(useNULL = TRUE)
axes3d()
#Plots out the W shape
plot_shape3d(verts, edges)
#Retrieves a direction vector from the camera position to the origin
direction <- get_direction_vector(theta, phi)
#Plots filtration planes
plot_filtration_planes(verts, edges, direction)
highlevel(integer())
rgl.viewpoint(theta, phi - 90, zoom=.4)
rglwidget()
}
ui <- fluidPage(
titlePanel("PD visualizer"),
tabsetPanel(
tabPanel("3D", fluid=TRUE,
sidebarLayout(
sidebarPanel(
sliderInput(inputId="theta", label="Choose a number between 0 and pi for theta",
value = 0, min=0, max=360, step=1, round=-2),
sliderInput(inputId="phi", label="Choose a number between 0 and two pi for phi",
value = 0, min=0, max=180, step=1, round=-2)
),
mainPanel(fluidRow(column(1, offset=0, rglwidgetOutput("ThreePD", width = 400, height = 150))))
)
)
)
)
server <- function(input, output){
output$PD <- renderCachedPlot({
createPlot(input$num, input$diagonals, input$filtlines, input$graphfile)
},
cacheKeyExpr = {paste(toString(input$num),"Diagonals: ", input$diagonals, "filtlines: ", input$filtlines, "file: ", input$graphfile$datapath)}
)
#output$plot1 <- renderPlot({plotPD3D(input$theta, input$phi, input$graphfile3d, input$diagonals3d)})
output$ThreePD <- renderRglwidget({
plot3d(input$phi, input$theta, input$graphfile3d)
}
)
}
shinyApp(ui = ui, server = server)
plot_filtration_planes 函数出现问题。我正在尝试绘制 5 个平面,每个顶点一个,这些平面与当前视点正交,如 phi 和 theta 所定义。该代码似乎可以很好地计算平面方程,但是当我使用 planes3d() 调用它们时,平面不会出现在 rgl 设备中的任何位置。任何建议将不胜感激!
一个平面是无限的。
rgl
根据场景中所有 非无限对象 的最大限制计算可视化立方体限制。
如果飞机不属于最大限制立方体,你将看不到它。
相反,如果场景包含一个被平面穿过的立方体,则出现平面:
library(rgl)
library(magrittr)
axes3d()
cube3d(color="black", alpha=0.1) %>% scale3d(4,4,4) %>% wire3d
planes3d(1, 0, 0, 3, col='red',alpha=1)
在此示例中,显示了与上述 plane3d
调用相对应的平面 x + 3 = 0
。