# Simulating Crook Deformations

## Overview

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

    library(crookR)
    data("example_stem")

# Example: XYZ point cloud for a single stem

    library(crookR)
    data("example_stem")
    str(example_stem)
    #> Classes 'data.table' and 'data.frame':   708145 obs. of  3 variables:
    #>  $ X: num  0.28 0.28 0.28 0.28 0.28 ...
    #>  $ Y: num  0.28 0.28 0.28 0.28 0.28 ...
    #>  $ Z: num  0.01 0.01 0.02 0.02 0.02 ...
    #>  - attr(*, ".internal.selfref")=<externalptr>
    head(example_stem)
    #>        X     Y     Z
    #>    <num> <num> <num>
    #> 1:  0.28  0.28  0.01
    #> 2:  0.28  0.28  0.01
    #> 3:  0.28  0.28  0.02
    #> 4:  0.28  0.28  0.02
    #> 5:  0.28  0.28  0.02
    #> 6:  0.26  0.26  0.00

# 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.

    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.

    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)