## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width = 6,
  fig.height = 4
)
library(crownmetrics)

## ----vol-ellipsoid------------------------------------------------------------
crown_volume_ellipsoid(crown_width = 3.5, crown_length = 5.0)

## ----vol-cone-----------------------------------------------------------------
crown_volume_cone(crown_width = 3.5, crown_length = 5.0)

## ----vol-cylinder-------------------------------------------------------------
crown_volume_cylinder(crown_width = 3.5, crown_length = 5.0)

## ----vol-paraboloid-----------------------------------------------------------
crown_volume_paraboloid(crown_width = 3.5, crown_length = 5.0)

## ----vol-fan------------------------------------------------------------------
crown_volume_fan(crown_length = 5.0, dbh = 22.0)

## ----volume-compare-----------------------------------------------------------
cw <- 3.5   # crown width (m)
cl <- 5.0   # crown length (m)

volumes <- c(
  ellipsoid  = crown_volume_ellipsoid(cw, cl),
  cone       = crown_volume_cone(cw, cl),
  cylinder   = crown_volume_cylinder(cw, cl),
  paraboloid = crown_volume_paraboloid(cw, cl)
)

round(volumes, 2)

## ----volume-bar, echo = FALSE-------------------------------------------------
barplot(
  volumes,
  col    = c("#2d6a4f", "#52b788", "#95d5b2", "#d8f3dc"),
  border = NA,
  ylab   = expression("Crown volume (m"^3*")"),
  main   = "Crown volume by geometric shape",
  las    = 1
)

## ----volume-dispatcher--------------------------------------------------------
crown_volume(crown_width = 3.5, crown_length = 5.0, shape = "ellipsoid")
crown_volume(crown_width = 3.5, crown_length = 5.0, shape = "cone")
crown_volume(crown_length = 5.0, dbh = 22.0, shape = "fan")

## ----area-profile-------------------------------------------------------------
crown_area(crown_width = 3.5, crown_length = 5.0, shape = "ellipse")
crown_area(crown_width = 3.5, crown_length = 5.0, shape = "triangle")
crown_area(crown_width = 3.5, crown_length = 5.0, shape = "rectangle")
crown_area(crown_length = 5.0, shape = "fan")

## ----area-projection----------------------------------------------------------
crown_projection_area(crown_width = 3.5)

## ----idx-pc-------------------------------------------------------------------
crown_ratio(crown_length = 5.0, total_height = 18.0)
crown_ratio(crown_length = 5.0, total_height = 18.0, as_percentage = TRUE)

## ----idx-fc-------------------------------------------------------------------
crown_form(crown_width = 3.5, crown_length = 5.0)

## ----idx-ge-------------------------------------------------------------------
slenderness(total_height = 18.0, dbh = 22.0)

## ----idx-is-------------------------------------------------------------------
salience_index(crown_width = 3.5, dbh = 22.0)

## ----idx-ia-------------------------------------------------------------------
scope_index(crown_width = 3.5, total_height = 18.0)

## ----idx-iev------------------------------------------------------------------
vital_space_index(crown_width = 3.5, dbh = 22.0)

## ----morph-single-------------------------------------------------------------
crown_morphometrics(
  crown_width  = 3.5,
  crown_length = 5.0,
  total_height = 18.0,
  dbh          = 22.0
)

## ----batch--------------------------------------------------------------------
inventory <- data.frame(
  tree_id      = 1:6,
  species      = c("Araucaria", "Pinus",  "Eucalyptus",
                   "Araucaria", "Pinus",  "Eucalyptus"),
  crown_width  = c(3.0,  2.0,  4.0,  3.5,  2.5,  4.5),
  crown_length = c(5.0,  6.0,  3.5,  4.5,  5.5,  3.0),
  total_height = c(18.0, 20.0, 12.0, 16.0, 19.0, 11.0),
  dbh          = c(22.0, 18.0, 25.0, 20.0, 17.0, 28.0),
  shape_code   = c(0L,   1L,   0L,   0L,   1L,   3L)
)

result <- crown_metrics(inventory, col_shape = "shape_code")

result[, c("tree_id", "species", "crown_volume_m3",
           "crown_projection_area_m2", "slenderness", "crown_ratio")]

