mirror of
https://github.com/IliesAllali/parenthese.git
synced 2026-11-18 16:52:11 +00:00
Merge pull request #83 from IliesAllali/fix/renvoi-fil-libre
Filiations : fil plein quand il ne croise pas une autre famille, un seul tronc par fratrie
This commit is contained in:
5 files changed
+160
-47
No files matched your search
@@ -1,5 +1,4 @@
|
||||
import { computeLayout, componentOrderPerGen, chainGap } from './elkLayout'
|
||||
import { RENVOI_MIN_DX } from './constants'
|
||||
|
||||
// ============================================================
|
||||
// Layout par blocs familiaux (Reingold-Tilford adapté aux couples)
|
||||
@@ -27,6 +26,18 @@ import { RENVOI_MIN_DX } from './constants'
|
||||
const NODE_SPACING = 30
|
||||
const FAMILY_GAP = 60
|
||||
|
||||
// Le fil d'une filiation union → enfant : une personne d'une autre famille, sur la ligne de l'enfant,
|
||||
// entre l'aplomb de l'union et l'enfant, est croisée. Frères et sœurs et conjoint(e)s de l'enfant ne comptent pas.
|
||||
export function filCroiseAutreFamille(nodes, childrenOfUnion, spouseKeysOf, u, c) {
|
||||
const byIdLocal = new Map(nodes.map(n => [n.id, n]))
|
||||
const kid = byIdLocal.get(c)
|
||||
const cx = (id) => byIdLocal.get(id).x + byIdLocal.get(id).width / 2
|
||||
const excluded = new Set([c, ...(childrenOfUnion.get(u) || []), ...(spouseKeysOf.get(c) || [])])
|
||||
const lo = Math.min(cx(u), cx(c)), hi = Math.max(cx(u), cx(c))
|
||||
return nodes.some(n => n._type === 'person' && n._generation === kid._generation && !excluded.has(n.id)
|
||||
&& n.x + n.width > lo && n.x < hi)
|
||||
}
|
||||
|
||||
export function computeBlockLayout(graphData = null) {
|
||||
const result = computeLayout(graphData)
|
||||
const nodes = result.children
|
||||
@@ -209,13 +220,25 @@ export function computeBlockLayout(graphData = null) {
|
||||
for (const n of nodes) if (pos.has(n.id)) minX = Math.min(minX, pos.get(n.id))
|
||||
for (const n of nodes) if (pos.has(n.id)) n.x = pos.get(n.id) - minX
|
||||
|
||||
// Arête longue vers un enfant placé sous l'autre lignée : dessinée en renvoi (voir drawFiliations)
|
||||
// Plusieurs renvois sous la même union : pastilles empilées (_renvoiIndex)
|
||||
const centerX = (id) => byId.get(id).x + byId.get(id).width / 2
|
||||
// Arête vers un enfant placé sous l'autre lignée : renvoi (voir drawFiliations) seulement si le fil
|
||||
// croise une autre famille. Plusieurs renvois sous la même union : pastilles empilées (_renvoiIndex)
|
||||
// Le fil descend de l'union puis file sur la ligne de la fratrie jusqu'à l'enfant (traceBus) :
|
||||
// il ne traverse une autre famille que si une personne de cette famille se trouve sur cette ligne,
|
||||
// entre l'aplomb de l'union et l'enfant. Frères et sœurs et conjoint(e)s de l'enfant ne comptent pas.
|
||||
const spouseKeysOf = new Map()
|
||||
for (const n of nodes) {
|
||||
if (n._type !== 'union' || !n._data?.p1Key || !n._data?.p2Key) continue
|
||||
const { p1Key, p2Key } = n._data
|
||||
if (!spouseKeysOf.has(p1Key)) spouseKeysOf.set(p1Key, new Set())
|
||||
if (!spouseKeysOf.has(p2Key)) spouseKeysOf.set(p2Key, new Set())
|
||||
spouseKeysOf.get(p1Key).add(p2Key)
|
||||
spouseKeysOf.get(p2Key).add(p1Key)
|
||||
}
|
||||
const crossesOtherFamily = (u, c) => filCroiseAutreFamille(nodes, childrenOfUnion, spouseKeysOf, u, c)
|
||||
const renvoisByUnion = new Map()
|
||||
for (const e of result.edges) {
|
||||
const u = e.sources[0], c = e.targets[0]
|
||||
if (!externalEdges.has(`${u}>${c}`) || Math.abs(centerX(u) - centerX(c)) <= RENVOI_MIN_DX) continue
|
||||
if (!externalEdges.has(`${u}>${c}`) || !crossesOtherFamily(u, c)) continue
|
||||
e._renvoi = true
|
||||
e._renvoiIndex = renvoisByUnion.get(u) || 0
|
||||
renvoisByUnion.set(u, e._renvoiIndex + 1)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { computeBlockLayout } from './blockLayout'
|
||||
import { computeBlockLayout, filCroiseAutreFamille } from './blockLayout'
|
||||
import { COMPACT_W, PERSON_W } from './constants'
|
||||
|
||||
const person = (id) => ({ id, firstName: id, lastName: '' })
|
||||
@@ -60,3 +60,50 @@ describe('computeBlockLayout, ordre de la fratrie', () => {
|
||||
expect(order(graphWith([1, 1, 1]))).toEqual(['a', 'b', 'c'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('computeBlockLayout, renvoi seulement si le fil croise une autre famille', () => {
|
||||
// Structure type : l'enfant (P) a sa fratrie à côté, son conjoint (Z) est dans la lignée de l'autre parent
|
||||
// Jacqueline + Roger ont 6 enfants, dont Marie, en couple avec Philippe ; Marcel + Yvette ont 5 enfants, dont Philippe
|
||||
const graph = {
|
||||
persons: ['jac', 'rog', 'marie', 'philippe', 'anais', 'greg', 'marcel', 'yvette', 'severine', 'isabelle', 'laurent', 'stephane', 'k1', 'k2', 'k3', 'k4', 'k5'].map(person),
|
||||
unions: [union('uJR', 'jac', 'rog'), union('uMP', 'marie', 'philippe'), union('uMY', 'marcel', 'yvette')],
|
||||
filiations: [
|
||||
...['k1', 'k2', 'k3', 'k4', 'k5'].map((k, i) => fil(`f${k}`, k, 'uJR', i + 1)), fil('fmarie', 'marie', 'uJR', 6),
|
||||
fil('fa', 'anais', 'uMP'), fil('fg', 'greg', 'uMP'),
|
||||
fil('fs', 'severine', 'uMY', 1), fil('fi', 'isabelle', 'uMY', 2), fil('fl', 'laurent', 'uMY', 3),
|
||||
fil('fst', 'stephane', 'uMY', 4), fil('fp', 'philippe', 'uMY', 5),
|
||||
],
|
||||
}
|
||||
|
||||
it('garde le fil vers Philippe quand la fratrie de son autre famille est à côté de lui', () => {
|
||||
const { edges } = computeBlockLayout(graph)
|
||||
expect(edges.find(e => e.sources[0] === 'u-uMY' && e.targets[0] === 'p-philippe')._renvoi).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('filCroiseAutreFamille', () => {
|
||||
// Ligne d'une génération : union u (aplomb x=0 à 120), enfant c en x=600
|
||||
const at = (id, x, type = 'person', gen = 1) => ({ id, x, width: 120, _type: type, _generation: gen })
|
||||
const base = () => [at('u', 0, 'union', 0), at('c', 600), at('sib', 300), at('spouse', 700), at('other', 400)]
|
||||
const childrenOfUnion = new Map([['u', ['c', 'sib']]])
|
||||
const spouseKeysOf = new Map([['c', new Set(['spouse'])]])
|
||||
|
||||
it('croise quand une personne d\'une autre famille est entre l\'union et l\'enfant', () => {
|
||||
expect(filCroiseAutreFamille(base(), childrenOfUnion, spouseKeysOf, 'u', 'c')).toBe(true)
|
||||
})
|
||||
|
||||
it('ne croise pas quand seuls les frères et sœurs sont entre l\'union et l\'enfant', () => {
|
||||
const nodes = base().filter(n => n.id !== 'other')
|
||||
expect(filCroiseAutreFamille(nodes, childrenOfUnion, spouseKeysOf, 'u', 'c')).toBe(false)
|
||||
})
|
||||
|
||||
it('ne croise pas pour le conjoint de l\'enfant placé du côté de l\'enfant', () => {
|
||||
const nodes = base().filter(n => n.id !== 'other').map(n => n.id === 'spouse' ? { ...n, x: 300 } : n)
|
||||
expect(filCroiseAutreFamille(nodes, childrenOfUnion, spouseKeysOf, 'u', 'c')).toBe(false)
|
||||
})
|
||||
|
||||
it('ignore une personne d\'une autre génération sur la même abscisse', () => {
|
||||
const nodes = base().filter(n => n.id !== 'other').concat(at('x', 400, 'person', 2))
|
||||
expect(filCroiseAutreFamille(nodes, childrenOfUnion, spouseKeysOf, 'u', 'c')).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -16,8 +16,6 @@ export const COMPACT_SCALE = 0.6
|
||||
export const COMPACT_R = PERSON_R * COMPACT_SCALE
|
||||
export const COMPACT_W = 40
|
||||
export const COMPACT_JOIN_GAP = -18
|
||||
// Filiation qui traverse l'arbre (enfant placé sous l'autre lignée) : renvoi cliquable au lieu du trait
|
||||
export const RENVOI_MIN_DX = 360
|
||||
export const MAX_ORBIT_MEDIAS = 3
|
||||
export const ORBIT_RADIUS = 55
|
||||
export const ORBIT_MEDIA_SIZE = 22
|
||||
|
||||
@@ -95,15 +95,14 @@ function computeCouplePoints(p1Pos, p2Pos, unionPos, p1R, p2R, nodeRandomData, s
|
||||
// Fil : terracotta clair, union en rond terracotta (le rond du logo).
|
||||
// Filiation en accolade souple : l'union descend vers une ligne
|
||||
// commune à la fratrie, chaque enfant s'en détache par une courbe.
|
||||
// Les virages sont des quadratiques larges, pas des coins.
|
||||
// Coins arrondis de rayon fixe (accolade franche, forme A).
|
||||
// ============================================================
|
||||
export const FIL = { color: '#E3A590', width: 1.1, dot: '#D2694A', dotR: 2.6 }
|
||||
// Ligne de la fratrie : sous les prénoms et années des parents, au-dessus des enfants
|
||||
const BUS_DY = 100
|
||||
// Rayon horizontal des virages (plafonné à la moitié de l'écart union → enfant)
|
||||
const BUS_TURN = 56
|
||||
// En dessous de cet écart union → enfant, pas de virage
|
||||
const BUS_STRAIGHT_DX = 30
|
||||
// Accolade : coins arrondis de rayon fixe (forme A, choisie par Patch), écart minimal en dessous duquel l'enfant descend droit
|
||||
const SIB_R = 24
|
||||
const SIB_STRAIGHT_DX = 12
|
||||
|
||||
function busY(sourceId, sy) {
|
||||
let h = 0
|
||||
@@ -112,32 +111,29 @@ function busY(sourceId, sy) {
|
||||
return sy + BUS_DY + (Math.abs(h) % 3) * 4
|
||||
}
|
||||
|
||||
function traceBus(ctx, sx, sy, by, ex, ey) {
|
||||
const dx = ex - sx
|
||||
// Une union, toute sa fratrie : un seul tracé. Tronc depuis l'union jusqu'à la barre, coins arrondis
|
||||
// de même rayon à chaque jonction, puis descente vers chaque enfant. Chaque enfant a sa propre trajectoire
|
||||
// (sur la même barre), donc le surlignage du chemin suit exactement la même géométrie.
|
||||
export function traceSibship(ctx, sourceId, sourcePos, items) {
|
||||
const sx = sourcePos.cx, sy = sourcePos.cy
|
||||
const by = busY(sourceId, sy)
|
||||
const rr = Math.min(SIB_R, by - sy - 2)
|
||||
ctx.beginPath()
|
||||
ctx.moveTo(sx, sy)
|
||||
// Enfant presque sous l'union (enfant unique, décalage aléatoire de quelques px) : trait direct, pas de S
|
||||
if (Math.abs(dx) < BUS_STRAIGHT_DX) { ctx.lineTo(ex, ey); ctx.stroke(); return }
|
||||
const dir = Math.sign(dx)
|
||||
const turn = Math.min(BUS_TURN, Math.abs(dx) / 2)
|
||||
// Descente de l'union, virage large vers la ligne de la fratrie
|
||||
const drop = Math.min(by - sy, 64)
|
||||
ctx.lineTo(sx, by - drop)
|
||||
ctx.quadraticCurveTo(sx, by, sx + dir * turn, by)
|
||||
// Ligne de la fratrie, puis courbe jusqu'au portrait de l'enfant
|
||||
ctx.lineTo(ex - dir * turn, by)
|
||||
ctx.quadraticCurveTo(ex, by, ex, ey)
|
||||
for (const { cx: ex, cy: ey } of items) {
|
||||
const dx = ex - sx
|
||||
ctx.moveTo(sx, sy)
|
||||
if (Math.abs(dx) < SIB_STRAIGHT_DX) { ctx.lineTo(sx, by); ctx.lineTo(ex, by); ctx.lineTo(ex, ey); continue }
|
||||
const dir = Math.sign(dx)
|
||||
const k = Math.min(rr, Math.abs(dx) / 2)
|
||||
ctx.lineTo(sx, by - k)
|
||||
ctx.arcTo(sx, by, sx + dir * k, by, k)
|
||||
ctx.lineTo(ex - dir * k, by)
|
||||
ctx.arcTo(ex, by, ex, ey, k)
|
||||
ctx.lineTo(ex, ey)
|
||||
}
|
||||
ctx.stroke()
|
||||
}
|
||||
|
||||
// Trace une filiation union → enfant. progress < 1 : entrée en fondu
|
||||
function traceFiliation(ctx, sourceId, sourcePos, targetPos, childR, progress) {
|
||||
const a = ctx.globalAlpha
|
||||
ctx.globalAlpha = a * progress
|
||||
traceBus(ctx, sourcePos.cx, sourcePos.cy, busY(sourceId, sourcePos.cy), targetPos.cx, targetPos.cy - childR)
|
||||
ctx.globalAlpha = a
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Courbes de couple (Bézier)
|
||||
// ============================================================
|
||||
@@ -255,6 +251,7 @@ export function drawFiliations(ctx, edges, posMap, nodeMap, nodeRandomData, entr
|
||||
renvoiHitAreas.length = 0
|
||||
renvoiPills.length = 0
|
||||
if (!edges) return
|
||||
const sibships = new Map()
|
||||
|
||||
edges.forEach(edge => {
|
||||
const sourceId = edge.sources[0]
|
||||
@@ -275,7 +272,22 @@ export function drawFiliations(ctx, edges, posMap, nodeMap, nodeRandomData, entr
|
||||
|
||||
const targetNode = nodeMap.get(targetId)
|
||||
const pType = edge._parentageType || 'biologique'
|
||||
const childR = nodeRadius(targetNode)
|
||||
if (edge._renvoi) {
|
||||
ctx.globalAlpha = Math.min(1, edgeProgress * 1.5)
|
||||
drawRenvoi(ctx, edge, sourcePos, targetPos, targetNode, childR, view)
|
||||
ctx.globalAlpha = 1
|
||||
return
|
||||
}
|
||||
// Les enfants d'une même union (même type de filiation) forment une seule fratrie, tracée d'un coup
|
||||
const key = `${sourceId}|${pType}`
|
||||
if (!sibships.has(key)) sibships.set(key, { sourceId, sourcePos, pType, items: [], progress: 0 })
|
||||
const sib = sibships.get(key)
|
||||
sib.items.push({ cx: targetPos.cx, cy: targetPos.cy - childR })
|
||||
sib.progress += edgeProgress
|
||||
})
|
||||
|
||||
for (const { sourceId, sourcePos, pType, items, progress } of sibships.values()) {
|
||||
if (pType === 'adoption') {
|
||||
ctx.setLineDash([8, 5])
|
||||
ctx.strokeStyle = FIL.color
|
||||
@@ -288,19 +300,12 @@ export function drawFiliations(ctx, edges, posMap, nodeMap, nodeRandomData, entr
|
||||
}
|
||||
ctx.lineWidth = FIL.width
|
||||
ctx.lineCap = 'round'
|
||||
ctx.globalAlpha = Math.min(1, edgeProgress * 1.5)
|
||||
|
||||
const childR = nodeRadius(targetNode)
|
||||
if (edge._renvoi) {
|
||||
drawRenvoi(ctx, edge, sourcePos, targetPos, targetNode, childR, view)
|
||||
ctx.globalAlpha = 1
|
||||
ctx.setLineDash([])
|
||||
return
|
||||
}
|
||||
traceFiliation(ctx, sourceId, sourcePos, targetPos, childR, edgeProgress)
|
||||
const p = progress / items.length
|
||||
ctx.globalAlpha = Math.min(1, p * 1.5) * p
|
||||
traceSibship(ctx, sourceId, sourcePos, items)
|
||||
ctx.globalAlpha = 1
|
||||
ctx.setLineDash([])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
@@ -373,7 +378,9 @@ export function drawPathHighlight(ctx, {
|
||||
ctx.lineCap = 'round'
|
||||
ctx.globalAlpha = 1
|
||||
|
||||
traceFiliation(ctx, edge.sources[0], sourcePos, targetPos, childR, hp)
|
||||
ctx.globalAlpha = hp
|
||||
traceSibship(ctx, edge.sources[0], sourcePos, [{ cx: targetPos.cx, cy: targetPos.cy - childR }])
|
||||
ctx.globalAlpha = 1
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { traceSibship } from './renderers'
|
||||
|
||||
// Contexte canvas minimal qui enregistre les appels de tracé
|
||||
const recorder = () => {
|
||||
const calls = []
|
||||
const ctx = new Proxy({}, { get: (_, name) => (...args) => { calls.push([name, ...args]) } })
|
||||
return { ctx, calls, count: (n) => calls.filter(c => c[0] === n).length }
|
||||
}
|
||||
const src = { cx: 500, cy: 300 }
|
||||
|
||||
describe('traceSibship (accolade franche)', () => {
|
||||
it('trace toute la fratrie en un seul chemin et un seul stroke', () => {
|
||||
const { ctx, count } = recorder()
|
||||
traceSibship(ctx, 'u-1', src, [{ cx: 200, cy: 480 }, { cx: 420, cy: 480 }, { cx: 800, cy: 480 }])
|
||||
expect(count('beginPath')).toBe(1)
|
||||
expect(count('stroke')).toBe(1)
|
||||
expect(count('moveTo')).toBe(3)
|
||||
})
|
||||
|
||||
it('un enfant seul rejoint la barre avec la même géométrie que dans la fratrie', () => {
|
||||
const child = { cx: 800, cy: 480 }
|
||||
const solo = recorder()
|
||||
traceSibship(solo.ctx, 'u-1', src, [child])
|
||||
const group = recorder()
|
||||
traceSibship(group.ctx, 'u-1', src, [child, { cx: 200, cy: 480 }])
|
||||
const soloArcs = solo.calls.filter(c => c[0] === 'arcTo')
|
||||
const groupArcs = group.calls.filter(c => c[0] === 'arcTo').slice(0, 2)
|
||||
expect(soloArcs.length).toBe(2)
|
||||
expect(groupArcs).toEqual(soloArcs)
|
||||
})
|
||||
|
||||
it('un enfant à l\'aplomb de l\'union descend droit, sans virage', () => {
|
||||
const { ctx, count } = recorder()
|
||||
traceSibship(ctx, 'u-1', src, [{ cx: 505, cy: 480 }])
|
||||
expect(count('arcTo')).toBe(0)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user