R/surface_geometry.R

Defines functions vc_plot_surface vc_cylindrical_surface vc_surface_sample .vc_surface_axis

Documented in vc_cylindrical_surface vc_plot_surface vc_surface_sample

.vc_surface_axis <- function(range,n,name) {
  range <- .vc_integral_interval(range)
  .vc_integer(n,name,2)
  if (range[1]==range[2]) return(range[1])
  nodes <- seq(range[1],range[2],length.out=n)
  if (any(!is.finite(nodes)) || anyDuplicated(nodes)) stop("Surface parameter grid is not representable.")
  nodes
}

#' Sample a rectangular parameter patch in space
#' @inheritParams vc_surface_integral
#' @param u_range,v_range Finite endpoint pairs in traversal order.
#' @param n_u,n_v Number of nodes in each parameter, at least two. Equal
#'   endpoints produce a single node along that axis.
#' @return List with type surface3d, parameter nodes u and v, and grid
#'   matrices x, y, z with v in rows and u in columns.
#' @examples
#' vc_surface_sample(function(p)c(p[1],p[2],prod(p)),c(0,1),c(0,1),n_u=3,n_v=3)
#' @export
vc_surface_sample <- function(surface,u_range,v_range,n_u=31L,n_v=31L) {
  if (!is.function(surface)) stop("'surface' must be a function.")
  u <- .vc_surface_axis(u_range,n_u,"n_u")
  v <- .vc_surface_axis(v_range,n_v,"n_v")
  if (as.double(length(u))*length(v)>.Machine$integer.max) stop("Surface grid too large.")
  uv <- expand.grid(v=v,u=u,KEEP.OUT.ATTRS=FALSE)
  xyz <- vapply(seq_len(nrow(uv)),function(i)
    .vc_spatial_field(surface(c(uv$u[i],uv$v[i]))),numeric(3))
  grid <- lapply(seq_len(3),function(i)matrix(xyz[i,],nrow=length(v)))
  names(grid) <- c("x","y","z")
  list(type="surface3d",u=u,v=v,grid=grid)
}

#' Sample a cylindrical surface generated by parallel straight rulings
#' @inheritParams vc_curve_sample
#' @param s_range Finite ruling distances in traversal order.
#' @param direction Nonzero Cartesian vector, normalized before use.
#' @param n_t,n_s Number of nodes along the generating curve and rulings.
#' @return Surface sample with u as curve parameter and v as ruling distance,
#'   plus the stored generating curve and unit ruling direction.
#' @details The surface is curve(t) + s times the unit direction. The
#'   generating curve may be noncircular. Negative s is allowed.
#' @examples
#' vc_cylindrical_surface(function(t)c(cos(t),sin(t),0),c(0,pi),
#'   c(-1,1),c(0,0,2),n_t=5,n_s=3)
#' @export
vc_cylindrical_surface <- function(curve,interval,s_range,direction,n_t=31L,n_s=11L) {
  direction <- .vc_point(direction,"direction")
  if (length(direction)!=3L) stop("'direction' must have three coordinates.")
  unit <- .vc_unit(unname(direction))
  base <- vc_curve_sample(curve,interval,n_t)
  s <- .vc_surface_axis(s_range,n_s,"n_s")
  d <- base$samples
  if (as.double(length(s))*nrow(d)>.Machine$integer.max) stop("Surface grid too large.")
  axes <- c("x","y","z")
  grid <- lapply(seq_along(axes),function(i)outer(s*unit[i],d[[axes[i]]],"+"))
  if (any(!is.finite(unlist(grid,use.names=FALSE)))) stop("Surface coordinate is not finite.")
  names(grid) <- axes
  list(type="surface3d",u=d$t,v=s,grid=grid,curve=base,direction=unit)
}

#' Display a stored surface patch
#' @param result Result of vc_surface_sample() or vc_cylindrical_surface().
#' @param opacity Surface opacity from zero to one.
#' @param show_curve Show the stored generating curve when available.
#' @param title Optional character title.
#' @return Plotly widget displaying the stored geometry. A single row or
#'   column is drawn as a line, and a single node as a point.
#' @examples
#' if (requireNamespace("plotly",quietly=TRUE)) {
#'   vc_surface_sample(function(p)c(p[1],p[2],prod(p)),c(0,1),c(0,1)) |>
#'     vc_plot_surface()
#' }
#' @export
vc_plot_surface <- function(result,opacity=.6,show_curve=TRUE,title=NULL) {
  if (!requireNamespace("plotly",quietly=TRUE)) stop("Plotly is required only for graphics.")
  if (!is.list(result) || !identical(result$type,"surface3d")) stop("Invalid surface sample.")
  if (!.vc_scalar(opacity) || opacity<0 || opacity>1) stop("Invalid opacity.")
  .vc_flag(show_curve,"show_curve")
  if (!is.null(title) && (!is.character(title) || length(title)!=1L || is.na(title))) stop("Invalid title.")
  g <- result$grid
  p <- if (all(dim(g$x)>1L)) {
    plotly::plot_ly(x=g$x,y=g$y,z=g$z,type="surface",opacity=opacity,showscale=FALSE,name="Surface")
  } else {
    plotly::plot_ly(x=as.vector(g$x),y=as.vector(g$y),z=as.vector(g$z),
      type="scatter3d",mode=if(length(g$x)==1L)"markers" else "lines",name="Surface samples")
  }
  if (show_curve && !is.null(result$curve)) {
    d <- result$curve$samples
    p <- plotly::add_trace(p,x=d$x,y=d$y,z=d$z,type="scatter3d",
      mode=if(nrow(d)==1L)"markers" else "lines",name="Generating curve",inherit=FALSE)
  }
  plotly::layout(p,title=if(is.null(title))"Sampled surface" else title,
    scene=list(aspectmode="data",xaxis=list(title="x"),yaxis=list(title="y"),zaxis=list(title="z")))
}

Try the vectorialcalculus package in your browser

Any scripts or data that you put into this service are public.

vectorialcalculus documentation built on Nov. 17, 2026, 1:09 a.m.