#Let's triangulate the built-in `volcano` dataset.
#Helper function to plot polygons over an `image()` plot.
plot_polys = function(tri_matrix) {
#reverse orienation for `image`
tri_matrix[,3] = max(tri_matrix[,3])-tri_matrix[,3]+1
for(i in seq_len(nrow(tri_matrix)/3)) {
polypath(tri_matrix[(3*(i-1)+1):(3*i), c(1,3)])
}
}
#Here, we don't accept any error, but still triangulate
tris = triangulate_matrix(volcano, maxError = 0, verbose = TRUE)
image(x=1:nrow(volcano), y = 1:ncol(volcano), volcano)
plot_polys(tris)
#Let's increase the allowable error:
tris = triangulate_matrix(volcano, maxError = 1, verbose = TRUE)
image(x=1:nrow(volcano), y = 1:ncol(volcano), volcano)
plot_polys(tris)
#Increase it again
tris = triangulate_matrix(volcano, maxError = 10, verbose = TRUE)
image(x=1:nrow(volcano), y = 1:ncol(volcano), volcano)
plot_polys(tris)
#Here, we set an allowable number of triangles instead, using exactly 20 triangles:
tris = triangulate_matrix(volcano, maxTriangles = 20, verbose = TRUE)
image(x=1:nrow(volcano), y = 1:ncol(volcano), volcano)
plot_polys(tris)
#The output of this function can be passed directly to `rgl::triangles3d()` for plotting in 3D.
Run the code above in your browser using DataLab