dmurdoch / rgl

rgl is a 3D visualization system based on OpenGL. It provides a medium to high level interface for use in R, currently modelled on classic R graphics, with extensions to allow for interaction.
https://dmurdoch.github.io/rgl/
GNU General Public License v2.0
85 stars 20 forks source link

R session crashes after drawing scene with `rayshader::plot_3d` #367

Closed fisher-j closed 1 year ago

fisher-j commented 1 year ago

I ran into a bug using rayshader::plot_3d to plot discontinuous subsets of a heightmap. It creates its own triangles from a height map and then calls rgl::triangles3d (and bg3d). I posted the issue there, but am reproducing here in case the issue is traceable to rgl.

Describe the bug When plotting some subsets of a heightmap using rayshader::plot_3d, the r session and rgl window both crash shortly after drawing the scene with [Process exited -1073741819]

This is happening on windows 10. I tested in windows terminal and regular cmd prompt. I realize this may be an issue with rgl, but I'm not sure how to reproduce with only rgl calls.

Session Info

R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8
other attached packages:
[1] raster_3.6-20    sp_1.6-0         stars_0.6-0      abind_1.4-5
[5] dplyr_1.1.1      rayshader_0.35.1 sf_1.0-12

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10        pillar_1.9.0       compiler_4.2.1     base64enc_0.1-3
 [5] prettyunits_1.1.1  class_7.3-20       iterators_1.0.14   tools_4.2.1
 [9] progress_1.2.2     digest_0.6.31      lattice_0.20-45    jsonlite_1.8.4
[13] lifecycle_1.0.3    tibble_3.2.1       pkgconfig_2.0.3    rlang_1.1.0
[17] foreach_1.5.2      DBI_1.1.3          cli_3.6.1          yaml_2.3.7
[21] parallel_4.2.1     xfun_0.38          fastmap_1.1.1      terra_1.7-18
[25] e1071_1.7-13       knitr_1.42         htmlwidgets_1.6.2  generics_0.1.3
[29] vctrs_0.6.1        hms_1.1.3          classInt_0.4-9     grid_4.2.1
[33] tidyselect_1.2.0   glue_1.6.2         R6_2.5.1           fansi_1.0.4
[37] rgl_1.1.3          magrittr_2.0.3     htmltools_0.5.5    codetools_0.2-18
[41] units_0.8-1        renv_0.17.2        utf8_1.2.3         KernSmooth_2.23-20
[45] proxy_0.4-27       doParallel_1.0.17  lwgeom_0.2-11      crayon_1.5.2

Reproducible Example

library(sf)
library(rayshader)
library(dplyr)
library(stars)
library(raster)

# coerce array to stars object for subsetting and reprojecting vector data
bay2 <- raster(montereybay) 
bay2 <- setExtent(bay2, attr(montereybay, "extent"))
crs(bay2) <- attr(montereybay, "crs")
montereybay2 <- st_as_stars(bay2)
counties2 <- st_transform(monterey_counties_sf, st_crs(montereybay2))

# to plot one or more permutations of subsets
plot_samples <- function(samp_list) {
  for (i in seq_along(samp_list)) {

    counties_samp <- samp_list[[i]]
    cat("Run ", i, "\n")
    dput(counties_samp)

    counties_sub <- filter(counties2, NAME %in% counties_samp)
    bay_sub <- montereybay2[counties_sub]
    # Stars object is an array
    bay_mat <- bay_sub[[1]]

    bay_mat |>
      sphere_shade(zscale = 50) |>
      plot_3d(bay_mat, zscale = 50)
  }
}

# generate permutations of subsets
rand_samp <- function(n, size = 6) {
  lapply(1:n, sample, x = as.character(monterey_counties_sf$NAME), size = 6)
} 

# Confirmed bad run
samp_list1 <- list(
  c("Salinas", "Soledad", "Watsonville", "Half Moon Bay", "Llagas-Uvas", "Lexington Hills")
)
# Confirmed bad run
samp_list2 <- list(
  c("Soledad", "Pajaro", "Half Moon Bay", "Watsonville", "San Jose", "Llagas-Uvas")
)

# This works (as do most)
set.seed(42)
plot_samples(rand_samp(1))

# But this causes my r session to crash without a message
plot_samples(samp_list1)

# The same thing happens with this subset as well
# plot_samples(samp_list2)
dmurdoch commented 1 year ago

When I run your code under lldb, I get a lot of warnings that deprecated functions are being called, then it crashes, with this message when run in lldb:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x130f52338)
    frame #0: 0x0000000116248044 rayshader.so`make_baselines_cpp(heightmap=0x00007ff7bfef9378, na_matrix=0x00007ff7bfef9338, waterdepth=0) at make_base_cpp.cpp:589:12 [opt]
Target 0: (R) stopped.

(The deprecation warnings don't need the debugger. If you weren't seeing them, you're not using an up to date version of rgl.) The crash appears to be happening in a rayshader function. So I see no evidence of rgl problems.

fisher-j commented 1 year ago

Thank you for the quick response and error report. I realize that I should have specified: devtools::install_github("tylermorganwall/rayshader")

Just to clarify, did the first example:

set.seed(42)
plot_samples(rand_samp(1))

Render without crashing?

dmurdoch commented 1 year ago

Yes, it was fine, with the crash in the second one.

fisher-j commented 1 year ago

Ok. Thank you for looking into it.

On Sat, May 13, 2023, 4:22 PM dmurdoch @.***> wrote:

Yes, it was fine, with the crash in the second one.

— Reply to this email directly, view it on GitHub https://github.com/dmurdoch/rgl/issues/367#issuecomment-1546767358, or unsubscribe https://github.com/notifications/unsubscribe-auth/APBWHQOBCL6RX4WWKBPYUBTXGAJUNANCNFSM6AAAAAAYAVIHWA . You are receiving this because you authored the thread.Message ID: @.***>