用rgl在球体上填充一个区域。

3
这是世界首都的球形Voronoï镶嵌图:

enter image description here

我有定义每个国家边界的点的坐标。我想用颜色填充这些国家。我的动机是将Voronoï边缘与海洋相同的颜色绘制,这样我们就不会在国家外看到它们。换句话说,我正在寻找一个类似于polygon函数但适用于球面多边形的函数。

编辑

嗯,我明白了。但是出于某种原因,水的青色和Voronoï边缘的青色不同:

enter image description here


@AllanCameron 我有边界的坐标,并且我使用 rgl::lines3d - Stéphane Laurent
@AllanCameron 这将需要带有约束条件的球面三角剖分。 - Stéphane Laurent
是的 - 你是这里少数几个能做到的人之一,我想。我猜你可能在寻找更直接的东西... - Allan Cameron
@AllanCameron 我想我有一个解决方案:在二维地图上绘制,填充多边形,并将颜色映射到球体上。我正在努力解决这个问题。 - Stéphane Laurent
使用材料属性lit = FALSE应该使得两个对cyan的使用看起来相同。 - user2554330
显示剩余13条评论
1个回答

1
这里是如何填充国家的方法。
library(maps)
data(worldMapEnv)

# make a PNG image of the world in 2D with filled countries
world <- map("world", plot = FALSE, fill = TRUE, wrap = c(0, 360))

png("world2D.png", width = 1024L, height = 1024L)
opar <- par(mar = c(0, 0, 0, 0))
plot(world$x, world$y, type = "n", xlab = NA, ylab = NA, axes = FALSE,
     xaxs = "i", yaxs = "i", xlim = c(0, 360), ylim = c(-90, 90))
polygon(world$x, world$y, col="orangered", lwd = 2)
par(opar)
dev.off()

enter image description here

# use spherical coordinates to make a mesh of the unit sphere
library(cgalMeshes)
sphericalCoordinates <- function(θ, ϕ){
  x <- cos(θ) * sin(ϕ)
  y <- sin(θ) * sin(ϕ)
  z <- cos(ϕ)
  rbind(x, y, z)
}
rmesh <- parametricMesh(
  sphericalCoordinates, urange = c(0, 2*pi), vrange = c(0, pi),
  periodic = c(TRUE, TRUE), nu = 1024L, nv = 1024L
)
rmesh$normals <- rmesh$vb[-4L, ]

# get the angles θ and ϕ of the vertices of the mesh
UV <- cooltools::car2sph(t(rmesh$vb[-4L, ]))
UV <- cbind(UV[, 3L], UV[, 2L])


# now load the PNG image
library(imager)
img <- load.image("world2D.png")
# take the r, g, b channels
r <- squeeze(R(img))
g <- squeeze(G(img))
b <- squeeze(B(img))

# make interpolation functions to get the colors of the UV points
library(cooltools) # to get the `approxfun2` function
x_ <- seq(0, 2*pi, length.out = 1024L)
y_ <- seq(0, pi, length.out = 1024L)
f_r <- approxfun2(x_, y_, r)
f_g <- approxfun2(x_, y_, g)
f_b <- approxfun2(x_, y_, b)

# now, interpolate the r, g, b values
UV_r <- f_r(UV[, 1L], UV[, 2L])
UV_g <- f_g(UV[, 1L], UV[, 2L])
UV_b <- f_b(UV[, 1L], UV[, 2L])

# convert rgb to hex codes
clrs <- rgb(UV_r, UV_g, UV_b)
clrs[clrs == "#FFFFFF"] <- "cyan" # replace white with cyan


# assign the colors to the vertices of the mesh
# I don't know why, but one has to reverse the colors
rmesh$material <- list(color = rev(clrs))


# plot
library(rgl)
open3d(windowRect = 50 + c(0, 0, 512, 512), zoom = 0.7)
shade3d(rmesh, meshColor = "vertices")
snapshot3d("world3D.png", webshot = FALSE)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接