### Install terrainmeshr from GitHub Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Install the development version of the terrainmeshr package from GitHub. Requires the devtools package. ```R # install.packages("devtools") devtools::install_github("tylermorganwall/terrainmeshr") ``` -------------------------------- ### Install terrainmeshr from CRAN Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Install the released version of the terrainmeshr package from CRAN. ```R install.packages("terrainmeshr") ``` -------------------------------- ### Triangulate Height Map with Maximum Error Source: https://cran.r-project.org/web/packages/terrainmeshr/refman/terrainmeshr.html Triangulates a height map matrix using Delaunay triangulation. This example shows triangulation with no allowable error, useful for precise representation. ```R tris = triangulate_matrix(volcano, maxError = 0, verbose = TRUE) image(x=1:nrow(volcano), y = 1:ncol(volcano), volcano) plot_polys(tris) ``` -------------------------------- ### 3D Visualization of Triangulated Volcano Data Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Compares a simplified triangulation (green) with the full triangulation (red) of the 'volcano' dataset in a 3D environment using rayshader and rgl. Requires loading these libraries. ```R library(rayshader) library(rgl) tri_volcano[,3] = -tri_volcano[,3] par3d(windowRect=c(0,0,600,600)) rgl::rgl.triangles(tris2, color="green", lit=TRUE) #> Warning in rgl::rgl.triangles(tris2, color = "green", lit = TRUE): 'rgl::rgl.triangles' is deprecated. #> Use 'triangles3d' instead. #> See help("Deprecated") rgl::rgl.triangles(tri_volcano, color="red", lit=TRUE) #> Warning in rgl::rgl.triangles(tri_volcano, color = "red", lit = TRUE): 'rgl::rgl.triangles' is deprecated. #> Use 'triangles3d' instead. #> See help("Deprecated") bg3d(color="black") par(mfrow=c(2,2)) render_camera(phi=30,fov=0,theta=45,zoom=0.9) render_snapshot() ``` ```R render_camera(phi=30,fov=0,theta=135,zoom=0.9) render_snapshot() ``` ```R render_camera(phi=30,fov=0,theta=225,zoom=0.9) render_snapshot() ``` ```R render_camera(phi=30,fov=0,theta=315,zoom=0.9) render_snapshot() ``` ```R par(mfrow=c(1,1)) rgl.close() #> Warning in rgl.close(): 'rgl.close' is deprecated. #> Use 'close3d' instead. #> See help("Deprecated") ``` -------------------------------- ### Generate and Apply Texture to 3D Mesh Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html This snippet computes a texture from a high-resolution mesh using rayshader, saves it, and then applies it to a low-resolution mesh using rgl. It also transforms texture coordinates for proper mapping. Use this when you need to visualize detailed surfaces with reduced polygon counts. ```R temp_texture = tempfile(fileext = ".png") #Compute the texture using the full-resolution mesh and save it to file volcano %>% sphere_shade() %>% add_shadow(ray_shade(volcano)) %>% save_png(temp_texture) #Compute the texcoords for the 3D mesh texcoords = tris2[,c(1,3)] texcoords[,1] = texcoords[,1]/max(texcoords[,1]) texcoords[,2] = texcoords[,2]/max(texcoords[,2]) texcoords2 = tri_volcano[,c(1,3)] texcoords2[,2] = -texcoords2[,2] texcoords2[,1] = texcoords2[,1]/max(texcoords2[,1]) texcoords2[,2] = 1-texcoords2[,2]/max(texcoords2[,2]) #Plot the two volcanos, side by side par3d(windowRect=c(0,0,600,600)) rgl::rgl.triangles(tris2, lit=FALSE, texture = temp_texture, texcoords = texcoords) #> Warning in rgl::rgl.triangles(tris2, lit = FALSE, texture = temp_texture, : 'rgl::rgl.triangles' is deprecated. #> Use 'triangles3d' instead. #> See help("Deprecated") rgl::rgl.triangles(tri_volcano, lit=FALSE, texture = temp_texture, texcoords = texcoords2) #> Warning in rgl::rgl.triangles(tri_volcano, lit = FALSE, texture = temp_texture, : 'rgl::rgl.triangles' is deprecated. #> Use 'triangles3d' instead. #> See help("Deprecated") bg3d(color="black") par(mfrow=c(2,2)) render_camera(phi=30,fov=0,theta=45,zoom=0.9) render_snapshot() ``` -------------------------------- ### Render Camera Snapshot (Theta 315) Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Renders a snapshot of the 3D scene with the camera positioned at theta=315 degrees. This is part of a sequence to capture different views of the rendered mesh. ```R render_camera(phi=30,fov=0,theta=315,zoom=0.9) render_snapshot() ``` -------------------------------- ### Triangulate Height Map with Increased Error Tolerance Source: https://cran.r-project.org/web/packages/terrainmeshr/refman/terrainmeshr.html Demonstrates triangulating a height map with an increasing maximum allowable error. This results in a courser approximation and fewer triangles, suitable for reducing model size. ```R tris = triangulate_matrix(volcano, maxError = 1, verbose = TRUE) image(x=1:nrow(volcano), y = 1:ncol(volcano), volcano) plot_polys(tris) ``` -------------------------------- ### Render Camera Snapshot (Theta 225) Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Renders a snapshot of the 3D scene with the camera positioned at theta=225 degrees. This is part of a sequence to capture different views of the rendered mesh. ```R render_camera(phi=30,fov=0,theta=225,zoom=0.9) render_snapshot() ``` -------------------------------- ### Render Camera Snapshot (Theta 135) Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Renders a snapshot of the 3D scene with the camera positioned at theta=135 degrees. This is part of a sequence to capture different views of the rendered mesh. ```R render_camera(phi=30,fov=0,theta=135,zoom=0.9) render_snapshot() ``` -------------------------------- ### Generate Full Triangulation of Volcano Dataset Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Generates a full triangulation of the built-in R 'volcano' dataset. This code manually creates triangles for each grid cell, resulting in a high number of triangles. ```R #Generate full triangulation of `volcano` tri_volcano = matrix(0, nrow = 86*60*3*2, ncol = 3) counter = 1 for(i in seq_len(nrow(volcano) - 1)) { for(j in seq_len(ncol(volcano) - 1)) { tri_volcano[6*(counter-1)+1, ] = c(i, volcano[i,j], j) tri_volcano[6*(counter-1)+2, ] = c(i, volcano[i,j+1], j+1) tri_volcano[6*(counter-1)+3, ] = c(i+1, volcano[i+1,j], j) tri_volcano[6*(counter-1)+4, ] = c(i+1, volcano[i+1,j+1], j+1) tri_volcano[6*(counter-1)+5, ] = c(i+1, volcano[i+1,j], j) tri_volcano[6*(counter-1)+6, ] = c(i, volcano[i,j+1], j+1) counter = counter + 1 } } #Function to plot triangles on top of image plot_polys = function(tri_matrix) { 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)]) } } image(x=seq_len(nrow(volcano)), y = seq_len(ncol(volcano)), volcano) plot_polys(tri_volcano) ``` -------------------------------- ### Triangulate Matrix with Max Triangles Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Uses the `triangulate_matrix` function to triangulate the 'volcano' height field, limiting the output to a maximum of 200 triangles. The `verbose` argument displays the reduction percentage and final error. ```R tris1 = triangulate_matrix(volcano, maxTriangles = 200, verbose = TRUE) #> 98.1% reduction: Number of triangles reduced from 10614 to 200. Error: 5.799988 image(x=seq_len(nrow(volcano)), y = seq_len(ncol(volcano)), volcano) plot_polys(tris1) ``` -------------------------------- ### Triangulate Matrix with Zero Max Error Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Uses the `triangulate_matrix` function to perform a triangulation of the 'volcano' height field with `maxError = 0`, ensuring no loss of precision. The `verbose` argument shows the reduction percentage. ```R library(terrainmeshr) tris = triangulate_matrix(volcano, maxError = 0, verbose = TRUE) #> 35.3% reduction: Number of triangles reduced from 10614 to 6867. Error: 0.000000 image(x=seq_len(nrow(volcano)), y = seq_len(ncol(volcano)), volcano) plot_polys(tris) ``` -------------------------------- ### Triangulate Height Map with Maximum Triangle Count Source: https://cran.r-project.org/web/packages/terrainmeshr/refman/terrainmeshr.html Triangulates a height map to produce a mesh with a specific, maximum number of triangles. This is useful when a strict triangle budget must be met. ```R tris = triangulate_matrix(volcano, maxTriangles = 20, verbose = TRUE) image(x=1:nrow(volcano), y = 1:ncol(volcano), volcano) plot_polys(tris) ``` -------------------------------- ### Triangulate Height Map with High Error Tolerance Source: https://cran.r-project.org/web/packages/terrainmeshr/refman/terrainmeshr.html Shows triangulation of a height map with a high maximum allowable error, leading to a significantly simplified mesh. This is useful for substantial model size reduction. ```R tris = triangulate_matrix(volcano, maxError = 10, verbose = TRUE) image(x=1:nrow(volcano), y = 1:ncol(volcano), volcano) plot_polys(tris) ``` -------------------------------- ### Triangulate a Height Map Source: https://cran.r-project.org/web/packages/terrainmeshr/refman/terrainmeshr.html Approximates a rectangular height field using Delaunay triangulation with constraints on maximum allowable error or a maximum number of triangles. This function is useful for reducing model size with minimal perceptual loss in terrain quality. ```APIDOC ## triangulate_matrix ### Description Uses Delaunay triangulation to approximate a rectangular height field (in matrix form) with constraints (either maximum allowable error, or a maximum number of triangles). Increasing the error limit will result in a courser approximation, but fewer triangles in the model. For many models (particularly those with large, flat regions or smooth changes in height), this can result in significant reductions in model size with no perceptual loss in terrain surface quality. ### Usage ```R triangulate_matrix( heightmap, maxError = 1e-04, maxTriangles = 0, y_up = TRUE, start_index = 1, verbose = FALSE ) ``` ### Arguments - **heightmap** (matrix) - A two-dimensional matrix, where each entry in the matrix is the elevation at that point. All points are assumed to be evenly spaced. - **maxError** (numeric) - Default '0.0001'. Maximum error allowed in triangulating the height map. - **maxTriangles** (numeric) - Default '0', which turns off this setting (and only uses the 'max_error' arg). Otherwise, specifies the maximum number of triangles when triangulating the height map. - **y_up** (logical) - Default 'TRUE'. Which axis is "upwards" in the return matrix. If 'FALSE', 'z' is up. - **start_index** (numeric) - Default '1'. The offset to the first 'x' and 'z' indices. - **verbose** (logical) - Default 'FALSE'. Prints reduction in number of triangles/max error. ### Value Returns a matrix of vertices and IDs for each triangle. ### Examples ```R #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. ``` ``` -------------------------------- ### Triangulate Matrix with Max Error Source: https://cran.r-project.org/web/packages/terrainmeshr/readme/README.html Uses the `triangulate_matrix` function to triangulate the 'volcano' height field, setting a maximum allowable error of 2. The `verbose` argument shows the reduction percentage and final error. ```R tris2 = triangulate_matrix(volcano, maxError = 2, verbose = TRUE) #> 91.7% reduction: Number of triangles reduced from 10614 to 878. Error: 2.000000 image(x=seq_len(nrow(volcano)), y = seq_len(ncol(volcano)), volcano) plot_polys(tris2) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.