@euphrasiologist/lwphylo 1.2.5 → 1.2.7

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