hypertidy/anglr

rayrender basics

mdsumner opened this issue · 1 comments

I have no idea about using rayrender, so this is just notes as I learn what it's about.

With

library(anglr)
## so we are in 0,1
plot3d(as.mesh3d(raster::raster(scales::rescale(volcano))))

image

we can do

rgl::writeOBJ("file.obj")
obj_model("file.obj", material = metal(color = "gold")) %>% render_scene(lookfrom = c(10, 10, 0))

image

woo

In rayrender a poly_list is created, and then it is converted into triangles, and then into a flat vector of properties, x, y, and normals.

At the end of the loop (before scene_list starts) it has been centred (if set) and flipped (if flip_horizontal and flip_vertical are both FALSE, it's still been flipped horizontally (dunno)).

us_states = spData::us_states
p = us_states[1:3, ] #us_states$NAME == "Texas",]
class(p$geometry) = c("list","sfc")
library(rayrender)
rd <- extruded_polygon(p, center = F,
                       material=diffuse(color="#ff2222",sigma=90), 
                        flip_horizontal = F, flip_vertical = F)



gibble::gibble(p)
# A tibble: 3 x 5
   nrow  ncol type         subobject object
  <int> <int> <chr>            <int>  <int>
1    51     2 MULTIPOLYGON         1      1
2    58     2 MULTIPOLYGON         1      2
3    28     2 MULTIPOLYGON         1      3

The first 3 objects of us_states then look like this:

Browse[1]> lapply(poly_list, nrow)
[[1]]
[1] 51

[[2]]
[1] 58

[[3]]
[1] 28

lapply(poly_list, head)

[1]]
            x        y hole
[1,] 88.20006 34.99563    0
[2,] 88.20296 35.00803    0
[3,] 87.42861 35.00279    0
[4,] 86.86215 34.99196    0
[5,] 85.60516 34.98468    0
[6,] 85.47047 34.32824    0

[[2]]
            x        y hole
[1,] 114.7196 32.71876    0
[2,] 114.5391 32.75695    0
[3,] 114.4690 32.84515    0
[4,] 114.5061 33.01701    0
[5,] 114.6708 33.03798    0
[6,] 114.7079 33.09743    0

[[3]]
            x        y hole
[1,] 109.0501 41.00066    0
[2,] 108.2506 41.00011    0
[3,] 107.6256 41.00212    0
[4,] 106.2176 40.99773    0
[5,] 105.7304 40.99689    0
[6,] 104.8553 40.99805    0

and above that code has

  • converted sf to Spatial
  • converted Spatial to raw geometry with raster::geom
  • triangulated

and by this stage vertex_list is the indices into poly_list

   poly_list[[1]] = as.matrix(data.frame(x=x,y=y,hole=holes))
    vertex_list[[1]] = decido::earcut(poly_list[[1]][,1:2],holes = holes)

It doesn't look like polygons are extruded, it looks like the triangles have walls put on them (check this).

I expect n triangles + 2 * n-segments per feature, or n triangles + n*3-edges * 2 for the walls, but it's not that exactly - not sure yet.

dim(rd)
[1] 524  34
silicate::TRI0(p)
class       : TRI0
type        : Primitive
vertices    : 133 (2-space)
primitives  : 128 (2-space)
crs         : EPSG:4269
silicate::SC(p)

128 + 2 * 128
[1] 384
 128 + 3 * 128
[1] 512

After that scene_list starts and the final df with the nested property lists are a triangle per row in

  scenefull = do.call(rbind,scenelist)


I don't know about speed, but at least the handling could be simplified and not care if input is sp or sf.