@thi.ng/lsys
Version:
Functional, extensible L-System architecture w/ support for probabilistic rules
150 lines • 4.09 kB
TypeScript
import type { Fn, Fn2, IObjectOf } from "@thi.ng/api";
import type { IRandom } from "@thi.ng/random";
import type { Vec } from "@thi.ng/vectors";
/**
* Symbol used as fallback for unmatched symbols in {@link RuleImplementations}.
*/
export declare const DEFAULT: unique symbol;
export type LSysSymbol = string | number;
export type ProductionResult = ArrayLike<LSysSymbol> & Iterable<LSysSymbol>;
export type ProductionRules = IObjectOf<ProductionResult | Fn<LSysSymbol, ProductionResult>>;
/**
* Object with L-System symbols as keys and their implementations/operators as
* values.
*
* @remarks
* The {@link DEFAULT} symbol can be used as fallback impl, used for any
* unmatched symbols.
*/
export type RuleImplementations<T extends object> = IObjectOf<Fn2<T, LSysSymbol, void>>;
export interface Turtle2D {
/**
* Current position.
*
* @defaultValue `[0,0]`
*/
pos: Vec;
/**
* Current direction (in radians)
*/
theta: number;
/**
* Rotation angle for "+" / "-" symbols
*/
delta: number;
/**
* Max. random direction change when processing "/" symbol. Normalized
* percentage of `delta`.
*
* @defaultValue 0.25 (25%)
*/
jitter: number;
/**
* Step distance.
*
* @defaultValue 1
*/
step: number;
/**
* Probability to keep current branch alive when processing "k" symbol.
*
* @defaultValue 0.99
*/
aliveProb: number;
/**
* Decay factor for `delta`. Should be in `(0,1)` interval.
*
* @defaultValue 0.9
*/
decayDelta: number;
/**
* Decay factor for `step`. Should be in `(0,1)` interval.
*
* @defaultValue 0.9
*/
decayStep: number;
/**
* Decay factor for `aliveProp`.
*
* @defaultValue 0.95
*/
decayAlive: number;
/**
* PRNG to use for probability checks.
*
* @defaultValue [`SYSTEM`](https://docs.thi.ng/umbrella/random/variables/SYSTEM.html)
*/
rnd: IRandom;
/**
* Alive flag.
*
* @defaultValue true
*/
alive: boolean;
/**
* Current recorded path
*/
curr: Vec[];
/**
* Accumulator of all recorded paths
*/
paths: Vec[][];
/**
* Branch stack
*/
stack: Turtle2D[];
}
export declare const TURTLE_IMPL_2D: RuleImplementations<Turtle2D>;
export declare const turtle2d: (state?: Partial<Turtle2D>) => Turtle2D;
/**
* Lazily transforms `src` iterable of symbols by rewriting each using provided
* rewrite `rules`.
*
* @remarks
* Any symbol without a matching rewrite rule will remain as is. A rewrite rule
* can replace a source symbol with any number of new symbols. If the rule is
* given as function, the function will be called with the original symbol and
* the return values used as replacements.
*
* Used by {@link expand} to process/rewrite a single iteration.
*
* @example
* ```ts tangle:../export/rewrite.ts
* import { rewrite } from "@thi.ng/lsys";
*
* const RULES = {
* a: ["b"],
* b: ["a", "c"],
* c: () => [Math.random() < 0.8 ? "a" : "x"]
* };
*
* console.log([...rewrite(RULES, "a/b/c")]);
* // [ "b", "/", "a", "c", "/", "x" ]
* ```
*
* @param rules
* @param syms
*/
export declare const rewrite: (rules: ProductionRules, syms: Iterable<LSysSymbol>) => IterableIterator<LSysSymbol>;
/**
* Expands `src` iterable of symbols by iteratively rewriting each symbol using
* provided rewrite `rules` over `limit` iterations.
*
* @param rules
* @param initial
* @param limit
*/
export declare const expand: (rules: ProductionRules, initial: LSysSymbol, limit?: number) => any;
/**
* Sequentially processes iterable of symbols using provided
* {@link RuleImplementations} and suitable execution context object.
*
* @remarks
* See {@link turtle2d} and {@link TURTLE_IMPL_2D}.
*
* @param ctx
* @param impls
* @param syms
*/
export declare const interpret: <T extends object>(ctx: T, impls: RuleImplementations<T>, syms: Iterable<LSysSymbol>) => T;
//# sourceMappingURL=index.d.ts.map