---
title: "Lidar simulation using TROLL"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Lidar simulation using TROLL}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.height = 6,
  fig.width = 6
)
```

This vignette gives a quick example of a simple workflow to produce discrete lidar point clouds from TROLL simulation and corresponding height canopy models.

# Setup

To use lidar modules in `rcontroll` you need the [`lidR` package](https://github.com/r-lidar/lidR).
Because `lidR` is not on CRAN anymore this is a suggested dependency and this needs to be installed independently first.
If not, the lidar outputs from `rcontroll` are not accessible and this vignette will be empty.

```{r}
if(!requireNamespace("lidR", quietly = TRUE))
  knitr::opts_chunk$set(eval = FALSE)
```


We will use `rcontroll` with a previous simulation (`TROLLv3_output`) as a starting point for new simulations. 

```{r}
suppressMessages(library(rcontroll))
data("TROLLv3_output")
```

# Lidar simulation

The `troll` function include a `lidar` input to specify the details of the lidar simulation that can be easily generated using the `generate_lidar` function.
Parameters for the lidar simulation includes mean and standard deviation of pulse density, the laser attenuation factor, the transmittance of laser after hit, and the iteration for point cloud generation (see `?generate_lidar`).

```{r }
sim <- troll(
  name = "test",
  global = update_parameters(TROLLv3_output, nbiter = 12 * 1),
  species = TROLLv3_output@inputs$species,
  climate = TROLLv3_output@inputs$climate,
  daily = TROLLv3_output@inputs$daily,
  forest = get_forest(TROLLv3_output),
  lidar = generate_lidar(mean_beam_pc = 10, iter_pointcloud_generation = 11),
  verbose = FALSE
)
```

Printing the output `rcontroll` inform the lidar simulation status:

```{r}
sim
```

The point cloud can be accessed in a list in the attribute `@las`:

```{r}
sim@las
```

```{r, eval=FALSE}
lidR::plot(sim@las[[1]])
```


> `lidR` include a `plot` method for las objects with a nice 3D interactive plot.

`rcontroll` includes a `get_chm` method to extract the canopy height model from the point cloud as a raster:

```{r}
get_chm(sim)
```

`rcontroll` includes in the `autoplot` method an automatic plot of the canopy height model when choosing `what = "lidar"` (if the lidar simulations as been activated obviously):

```{r}
rcontroll::autoplot(sim, what = "lidar")
```

> Prefer to use `rcontroll::autoplot` instead of `autoplot` alone to avoid namespace conflict with `ggplot2`.

# Lidar stack

Similarly, the `stack` function include a `lidar` input to specify the details of the lidar simulation that can be easily generated using the `generate_lidar` function.
Here we test the difference between a simulation with mean pulse density of 10 (`10pc`) and 20 (`20pc`):

```{r}
sim_stack <- stack(
  name = "teststack",
  simulations = c("10pc", "20pc"),
  global = update_parameters(TROLLv3_output, nbiter = 12 * 1),
  species = TROLLv3_output@inputs$species,
  climate = TROLLv3_output@inputs$climate,
  daily = TROLLv3_output@inputs$daily,
  forest = get_forest(TROLLv3_output),
  lidar = list(
    "10pc" = generate_lidar(
      mean_beam_pc = 10,
      iter_pointcloud_generation = 11
    ),
    "20pc" = generate_lidar(
      mean_beam_pc = 10,
      iter_pointcloud_generation = 11
    )
  ) %>% dplyr::bind_rows(.id = "simulation"),
  verbose = FALSE,
  cores = 2,
  thin = c(1, 5, 10)
)
```

Printing the output `rcontroll` inform the lidar simulation status:

```{r}
sim_stack
```

The point clouds can be accessed in a list in the attribute `@las`:

```{r}
sim_stack@las
```

```{r, eval=FALSE}
lidR::plot(sim_stack@las[[2]])
```

`rcontroll` includes a `get_chm` method to extract the canopy height model from the point clouds as a raster:

```{r}
get_chm(sim_stack)
```

`rcontroll` includes in the `autoplot` method an automatic plot of the canopy height model when choosing `what = "lidar"` (if the lidar simulations as been activated obviously):

```{r}
rcontroll::autoplot(sim_stack, what = "lidar")
```
