Simulating elevation data in R

I was looking for a quick way to create dummy digital elevation model (DEM) datasets in R, and a kind soul in StackOverload shared the following code. It is an implementation of the Perlin noise algorithm written by Ken Parlin in the early 1980s and is now widely used to create realistic looking CGI landscapes and other natural patterns.

The function:

The function perlin_noise(xFieldSize, yFieldSize, x_grid, y_grid) produces a matrix of Parlin noise values that can be quickly visualised:

image(1:150, 1:100, perlin_noise(10,7,150,100), col = grey.colors(100), asp=T)

Producing something like this:
Screen Shot 2013-03-13 at 16.23.32


Appendum (14.3.13)

As the aforementioned kind soul (Vincent Zoonekynd) in *StackOverflow* has added, it is possible to apply a fractal dimension to the function above to achieve a better output.

scaler <- .6
fractDim <- 7
m <- perlin_noise(2, 2, 2^fractDim, 2^fractDim)
for( i in 2:fractDim ){
  m <- m + scaler^i * perlin_noise(2^i, 2^i, 2^fractDim, 2^fractDim)

Each iteration overlays a new fractal level of noise:

fractal noise

The {rgl} package (here I've used the surface3d function) is an effective way to visualise the data in 3D:

Screen Shot 2013-03-14 at 22.37.26