---
title: "Simulating Crook Deformations"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Simulating Crook Deformations}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  dpi = 120,
  fig.align = "center"
)
set.seed(1)
require(lidR)
```

## Overview

This vignette shows how to apply crook deformations to LiDAR-derived stem point clouds using {crookR} (deformation-only).

```{r }
library(crookR)
data("example_stem")
```

# Example: XYZ point cloud for a single stem

```{r }

library(crookR)
data("example_stem")
str(example_stem)
head(example_stem)

```

# Single-direction deformation (krok_type = "1dir")

A smooth lateral deviation of amplitude krok_deviation is applied over a vertical range of length krok_length, starting at krok_start.

```{r }
stem_1dir <- crook_deform(
  x = example_stem,
  krok_length     = 0.5,
  krok_start      = 4.0,
  krok_type       = "1dir", 
  krok_deviation  = 0.08,   # try 0.05..0.12 for milder/stronger bends
  az              = 0,      # bend along +X
  spar            = 0.8
)

# Quick plan-view check

op <- par(mfrow = c(1,2), mar = c(3,3,2,1))
plot(example_stem$X, example_stem$Z, pch=16, cex=0.3, xlab="X (m)", ylab="Z (m)", main="Original")
plot(stem_1dir$X,     stem_1dir$Z,     pch=16, cex=0.3, xlab="X (m)", ylab="Z (m)", main="Crooked 1dir (az = 0°)")
par(op)
```

# Double-direction deformation (krok_type = "2dir") with inflection

The deviation changes direction at a fraction inflektion_X of the length (0 < inflektion_X < 1). inflektion_ext scales the amplitude after the inflection.

```{r }
stem_2dir <- crook_deform( 
  x = example_stem,
  krok_length = 0.5,
  krok_start = 4.0,
  krok_type = "2dir",
  krok_deviation = 0.1,
  inflektion_X = 1/3,    # inflection at ~33% of the deformed length
  inflektion_ext = 1/2,    # second leg amplitude relative to first
  az = 0,     
  spar = 0.8
  )

op <- par(mfrow = c(1,2), mar = c(3,3,2,1))
plot(example_stem$X, example_stem$Z, pch=16, cex=0.3, xlab="X (m)", ylab="Z (m)", main="Original")
plot(stem_2dir$X,     stem_2dir$Z,     pch=16, cex=0.3, xlab="X (m)", ylab="Z (m)", main="Crooked 2dir (ip = 0.33)")
par(op)
```
