R/remove_cyl.R

Defines functions remove_cyl

Documented in remove_cyl

#' \lifecycle{experimental}
#'
#' Remove the specimen container from an oriented 3D mesh
#'
#' Boolean mesh operation to keep all points inside or outside the clipping surface
#'
#' @param mesh The "mesh3d" object to trim
#' @param radius Radius of clipping surface in mm
#' @param clip_fun A function corresponding to the bounding surface, defaults to an open cylinder
#' @param keep "inside" for vertices contained by the surface; "outside" for those falling beyond it
#' @param ... allows user to set the `attribute` argument if needed (see \link[rgl]{clipMesh3d})
#'
#' @return A "mesh3d" object with undesired vertices removed
#' @export
#'
#' @example inst/examples/remove_cyl_example.r
remove_cyl <- function(mesh, radius = 70, clip_fun = "default", keep = "inside", ...) {

  boundary_function <- function(m){m[,1]^2 + m[,2]^2}

  if (clip_fun == "default"){
   clipping_function <- function(m){m[,1]^2 + m[,2]^2} } else {
   clipping_function <- clip_fun}

  if (keep == "inside") {
    clipped_mesh <- rgl::clipMesh3d(mesh = mesh, fn = clipping_function, bound = radius^2, greater = FALSE) }
    else if (keep == "outside") {
      clipped_mesh <- rgl::clipMesh3d(mesh = mesh, fn = clipping_function, bound = radius^2, greater = TRUE)
    } else{
      stop("\nYou have to keep either the inside or the outside of the mesh! Pick one and check your spelling!")
    }

  return(clipped_mesh)
}
evanmascitti/soilmesh documentation built on Sept. 30, 2021, 7:57 a.m.