### Install terrainmeshr from GitHub Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Install the development version of the terrainmeshr package from GitHub using devtools. ```r # install.packages("devtools") devtools::install_github("tylermorganwall/terrainmeshr") ``` -------------------------------- ### Install terrainmeshr from CRAN Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Install the released version of the terrainmeshr package from CRAN. ```r install.packages("terrainmeshr") ``` -------------------------------- ### Install Latest Package Version Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/CONTRIBUTING.md Install the latest version of the package from GitHub using the 'remotes' package. This is a troubleshooting step for bug reports. ```R remotes::install_github("tylermorganwall/{package name}") ``` -------------------------------- ### Get System Information Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/CONTRIBUTING.md Capture system information using the Sys.info() function. This is required for bug reports to help diagnose environment-specific issues. ```R Sys.info() ``` -------------------------------- ### Check Package on Windows Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/CONTRIBUTING.md Check the package on a Windows environment using rhub::check_on_windows(). This is required when making changes to compiled code. ```R rhub::check_on_windows() ``` -------------------------------- ### Check Package on Solaris Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/CONTRIBUTING.md Check the package on a Solaris environment using rhub::check_on_solaris(). This is required when making changes to compiled code. ```R rhub::check_on_solaris() ``` -------------------------------- ### Render Camera Snapshot Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Adjusts the camera perspective and captures a snapshot of the 3D scene. This is typically used after setting up the scene with functions like 'render_camera'. ```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() ``` -------------------------------- ### Full Triangulation of Volcano Dataset Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Generates a full triangulation of the built-in 'volcano' dataset in R. This serves as a baseline for comparison with simplified triangulations. ```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) ``` -------------------------------- ### Check Package on macOS Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/CONTRIBUTING.md Check the package on a macOS environment using rhub::check_on_macos(). This is required when making changes to compiled code. ```R rhub::check_on_macos() ``` -------------------------------- ### Visualize 3D Terrain with rgl Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Renders 3D terrain using the rgl package. Note that 'rgl::rgl.triangles' is deprecated and 'triangles3d' should be used instead. This snippet visualizes two different terrain datasets with distinct colors. ```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() ``` -------------------------------- ### Triangulate Volcano Dataset with Max Triangles Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Triangulates the 'volcano' dataset using a specified maximum number of triangles (`maxTriangles = 200`). This results in a significant reduction in model complexity. ```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 Volcano Dataset with Max Error 0 Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Performs triangulation of the 'volcano' dataset with `maxError = 0`, ensuring no loss of precision. This demonstrates the initial reduction in triangles while maintaining accuracy. ```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) ``` -------------------------------- ### Apply Texture to Low-Res Mesh Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Applies a texture generated from a high-resolution mesh to a low-resolution mesh. This allows for the appearance of detail without the computational cost. Note the deprecation warnings for 'rgl::rgl.triangles'. ```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() ``` -------------------------------- ### Check Package on CRAN Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/CONTRIBUTING.md Perform a CRAN-like check on the package using rhub::check_for_cran(). This is part of the pull request process to ensure package health. ```R rhub::check_for_cran() ``` -------------------------------- ### Triangulate Volcano Dataset with Max Error Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Triangulates the 'volcano' dataset with a specified maximum allowable error (`maxError = 2`). This balances model size and accuracy. ```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) ``` -------------------------------- ### Close rgl Device Source: https://github.com/tylermorganwall/terrainmeshr/blob/master/README.md Closes the current rgl device. Note that 'rgl.close' is deprecated and 'close3d' should be used instead. ```r par(mfrow=c(1,1)) rgl.close() #> Warning in rgl.close(): 'rgl.close' is deprecated. #> Use 'close3d' instead. #> See help("Deprecated") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.