@euphrasiologist/lwphylo 1.2.5 → 1.2.6

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.
@@ -334,6 +334,112 @@ function getChildArcs(pd) {
334
334
  return arcs;
335
335
  }
336
336
 
337
+ // fanAngles.js
338
+ const TAU = Math.PI * 2;
339
+ const norm = (t) => ((t % TAU) + TAU) % TAU;
340
+
341
+ // Unwrap angles around a reference so they sit within [ref-π, ref+π]
342
+ function unwrapAround(ref, a) {
343
+ let x = a;
344
+ while (x < ref - Math.PI) x += TAU;
345
+ while (x > ref + Math.PI) x -= TAU;
346
+ return x;
347
+ }
348
+
349
+ /**
350
+ * Compute APE "fan" compatible angles:
351
+ * - Tips evenly spaced over [0, span] where span = 2π*(1 - 1/Ntip) - gap
352
+ * - Then + rotate (radians)
353
+ * - Internal nodes = arithmetic mean of child angles (unwrapped)
354
+ *
355
+ * pd: fortified nodes array (has thisId, parentId, children[])
356
+ * opts: { openAngleDeg=0, rotateDeg=0 }
357
+ * returns: Map(nodeId -> angle)
358
+ */
359
+ function fanAngles(pd, opts = {}) {
360
+ const { openAngleDeg = 0, rotateDeg = 0 } = opts;
361
+ const gap = (openAngleDeg / 360) * TAU;
362
+ const rotate = (rotateDeg / 360) * TAU;
363
+
364
+ // Find root and collect tips in cladewise/DFS order
365
+ let root = null;
366
+ const kids = new Map(pd.map(d => [d.thisId, d.children || []]));
367
+ for (const d of pd) if (d.parentId == null) { root = d.thisId; break; }
368
+
369
+ const tipIds = [];
370
+ (function dfs(id) {
371
+ const c = kids.get(id) || [];
372
+ if (!c.length) { tipIds.push(id); return; }
373
+ for (const ch of c) dfs(ch);
374
+ })(root);
375
+
376
+ const N = Math.max(1, tipIds.length);
377
+ // APE: 0 .. 2π*(1 - 1/N) - gap, length.out=N (no last step overlap)
378
+ const maxA = TAU * (1 - 1 / N) - gap;
379
+ const step = N > 1 ? maxA / (N - 1) : 0;
380
+
381
+ const angle = new Map();
382
+ tipIds.forEach((id, i) => {
383
+ angle.set(id, norm(i * step + rotate));
384
+ });
385
+
386
+ // Internal nodes: arithmetic mean of child angles (unwrapped)
387
+ (function setInternal(id) {
388
+ const c = kids.get(id) || [];
389
+ for (const ch of c) setInternal(ch);
390
+ if (c.length > 0) {
391
+ // unwrap child angles around the first child's angle
392
+ const a0 = angle.get(c[0]);
393
+ const unwrapped = c.map(ch => unwrapAround(a0, angle.get(ch)));
394
+ const mean = unwrapped.reduce((s, v) => s + v, 0) / unwrapped.length;
395
+ angle.set(id, norm(mean));
396
+ }
397
+ })(root);
398
+
399
+ return angle;
400
+ }
401
+
402
+ // getArcsFan.js
403
+
404
+ /**
405
+ * Build arcs like APE's circular.plot:
406
+ * For each internal parent, draw a single arc at radius=parent.r
407
+ * going from first child's angle to last child's angle in child order.
408
+ * If last < first (wrap), we draw CW (decreasing) to stay on the block.
409
+ *
410
+ * pd: array with { thisId, parentId, r, children[], angle }
411
+ * returns: [{ parentId, thisId, radius, start, end, sweep }]
412
+ * where sweep=0 means CCW (start→end increasing),
413
+ * sweep=1 means CW (start→end decreasing across wrap).
414
+ */
415
+ function getArcsFan(pd) {
416
+ const byId = new Map(pd.map(d => [d.thisId, d]));
417
+ const arcs = [];
418
+
419
+ for (const p of pd) {
420
+ const c = p.children || [];
421
+ if (c.length < 2) continue;
422
+ const A = c.map(id => byId.get(id)?.angle).filter(a => a != null);
423
+ if (A.length < 2 || !isFinite(p.r) || p.r <= 0) continue;
424
+
425
+ // Children are contiguous in tip order; take first and last
426
+ let start = A[0];
427
+ let end = A[A.length - 1];
428
+
429
+ // Decide direction like APE’s seq(start, end):
430
+ // if end >= start → CCW; else CW across wrap.
431
+ const sweep = end >= start ? 0 : 1;
432
+
433
+ arcs.push({
434
+ parentId: p.parentId,
435
+ thisId: p.thisId,
436
+ radius: p.r,
437
+ start, end, sweep
438
+ });
439
+ }
440
+ return arcs;
441
+ }
442
+
337
443
  /**
338
444
  * Simple wrapper for radial layout:
339
445
  * - data: per-node { angle, r, x, y, ... }
@@ -341,12 +447,29 @@ function getChildArcs(pd) {
341
447
  * - arcs: per-parent arcs spanning all children at parent's radius
342
448
  * - child_arcs: per-child half-arcs (parent.angle → child.angle) at parent's radius
343
449
  */
344
- function radialLayout(node) {
450
+ function radialLayout(node, opts = {}) {
345
451
  const data = {};
346
452
  data.data = radialData(node);
347
453
  data.radii = getRadii(node);
348
454
  data.arcs = getArcs(data.data);
349
455
  data.child_arcs = getChildArcs(data.data);
456
+
457
+ const pd = fortify(node, true);
458
+ const angleMap = fanAngles(pd, {
459
+ openAngleDeg: opts.openAngleDeg ?? 0,
460
+ rotateDeg: opts.rotateDeg ?? 0
461
+ });
462
+
463
+ // stamp angles + x,y back onto pd (r stays your cumulative edge length)
464
+ for (const d of pd) {
465
+ d.angle = angleMap.get(d.thisId) ?? 0;
466
+ d.x = d.r * Math.cos(d.angle);
467
+ d.y = d.r * Math.sin(d.angle);
468
+ }
469
+
470
+ data.data_pd = pd;
471
+ data.arcs_fan = getArcsFan(pd);
472
+
350
473
  return data;
351
474
  }
352
475