Skip to contents

You have a continuous value at every cortical vertex–for example a t, z, thickness, or connectivity map–and need a static figure that remains scientifically interpretable after thresholding. The important distinction is that the value is a field on a triangle mesh, not a parcel label. A polished figure should therefore interpolate the scalar within each triangle, test visibility with a real depth buffer, and add anatomy without drawing atlas or occlusion lines over the result.

Two functions cover this task. surface_figure() renders both hemispheres from several views and returns one finished figure with a shared colour bar; write_surface_figure() writes it to PNG. Underneath it, render_surface_rgba() is the deterministic, headless single-panel primitive: it returns an RGBA raster plus diagnostic buffers and provenance, and write_surface_rgba() writes one panel to PNG. Both run identically in CI, on a cluster, and on a laptop, with no OpenGL and no browser.

What are the inputs?

You need a display geometry and one numeric value per vertex. For anatomical context, use sulcal depth or curvature from the same subject or template rather than curvature computed on the inflated display shape. Sulci render darker than gyri; the renderer detects whether a metric is larger in sulci (as FreeSurfer sulc and curv are) and orients it accordingly.

The package bundles the fsaverage5 template (10,242 vertices per hemisphere) with its sulcal depth, so this example runs offline.

inflated <- load_fsaverage("fsaverage5", "inflated")
white <- load_fsaverage("fsaverage5", "white")
sulc <- load_fsaverage_sulc("fsaverage5")

Here smooth clusters stand in for your measured statistic. Each is placed at a white-surface coordinate and falls off with distance measured over the inflated surface, which follows the cortical sheet instead of cutting across neighbouring sulcal banks. Widths differ between clusters and peaks differ between hemispheres, as real effects do. The renderer treats the result like any other vertex-wise numeric vector.

example_statistic <- function(hemi, side, peaks) {
  cluster <- function(centre, peak, width) {
    seed <- which.min(colSums((t(coords(white[[hemi]])) - centre)^2))
    xyz <- coords(inflated[[hemi]])
    peak * exp(-colSums((t(xyz) - xyz[seed, ])^2) / (2 * width^2))
  }
  # Inferior parietal, middle frontal, superior temporal and posterior
  # cingulate clusters
  cluster(c(45 * side, -60, 20), peaks[1], 12) +
    cluster(c(40 * side, 20, 30), peaks[2], 9) +
    cluster(c(55 * side, -20, 0), peaks[3], 8) +
    cluster(c(5 * side, -46, 22), peaks[4], 11)
}

stat_lh <- example_statistic("lh", -1, c(4, 3, -3.5, -3))
stat_rh <- example_statistic("rh", 1, c(3, 3.5, -2.5, -3.5))

The non-cortical medial wall should never carry statistical colour, so the figures below also pass a cortex mask, one logical value per vertex. For a subject, read it from FreeSurfer’s ?h.cortex.label. For the bundled template, load_fsaverage_cortex() returns the same domain from FreeSurfer’s Destrieux parcellation: every vertex except the medial wall.

cortex <- load_fsaverage_cortex("fsaverage5")

How do you render one thresholded view?

Choose the threshold and colour limits as scientific inputs. In this example, the colour scale stays fixed at [-4, 4], while an opacity ramp softens only the visual transition immediately above |z| = 1.5.

lh_lateral <- render_surface_rgba(
  inflated$lh,
  vertex_values = stat_lh,
  anatomy_metric = sulc$lh,
  cortex_mask = cortex$lh,
  camera = "lateral",
  threshold = 1.5,
  tail = "two_sided",
  limits = c(-4, 4),
  alpha_ramp = 0.25,
  antialias = 3
)
Left hemisphere, lateral view. Two-sided *z*; |*z*| &lt; 1.5 is not drawn; colour limits &plusmn;4, blue negative, red positive. The four-view figure below carries the key.

Left hemisphere, lateral view. Two-sided z; |z| < 1.5 is not drawn; colour limits ±4, blue negative, red positive. The four-view figure below carries the key.

The colour at each covered sample comes from the barycentrically interpolated scalar. Thresholding and palette mapping happen after that interpolation, so a threshold boundary may cross a triangle instead of jumping from one mesh edge to the next. The per-sample z-buffer independently decides which folded surface fragment is visible.

Surfaces are lit with interpolated normals, so cortical form reads without drawn outlines, and the overlay keeps its colour-bar colour to within a few percent at any viewing angle. Coarse meshes are subdivided for display (subdivide = "auto"); the subdivision keeps every original vertex value and never creates a suprathreshold region that linear interpolation would not.

