## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval=FALSE---------------------------------------------------------------
# library(scimesh)
# 
# sphere <- generate_sphere(c(0, 0, 0), radius = 1.2,
#                           segments = 32, color = c(0.9, 0.3, 0.2, 1.0))
# cam <- camera_auto(sphere, direction = c(1.2, 0.8, 1))
# 
# # Flat-shaded sphere
# img <- render_mesh(sphere$vertices, sphere$triangles,
#     colors = sphere$colors, camera = cam,
#     options = render_options(
#         lights = list(
#             list(position = c(0.5, 1.0, 0.8), intensity = 1.5),
#             list(position = c(-0.5, 0.2, 0.6), intensity = 0.5))))
# 
# tmp_file <- tempfile(fileext = ".png")
# write_png(img, tmp_file)
# printf("Rendered sphere written to: %s\n", tmp_file)

## ----eval=FALSE---------------------------------------------------------------
# library(scimesh)
# 
# white <- freesurferformats::read.fs.surface("sub-01/surf/lh.white")
# pial  <- freesurferformats::read.fs.surface("sub-01/surf/lh.pial")
# 
# nv <- nrow(white$vertices)
# 
# white_mesh <- list(
#     vertices  = white$vertices,
#     triangles = white$faces,
#     colors    = matrix(c(0.7, 0.7, 0.7, 1.0), nv, 4, byrow = TRUE))
# 
# pial_mesh <- list(
#     vertices  = pial$vertices,
#     triangles = pial$faces,
#     colors    = matrix(c(0.9, 0.3, 0.2, 0.35), nv, 4, byrow = TRUE))
# 
# cam <- camera_auto(pial_mesh, direction = c(-1, 0, 0.2))
# img <- render_scene(list(white_mesh, pial_mesh), cam,
#     render_options(width = 1200, height = 900,
#         backface_culling = FALSE,
#         specular_color = c(0.4, 0.4, 0.4, 1),
#         shininess = 64))
# temp_file <- tempfile(fileext = ".png")
# write_png(img, temp_file)
# printf("Transparent render written to: %s\n", temp_file)

## ----eval=FALSE---------------------------------------------------------------
# surface <- freesurferformats::read.fs.surface("sub-01/surf/lh.white")
# sulc    <- freesurferformats::read.fs.morph("sub-01/surf/lh.sulc")
# cortex  <- freesurferformats::read.fs.label("sub-01/label/lh.cortex.label")
# 
# nv <- nrow(surface$vertices)
# medial_wall <- !(seq_len(nv) %in% cortex)
# sulc[medial_wall] <- NA                        # no data on the medial wall
# 
# colors <- apply_colormap(sulc, colormap = viridis_colormap(256L),
#     nan_color = c(1, 1, 1, 1), winsor_percentiles = c(2, 98))
# if (ncol(colors) == 3) colors <- cbind(colors, 1)
# colors[medial_wall, 4] <- 0.5                  # 50 % transparent
# 
# brain <- list(vertices = surface$vertices, triangles = surface$faces,
#               colors = colors)
# img <- render_scene(list(brain), camera_auto(brain, direction = c(1, 0, 0)),
#     render_options(background_color = c(1, 1, 1, 1)))

## ----eval=FALSE---------------------------------------------------------------
# quads <- generate_plane(color = c(0.8, 0.2, 0.2, 1))
# quads$colors[, 4] <- rep(c(0, 1), each = nrow(quads$colors) / 2)

## ----eval=FALSE---------------------------------------------------------------
# surf <- generate_sphere(c(0, 0, 0), radius = 1)
# cam  <- camera_auto(list(surf), direction = c(0, 0, 1))
# opts <- render_options(width = 500, height = 400,
#                        background_color = c(1, 1, 1, 1))
# 
# sc <- scene(list(surf), camera = cam, options = opts,
#             texts = list(
#                 # world space: anchored above the sphere, follows the camera
#                 text_layer(matrix(c(0, 1.3, 0), ncol = 3), "superior",
#                            size = 20, adj = c(0.5, 0)),
#                 # a direction label that must stay readable: no depth test
#                 text_layer(matrix(c(1.4, 0, 0), ncol = 3), "anterior",
#                            size = 18, adj = c(0, 0.5),
#                            colors = c(0.75, 0.15, 0.15, 1), depth_test = FALSE),
#                 # screen space: a panel tag in the top-left corner
#                 text_layer(c(12, 10), "A", space = "screen", adj = c(0, 1),
#                            size = 28, halo_color = c(1, 1, 1, 0.9)),
#                 # screen space: a vertical axis label (rotation in degrees,
#                 # counter-clockwise, about the anchor)
#                 text_layer(c(16, 210), "sulcal depth", space = "screen",
#                            adj = c(0, 0.5), size = 16, rotation = 90)))
# img <- render_scene(sc)

## ----eval=FALSE---------------------------------------------------------------
# surface <- freesurferformats::read.fs.surface("sub-01/surf/lh.white")
# sulc    <- freesurferformats::read.fs.morph("sub-01/surf/lh.sulc")
# colors  <- apply_colormap(sulc, colormap = viridis_colormap(256L),
#                           winsor_percentiles = c(2, 98))
# if (ncol(colors) == 3) colors <- cbind(colors, 1)
# 
# brain <- list(vertices = surface$vertices, triangles = surface$faces,
#               colors = colors)
# cam <- camera_auto(brain, direction = c(1, 0, 0))   # lateral view
# 
# # The free surface is roughly 100 mm wide, so the labels sit at x = +-55 mm.
# sc <- scene(list(brain), camera = cam,
#             options = render_options(width = 600, height = 500,
#                                      background_color = c(1, 1, 1, 1)),
#             texts = list(
#                 text_layer(matrix(c(-55, 0, 0), ncol = 3), "posterior",
#                            size = 22, adj = c(1, 0.5), depth_test = FALSE),
#                 text_layer(matrix(c(55, 0, 0), ncol = 3), "anterior",
#                            size = 22, adj = c(0, 0.5), depth_test = FALSE)))
# img <- render_scene(sc)

## ----eval=FALSE---------------------------------------------------------------
# # Read a Wavefront OBJ file (with optional UVs and normals)
# mesh <- read_obj("model.obj")
# 
# # Read a Stanford PLY file (with optional vertex colors)
# mesh <- read_ply("model.ply")
# 
# # Read an STL file (binary or ASCII)
# mesh <- read_stl("model.stl")
# 
# # Write a mesh to STL
# tmp_file1 <- tempfile(fileext = ".stl")
# tmp_file2 <- tempfile(fileext = ".stl")
# 
# write_stl(mesh, tmp_file1)               # binary (default)
# write_stl(mesh, tmp_file2, format = "ascii")

