Skip to content

Commit

Permalink
rayshader v0.38.0: Add variable point and line width support to `rend…
Browse files Browse the repository at this point in the history
…er_points()` and `render_lines()`
  • Loading branch information
tylermorganwall committed Mar 28, 2024
1 parent 4a69c72 commit f9b2204
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 45 deletions.
17 changes: 7 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Package: rayshader
Type: Package
Title: Create Maps and Visualize Data in 2D and 3D
Version: 0.37.3
Date: 2024-02-19
Author: Tyler Morgan-Wall
Version: 0.38.0
Date: 2024-03-28
Authors@R:
person("Tyler", "Morgan-Wall", email = "tylermw@gmail.com",role = c("aut", "cph", "cre"),
comment = c(ORCID = "0000-0002-3131-3814"))
Maintainer: Tyler Morgan-Wall <tylermw@gmail.com>
Description: Uses a combination of raytracing and multiple hill shading methods to produce 2D and 3D data visualizations and maps. Includes water detection and layering functions, programmable color palette generation, several built-in textures for hill shading, 2D and 3D plotting options, a built-in path tracer, 'Wavefront' OBJ file export, and the ability to save 3D visualizations to a 3D printable format.
License: GPL-3
Expand Down Expand Up @@ -47,14 +49,9 @@ Suggests:
osmdata,
raybevel
LinkingTo: Rcpp, progress, RcppArmadillo
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
URL: https://www.rayshader.com,
https://github.com/tylermorganwall/rayshader
BugReports: https://github.com/tylermorganwall/rayshader/issues
Remotes: tylermorganwall/rayimage,
tylermorganwall/rayrender,
tylermorganwall/rayvertex,
tylermorganwall/raybevel,
dmurdoch/rgl
Config/testthat/edition: 3
Additional_Repositories: https://github.com/tylermorganwall/raybevel
Additional_repositories: https://tylermorganwall.r-universe.dev/
4 changes: 2 additions & 2 deletions R/convert_rgl_to_raymesh.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ convert_rgl_to_raymesh = function(save_shadow = TRUE) {
dissolve = alpha,
specular = specular)))
}
for(row in 1:nrow(vertex_info)) {
for(row in seq_len(nrow(vertex_info))) {
if(!is.na(vertex_info$lit[row])) {
lit_val = unlist(vertex_info$lit[row])
} else {
Expand Down Expand Up @@ -130,7 +130,7 @@ convert_rgl_to_raymesh = function(save_shadow = TRUE) {

indices = indices - 1
indices = matrix(indices, ncol=3, byrow=TRUE)
indices = indices[1:(nrow(indices)-na_counter),]
indices = indices[seq_len(nrow(indices)-na_counter),]
texture_loc = vertex_info$texture_file[[row]]
texture_loc = ifelse(!is.na(texture_loc), texture_loc, "")
final_scene[[num_elems]] = rayvertex::construct_mesh(indices = indices,
Expand Down
2 changes: 1 addition & 1 deletion R/generate_scalebar_overlay.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
#' latlong=TRUE)) %>%
#' plot_map()
#'}
##'if(run_documentation()) {
#'if(run_documentation()) {
#'#64373.8 meters in 40 miles
#'#Create custom labels, change font and text size, remove the border/ticks, and change the color
#'#Here, we specify a width and height to double the resolution of the image (for sharper text)
Expand Down
12 changes: 8 additions & 4 deletions R/render_highquality.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
#'than straight segments.
#'@param use_extruded_paths Default `TRUE`. If `FALSE`, paths will be generated with the `rayrender::path()` object, instead
#'of `rayrender::extruded_path()`.
#'@param point_radius Default `0.5`. Radius of 3D points (rendered with `render_points()`.
#'@param point_radius Default `1`. Radius of 3D points (rendered with `render_points()`). This scales the existing
#'value of size specified in `render_points()`.
#'@param scale_text_angle Default `NULL`. Same as `text_angle`, but for the scale bar.
#'@param scale_text_size Default `6`. Height of the scale bar text.
#'@param scale_text_offset Default `c(0,0,0)`. Offset to be applied to all scale bar text labels.
Expand Down Expand Up @@ -451,20 +452,22 @@ render_highquality = function(filename = NA, samples = 128,
for(i in seq_len(length(pathids))) {
temp_verts = rgl.attrib(pathids[i], "vertices")
temp_color = rgl.attrib(pathids[i], "colors")
temp_lwd = material3d("lwd", id = pathids[i]) * line_radius

if(nrow(temp_color) == 1) {
temp_color = matrix(temp_color[1:3], byrow = TRUE, ncol = 3, nrow = nrow(temp_verts))
}
matrix_center = matrix(bbox_center, byrow=TRUE,ncol=3,nrow = nrow(temp_verts))
path_material_args$color = temp_color[1,1:3]
if(use_extruded_paths) {
pathline[[counter]] = rayrender::extruded_path(points = temp_verts - matrix_center ,
width = line_radius * 2,
width = temp_lwd * 2,
smooth_normals = TRUE,
straight = !smooth_line,
material = do.call("path_material", args = path_material_args))
} else {
pathline[[counter]] = rayrender::path(points = temp_verts - matrix_center,
width = line_radius * 2,
width = temp_lwd * 2,
straight = !smooth_line,
material = do.call("path_material", args = path_material_args))
}
Expand All @@ -476,6 +479,7 @@ render_highquality = function(filename = NA, samples = 128,
for(i in seq_len(length(pointids))) {
temp_verts = rgl.attrib(pointids[i], "vertices")
temp_color = rgl.attrib(pointids[i], "colors")
temp_size = material3d("size", id = pointids[i]) * point_radius
if(nrow(temp_color) == 1) {
temp_color = matrix(temp_color[1:3], byrow = TRUE, ncol = 3, nrow = nrow(temp_verts))
}
Expand All @@ -485,7 +489,7 @@ render_highquality = function(filename = NA, samples = 128,
pointlist[[counter]] = rayrender::sphere(x = temp_verts[j,1] - bbox_center[1],
y = temp_verts[j,2] - bbox_center[2],
z = temp_verts[j,3] - bbox_center[3],
radius = point_radius,
radius = temp_size,
material = do.call("point_material", args = point_material_args))
counter = counter + 1
}
Expand Down
69 changes: 56 additions & 13 deletions R/render_path.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,36 @@ render_path = function(lat, long = NULL, altitude = NULL, groups = NULL,
}
if(resample_evenly) {
stopifnot(resample_n > 1)
xyz = render_path(extent = extent, lat = lat, long = long, altitude = altitude,
zscale=zscale, heightmap = heightmap, offset = offset, resample_evenly = FALSE,
xyz = render_path(extent = extent, lat = lat, long = long,
altitude = altitude,
zscale=zscale, heightmap = heightmap,
offset = offset, resample_evenly = FALSE,
reorder = reorder, reorder_first_index = reorder_first_index,
reorder_duplicate_tolerance = reorder_duplicate_tolerance,
reorder_merge_tolerance = reorder_merge_tolerance,
simplify_tolerance = simplify_tolerance,
clear_previous = FALSE, return_coords = TRUE)
xyz = lapply(xyz, get_interpolated_points_path, n = resample_n)
xyz = do.call("rbind",lapply(xyz,
\(x) rbind(x,matrix(NA,ncol=3,nrow=1))))
xyz = do.call("rbind",lapply(xyz, \(x) rbind(x,matrix(NA,ncol=3,nrow=1))))
if(!return_coords) {
rgl::lines3d(xyz,
color = color, tag = tag, lwd = linewidth, line_antialias = antialias)
return(invisible())
if(length(linewidth) > 1) {
if(length(linewidth) == nrow(xyz)) {
linewidth = (linewidth[seq_len(length(linewidth))[-1]] +
linewidth[seq_len(length(linewidth)-1)])/2
}
color_length = length(color)
for(i in seq_len(nrow(xyz)-1)) {
rgl::lines3d(xyz[i:(i+1),],
color = color[((i-1) %% color_length) + 1],
tag = tag, lwd = linewidth[i],
line_antialias = antialias)
}
return(invisible())
} else {
rgl::lines3d(xyz,
color = color, tag = tag, lwd = linewidth, line_antialias = antialias)
return(invisible())
}
} else {
return(xyz)
}
Expand Down Expand Up @@ -272,12 +288,39 @@ render_path = function(lat, long = NULL, altitude = NULL, groups = NULL,
altitude, offset, zscale, filter_bounds = FALSE)
}
if(!return_coords) {
xyz = do.call("rbind",lapply(coord_list,
\(x) rbind(x,matrix(NA,ncol=3,nrow=1))))
xyz = xyz[-nrow(xyz),]
rgl::lines3d(xyz,
color = color, tag = tag,
lwd = linewidth, line_antialias = antialias)
if(length(linewidth) > 1) {
if(length(coord_list) == 1) {
xyz = do.call("rbind",coord_list)
if(length(linewidth) == nrow(xyz)) {
linewidth = (linewidth[seq_len(length(linewidth))[-1]] +
linewidth[seq_len(length(linewidth)-1)])/2
}
stopifnot(length(linewidth) == (nrow(xyz)-1))
color_length = length(color)
for(i in seq_len(nrow(xyz)-1)) {
rgl::lines3d(xyz[i:(i+1),],
color = color[((i-1) %% color_length) + 1],
tag = tag, lwd = linewidth[i],
line_antialias = antialias)
}
} else {
stopifnot(length(coord_list) == length(linewidth))
color_length = length(color)
for(i in seq_len(length(coord_list))) {
rgl::lines3d(coord_list[[i]],
color = color[((i-1) %% color_length) + 1],
tag = tag, lwd = linewidth[i],
line_antialias = antialias)
}
}
} else {
xyz = do.call("rbind",lapply(coord_list,
\(x) rbind(x,matrix(NA,ncol=3,nrow=1))))
xyz = xyz[-nrow(xyz),]
rgl::lines3d(xyz,
color = color, tag = tag,
lwd = linewidth, line_antialias = antialias)
}
} else {
return(coord_list)
}
Expand Down
18 changes: 14 additions & 4 deletions R/render_points.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#'system defined by the extent object. If no altitude is provided, the points will be elevated a constant offset
#'above the heightmap. If the points goes off the edge, the nearest height on the heightmap will be used.
#'
#'@param long Vector of longitudes (or other coordinate in the same coordinate reference system as extent).
#'@param lat Vector of latitudes (or other coordinate in the same coordinate reference system as extent).
#'@param long Vector of longitudes (or other coordinate in the same coordinate reference system as extent).
#'@param altitude Default `NULL`. Elevation of each point, in units of the elevation matrix (scaled by zscale). If a single value,
#'all data will be rendered at that altitude.
#'@param extent Either an object representing the spatial extent of the 3D scene
Expand All @@ -16,8 +16,9 @@
#'@param heightmap Default `NULL`. Automatically extracted from the rgl window--only use if auto-extraction
#'of matrix extent isn't working. A two-dimensional matrix, where each entry in the matrix is the elevation at that point.
#' All points are assumed to be evenly spaced.
#'@param size Default `3`. The point size.
#'@param color Default `black`. Color of the point.
#'@param size Default `3`. The point size. This can be a vector (the same length as `lat` and `long`) specifying
#'a size for each point.
#'@param color Default `black`. Color of the point. This can also be a vector specifying the color of each point.
#'@param offset Default `5`. Offset of the track from the surface, if `altitude = NULL`.
#'@param clear_previous Default `FALSE`. If `TRUE`, it will clear all existing points.
#'@export
Expand Down Expand Up @@ -110,5 +111,14 @@ render_points = function(lat = NULL, long = NULL, altitude = NULL, extent = NULL
}
xyz = transform_into_heightmap_coords(extent, heightmap, lat, long,
altitude, offset, zscale)
rgl::points3d(xyz[,1], xyz[,2], xyz[,3], color = color, tag = "points3d", size = size)
if(length(size) > 1) {
stopifnot(length(size) == nrow(xyz))
color_length = length(color)
for(i in seq_len(nrow(xyz))) {
rgl::points3d(xyz[i,1], xyz[i,2], xyz[i,3], color = color[((i-1) %% color_length) + 1],
tag = "points3d", size = size[i])
}
} else {
rgl::points3d(xyz[,1], xyz[,2], xyz[,3], color = color, tag = "points3d", size = size)
}
}
7 changes: 4 additions & 3 deletions R/render_snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
#'will specify the absolute angle all the labels are facing. If three angles, this will specify all three orientations
#'(relative to the x,y, and z axes) of the text labels.
#'@param text_size Default `30`. Height of the text.
#'@param point_radius Default `2`. Radius of 3D points (rendered with `render_points()`.
#'@param point_radius Default `1`. Radius of 3D points (rendered with `render_points()`). This scales the existing
#'value of size specified in `render_points()`.
#'@param line_offset Default `1e-7`. Small number indicating the offset in the scene to apply to lines if using software rendering. Increase this if your lines
#'aren't showing up, or decrease it if lines are appearing through solid objects.
#'@param thick_lines Default `TRUE`. If `software_render = TRUE`, this will render path segments as thick cylinders. Otherwise, it will
Expand Down Expand Up @@ -117,8 +118,8 @@ render_snapshot = function(filename, clear=FALSE,
software_render = FALSE, camera_location = NULL, camera_lookat = c(0,0,0),
background = NULL,
text_angle = NULL, text_size = 30, text_offset = c(0,0,0),
point_radius = 2,
line_offset = 1e-7, thick_lines = TRUE, line_radius = 0.5,
point_radius = 1,
line_offset = 1e-7, thick_lines = TRUE, line_radius = 1,
cache_scene = FALSE, reset_scene_cache = FALSE, new_page = TRUE,
print_scene_info = FALSE, fsaa = 1,
rayvertex_lighting = FALSE, rayvertex_lights = NULL,
Expand Down
10 changes: 7 additions & 3 deletions R/render_snapshot_software.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ render_snapshot_software = function(filename, cache_scene = FALSE, camera_locati
for(i in seq_len(length(pathids))) {
temp_verts = rgl.attrib(pathids[i], "vertices")
temp_color = rgl.attrib(pathids[i], "colors")
temp_lwd = material3d("lwd", id = pathids[i]) * line_radius

if(nrow(temp_color) == 1) {
temp_color = matrix(temp_color[1:3], byrow = TRUE, ncol = 3, nrow = nrow(temp_verts))
}
Expand All @@ -220,7 +222,7 @@ render_snapshot_software = function(filename, cache_scene = FALSE, camera_locati
if(!all(temp_verts[j+1,] == temp_verts[j,])) {
line_scene[[line_counter]] = rayvertex::segment_mesh(start = temp_verts[j,] - bbox_center,
end = temp_verts[j+1,] - bbox_center,
radius = line_radius,
radius = temp_lwd,
material = line_mat)
line_counter = line_counter + 1
}
Expand All @@ -239,14 +241,16 @@ render_snapshot_software = function(filename, cache_scene = FALSE, camera_locati
for(i in seq_len(length(pointids))) {
temp_verts = rgl.attrib(pointids[i], "vertices")
temp_color = rgl.attrib(pointids[i], "colors")
temp_size = material3d("size", id = pointids[i]) * point_radius
if(nrow(temp_color) == 1) {
temp_color = matrix(temp_color[1:3], byrow = TRUE, ncol = 3, nrow = nrow(temp_verts))
}
for(j in seq_len(nrow(temp_verts))) {
pointlist = rayvertex::add_shape(pointlist, rayvertex::sphere_mesh(position = c(temp_verts[j,1] - bbox_center[1],
pointlist = rayvertex::add_shape(pointlist,
rayvertex::sphere_mesh(position = c(temp_verts[j,1] - bbox_center[1],
temp_verts[j,2] - bbox_center[2],
temp_verts[j,3] - bbox_center[3]),
radius = point_radius,
radius = temp_size,
material = rayvertex::material_list(diffuse = temp_color[j,1:3])))
}
}
Expand Down
3 changes: 2 additions & 1 deletion man/render_highquality.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/render_points.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/render_snapshot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f9b2204

Please sign in to comment.