How do you build the four canonical views?

surface_figure() renders every hemisphere-view combination with one shared threshold, colour limit, and palette, and adds the colour bar. The camera contract is explicit: camera_mode = "canonical" is strict orthographic output, while "presentation" adds a small declared obliquity.

figure <- surface_figure(
  lh = inflated$lh,
  rh = inflated$rh,
  values = list(lh = stat_lh, rh = stat_rh),
  anatomy = sulc,
  cortex_mask = cortex,
  views = c("lateral", "medial"),
  threshold = 1.5,
  tail = "two_sided",
  limits = c(-4, 4),
  alpha_ramp = 0.25,
  legend_title = "z",
  panel_width = 720,
  panel_height = 450,
  antialias = 3
)
fsaverage5 inflated surfaces, lateral (top) and medial (bottom) views. Two-sided *z* with |*z*| &lt; 1.5 not drawn; colour fades in over the next 0.25. The grey medial wall carries no colour, so the posterior cingulate cluster stops at the cortex mask.

fsaverage5 inflated surfaces, lateral (top) and medial (bottom) views. Two-sided z with |z| < 1.5 not drawn; colour fades in over the next 0.25. The grey medial wall carries no colour, so the posterior cingulate cluster stops at the cortex mask.

In an interactive session, plot(figure) draws the same figure on the current graphics device.

The lateral and medial views differ because each panel rasterizes the visible triangles from its own camera. Internal sulcal occlusion edges are not drawn as outlines; outer_contour = TRUE marks only cortex touching background that is connected to the image exterior.

Where do the medial wall and cortex mask enter?

A real cortex mask is a separate domain object, not a consequence of parcel value zero. Pass one logical value per vertex. Triangles touching a masked vertex never receive overlay colour. medial_wall then controls whether the excluded domain is quietly shaded, masked out, or outlined.

The panels below repeat the left medial view with each policy, using the cortex mask from above. The posterior cingulate cluster runs up to the wall; triangles that touch the wall receive no overlay colour under any policy, so the cluster stops at the cortical boundary.

policies <- c("shade", "mask", "outline")
medial_panels <- lapply(policies, function(policy) {
  render_surface_rgba(
    inflated$lh, stat_lh, anatomy_metric = sulc$lh,
    camera = "medial", threshold = 1.5, limits = c(-4, 4),
    cortex_mask = cortex$lh, medial_wall = policy,
    width = 480, height = 300, antialias = 3
  )
})
surface_panels(medial_panels, sprintf('medial_wall = "%s"', policies),
               legend_title = "z", label_family = "mono")
Left medial view of the same z map under the three medial_wall policies.

Left medial view of the same z map under the three medial_wall policies.

Atlas-aware callers such as neuroatlas::plot_brain() can resolve the mask and its provenance from an atlas annotation. At this low level, neurosurf requires the caller to supply the domain explicitly rather than guessing that an atlas label of zero always means medial wall.

How should a volume become a continuous surface field?

For a continuous image, use explicit trilinear sampling through the cortical ribbon. Interpolation, aggregation across depth, and tangential smoothing are separate choices. The following call samples five depths, averages them, and applies no surface smoothing. It needs your own subject’s white_surface, pial_surface and statistic_volume, so it is not run here:

mapped <- vol_to_surf(
  white_surface,
  pial_surface,
  statistic_volume,
  fun = "avg",
  sampling = "thickness",
  interpolation = "linear",
  depth = seq(0.1, 0.9, length.out = 5),
  aggregate = "mean",
  surface_smooth_fwhm = 0
)

Use interpolation = "nearest" for discrete nearest-voxel sampling. The historical Gaussian KNN behaviour remains available as interpolation = "legacy". Categorical aggregate = "mode" is intentionally incompatible with linear interpolation.

Which function draws what you need?

Start from the output you want:

You want Use
A finished static figure: views by hemispheres, one colour bar surface_figure(), then write_surface_figure()
One panel, or a custom layout you compose yourself render_surface_rgba(), then write_surface_rgba()
To rotate and inspect a map on your desktop during analysis view_surface()
An interactive figure in an HTML report or Shiny app surfwidget(); see vignette("interactive-surfaces")

The first two run headlessly and deterministically; the last two are interactive sessions.

Two companion packages build on the same renderer, so their pixels agree with the figures here: neuroatlas::plot_brain() adds atlas-derived masks, labels, and orientation marks, and neuromosaic::surf_montage() starts from a statistic volume rather than vertex values. Atlas outlines belong in figures about the atlas; the default continuous-statistic view omits them.

Next steps