webgpu-trees 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1547 -2
- package/assets/SOURCES.md +263 -0
- package/assets/bark/alder.albedo.avif +0 -0
- package/assets/bark/alder.normal.avif +0 -0
- package/assets/bark/aleppoPine.albedo.avif +0 -0
- package/assets/bark/aleppoPine.normal.avif +0 -0
- package/assets/bark/ash.albedo.avif +0 -0
- package/assets/bark/ash.normal.avif +0 -0
- package/assets/bark/aspen.albedo.avif +0 -0
- package/assets/bark/aspen.normal.avif +0 -0
- package/assets/bark/beech.albedo.avif +0 -0
- package/assets/bark/beech.normal.avif +0 -0
- package/assets/bark/birch.albedo.avif +0 -0
- package/assets/bark/birch.normal.avif +0 -0
- package/assets/bark/birchFoot.albedo.avif +0 -0
- package/assets/bark/birchFoot.normal.avif +0 -0
- package/assets/bark/corsicanPine.albedo.avif +0 -0
- package/assets/bark/corsicanPine.normal.avif +0 -0
- package/assets/bark/cypress.albedo.avif +0 -0
- package/assets/bark/cypress.normal.avif +0 -0
- package/assets/bark/douglas.albedo.avif +0 -0
- package/assets/bark/douglas.normal.avif +0 -0
- package/assets/bark/eucalyptus.albedo.avif +0 -0
- package/assets/bark/eucalyptus.normal.avif +0 -0
- package/assets/bark/eucalyptusUpper.albedo.avif +0 -0
- package/assets/bark/eucalyptusUpper.normal.avif +0 -0
- package/assets/bark/fibrous.albedo.avif +0 -0
- package/assets/bark/fibrous.normal.avif +0 -0
- package/assets/bark/hemlock.albedo.avif +0 -0
- package/assets/bark/hemlock.normal.avif +0 -0
- package/assets/bark/hornbeam.albedo.avif +0 -0
- package/assets/bark/hornbeam.normal.avif +0 -0
- package/assets/bark/larch.albedo.avif +0 -0
- package/assets/bark/larch.normal.avif +0 -0
- package/assets/bark/lime.albedo.avif +0 -0
- package/assets/bark/lime.normal.avif +0 -0
- package/assets/bark/liveOak.albedo.avif +0 -0
- package/assets/bark/liveOak.normal.avif +0 -0
- package/assets/bark/magnolia.albedo.avif +0 -0
- package/assets/bark/magnolia.normal.avif +0 -0
- package/assets/bark/maple.albedo.avif +0 -0
- package/assets/bark/maple.normal.avif +0 -0
- package/assets/bark/montereyPine.albedo.avif +0 -0
- package/assets/bark/montereyPine.normal.avif +0 -0
- package/assets/bark/norMaple.albedo.avif +0 -0
- package/assets/bark/norMaple.normal.avif +0 -0
- package/assets/bark/oak.albedo.avif +0 -0
- package/assets/bark/oak.normal.avif +0 -0
- package/assets/bark/palmLattice.albedo.avif +0 -0
- package/assets/bark/palmLattice.normal.avif +0 -0
- package/assets/bark/palmPineapple.albedo.avif +0 -0
- package/assets/bark/palmPineapple.normal.avif +0 -0
- package/assets/bark/palmRings.albedo.avif +0 -0
- package/assets/bark/palmRings.normal.avif +0 -0
- package/assets/bark/pine.albedo.avif +0 -0
- package/assets/bark/pine.normal.avif +0 -0
- package/assets/bark/pineFlakes.albedo.avif +0 -0
- package/assets/bark/pineFlakes.normal.avif +0 -0
- package/assets/bark/plane.albedo.avif +0 -0
- package/assets/bark/plane.normal.avif +0 -0
- package/assets/bark/poplar.albedo.avif +0 -0
- package/assets/bark/poplar.normal.avif +0 -0
- package/assets/bark/redAlder.albedo.avif +0 -0
- package/assets/bark/redAlder.normal.avif +0 -0
- package/assets/bark/redOak.albedo.avif +0 -0
- package/assets/bark/redOak.normal.avif +0 -0
- package/assets/bark/ridged.albedo.avif +0 -0
- package/assets/bark/ridged.normal.avif +0 -0
- package/assets/bark/sitkaSpruce.albedo.avif +0 -0
- package/assets/bark/sitkaSpruce.normal.avif +0 -0
- package/assets/bark/spruce.albedo.avif +0 -0
- package/assets/bark/spruce.normal.avif +0 -0
- package/assets/bark/stonePine.albedo.avif +0 -0
- package/assets/bark/stonePine.normal.avif +0 -0
- package/assets/bark/sweetgum.albedo.avif +0 -0
- package/assets/bark/sweetgum.normal.avif +0 -0
- package/assets/bark/sycMaple.albedo.avif +0 -0
- package/assets/bark/sycMaple.normal.avif +0 -0
- package/assets/bark/torreyPine.albedo.avif +0 -0
- package/assets/bark/torreyPine.normal.avif +0 -0
- package/assets/bark/whitePine.albedo.avif +0 -0
- package/assets/bark/whitePine.normal.avif +0 -0
- package/assets/bark/whiteSpruce.albedo.avif +0 -0
- package/assets/bark/whiteSpruce.normal.avif +0 -0
- package/assets/bark/whiteWillow.albedo.avif +0 -0
- package/assets/bark/whiteWillow.normal.avif +0 -0
- package/assets/bark/willow.albedo.avif +0 -0
- package/assets/bark/willow.normal.avif +0 -0
- package/assets/leaves/alder.albedo.avif +0 -0
- package/assets/leaves/alder.normal.avif +0 -0
- package/assets/leaves/ash.albedo.avif +0 -0
- package/assets/leaves/ash.normal.avif +0 -0
- package/assets/leaves/aspen.albedo.avif +0 -0
- package/assets/leaves/aspen.normal.avif +0 -0
- package/assets/leaves/beech.albedo.avif +0 -0
- package/assets/leaves/beech.normal.avif +0 -0
- package/assets/leaves/birch.albedo.avif +0 -0
- package/assets/leaves/birch.normal.avif +0 -0
- package/assets/leaves/blackthorn.albedo.avif +0 -0
- package/assets/leaves/blackthorn.normal.avif +0 -0
- package/assets/leaves/blade.albedo.avif +0 -0
- package/assets/leaves/blade.normal.avif +0 -0
- package/assets/leaves/broomSprig.albedo.avif +0 -0
- package/assets/leaves/broomSprig.normal.avif +0 -0
- package/assets/leaves/corsicanShoot.albedo.avif +0 -0
- package/assets/leaves/corsicanShoot.normal.avif +0 -0
- package/assets/leaves/cypress.albedo.avif +0 -0
- package/assets/leaves/cypress.normal.avif +0 -0
- package/assets/leaves/cypressPad.albedo.avif +0 -0
- package/assets/leaves/cypressPad.normal.avif +0 -0
- package/assets/leaves/deadFronds.albedo.avif +0 -0
- package/assets/leaves/deadFronds.normal.avif +0 -0
- package/assets/leaves/deltoid.albedo.avif +0 -0
- package/assets/leaves/deltoid.normal.avif +0 -0
- package/assets/leaves/douglasShoot.albedo.avif +0 -0
- package/assets/leaves/douglasShoot.normal.avif +0 -0
- package/assets/leaves/elderSprig.albedo.avif +0 -0
- package/assets/leaves/elderSprig.normal.avif +0 -0
- package/assets/leaves/elliptic.albedo.avif +0 -0
- package/assets/leaves/elliptic.normal.avif +0 -0
- package/assets/leaves/eucalyptus.albedo.avif +0 -0
- package/assets/leaves/eucalyptus.normal.avif +0 -0
- package/assets/leaves/fan.albedo.avif +0 -0
- package/assets/leaves/fan.normal.avif +0 -0
- package/assets/leaves/fanB.albedo.avif +0 -0
- package/assets/leaves/fanB.normal.avif +0 -0
- package/assets/leaves/fir.albedo.avif +0 -0
- package/assets/leaves/fir.normal.avif +0 -0
- package/assets/leaves/frond.albedo.avif +0 -0
- package/assets/leaves/frond.normal.avif +0 -0
- package/assets/leaves/gorse.albedo.avif +0 -0
- package/assets/leaves/gorse.normal.avif +0 -0
- package/assets/leaves/hawthorn.albedo.avif +0 -0
- package/assets/leaves/hawthorn.normal.avif +0 -0
- package/assets/leaves/hemlockShoot.albedo.avif +0 -0
- package/assets/leaves/hemlockShoot.normal.avif +0 -0
- package/assets/leaves/hornbeam.albedo.avif +0 -0
- package/assets/leaves/hornbeam.normal.avif +0 -0
- package/assets/leaves/lance.albedo.avif +0 -0
- package/assets/leaves/lance.normal.avif +0 -0
- package/assets/leaves/larchShoot.albedo.avif +0 -0
- package/assets/leaves/larchShoot.normal.avif +0 -0
- package/assets/leaves/lime.albedo.avif +0 -0
- package/assets/leaves/lime.normal.avif +0 -0
- package/assets/leaves/liveOak.albedo.avif +0 -0
- package/assets/leaves/liveOak.normal.avif +0 -0
- package/assets/leaves/londonPlane.albedo.avif +0 -0
- package/assets/leaves/londonPlane.normal.avif +0 -0
- package/assets/leaves/magnolia.albedo.avif +0 -0
- package/assets/leaves/magnolia.normal.avif +0 -0
- package/assets/leaves/maple.albedo.avif +0 -0
- package/assets/leaves/maple.normal.avif +0 -0
- package/assets/leaves/norMaple.albedo.avif +0 -0
- package/assets/leaves/norMaple.normal.avif +0 -0
- package/assets/leaves/oak.albedo.avif +0 -0
- package/assets/leaves/oak.normal.avif +0 -0
- package/assets/leaves/paintFan.albedo.avif +0 -0
- package/assets/leaves/paintFan.normal.avif +0 -0
- package/assets/leaves/paintFrond.albedo.avif +0 -0
- package/assets/leaves/paintFrond.normal.avif +0 -0
- package/assets/leaves/pine.albedo.avif +0 -0
- package/assets/leaves/pine.normal.avif +0 -0
- package/assets/leaves/pineClumpDark.albedo.avif +0 -0
- package/assets/leaves/pineClumpDark.normal.avif +0 -0
- package/assets/leaves/pineClumpLight.albedo.avif +0 -0
- package/assets/leaves/pineClumpLight.normal.avif +0 -0
- package/assets/leaves/poplar.albedo.avif +0 -0
- package/assets/leaves/poplar.normal.avif +0 -0
- package/assets/leaves/queenFrond.albedo.avif +0 -0
- package/assets/leaves/queenFrond.normal.avif +0 -0
- package/assets/leaves/redAlder.albedo.avif +0 -0
- package/assets/leaves/redAlder.normal.avif +0 -0
- package/assets/leaves/redOak.albedo.avif +0 -0
- package/assets/leaves/redOak.normal.avif +0 -0
- package/assets/leaves/round.albedo.avif +0 -0
- package/assets/leaves/round.normal.avif +0 -0
- package/assets/leaves/scale.albedo.avif +0 -0
- package/assets/leaves/scale.normal.avif +0 -0
- package/assets/leaves/scotsPineClump.albedo.avif +0 -0
- package/assets/leaves/scotsPineClump.normal.avif +0 -0
- package/assets/leaves/seaBuckthornSprig.albedo.avif +0 -0
- package/assets/leaves/seaBuckthornSprig.normal.avif +0 -0
- package/assets/leaves/serrate.albedo.avif +0 -0
- package/assets/leaves/serrate.normal.avif +0 -0
- package/assets/leaves/sitkaShoot.albedo.avif +0 -0
- package/assets/leaves/sitkaShoot.normal.avif +0 -0
- package/assets/leaves/stonePineCanopy.albedo.avif +0 -0
- package/assets/leaves/stonePineCanopy.normal.avif +0 -0
- package/assets/leaves/stonePineCushion.albedo.avif +0 -0
- package/assets/leaves/stonePineCushion.normal.avif +0 -0
- package/assets/leaves/sweetgum.albedo.avif +0 -0
- package/assets/leaves/sweetgum.normal.avif +0 -0
- package/assets/leaves/sycamore.albedo.avif +0 -0
- package/assets/leaves/sycamore.normal.avif +0 -0
- package/assets/leaves/torreyPine.albedo.avif +0 -0
- package/assets/leaves/torreyPine.normal.avif +0 -0
- package/assets/leaves/whitePine.albedo.avif +0 -0
- package/assets/leaves/whitePine.normal.avif +0 -0
- package/assets/leaves/whiteSpruceShoot.albedo.avif +0 -0
- package/assets/leaves/whiteSpruceShoot.normal.avif +0 -0
- package/assets/leaves/whiteWillow.albedo.avif +0 -0
- package/assets/leaves/whiteWillow.normal.avif +0 -0
- package/assets/leaves/willow.albedo.avif +0 -0
- package/assets/leaves/willow.normal.avif +0 -0
- package/assets/marks/marks.json +894 -0
- package/assets/marks/painterly.avif +0 -0
- package/assets/marks/sheets.avif +0 -0
- package/assets/scans.json +6406 -0
- package/lib/age.d.ts +36 -0
- package/lib/age.d.ts.map +1 -0
- package/lib/age.js +170 -0
- package/lib/age.js.map +1 -0
- package/lib/atmosphere.d.ts +64 -0
- package/lib/atmosphere.d.ts.map +1 -0
- package/lib/atmosphere.js +260 -0
- package/lib/atmosphere.js.map +1 -0
- package/lib/dabs.d.ts +4 -0
- package/lib/dabs.d.ts.map +1 -0
- package/lib/dabs.js +77 -0
- package/lib/dabs.js.map +1 -0
- package/lib/engine.d.ts +211 -0
- package/lib/engine.d.ts.map +1 -0
- package/lib/engine.js +3808 -0
- package/lib/engine.js.map +1 -0
- package/lib/genetics.d.ts +129 -0
- package/lib/genetics.d.ts.map +1 -0
- package/lib/genetics.js +721 -0
- package/lib/genetics.js.map +1 -0
- package/lib/geometry.d.ts +237 -0
- package/lib/geometry.d.ts.map +1 -0
- package/lib/geometry.js +1944 -0
- package/lib/geometry.js.map +1 -0
- package/lib/grow.d.ts +83 -0
- package/lib/grow.d.ts.map +1 -0
- package/lib/grow.js +3851 -0
- package/lib/grow.js.map +1 -0
- package/lib/hiz.d.ts +74 -0
- package/lib/hiz.d.ts.map +1 -0
- package/lib/hiz.js +161 -0
- package/lib/hiz.js.map +1 -0
- package/lib/impostor.d.ts +106 -0
- package/lib/impostor.d.ts.map +1 -0
- package/lib/impostor.js +450 -0
- package/lib/impostor.js.map +1 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +16 -0
- package/lib/index.js.map +1 -0
- package/lib/leaves.d.ts +30 -0
- package/lib/leaves.d.ts.map +1 -0
- package/lib/leaves.js +829 -0
- package/lib/leaves.js.map +1 -0
- package/lib/light.d.ts +110 -0
- package/lib/light.d.ts.map +1 -0
- package/lib/light.js +430 -0
- package/lib/light.js.map +1 -0
- package/lib/lobes.d.ts +25 -0
- package/lib/lobes.d.ts.map +1 -0
- package/lib/lobes.js +151 -0
- package/lib/lobes.js.map +1 -0
- package/lib/marks.d.ts +63 -0
- package/lib/marks.d.ts.map +1 -0
- package/lib/marks.js +553 -0
- package/lib/marks.js.map +1 -0
- package/lib/mips.d.ts +42 -0
- package/lib/mips.d.ts.map +1 -0
- package/lib/mips.js +220 -0
- package/lib/mips.js.map +1 -0
- package/lib/paintedMarks.d.ts +21 -0
- package/lib/paintedMarks.d.ts.map +1 -0
- package/lib/paintedMarks.js +53 -0
- package/lib/paintedMarks.js.map +1 -0
- package/lib/palm.d.ts +162 -0
- package/lib/palm.d.ts.map +1 -0
- package/lib/palm.js +779 -0
- package/lib/palm.js.map +1 -0
- package/lib/palmAtlas.d.ts +79 -0
- package/lib/palmAtlas.d.ts.map +1 -0
- package/lib/palmAtlas.js +335 -0
- package/lib/palmAtlas.js.map +1 -0
- package/lib/palmGeometry.d.ts +41 -0
- package/lib/palmGeometry.d.ts.map +1 -0
- package/lib/palmGeometry.js +309 -0
- package/lib/palmGeometry.js.map +1 -0
- package/lib/phenology.d.ts +21 -0
- package/lib/phenology.d.ts.map +1 -0
- package/lib/phenology.js +168 -0
- package/lib/phenology.js.map +1 -0
- package/lib/random.d.ts +34 -0
- package/lib/random.d.ts.map +1 -0
- package/lib/random.js +92 -0
- package/lib/random.js.map +1 -0
- package/lib/scans.d.ts +232 -0
- package/lib/scans.d.ts.map +1 -0
- package/lib/scans.js +397 -0
- package/lib/scans.js.map +1 -0
- package/lib/scatter.d.ts +70 -0
- package/lib/scatter.d.ts.map +1 -0
- package/lib/scatter.js +186 -0
- package/lib/scatter.js.map +1 -0
- package/lib/species.d.ts +369 -0
- package/lib/species.d.ts.map +1 -0
- package/lib/species.js +2978 -0
- package/lib/species.js.map +1 -0
- package/lib/sprays.d.ts +112 -0
- package/lib/sprays.d.ts.map +1 -0
- package/lib/sprays.js +1137 -0
- package/lib/sprays.js.map +1 -0
- package/lib/stand.d.ts +52 -0
- package/lib/stand.d.ts.map +1 -0
- package/lib/stand.js +279 -0
- package/lib/stand.js.map +1 -0
- package/lib/styles.d.ts +135 -0
- package/lib/styles.d.ts.map +1 -0
- package/lib/styles.js +79 -0
- package/lib/styles.js.map +1 -0
- package/lib/tree.d.ts +134 -0
- package/lib/tree.d.ts.map +1 -0
- package/lib/tree.js +57 -0
- package/lib/tree.js.map +1 -0
- package/lib/variants.d.ts +157 -0
- package/lib/variants.d.ts.map +1 -0
- package/lib/variants.js +532 -0
- package/lib/variants.js.map +1 -0
- package/lib/visibility.d.ts +105 -0
- package/lib/visibility.d.ts.map +1 -0
- package/lib/visibility.js +242 -0
- package/lib/visibility.js.map +1 -0
- package/package.json +62 -3
package/lib/genetics.js
ADDED
|
@@ -0,0 +1,721 @@
|
|
|
1
|
+
// Arboretum DNA & Evolutionary Tree Breeding (`trees/src/genetics.ts`).
|
|
2
|
+
// Five linked diploid loci over morphological, Crown allometric, tropism, foliage/bark,
|
|
3
|
+
// and phenology/vigour traits with mechanical pipe-model safety guards and a compact `DNA1-` codec.
|
|
4
|
+
import { ALL_SPECIES, LEAF_KIND_ROWS, checkSpecies, } from "./species.js";
|
|
5
|
+
import { BARK_SETS, LEAF_SETS, SPRAY_KINDS, scanLookOf } from "./scans.js";
|
|
6
|
+
export const LOCUS_NAMES = ["architecture", "crown", "tropism", "foliageBark", "phenologyVigour"];
|
|
7
|
+
const ARCH_LIST = ["broadleaf", "conifer", "shrub", "palm"];
|
|
8
|
+
const SHAPE_LIST = [
|
|
9
|
+
"conical",
|
|
10
|
+
"spherical",
|
|
11
|
+
"hemispherical",
|
|
12
|
+
"cylindrical",
|
|
13
|
+
"taperedCylindrical",
|
|
14
|
+
"flame",
|
|
15
|
+
"inverseConical",
|
|
16
|
+
"tendFlame",
|
|
17
|
+
];
|
|
18
|
+
const PHYLLO_LIST = ["spiral", "opposite", "distichous"];
|
|
19
|
+
const PALM_LEAF_LIST = ["feather", "fan"];
|
|
20
|
+
const H_MIN = 0.8;
|
|
21
|
+
const H_MAX = 55;
|
|
22
|
+
const H_LOG_SPAN = Math.log(H_MAX / H_MIN);
|
|
23
|
+
function clamp(v, lo, hi) {
|
|
24
|
+
return v < lo ? lo : v > hi ? hi : v;
|
|
25
|
+
}
|
|
26
|
+
function clamp01(v) {
|
|
27
|
+
return clamp(v, 0, 1);
|
|
28
|
+
}
|
|
29
|
+
function hash01(index, seed) {
|
|
30
|
+
let h = Math.imul(index ^ seed, 0x9e3779b1) >>> 0;
|
|
31
|
+
h ^= h >>> 16;
|
|
32
|
+
h = Math.imul(h, 0x7feb352d) >>> 0;
|
|
33
|
+
h ^= h >>> 15;
|
|
34
|
+
h = Math.imul(h, 0x846ca68b) >>> 0;
|
|
35
|
+
h ^= h >>> 16;
|
|
36
|
+
return (h >>> 0) / 4294967296;
|
|
37
|
+
}
|
|
38
|
+
function express(pair) {
|
|
39
|
+
return clamp01(0.5 * (pair[0] + pair[1]));
|
|
40
|
+
}
|
|
41
|
+
function pairOf(v, spread = 0, seed = 1) {
|
|
42
|
+
const c = clamp01(v);
|
|
43
|
+
if (spread <= 0)
|
|
44
|
+
return [c, c];
|
|
45
|
+
const d = (hash01(seed, 313) - 0.5) * spread;
|
|
46
|
+
return [clamp01(c - d), clamp01(c + d)];
|
|
47
|
+
}
|
|
48
|
+
function hexToRgb(hex) {
|
|
49
|
+
const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim());
|
|
50
|
+
if (!m)
|
|
51
|
+
return [0.3, 0.45, 0.25];
|
|
52
|
+
const n = parseInt(m[1], 16);
|
|
53
|
+
return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];
|
|
54
|
+
}
|
|
55
|
+
function rgbToHex(r, g, b) {
|
|
56
|
+
const toByte = (v) => clamp(Math.round(clamp01(v) * 255), 0, 255).toString(16).padStart(2, "0");
|
|
57
|
+
return `#${toByte(r)}${toByte(g)}${toByte(b)}`;
|
|
58
|
+
}
|
|
59
|
+
function rgbToHsl(r, g, b) {
|
|
60
|
+
const mx = Math.max(r, g, b);
|
|
61
|
+
const mn = Math.min(r, g, b);
|
|
62
|
+
const l = 0.5 * (mx + mn);
|
|
63
|
+
if (mx === mn)
|
|
64
|
+
return [0.28, 0, l];
|
|
65
|
+
const d = mx - mn;
|
|
66
|
+
const s = l > 0.5 ? d / (2 - mx - mn) : d / (mx + mn);
|
|
67
|
+
let h = 0;
|
|
68
|
+
if (mx === r)
|
|
69
|
+
h = (g - b) / d + (g < b ? 6 : 0);
|
|
70
|
+
else if (mx === g)
|
|
71
|
+
h = (b - r) / d + 2;
|
|
72
|
+
else
|
|
73
|
+
h = (r - g) / d + 4;
|
|
74
|
+
return [h / 6, clamp01(s), clamp01(l)];
|
|
75
|
+
}
|
|
76
|
+
function hslToRgb(h, s, l) {
|
|
77
|
+
const hn = ((h % 1) + 1) % 1;
|
|
78
|
+
const sn = clamp01(s);
|
|
79
|
+
const ln = clamp01(l);
|
|
80
|
+
if (sn === 0)
|
|
81
|
+
return [ln, ln, ln];
|
|
82
|
+
const q = ln < 0.5 ? ln * (1 + sn) : ln + sn - ln * sn;
|
|
83
|
+
const p = 2 * ln - q;
|
|
84
|
+
const hue2rgb = (t0) => {
|
|
85
|
+
let t = t0;
|
|
86
|
+
if (t < 0)
|
|
87
|
+
t += 1;
|
|
88
|
+
if (t > 1)
|
|
89
|
+
t -= 1;
|
|
90
|
+
if (t < 1 / 6)
|
|
91
|
+
return p + (q - p) * 6 * t;
|
|
92
|
+
if (t < 1 / 2)
|
|
93
|
+
return q;
|
|
94
|
+
if (t < 2 / 3)
|
|
95
|
+
return p + (q - p) * (2 / 3 - t) * 6;
|
|
96
|
+
return p;
|
|
97
|
+
};
|
|
98
|
+
return [hue2rgb(hn + 1 / 3), hue2rgb(hn), hue2rgb(hn - 1 / 3)];
|
|
99
|
+
}
|
|
100
|
+
function pipeMinTrunkRadius(height, crownRadius) {
|
|
101
|
+
return Math.max(0.025, 0.014 * Math.pow(Math.max(1, height), 1.12) * (0.7 + (0.6 * crownRadius) / Math.max(1, height)));
|
|
102
|
+
}
|
|
103
|
+
/** Encode any valid `TreeSpecies` (e.g. from `ALL_SPECIES`) into a linked 5-locus diploid `TreeGenome`. */
|
|
104
|
+
export function speciesToGenome(sp, seed = 101) {
|
|
105
|
+
const look = scanLookOf(sp);
|
|
106
|
+
const [lr, lg, lb] = hexToRgb(sp.leafColor);
|
|
107
|
+
const [lh, ls, ll] = rgbToHsl(lr, lg, lb);
|
|
108
|
+
const [br, bg, bb] = hexToRgb(sp.barkColor);
|
|
109
|
+
const pipeMin = pipeMinTrunkRadius(sp.height, sp.crownRadius);
|
|
110
|
+
const slenderness = clamp((sp.trunkRadius / pipeMin - 0.85) / 1.4, 0, 1);
|
|
111
|
+
return {
|
|
112
|
+
version: 1,
|
|
113
|
+
generation: 1,
|
|
114
|
+
seed,
|
|
115
|
+
architecture: {
|
|
116
|
+
architecture: sp.architecture,
|
|
117
|
+
phyllotaxis: sp.phyllotaxis ?? "spiral",
|
|
118
|
+
palmLeaf: sp.palmLeaf ?? "feather",
|
|
119
|
+
stems: pairOf(clamp((sp.stems - 1) / 11, 0, 1), 0.04, seed + 1),
|
|
120
|
+
leader: pairOf(sp.leader, 0.04, seed + 2),
|
|
121
|
+
branchAngle: pairOf(clamp((sp.branchAngle - 15) / 95, 0, 1), 0.04, seed + 3),
|
|
122
|
+
branchAngleSpread: pairOf(clamp(sp.branchAngleSpread / 40, 0, 1), 0.04, seed + 4),
|
|
123
|
+
scaffolds: pairOf(clamp((sp.scaffolds ?? 6) / 12, 0, 1), 0.04, seed + 5),
|
|
124
|
+
fork: pairOf(sp.fork ?? 0, 0.04, seed + 6),
|
|
125
|
+
forkShare: pairOf(sp.forkShare ?? 0, 0.04, seed + 7),
|
|
126
|
+
forkSpread: pairOf(sp.forkSpread ?? 0.3, 0.04, seed + 8),
|
|
127
|
+
forkStems: pairOf(clamp(((sp.forkStems ?? 2) - 1) / 3, 0, 1), 0.04, seed + 9),
|
|
128
|
+
whorl: pairOf(clamp(sp.whorl / 24, 0, 1), 0.04, seed + 10),
|
|
129
|
+
whorlsPerMetre: pairOf(clamp(sp.whorlsPerMetre / 3.5, 0, 1), 0.04, seed + 11),
|
|
130
|
+
branchiness: pairOf(sp.branchiness, 0.04, seed + 12),
|
|
131
|
+
},
|
|
132
|
+
crown: {
|
|
133
|
+
shape: sp.shape,
|
|
134
|
+
height: pairOf(clamp(Math.log(Math.max(H_MIN, sp.height) / H_MIN) / H_LOG_SPAN, 0, 1), 0.02, seed + 21),
|
|
135
|
+
crownRadiusRatio: pairOf(clamp((sp.crownRadius / Math.max(0.4, sp.height) - 0.06) / 0.84, 0, 1), 0.02, seed + 22),
|
|
136
|
+
crownBaseRatio: pairOf(clamp(sp.crownBase / Math.max(0.4, sp.height) / 0.85, 0, 1), 0.02, seed + 23),
|
|
137
|
+
crownPeak: pairOf(clamp((sp.crownPeak ?? 0.42) / 0.92, 0, 1), 0.04, seed + 24),
|
|
138
|
+
crownRound: pairOf(clamp(((sp.crownRound ?? 2) - 0.85) / 5.15, 0, 1), 0.04, seed + 25),
|
|
139
|
+
lobes: pairOf(sp.lobes ?? 0.25, 0.04, seed + 26),
|
|
140
|
+
foliageDepth: pairOf(sp.foliageDepth, 0.04, seed + 27),
|
|
141
|
+
openness: pairOf(sp.openness, 0.04, seed + 28),
|
|
142
|
+
pads: pairOf(sp.pads ?? 0, 0.04, seed + 29),
|
|
143
|
+
clusters: pairOf(sp.clusters ?? 0, 0.04, seed + 30),
|
|
144
|
+
canopyLift: pairOf(sp.canopyLift ?? 0, 0.04, seed + 31),
|
|
145
|
+
},
|
|
146
|
+
tropism: {
|
|
147
|
+
bend: pairOf(clamp((sp.bend + 1) * 0.5, 0, 1), 0.04, seed + 41),
|
|
148
|
+
weep: pairOf(sp.weep ?? 0, 0.04, seed + 42),
|
|
149
|
+
limbCurve: pairOf(clamp(((sp.limbCurve ?? 0) + 90) / 180, 0, 1), 0.04, seed + 43),
|
|
150
|
+
limbCurveBack: pairOf(clamp(((sp.limbCurveBack ?? 0) + 90) / 180, 0, 1), 0.04, seed + 44),
|
|
151
|
+
crooked: pairOf(sp.crooked, 0.04, seed + 45),
|
|
152
|
+
zigzag: pairOf(clamp((sp.zigzag ?? 8) / 45, 0, 1), 0.04, seed + 46),
|
|
153
|
+
lean: pairOf(clamp((sp.lean ?? 0) / 28, 0, 1), 0.04, seed + 47),
|
|
154
|
+
buttress: pairOf(sp.buttress ?? 0.25, 0.04, seed + 48),
|
|
155
|
+
stiffness: pairOf(sp.stiffness, 0.04, seed + 49),
|
|
156
|
+
trunkSlenderness: pairOf(slenderness, 0.04, seed + 50),
|
|
157
|
+
},
|
|
158
|
+
foliageBark: {
|
|
159
|
+
leafKind: sp.leafKind,
|
|
160
|
+
barkId: look.bark,
|
|
161
|
+
leavesId: look.leaves,
|
|
162
|
+
spray: look.spray,
|
|
163
|
+
clumpSize: pairOf(clamp((sp.clumpSize - 0.35) / 2.25, 0, 1), 0.04, seed + 61),
|
|
164
|
+
clumpSpread: pairOf(sp.clumpSpread, 0.04, seed + 62),
|
|
165
|
+
leafHue: pairOf(lh, 0.02, seed + 63),
|
|
166
|
+
leafSat: pairOf(ls, 0.04, seed + 64),
|
|
167
|
+
leafLight: pairOf(ll, 0.04, seed + 65),
|
|
168
|
+
barkR: pairOf(br, 0.04, seed + 66),
|
|
169
|
+
barkG: pairOf(bg, 0.04, seed + 67),
|
|
170
|
+
barkB: pairOf(bb, 0.04, seed + 68),
|
|
171
|
+
barkPale: pairOf(sp.barkPale, 0.04, seed + 69),
|
|
172
|
+
leafVariation: pairOf(sp.leafVariation, 0.04, seed + 70),
|
|
173
|
+
treeVariation: pairOf(sp.treeVariation, 0.04, seed + 71),
|
|
174
|
+
},
|
|
175
|
+
phenologyVigour: {
|
|
176
|
+
throughR: pairOf(clamp(sp.throughTint[0] / 1.4, 0, 1), 0.04, seed + 81),
|
|
177
|
+
throughG: pairOf(clamp(sp.throughTint[1] / 1.4, 0, 1), 0.04, seed + 82),
|
|
178
|
+
throughB: pairOf(clamp(sp.throughTint[2] / 1.4, 0, 1), 0.04, seed + 83),
|
|
179
|
+
sprayHang: pairOf(sp.sprayHang ?? 0, 0.04, seed + 84),
|
|
180
|
+
shed: pairOf(sp.shed ?? 0, 0.04, seed + 85),
|
|
181
|
+
vigour: pairOf(0.5, 0.04, seed + 86),
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Develop a `TreeGenome` into a validated `TreeSpecies` guarded by pipe-model trunk safety,
|
|
187
|
+
* architecture invariants, and clump density caps (`checkSpecies(out).length === 0`).
|
|
188
|
+
*/
|
|
189
|
+
export function genomeToSpecies(genome, name = `Cultivar #${genome.seed % 1000}`) {
|
|
190
|
+
const arch = genome.architecture.architecture;
|
|
191
|
+
const rawHeight = H_MIN * Math.exp(express(genome.crown.height) * H_LOG_SPAN);
|
|
192
|
+
const vigourScale = 0.88 + 0.24 * express(genome.phenologyVigour.vigour);
|
|
193
|
+
let height = clamp(rawHeight * vigourScale, 0.85, 55);
|
|
194
|
+
if (arch === "shrub")
|
|
195
|
+
height = clamp(height, 0.8, 4.5);
|
|
196
|
+
const crRatio = clamp(0.06 + express(genome.crown.crownRadiusRatio) * 0.84, 0.09, 0.88);
|
|
197
|
+
let crownRadius = clamp(height * crRatio, 0.45, Math.max(0.8, height * 0.85));
|
|
198
|
+
if (arch === "palm")
|
|
199
|
+
crownRadius = clamp(crownRadius, 1.4, Math.min(7.5, height * 0.65));
|
|
200
|
+
const cbRatio = clamp(express(genome.crown.crownBaseRatio) * 0.85, 0.03, 0.78);
|
|
201
|
+
let crownBase = clamp(height * cbRatio, 0.05, height * 0.75);
|
|
202
|
+
if (arch === "shrub")
|
|
203
|
+
crownBase = clamp(crownBase, 0.04, Math.min(0.6, height * 0.28));
|
|
204
|
+
if (arch === "palm")
|
|
205
|
+
crownBase = clamp(crownBase, height * 0.5, height * 0.84);
|
|
206
|
+
if (crownBase >= height - 0.35)
|
|
207
|
+
crownBase = Math.max(0.05, height - 0.4);
|
|
208
|
+
const pipeMin = pipeMinTrunkRadius(height, crownRadius);
|
|
209
|
+
const slenderness = 0.85 + express(genome.tropism.trunkSlenderness) * 1.4;
|
|
210
|
+
let trunkRadius = clamp(pipeMin * slenderness, 0.025, Math.min(2.2, crownRadius * 0.42));
|
|
211
|
+
if (arch === "shrub")
|
|
212
|
+
trunkRadius = clamp(trunkRadius, 0.025, 0.14);
|
|
213
|
+
const stems = arch === "shrub" ? clamp(Math.round(3 + express(genome.architecture.stems) * 7), 3, 10) : 1;
|
|
214
|
+
const leafKind = arch === "palm"
|
|
215
|
+
? "frond"
|
|
216
|
+
: arch === "conifer"
|
|
217
|
+
? genome.foliageBark.leafKind === "pine" || genome.foliageBark.leafKind === "needle"
|
|
218
|
+
? genome.foliageBark.leafKind
|
|
219
|
+
: "needle"
|
|
220
|
+
: genome.foliageBark.leafKind === "frond"
|
|
221
|
+
? "broad"
|
|
222
|
+
: genome.foliageBark.leafKind;
|
|
223
|
+
const whorl = arch === "conifer"
|
|
224
|
+
? clamp(Math.round(3 + express(genome.architecture.whorl) * 9), 3, 12)
|
|
225
|
+
: arch === "palm"
|
|
226
|
+
? clamp(Math.round(8 + express(genome.architecture.whorl) * 18), 8, 28)
|
|
227
|
+
: 0;
|
|
228
|
+
const whorlsPerMetre = arch === "conifer" ? clamp(0.8 + express(genome.architecture.whorlsPerMetre) * 2.5, 0.8, 3.5) : 0;
|
|
229
|
+
const [lr, lg, lb] = hslToRgb(express(genome.foliageBark.leafHue), express(genome.foliageBark.leafSat), clamp(express(genome.foliageBark.leafLight), 0.12, 0.72));
|
|
230
|
+
const barkHex = rgbToHex(express(genome.foliageBark.barkR), express(genome.foliageBark.barkG), express(genome.foliageBark.barkB));
|
|
231
|
+
// Clump density safety floor so large bred hybrids stay fast in `growTree` & GPU draws.
|
|
232
|
+
const rawClump = 0.35 + express(genome.foliageBark.clumpSize) * 2.25;
|
|
233
|
+
const safetyClumpFloor = Math.max(0.45, Math.min(crownRadius, height - crownBase) * 0.17, height * 0.055);
|
|
234
|
+
const clumpSize = clamp(Math.max(rawClump, safetyClumpFloor), 0.42, 3.2);
|
|
235
|
+
const forkVal = express(genome.architecture.fork);
|
|
236
|
+
const forkShareVal = arch === "broadleaf" ? clamp(express(genome.architecture.forkShare), 0, 0.92) : 0;
|
|
237
|
+
const forkStemsVal = clamp(Math.round(1 + express(genome.architecture.forkStems) * 3), 1, 4);
|
|
238
|
+
const sp = {
|
|
239
|
+
name: name.trim() || "Cultivar",
|
|
240
|
+
latin: `Hybridus cult. G${genome.generation}`,
|
|
241
|
+
architecture: arch,
|
|
242
|
+
height: Number(height.toFixed(2)),
|
|
243
|
+
crownRadius: Number(crownRadius.toFixed(2)),
|
|
244
|
+
crownBase: Number(crownBase.toFixed(2)),
|
|
245
|
+
trunkRadius: Number(trunkRadius.toFixed(3)),
|
|
246
|
+
shape: genome.crown.shape,
|
|
247
|
+
crownPeak: Number(clamp(express(genome.crown.crownPeak) * 0.92, 0.05, 0.92).toFixed(2)),
|
|
248
|
+
crownRound: Number(clamp(0.85 + express(genome.crown.crownRound) * 5.15, 0.85, 6.0).toFixed(2)),
|
|
249
|
+
lobes: Number(express(genome.crown.lobes).toFixed(2)),
|
|
250
|
+
foliageDepth: Number(clamp(express(genome.crown.foliageDepth), 0.12, 0.95).toFixed(2)),
|
|
251
|
+
openness: Number(clamp(express(genome.crown.openness), 0.08, 0.82).toFixed(2)),
|
|
252
|
+
stems,
|
|
253
|
+
phyllotaxis: genome.architecture.phyllotaxis,
|
|
254
|
+
branchAngle: Number(clamp(15 + express(genome.architecture.branchAngle) * 95, 18, 110).toFixed(1)),
|
|
255
|
+
branchAngleSpread: Number(clamp(express(genome.architecture.branchAngleSpread) * 40, 2, 38).toFixed(1)),
|
|
256
|
+
bend: Number(clamp(express(genome.tropism.bend) * 2 - 1, -0.95, 0.95).toFixed(2)),
|
|
257
|
+
crooked: Number(express(genome.tropism.crooked).toFixed(2)),
|
|
258
|
+
leader: Number((arch === "conifer" ? clamp(0.7 + express(genome.architecture.leader) * 0.3, 0.65, 1) : express(genome.architecture.leader)).toFixed(2)),
|
|
259
|
+
whorl,
|
|
260
|
+
whorlsPerMetre: Number(whorlsPerMetre.toFixed(2)),
|
|
261
|
+
branchiness: Number(clamp(express(genome.architecture.branchiness), 0.32, 0.85).toFixed(2)),
|
|
262
|
+
scaffolds: clamp(Math.round(express(genome.architecture.scaffolds) * 10) + 2, 2, 11),
|
|
263
|
+
limbCurve: Number(clamp(express(genome.tropism.limbCurve) * 180 - 90, -85, 85).toFixed(1)),
|
|
264
|
+
limbCurveBack: Number(clamp(express(genome.tropism.limbCurveBack) * 180 - 90, -85, 85).toFixed(1)),
|
|
265
|
+
zigzag: Number(clamp(express(genome.tropism.zigzag) * 45, 0, 42).toFixed(1)),
|
|
266
|
+
lean: Number(clamp(express(genome.tropism.lean) * 26, 0, 26).toFixed(1)),
|
|
267
|
+
buttress: Number(express(genome.tropism.buttress).toFixed(2)),
|
|
268
|
+
pads: Number(express(genome.crown.pads).toFixed(2)),
|
|
269
|
+
clusters: Number(express(genome.crown.clusters).toFixed(2)),
|
|
270
|
+
canopyLift: Number(express(genome.crown.canopyLift).toFixed(2)),
|
|
271
|
+
weep: Number(express(genome.tropism.weep).toFixed(2)),
|
|
272
|
+
sprayHang: Number(express(genome.phenologyVigour.sprayHang).toFixed(2)),
|
|
273
|
+
shed: Number(express(genome.phenologyVigour.shed).toFixed(2)),
|
|
274
|
+
leafKind,
|
|
275
|
+
clumpSize: Number(clumpSize.toFixed(2)),
|
|
276
|
+
clumpSpread: Number(clamp(express(genome.foliageBark.clumpSpread), 0.14, 0.65).toFixed(2)),
|
|
277
|
+
leafColor: rgbToHex(lr, lg, lb),
|
|
278
|
+
leafVariation: Number(clamp(express(genome.foliageBark.leafVariation), 0.08, 0.45).toFixed(2)),
|
|
279
|
+
treeVariation: Number(clamp(express(genome.foliageBark.treeVariation), 0.08, 0.45).toFixed(2)),
|
|
280
|
+
throughTint: [
|
|
281
|
+
Number(clamp(express(genome.phenologyVigour.throughR) * 1.4, 0.4, 1.3).toFixed(2)),
|
|
282
|
+
Number(clamp(express(genome.phenologyVigour.throughG) * 1.4, 0.5, 1.35).toFixed(2)),
|
|
283
|
+
Number(clamp(express(genome.phenologyVigour.throughB) * 1.4, 0.25, 1.1).toFixed(2)),
|
|
284
|
+
],
|
|
285
|
+
barkColor: barkHex,
|
|
286
|
+
barkPale: Number(express(genome.foliageBark.barkPale).toFixed(2)),
|
|
287
|
+
stiffness: Number(clamp(express(genome.tropism.stiffness), 0.25, 0.96).toFixed(2)),
|
|
288
|
+
...(arch === "broadleaf" && forkShareVal > 0.08
|
|
289
|
+
? {
|
|
290
|
+
fork: Number(clamp(forkVal, 0.2, 0.92).toFixed(2)),
|
|
291
|
+
forkShare: Number(forkShareVal.toFixed(2)),
|
|
292
|
+
forkSpread: Number(clamp(express(genome.architecture.forkSpread), 0.12, 0.65).toFixed(2)),
|
|
293
|
+
forkStems: forkStemsVal,
|
|
294
|
+
forkStemsMost: Math.min(4, Math.max(forkStemsVal, Math.min(4, forkStemsVal + 1))),
|
|
295
|
+
}
|
|
296
|
+
: {}),
|
|
297
|
+
...(arch === "palm" ? { palmLeaf: genome.architecture.palmLeaf } : {}),
|
|
298
|
+
};
|
|
299
|
+
const errs = checkSpecies(sp);
|
|
300
|
+
if (errs.length > 0) {
|
|
301
|
+
throw new Error(`genomeToSpecies produced invalid TreeSpecies: ${errs.join("; ")}`);
|
|
302
|
+
}
|
|
303
|
+
return sp;
|
|
304
|
+
}
|
|
305
|
+
/** Derive a compatible `Partial<ScanLook>` so bred cultivars wear matching real bark/leaf scans. */
|
|
306
|
+
export function genomeToScanLook(genome) {
|
|
307
|
+
const sp = genomeToSpecies(genome, "tmp");
|
|
308
|
+
const fallback = scanLookOf(sp);
|
|
309
|
+
const barkValid = BARK_SETS.some((b) => b.id === genome.foliageBark.barkId) ? genome.foliageBark.barkId : fallback.bark;
|
|
310
|
+
const leavesValid = LEAF_SETS.some((l) => l.id === genome.foliageBark.leavesId) ? genome.foliageBark.leavesId : fallback.leaves;
|
|
311
|
+
const sprayValid = sp.architecture === "palm"
|
|
312
|
+
? sp.palmLeaf === "fan"
|
|
313
|
+
? "fan"
|
|
314
|
+
: "pinnate"
|
|
315
|
+
: sp.architecture === "conifer"
|
|
316
|
+
? genome.foliageBark.spray === "tufts" || genome.foliageBark.spray === "sprays"
|
|
317
|
+
? genome.foliageBark.spray
|
|
318
|
+
: fallback.spray
|
|
319
|
+
: genome.foliageBark.spray;
|
|
320
|
+
return {
|
|
321
|
+
bark: barkValid,
|
|
322
|
+
leaves: leavesValid,
|
|
323
|
+
spray: sprayValid,
|
|
324
|
+
...(express(genome.tropism.weep) > 0.45 ? { hang: Number(express(genome.tropism.weep).toFixed(2)) } : {}),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function cloneLocus(locus) {
|
|
328
|
+
const out = {};
|
|
329
|
+
for (const [k, v] of Object.entries(locus)) {
|
|
330
|
+
out[k] = Array.isArray(v) ? [v[0], v[1]] : v;
|
|
331
|
+
}
|
|
332
|
+
return out;
|
|
333
|
+
}
|
|
334
|
+
export function cloneGenome(genome) {
|
|
335
|
+
return {
|
|
336
|
+
version: genome.version,
|
|
337
|
+
generation: genome.generation,
|
|
338
|
+
seed: genome.seed,
|
|
339
|
+
architecture: cloneLocus(genome.architecture),
|
|
340
|
+
crown: cloneLocus(genome.crown),
|
|
341
|
+
tropism: cloneLocus(genome.tropism),
|
|
342
|
+
foliageBark: cloneLocus(genome.foliageBark),
|
|
343
|
+
phenologyVigour: cloneLocus(genome.phenologyVigour),
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
function blendAllelePair(a, b, alpha, idx, seed) {
|
|
347
|
+
const pickFrom = (pA, pB, s) => {
|
|
348
|
+
const lo = Math.min(pA, pB);
|
|
349
|
+
const hi = Math.max(pA, pB);
|
|
350
|
+
const span = hi - lo;
|
|
351
|
+
const u = hash01(idx * 17 + s, seed);
|
|
352
|
+
return clamp01(lo - alpha * span + u * (span * (1 + 2 * alpha)));
|
|
353
|
+
};
|
|
354
|
+
return [pickFrom(a[0], b[Math.round(hash01(idx, seed + 5))], 11), pickFrom(b[0], a[Math.round(hash01(idx, seed + 7))], 29)];
|
|
355
|
+
}
|
|
356
|
+
/** Linked-locus BLX-alpha diploid crossover across all 5 loci, honoring `lockedLoci` from `parentA`. */
|
|
357
|
+
export function crossoverGenomes(parentA, parentB, opts) {
|
|
358
|
+
const seed = opts?.seed ?? ((parentA.seed * 73856093) ^ (parentB.seed * 19349663) ^ 42) >>> 0;
|
|
359
|
+
const alpha = opts?.alpha ?? 0.22;
|
|
360
|
+
const locks = new Set(opts?.lockedLoci ?? []);
|
|
361
|
+
const generation = Math.max(parentA.generation, parentB.generation) + 1;
|
|
362
|
+
const blendObj = (objA, objB, baseIdx, locus) => {
|
|
363
|
+
if (locks.has(locus))
|
|
364
|
+
return cloneLocus(objA);
|
|
365
|
+
const out = {};
|
|
366
|
+
const keys = Object.keys(objA);
|
|
367
|
+
for (let k = 0; k < keys.length; k++) {
|
|
368
|
+
const key = keys[k];
|
|
369
|
+
const va = objA[key];
|
|
370
|
+
const vb = objB[key];
|
|
371
|
+
if (Array.isArray(va) && Array.isArray(vb)) {
|
|
372
|
+
out[key] = blendAllelePair(va, vb, alpha, baseIdx + k, seed);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
out[key] = hash01(baseIdx + k, seed + 13) < 0.5 ? va : vb;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return out;
|
|
379
|
+
};
|
|
380
|
+
return {
|
|
381
|
+
version: 1,
|
|
382
|
+
generation,
|
|
383
|
+
seed: Math.max(1, seed & 0xfffffff),
|
|
384
|
+
architecture: blendObj(parentA.architecture, parentB.architecture, 10, "architecture"),
|
|
385
|
+
crown: blendObj(parentA.crown, parentB.crown, 40, "crown"),
|
|
386
|
+
tropism: blendObj(parentA.tropism, parentB.tropism, 70, "tropism"),
|
|
387
|
+
foliageBark: blendObj(parentA.foliageBark, parentB.foliageBark, 100, "foliageBark"),
|
|
388
|
+
phenologyVigour: blendObj(parentA.phenologyVigour, parentB.phenologyVigour, 130, "phenologyVigour"),
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
/** Gaussian/triangular allelic mutation over unlocked loci. */
|
|
392
|
+
export function mutateGenome(genome, opts) {
|
|
393
|
+
const rate = clamp01(opts?.rate ?? 0.22);
|
|
394
|
+
const strength = clamp(opts?.strength ?? 0.14, 0, 0.5);
|
|
395
|
+
const seed = opts?.seed ?? (genome.seed * 48271 + 97) >>> 0;
|
|
396
|
+
const locks = new Set(opts?.lockedLoci ?? []);
|
|
397
|
+
const mutPair = (p, idx) => {
|
|
398
|
+
const step = (s, val) => {
|
|
399
|
+
if (hash01(idx * 23 + s, seed + 3) > rate)
|
|
400
|
+
return val;
|
|
401
|
+
// Sums of uniforms give an approximate bell curve centred at 0.
|
|
402
|
+
const noise = (hash01(idx * 31 + s, seed + 19) + hash01(idx * 37 + s, seed + 41) - 1) * strength;
|
|
403
|
+
return clamp01(val + noise);
|
|
404
|
+
};
|
|
405
|
+
return [step(1, p[0]), step(2, p[1])];
|
|
406
|
+
};
|
|
407
|
+
const mutObj = (obj, baseIdx, locus) => {
|
|
408
|
+
if (locks.has(locus))
|
|
409
|
+
return cloneLocus(obj);
|
|
410
|
+
const out = {};
|
|
411
|
+
const keys = Object.keys(obj);
|
|
412
|
+
for (let k = 0; k < keys.length; k++) {
|
|
413
|
+
const key = keys[k];
|
|
414
|
+
const v = obj[key];
|
|
415
|
+
if (Array.isArray(v))
|
|
416
|
+
out[key] = mutPair(v, baseIdx + k);
|
|
417
|
+
else
|
|
418
|
+
out[key] = v;
|
|
419
|
+
}
|
|
420
|
+
return out;
|
|
421
|
+
};
|
|
422
|
+
return {
|
|
423
|
+
version: 1,
|
|
424
|
+
generation: genome.generation,
|
|
425
|
+
seed: Math.max(1, seed & 0xfffffff),
|
|
426
|
+
architecture: mutObj(genome.architecture, 200, "architecture"),
|
|
427
|
+
crown: mutObj(genome.crown, 240, "crown"),
|
|
428
|
+
tropism: mutObj(genome.tropism, 280, "tropism"),
|
|
429
|
+
foliageBark: mutObj(genome.foliageBark, 320, "foliageBark"),
|
|
430
|
+
phenologyVigour: mutObj(genome.phenologyVigour, 360, "phenologyVigour"),
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
/** Breed an entire nursery cohort (`12` default cultivars) from 1..N selected parent genomes. */
|
|
434
|
+
export function breedCohort(parentsInput, opts = {}) {
|
|
435
|
+
const parents = parentsInput.length > 0
|
|
436
|
+
? parentsInput.map((p, i) => ("architecture" in p && typeof p.architecture === "object" ? cloneGenome(p) : speciesToGenome(p, 100 + i)))
|
|
437
|
+
: [speciesToGenome(ALL_SPECIES[0], 101), speciesToGenome(ALL_SPECIES[1], 102)];
|
|
438
|
+
const count = clamp(Math.round(opts.count ?? 12), 1, 24);
|
|
439
|
+
const keepElites = clamp(Math.round(opts.keepElites ?? 1), 0, Math.min(parents.length, count));
|
|
440
|
+
const seed = opts.seed ?? (parents.reduce((acc, p, idx) => acc ^ (p.seed + idx * 919), 0x1357) >>> 0);
|
|
441
|
+
const nextGen = Math.max(...parents.map((p) => p.generation)) + 1;
|
|
442
|
+
const genomes = [];
|
|
443
|
+
for (let i = 0; i < keepElites; i++) {
|
|
444
|
+
const elite = cloneGenome(parents[i]);
|
|
445
|
+
elite.generation = nextGen;
|
|
446
|
+
genomes.push(elite);
|
|
447
|
+
}
|
|
448
|
+
for (let i = keepElites; i < count; i++) {
|
|
449
|
+
const aIdx = i % parents.length;
|
|
450
|
+
const bIdx = parents.length > 1 ? (aIdx + 1 + Math.floor(hash01(i * 19, seed) * (parents.length - 1))) % parents.length : 0;
|
|
451
|
+
const cross = crossoverGenomes(parents[aIdx], parents[bIdx], {
|
|
452
|
+
seed: seed + i * 1009 + 7,
|
|
453
|
+
lockedLoci: opts.lockedLoci,
|
|
454
|
+
});
|
|
455
|
+
const mut = mutateGenome(cross, {
|
|
456
|
+
rate: opts.mutationRate ?? 0.24,
|
|
457
|
+
strength: opts.mutationStrength ?? 0.14,
|
|
458
|
+
seed: seed + i * 2017 + 13,
|
|
459
|
+
lockedLoci: opts.lockedLoci,
|
|
460
|
+
});
|
|
461
|
+
mut.generation = nextGen;
|
|
462
|
+
mut.seed = Math.max(1, (seed + i * 313 + 29) & 0xffffff);
|
|
463
|
+
genomes.push(mut);
|
|
464
|
+
}
|
|
465
|
+
const species = [];
|
|
466
|
+
const scanLooks = {};
|
|
467
|
+
const dnaCodes = [];
|
|
468
|
+
for (let i = 0; i < genomes.length; i++) {
|
|
469
|
+
const name = `Cultivar #${i + 1}`;
|
|
470
|
+
species.push(genomeToSpecies(genomes[i], name));
|
|
471
|
+
scanLooks[name] = genomeToScanLook(genomes[i]);
|
|
472
|
+
dnaCodes.push(encodeDna(genomes[i]));
|
|
473
|
+
}
|
|
474
|
+
return { genomes, species, scanLooks, dnaCodes, generation: nextGen };
|
|
475
|
+
}
|
|
476
|
+
// ---- Compact URL-safe `DNA1-...` Codec with 16-bit FNV-1a Checksum ----
|
|
477
|
+
const B64_URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
478
|
+
const B64_INV = (() => {
|
|
479
|
+
const m = new Map();
|
|
480
|
+
for (let i = 0; i < B64_URL.length; i++)
|
|
481
|
+
m.set(B64_URL[i], i);
|
|
482
|
+
return m;
|
|
483
|
+
})();
|
|
484
|
+
function toBase64Url(bytes) {
|
|
485
|
+
let out = "";
|
|
486
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
487
|
+
const a = bytes[i];
|
|
488
|
+
const b = i + 1 < bytes.length ? bytes[i + 1] : 0;
|
|
489
|
+
const c = i + 2 < bytes.length ? bytes[i + 2] : 0;
|
|
490
|
+
const triple = (a << 16) | (b << 8) | c;
|
|
491
|
+
out += B64_URL[(triple >> 18) & 63];
|
|
492
|
+
out += B64_URL[(triple >> 12) & 63];
|
|
493
|
+
if (i + 1 < bytes.length)
|
|
494
|
+
out += B64_URL[(triple >> 6) & 63];
|
|
495
|
+
if (i + 2 < bytes.length)
|
|
496
|
+
out += B64_URL[triple & 63];
|
|
497
|
+
}
|
|
498
|
+
return out;
|
|
499
|
+
}
|
|
500
|
+
function fromBase64Url(str) {
|
|
501
|
+
const out = [];
|
|
502
|
+
for (let i = 0; i < str.length; i += 4) {
|
|
503
|
+
const v0 = B64_INV.get(str[i]);
|
|
504
|
+
const v1 = B64_INV.get(str[i + 1]);
|
|
505
|
+
if (v0 === undefined || v1 === undefined)
|
|
506
|
+
throw new Error("Invalid DNA base64url character");
|
|
507
|
+
const c2 = str[i + 2];
|
|
508
|
+
const c3 = str[i + 3];
|
|
509
|
+
const v2 = c2 !== undefined ? B64_INV.get(c2) : 0;
|
|
510
|
+
const v3 = c3 !== undefined ? B64_INV.get(c3) : 0;
|
|
511
|
+
if ((c2 !== undefined && v2 === undefined) || (c3 !== undefined && v3 === undefined)) {
|
|
512
|
+
throw new Error("Invalid DNA base64url character");
|
|
513
|
+
}
|
|
514
|
+
if (c2 === undefined && (v1 & 0x0f) !== 0) {
|
|
515
|
+
throw new Error("Invalid DNA non-canonical padding bits");
|
|
516
|
+
}
|
|
517
|
+
if (c2 !== undefined && c3 === undefined && ((v2 ?? 0) & 0x03) !== 0) {
|
|
518
|
+
throw new Error("Invalid DNA non-canonical padding bits");
|
|
519
|
+
}
|
|
520
|
+
const triple = (v0 << 18) | (v1 << 12) | ((v2 ?? 0) << 6) | (v3 ?? 0);
|
|
521
|
+
out.push((triple >> 16) & 255);
|
|
522
|
+
if (c2 !== undefined)
|
|
523
|
+
out.push((triple >> 8) & 255);
|
|
524
|
+
if (c3 !== undefined)
|
|
525
|
+
out.push(triple & 255);
|
|
526
|
+
}
|
|
527
|
+
return Uint8Array.from(out);
|
|
528
|
+
}
|
|
529
|
+
function fnv1a16(bytes, len) {
|
|
530
|
+
let h = 0x811c9dc5;
|
|
531
|
+
for (let i = 0; i < len; i++) {
|
|
532
|
+
h ^= bytes[i];
|
|
533
|
+
h = Math.imul(h, 0x01000193) >>> 0;
|
|
534
|
+
}
|
|
535
|
+
return ((h >>> 16) ^ (h & 0xffff)) & 0xffff;
|
|
536
|
+
}
|
|
537
|
+
function orderedAllelePairs(g) {
|
|
538
|
+
return [
|
|
539
|
+
g.architecture.stems,
|
|
540
|
+
g.architecture.leader,
|
|
541
|
+
g.architecture.branchAngle,
|
|
542
|
+
g.architecture.branchAngleSpread,
|
|
543
|
+
g.architecture.scaffolds,
|
|
544
|
+
g.architecture.fork,
|
|
545
|
+
g.architecture.forkShare,
|
|
546
|
+
g.architecture.forkSpread,
|
|
547
|
+
g.architecture.forkStems,
|
|
548
|
+
g.architecture.whorl,
|
|
549
|
+
g.architecture.whorlsPerMetre,
|
|
550
|
+
g.architecture.branchiness,
|
|
551
|
+
g.crown.height,
|
|
552
|
+
g.crown.crownRadiusRatio,
|
|
553
|
+
g.crown.crownBaseRatio,
|
|
554
|
+
g.crown.crownPeak,
|
|
555
|
+
g.crown.crownRound,
|
|
556
|
+
g.crown.lobes,
|
|
557
|
+
g.crown.foliageDepth,
|
|
558
|
+
g.crown.openness,
|
|
559
|
+
g.crown.pads,
|
|
560
|
+
g.crown.clusters,
|
|
561
|
+
g.crown.canopyLift,
|
|
562
|
+
g.tropism.bend,
|
|
563
|
+
g.tropism.weep,
|
|
564
|
+
g.tropism.limbCurve,
|
|
565
|
+
g.tropism.limbCurveBack,
|
|
566
|
+
g.tropism.crooked,
|
|
567
|
+
g.tropism.zigzag,
|
|
568
|
+
g.tropism.lean,
|
|
569
|
+
g.tropism.buttress,
|
|
570
|
+
g.tropism.stiffness,
|
|
571
|
+
g.tropism.trunkSlenderness,
|
|
572
|
+
g.foliageBark.clumpSize,
|
|
573
|
+
g.foliageBark.clumpSpread,
|
|
574
|
+
g.foliageBark.leafHue,
|
|
575
|
+
g.foliageBark.leafSat,
|
|
576
|
+
g.foliageBark.leafLight,
|
|
577
|
+
g.foliageBark.barkR,
|
|
578
|
+
g.foliageBark.barkG,
|
|
579
|
+
g.foliageBark.barkB,
|
|
580
|
+
g.foliageBark.barkPale,
|
|
581
|
+
g.foliageBark.leafVariation,
|
|
582
|
+
g.foliageBark.treeVariation,
|
|
583
|
+
g.phenologyVigour.throughR,
|
|
584
|
+
g.phenologyVigour.throughG,
|
|
585
|
+
g.phenologyVigour.throughB,
|
|
586
|
+
g.phenologyVigour.sprayHang,
|
|
587
|
+
g.phenologyVigour.shed,
|
|
588
|
+
g.phenologyVigour.vigour,
|
|
589
|
+
];
|
|
590
|
+
}
|
|
591
|
+
const ALLELE_COUNT = 50;
|
|
592
|
+
const HEADER_BYTES = 11;
|
|
593
|
+
const PAYLOAD_BYTES = HEADER_BYTES + ALLELE_COUNT * 2 + 2;
|
|
594
|
+
/** Encode a `TreeGenome` into a shareable `DNA1-...` string with FNV-1a checksum. */
|
|
595
|
+
export function encodeDna(g) {
|
|
596
|
+
const bytes = new Uint8Array(PAYLOAD_BYTES);
|
|
597
|
+
const barkIdx = Math.max(0, BARK_SETS.findIndex((b) => b.id === g.foliageBark.barkId));
|
|
598
|
+
const leavesIdx = Math.max(0, LEAF_SETS.findIndex((l) => l.id === g.foliageBark.leavesId));
|
|
599
|
+
const sprayIdx = Math.max(0, SPRAY_KINDS.indexOf(g.foliageBark.spray));
|
|
600
|
+
bytes[0] = 1;
|
|
601
|
+
bytes[1] = clamp(g.generation, 0, 255);
|
|
602
|
+
bytes[2] = (g.seed >> 8) & 255;
|
|
603
|
+
bytes[3] = g.seed & 255;
|
|
604
|
+
bytes[4] = Math.max(0, ARCH_LIST.indexOf(g.architecture.architecture));
|
|
605
|
+
bytes[5] = Math.max(0, SHAPE_LIST.indexOf(g.crown.shape));
|
|
606
|
+
bytes[6] = Math.max(0, LEAF_KIND_ROWS.indexOf(g.foliageBark.leafKind));
|
|
607
|
+
bytes[7] = (Math.max(0, PHYLLO_LIST.indexOf(g.architecture.phyllotaxis)) << 2) | Math.max(0, PALM_LEAF_LIST.indexOf(g.architecture.palmLeaf));
|
|
608
|
+
bytes[8] = barkIdx & 255;
|
|
609
|
+
bytes[9] = leavesIdx & 255;
|
|
610
|
+
bytes[10] = sprayIdx & 255;
|
|
611
|
+
const pairs = orderedAllelePairs(g);
|
|
612
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
613
|
+
bytes[HEADER_BYTES + i * 2] = Math.round(clamp01(pairs[i][0]) * 255);
|
|
614
|
+
bytes[HEADER_BYTES + i * 2 + 1] = Math.round(clamp01(pairs[i][1]) * 255);
|
|
615
|
+
}
|
|
616
|
+
const csum = fnv1a16(bytes, PAYLOAD_BYTES - 2);
|
|
617
|
+
bytes[PAYLOAD_BYTES - 2] = (csum >> 8) & 255;
|
|
618
|
+
bytes[PAYLOAD_BYTES - 1] = csum & 255;
|
|
619
|
+
return `DNA1-${toBase64Url(bytes)}`;
|
|
620
|
+
}
|
|
621
|
+
/** Decode and verify a `DNA1-...` string back into a `TreeGenome`. */
|
|
622
|
+
export function decodeDna(code) {
|
|
623
|
+
const clean = code.trim();
|
|
624
|
+
if (!clean.startsWith("DNA1-"))
|
|
625
|
+
throw new Error("DNA string must start with DNA1-");
|
|
626
|
+
const bytes = fromBase64Url(clean.slice(5));
|
|
627
|
+
if (bytes.length !== PAYLOAD_BYTES || bytes[0] !== 1) {
|
|
628
|
+
throw new Error(`Unsupported or corrupted DNA payload (length ${bytes.length}, expected ${PAYLOAD_BYTES})`);
|
|
629
|
+
}
|
|
630
|
+
const expected = fnv1a16(bytes, PAYLOAD_BYTES - 2);
|
|
631
|
+
const actual = (bytes[PAYLOAD_BYTES - 2] << 8) | bytes[PAYLOAD_BYTES - 1];
|
|
632
|
+
if (expected !== actual) {
|
|
633
|
+
throw new Error("DNA checksum mismatch (corrupted cultivar code)");
|
|
634
|
+
}
|
|
635
|
+
const pairAt = (idx) => [
|
|
636
|
+
bytes[HEADER_BYTES + idx * 2] / 255,
|
|
637
|
+
bytes[HEADER_BYTES + idx * 2 + 1] / 255,
|
|
638
|
+
];
|
|
639
|
+
const arch = ARCH_LIST[bytes[4]] ?? "broadleaf";
|
|
640
|
+
const shape = SHAPE_LIST[bytes[5]] ?? "hemispherical";
|
|
641
|
+
const leafKind = LEAF_KIND_ROWS[bytes[6]] ?? "broad";
|
|
642
|
+
const phyllo = PHYLLO_LIST[(bytes[7] >> 2) & 3] ?? "spiral";
|
|
643
|
+
const palmLeaf = PALM_LEAF_LIST[bytes[7] & 3] ?? "feather";
|
|
644
|
+
const barkId = BARK_SETS[bytes[8]]?.id ?? BARK_SETS[0].id;
|
|
645
|
+
const leavesId = LEAF_SETS[bytes[9]]?.id ?? LEAF_SETS[0].id;
|
|
646
|
+
const spray = SPRAY_KINDS[bytes[10]] ?? "alternate";
|
|
647
|
+
return {
|
|
648
|
+
version: 1,
|
|
649
|
+
generation: bytes[1],
|
|
650
|
+
seed: ((bytes[2] << 8) | bytes[3]) || 1,
|
|
651
|
+
architecture: {
|
|
652
|
+
architecture: arch,
|
|
653
|
+
phyllotaxis: phyllo,
|
|
654
|
+
palmLeaf,
|
|
655
|
+
stems: pairAt(0),
|
|
656
|
+
leader: pairAt(1),
|
|
657
|
+
branchAngle: pairAt(2),
|
|
658
|
+
branchAngleSpread: pairAt(3),
|
|
659
|
+
scaffolds: pairAt(4),
|
|
660
|
+
fork: pairAt(5),
|
|
661
|
+
forkShare: pairAt(6),
|
|
662
|
+
forkSpread: pairAt(7),
|
|
663
|
+
forkStems: pairAt(8),
|
|
664
|
+
whorl: pairAt(9),
|
|
665
|
+
whorlsPerMetre: pairAt(10),
|
|
666
|
+
branchiness: pairAt(11),
|
|
667
|
+
},
|
|
668
|
+
crown: {
|
|
669
|
+
shape,
|
|
670
|
+
height: pairAt(12),
|
|
671
|
+
crownRadiusRatio: pairAt(13),
|
|
672
|
+
crownBaseRatio: pairAt(14),
|
|
673
|
+
crownPeak: pairAt(15),
|
|
674
|
+
crownRound: pairAt(16),
|
|
675
|
+
lobes: pairAt(17),
|
|
676
|
+
foliageDepth: pairAt(18),
|
|
677
|
+
openness: pairAt(19),
|
|
678
|
+
pads: pairAt(20),
|
|
679
|
+
clusters: pairAt(21),
|
|
680
|
+
canopyLift: pairAt(22),
|
|
681
|
+
},
|
|
682
|
+
tropism: {
|
|
683
|
+
bend: pairAt(23),
|
|
684
|
+
weep: pairAt(24),
|
|
685
|
+
limbCurve: pairAt(25),
|
|
686
|
+
limbCurveBack: pairAt(26),
|
|
687
|
+
crooked: pairAt(27),
|
|
688
|
+
zigzag: pairAt(28),
|
|
689
|
+
lean: pairAt(29),
|
|
690
|
+
buttress: pairAt(30),
|
|
691
|
+
stiffness: pairAt(31),
|
|
692
|
+
trunkSlenderness: pairAt(32),
|
|
693
|
+
},
|
|
694
|
+
foliageBark: {
|
|
695
|
+
leafKind,
|
|
696
|
+
barkId,
|
|
697
|
+
leavesId,
|
|
698
|
+
spray,
|
|
699
|
+
clumpSize: pairAt(33),
|
|
700
|
+
clumpSpread: pairAt(34),
|
|
701
|
+
leafHue: pairAt(35),
|
|
702
|
+
leafSat: pairAt(36),
|
|
703
|
+
leafLight: pairAt(37),
|
|
704
|
+
barkR: pairAt(38),
|
|
705
|
+
barkG: pairAt(39),
|
|
706
|
+
barkB: pairAt(40),
|
|
707
|
+
barkPale: pairAt(41),
|
|
708
|
+
leafVariation: pairAt(42),
|
|
709
|
+
treeVariation: pairAt(43),
|
|
710
|
+
},
|
|
711
|
+
phenologyVigour: {
|
|
712
|
+
throughR: pairAt(44),
|
|
713
|
+
throughG: pairAt(45),
|
|
714
|
+
throughB: pairAt(46),
|
|
715
|
+
sprayHang: pairAt(47),
|
|
716
|
+
shed: pairAt(48),
|
|
717
|
+
vigour: pairAt(49),
|
|
718
|
+
},
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
//# sourceMappingURL=genetics.js.map
|