Chapter 6 Examples and exercises

In each chunk below consider each 3D scene as being a “fresh start”. Use rgl::clear3d() to ensure there is no existing data in a scene.

If using RStudio Server (rstudio.cloud, binder, etc.) then calling rglwidget() is required to capture the 3D plot (or changes to it) and refresh the htmlwidget viewer.

6.1 Exercise 1

Why are the vertex and index matrices stored in transpose form?

## List of 8
##  $ vb             : num [1:4, 1:16] 0 3 0.312 1 1 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:4] "x" "y" "z" "1"
##   .. ..$ : NULL
##  $ ib             : int [1:4, 1:9] 1 2 6 5 2 3 7 6 3 4 ...
##  $ primitivetype  : chr "quad"
##  $ material       : list()
##  $ normals        : NULL
##  $ texcoords      : NULL
##  $ raster_metadata:List of 7
##   ..$ xmn  : num 0
##   ..$ xmx  : num 3
##   ..$ ymn  : num 0
##   ..$ ymx  : num 3
##   ..$ ncols: int 3
##   ..$ nrows: int 3
##   ..$ crs  : chr "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
##  $ crs            : chr "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
##  - attr(*, "class")= chr [1:3] "quadmesh" "mesh3d" "shape3d"

6.1.1 EX 1 ANSWER

EX1 SOLUTION

So that sets of coordinates and primitive-indexes are stored contiguously in memory. I think this matches more native implementations in other languages.

Code to plot quads directly in rgl looks like this:

We can use to to advantage in 2D, for a quick check of our intepretation.

6.2 Exercise 2

  1. Run this code
  2. Think about what is wrong with the scene.
  3. What can we do about the ugly plot?

6.2.1 EX 2 ANSWER

EX2 SOLUTION

We need to modify the aspect ratio, because we are plotting coordinates in degrees against elevation in metres. There’s no one right answer, getting a sensible aspect ratio will depend on the data in the scene.

6.4 Examples

6.4.2 Polygon triangulation

NOTE: the functions DEL() and TRI() will triangulate polygon layers into an currently-experimental form, using the development packages anglr and silicate. The plot3d() function converts these

Polygon-triangulation vesion of the North Carolina data set - we add Z values (copy_down()) to the triangles from an elevation topography raster.

Copy-down for a raster value considers Z a continuous measure, so each feature is connected by shared vertices to neighbours.

Copy-down for a polygon value considers Z a discrete measure, so each feature is separated.

6.5 Try with own data

  • Convert rasters to mesh3d with quadmesh()
  • Convert polygons to mesh3d with as.mesh3d() (using anglr package)
  • Plot mesh3d or spatial objects with plot3d()