Skip to content
Prev 386056 / 398513 Next

3d plot of earth with cut

On 21/10/2020 8:45 a.m., Balint Radics wrote:
The rgl package has a full sphere of the Earth with (obsolete) political 
boundaries in example(persp3d).  To cut it in half along the plane 
through a given longitude (and that longitude + 180 deg), you could use 
clipPlanes3d, or clipObj3d.  For example,

library(rgl)
lat <- matrix(seq(90, -90, len = 50)*pi/180, 50, 50, byrow = TRUE)
long <- matrix(seq(-180, 180, len = 50)*pi/180, 50, 50)

r <- 6378.1 # radius of Earth in km
x <- r*cos(lat)*cos(long)
y <- r*cos(lat)*sin(long)
z <- r*sin(lat)

open3d()
ids <- persp3d(x, y, z, col = "white",
         texture = system.file("textures/worldsmall.png", package = "rgl"),
         specular = "black", axes = FALSE, box = FALSE, xlab = "", ylab 
= "", zlab = "",
         normal_x = x, normal_y = y, normal_z = z)

clipFn <- function(coords) {
   pmax(coords[,1], coords[,2]) # Just an example...
}
clipObj3d(ids["surface"], clipFn)


Filling in the exposed surface could be done with polygon3d(), with some 
work to construct the polygon. Displaying the function across that 
surface could be done in a couple of ways, either by using a texture map 
(like for the map), or subdividing the polygon and setting colour by the 
coordinates of each vertex.

Note that the clipObj3d function isn't on CRAN yet; there you'd have to 
use clipPlanes3d.  You can get the newer version from R-forge.

Duncan Murdoch