Learn R Programming

isocubes (version 1.0.0)

calc_heightmap_coords: Calculate voxel coordinates from a matrix where values in the matrix indicate height above the ground

Description

Calculate voxel coordinates from a matrix where values in the matrix indicate height above the ground

Usage

calc_heightmap_coords(
  mat,
  fill = NULL,
  scale = 1,
  flipx = FALSE,
  flipy = TRUE,
  ground = "xy",
  solid = TRUE,
  check_visibility = FALSE,
  verbose = FALSE
)

Value

data.frame of voxel coordinates

Arguments

mat

integer matrix. The matrix will be interpreted as cubes flat on the page, with the value in the matrix interpreted as the height above the page.

fill

matrix of colours the same dimensions as the mat argument. Default: NULL. If fill is not NULL, then a fill column will be included in the final returned coordinates.

scale

scale factor for values in matrix. Default = 1

flipx, flipy

Should the matrix be flipped in the horizontal/vertical directions (respectively)? Default: flipx = FALSE, flipy = TRUE.

Note: flipy defaults to TRUE as matrices are indexed from the top-down, but the isometric coordinate space is increasing from the bottom up. Flipping the matrix vertically is usually what you want.

ground

Orientation of the ground plane. Default: 'xy'. Possible values 'xz', 'xy'

solid

Should the heightmap be made 'solid' i.e. without holes? default: TRUE. This can be an expensive operation in terms of both memory and CPU, but should be OK for simple examples. Set to FALSE if things take too long. This operation works by extruding cubes down from the top of the height map to the floor to ensure gaps do not appear when the slope is too great.

check_visibility

Should non-visible cubes be removed? Default: FALSE. If you plan on rotating or manipulating the returned coordinates then this should definitely by FALSE. If TRUE, then non-visible voxels will be entirely removed from the returned coordinates i.e. they will be missing if you change the rendering viewpoint from the default.

verbose

Be verbose? default: FALSE

Examples

Run this code
# Plot the standard volcano 
mat <- volcano

# normalise height
mat <- mat - min(mat)

# Assign a distinct colour for each height value
val <- as.vector(mat)
val <- round(255 * val / max(val))
fill <- matrix("", nrow=nrow(mat), ncol=ncol(mat))
fill[] <- terrain.colors(256)[val + 1L]

# Calculate coordinates of heightmap, render as isocubes
coords <- calc_heightmap_coords(mat, fill = fill, scale = 0.3)
head(coords)
isocubesGrob(coords, size = 2, y = 0) |>
  grid::grid.draw()

Run the code above in your browser using DataLab