summaryrefslogtreecommitdiffstats
path: root/source/diagram/diagram_layout.cpp
blob: 4f3a566793999a823d2b257a90a8c8a83f88ccca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
//- geometry & routing helpers

internal f32 diagram_member_world_y(Diagram *d, s32 node_index, s32 member_index)
{
	if(node_index < 0 || node_index >= (s32)d->node_count || !d->nodes[node_index].alive) return 0.0f;
	DiagramNode *n = &d->nodes[node_index];
	if(n->collapsed || member_index < 0) return n->y + n->h * 0.5f;
	f32 y = n->y + n->header_h;
	if(n->note[0]) y += 24.0f;
	for(s32 s = n->first_section; s >= 0; s = d->sections[s].next) {
		NodeSection *sec = &d->sections[s];
		y += 20.0f;
		if(!sec->collapsed) {
			for(s32 m = sec->first_member; m >= 0; m = d->members[m].next) {
				if(m == member_index) return y + 10.0f;
				y += 20.0f;
			}
			y += 4.0f;
		}
	}
	return n->y + n->h * 0.5f;
}

internal Vec2f port_normal(u32 side)
{
	switch(side) {
		case PORT_NORTH: return Vec2f{0.0f, -1.0f};
		case PORT_SOUTH: return Vec2f{0.0f,  1.0f};
		case PORT_EAST:  return Vec2f{1.0f,  0.0f};
		case PORT_WEST:  return Vec2f{-1.0f, 0.0f};
	}
	return Vec2f{1.0f, 0.0f};
}

internal Vec2f node_port_point(Diagram *d, s32 node_index, u32 side, s32 member_index, f32 slot_offset)
{
	DiagramNode *n = &d->nodes[node_index];
	f32 cx = n->x + n->w * 0.5f;
	f32 cy = n->y + n->h * 0.5f;
	if(member_index >= 0 && (side == PORT_EAST || side == PORT_WEST)) {
		f32 my = Clamp(diagram_member_world_y(d, node_index, member_index), n->y + 12.0f, n->y + n->h - 12.0f);
		return Vec2f{(side == PORT_EAST) ? (n->x + n->w) : n->x, my};
	}
	f32 ox = Clamp(slot_offset, -n->w * 0.35f, n->w * 0.35f);
	f32 oy = Clamp(slot_offset, -n->h * 0.35f, n->h * 0.35f);
	switch(side) {
		case PORT_NORTH: return Vec2f{cx + ox, n->y};
		case PORT_SOUTH: return Vec2f{cx + ox, n->y + n->h};
		case PORT_EAST:  return Vec2f{n->x + n->w, cy + oy};
		case PORT_WEST:  return Vec2f{n->x, cy + oy};
	}
	return Vec2f{cx, cy};
}

internal Vec2f rect_clip_ray(DiagramNode *n, f32 tx, f32 ty)
{
	f32 cx = n->x + n->w * 0.5f;
	f32 cy = n->y + n->h * 0.5f;
	f32 dx = tx - cx;
	f32 dy = ty - cy;
	if(fabsf(dx) < 1e-4f && fabsf(dy) < 1e-4f) return Vec2f{cx, cy};
	f32 hw = n->w * 0.5f;
	f32 hh = n->h * 0.5f;
	f32 sx = (fabsf(dx) > 1e-4f) ? (hw / fabsf(dx)) : 1e9f;
	f32 sy = (fabsf(dy) > 1e-4f) ? (hh / fabsf(dy)) : 1e9f;
	f32 s  = Min(sx, sy);
	return Vec2f{cx + dx * s, cy + dy * s};
}

internal void edge_parallel_slot(Diagram *d, s32 edge_index, s32 *out_rank, s32 *out_total)
{
	DiagramEdge *e = &d->edges[edge_index];
	s32 total = 0, rank = 0;
	for(u32 i = 0; i < d->edge_count; ++i) {
		DiagramEdge *o = &d->edges[i];
		if(!o->alive) continue;
		b32 same = (o->src == e->src && o->dst == e->dst) || (o->src == e->dst && o->dst == e->src);
		if(same) {
			if((s32)i == edge_index) rank = total;
			++total;
		}
	}
	*out_rank  = rank;
	*out_total = total;
}

internal b32 diagram_edge_route(Diagram *d, s32 edge_index, EdgePath *out)
{
	MemSetZero(out, sizeof(*out));
	if(edge_index < 0 || edge_index >= (s32)d->edge_count || !d->edges[edge_index].alive) return false;
	DiagramEdge *e = &d->edges[edge_index];
	if(e->src < 0 || e->dst < 0 || !d->nodes[e->src].alive || !d->nodes[e->dst].alive) return false;

	DiagramNode *a = &d->nodes[e->src];
	DiagramNode *b = &d->nodes[e->dst];
	u32 routing = (e->routing == ROUTE_AUTO) ? d->default_routing : e->routing;
	if(routing == ROUTE_AUTO) routing = ROUTE_ORTHOGONAL;
	out->routing = routing;

	//- sequence message: a horizontal arrow at msg_y between the lifelines (or a loop on one)
	if(diagram_edge_is_message(d, edge_index)) {
		f32 y  = e->msg_y;
		f32 ax = node_kind_is_lifeline(a->kind) ? a->x + a->w * 0.5f : 0.0f;
		f32 bx = node_kind_is_lifeline(b->kind) ? b->x + b->w * 0.5f : 0.0f;
		if(!node_kind_is_lifeline(a->kind)) ax = (b->x + b->w * 0.5f >= a->x + a->w * 0.5f) ? a->x + a->w : a->x;
		if(!node_kind_is_lifeline(b->kind)) bx = (a->x + a->w * 0.5f >= b->x + b->w * 0.5f) ? b->x + b->w : b->x;
		out->routing = ROUTE_ORTHOGONAL;
		if(e->src == e->dst) {
			f32 w = 44.0f;
			out->point_count  = 4;
			out->points[0]    = Vec2f{ax + 5.0f, y};
			out->points[1]    = Vec2f{ax + w, y};
			out->points[2]    = Vec2f{ax + w, y + 22.0f};
			out->points[3]    = Vec2f{ax + 5.0f, y + 22.0f};
			out->src_dir      = Vec2f{1.0f, 0.0f};
			out->dst_dir      = Vec2f{-1.0f, 0.0f};
			out->label_pos    = Vec2f{ax + w + 8.0f, y + 11.0f};
			out->src_mult_pos = out->points[0];
			out->dst_mult_pos = out->points[3];
			out->bounds       = Rect{ax - 8.0f, y - 24.0f, ax + w + 200.0f, y + 46.0f};
			return true;
		}
		f32 dir = (bx >= ax) ? 1.0f : -1.0f;
		Vec2f p0 = {ax + dir * 5.0f, y};
		Vec2f p1 = {bx - dir * 5.0f, y};
		out->point_count  = 2;
		out->points[0]    = p0;
		out->points[1]    = p1;
		out->src_dir      = Vec2f{dir, 0.0f};
		out->dst_dir      = Vec2f{dir, 0.0f};
		out->label_pos    = Vec2f{(p0.x + p1.x) * 0.5f, y - 12.0f};
		out->src_mult_pos = Vec2f{p0.x + dir * 14.0f, y + 12.0f};
		out->dst_mult_pos = Vec2f{p1.x - dir * 24.0f, y + 12.0f};
		out->bounds       = Rect{Min(p0.x, p1.x) - 12.0f, y - 30.0f, Max(p0.x, p1.x) + 12.0f, y + 18.0f};
		return true;
	}

	s32 rank = 0, total = 1;
	edge_parallel_slot(d, edge_index, &rank, &total);
	f32 slot_offset = (total > 1) ? ((f32)rank - (f32)(total - 1) * 0.5f) * 18.0f : 0.0f;

	//- self-loop
	if(e->src == e->dst) {
		f32 loop_w = 42.0f + (f32)rank * 16.0f;
		f32 loop_h = 34.0f + (f32)rank * 12.0f;
		Vec2f p0 = {a->x + a->w, a->y + Min(a->h * 0.35f + (f32)rank * 8.0f, a->h - 10.0f)};
		Vec2f p1 = {p0.x + loop_w, p0.y};
		Vec2f p2 = {p0.x + loop_w, a->y - loop_h};
		Vec2f p3 = {a->x + a->w * 0.65f, a->y - loop_h};
		Vec2f p4 = {a->x + a->w * 0.65f, a->y};
		out->routing     = ROUTE_ORTHOGONAL;
		out->point_count = 5;
		out->points[0]   = p0;
		out->points[1]   = p1;
		out->points[2]   = p2;
		out->points[3]   = p3;
		out->points[4]   = p4;
		out->src_dir     = Vec2f{1.0f, 0.0f};
		out->dst_dir     = Vec2f{0.0f, 1.0f};
		out->label_pos   = Vec2f{(p1.x + p2.x) * 0.5f + 8.0f, (p1.y + p2.y) * 0.5f};
		out->src_mult_pos = Vec2f{p0.x + 12.0f, p0.y - 10.0f};
		out->dst_mult_pos = Vec2f{p4.x + 10.0f, p4.y - 12.0f};
		out->bounds       = Rect{a->x, p2.y - 12.0f, p1.x + 12.0f, a->y + a->h};
		return true;
	}

	f32 acx = a->x + a->w * 0.5f, acy = a->y + a->h * 0.5f;
	f32 bcx = b->x + b->w * 0.5f, bcy = b->y + b->h * 0.5f;
	f32 dx  = bcx - acx;
	f32 dy  = bcy - acy;

	// pick auto port sides based on relative placement & member pinning
	u32 src_side = e->src_port;
	u32 dst_side = e->dst_port;
	b32 prefer_h = (e->src_member >= 0 || e->dst_member >= 0) || (fabsf(dx) * 0.85f >= fabsf(dy));
	if(e->waypoint_count > 0) {
		// NOTE: with bends the ends face their nearest bend, not the other node
		Vec2f first = e->waypoints[0];
		Vec2f last  = e->waypoints[e->waypoint_count - 1];
		if(src_side == PORT_AUTO) {
			f32 sx = first.x - acx, sy = first.y - acy;
			b32 h  = (e->src_member >= 0) || fabsf(sx) * a->h >= fabsf(sy) * a->w;
			src_side = h ? ((sx >= 0.0f) ? PORT_EAST : PORT_WEST) : ((sy >= 0.0f) ? PORT_SOUTH : PORT_NORTH);
		}
		if(dst_side == PORT_AUTO) {
			f32 tx = last.x - bcx, ty = last.y - bcy;
			b32 h  = (e->dst_member >= 0) || fabsf(tx) * b->h >= fabsf(ty) * b->w;
			dst_side = h ? ((tx >= 0.0f) ? PORT_EAST : PORT_WEST) : ((ty >= 0.0f) ? PORT_SOUTH : PORT_NORTH);
		}
	}
	if(src_side == PORT_AUTO) {
		if(prefer_h) src_side = (dx >= 0.0f) ? PORT_EAST : PORT_WEST;
		else         src_side = (dy >= 0.0f) ? PORT_SOUTH : PORT_NORTH;
	}
	if(dst_side == PORT_AUTO) {
		if(prefer_h) dst_side = (dx >= 0.0f) ? PORT_WEST : PORT_EAST;
		else         dst_side = (dy >= 0.0f) ? PORT_NORTH : PORT_SOUTH;
	}

	Vec2f p0, p1;
	if(routing == ROUTE_STRAIGHT && e->src_port == PORT_AUTO && e->dst_port == PORT_AUTO && e->src_member < 0 && e->dst_member < 0 && e->waypoint_count == 0) {
		// clean straight ray clipped to boxes (with perpendicular parallel-edge offset)
		f32 len = Max(sqrtf(dx * dx + dy * dy), 1e-4f);
		f32 nx  = -dy / len, ny = dx / len;
		p0 = rect_clip_ray(a, bcx + nx * slot_offset, bcy + ny * slot_offset);
		p1 = rect_clip_ray(b, acx + nx * slot_offset, acy + ny * slot_offset);
		f32 rdx = p1.x - p0.x, rdy = p1.y - p0.y;
		f32 rlen = Max(sqrtf(rdx * rdx + rdy * rdy), 1e-4f);
		out->point_count  = 2;
		out->points[0]    = p0;
		out->points[1]    = p1;
		out->src_dir      = Vec2f{rdx / rlen, rdy / rlen};
		out->dst_dir      = out->src_dir;
		out->label_pos    = Vec2f{(p0.x + p1.x) * 0.5f, (p0.y + p1.y) * 0.5f};
		out->src_mult_pos = Vec2f{p0.x + out->src_dir.x * 18.0f + ny * 10.0f, p0.y + out->src_dir.y * 18.0f - nx * 10.0f};
		out->dst_mult_pos = Vec2f{p1.x - out->dst_dir.x * 22.0f + ny * 10.0f, p1.y - out->dst_dir.y * 22.0f - nx * 10.0f};
	} else if(e->waypoint_count > 0) {
		p0 = node_port_point(d, e->src, src_side, e->src_member, slot_offset);
		p1 = node_port_point(d, e->dst, dst_side, e->dst_member, slot_offset);
		Vec2f via[DIAGRAM_MAX_WAYPOINTS + 2];
		u32   via_count = 0;
		via[via_count++] = p0;
		for(u32 w = 0; w < e->waypoint_count && w < DIAGRAM_MAX_WAYPOINTS; ++w) via[via_count++] = e->waypoints[w];
		via[via_count++] = p1;
		out->point_count = 0;
		if(routing == ROUTE_ORTHOGONAL) {
			// NOTE: manhattan through every bend: each leg gets one elbow, legs alternate between
			// horizontal-first and vertical-first, the first leg leaves along the source port's
			// axis and the last one arrives along the target port's
			b32 horizontal = (src_side == PORT_EAST || src_side == PORT_WEST);
			b32 end_h      = (dst_side == PORT_EAST || dst_side == PORT_WEST);
			Vec2f pts[EDGE_PATH_MAX_POINTS];
			u32   n = 0;
			pts[n++] = via[0];
			for(u32 i = 0; i + 1 < via_count && n + 2 < EDGE_PATH_MAX_POINTS; ++i) {
				Vec2f from = via[i], to = via[i + 1];
				b32   last_leg = (i + 2 == via_count);
				b32   first_h  = horizontal;
				if(last_leg) first_h = !end_h; // arrive along the target's axis
				if(fabsf(from.x - to.x) > 0.5f && fabsf(from.y - to.y) > 0.5f) {
					pts[n++] = first_h ? Vec2f{to.x, from.y} : Vec2f{from.x, to.y};
					horizontal = !first_h; // the leg ended in the other direction
				} else {
					horizontal = fabsf(from.y - to.y) <= 0.5f;
				}
				pts[n++]   = to;
				horizontal = !horizontal; // the next leg turns
			}
			// drop duplicates and collinear middles
			for(u32 i = 0; i < n; ++i) {
				u32 c = out->point_count;
				if(c > 0 && fabsf(pts[i].x - out->points[c - 1].x) < 0.5f && fabsf(pts[i].y - out->points[c - 1].y) < 0.5f) continue;
				if(c >= 2) {
					Vec2f u = out->points[c - 2], v = out->points[c - 1], w = pts[i];
					if((fabsf(u.x - v.x) < 0.5f && fabsf(v.x - w.x) < 0.5f) || (fabsf(u.y - v.y) < 0.5f && fabsf(v.y - w.y) < 0.5f)) {
						out->points[c - 1] = w;
						continue;
					}
				}
				out->points[out->point_count++] = pts[i];
			}
			if(out->point_count < 2) { out->points[0] = p0; out->points[1] = p1; out->point_count = 2; }
			out->routing = ROUTE_ORTHOGONAL;
		} else {
			out->routing = ROUTE_STRAIGHT;
			for(u32 i = 0; i < via_count; ++i) out->points[out->point_count++] = via[i];
		}
		Vec2f s0 = out->points[0], s1 = out->points[1];
		f32   sl = Max(sqrtf((s1.x - s0.x) * (s1.x - s0.x) + (s1.y - s0.y) * (s1.y - s0.y)), 1e-4f);
		out->src_dir = Vec2f{(s1.x - s0.x) / sl, (s1.y - s0.y) / sl};
		Vec2f d0 = out->points[out->point_count - 2], d1 = out->points[out->point_count - 1];
		f32   dl = Max(sqrtf((d1.x - d0.x) * (d1.x - d0.x) + (d1.y - d0.y) * (d1.y - d0.y)), 1e-4f);
		out->dst_dir = Vec2f{(d1.x - d0.x) / dl, (d1.y - d0.y) / dl};
		f32 best_len = -1.0f;
		u32 best_seg = 0;
		for(u32 i = 0; i + 1 < out->point_count; ++i) {
			f32 sl = fabsf(out->points[i + 1].x - out->points[i].x) + fabsf(out->points[i + 1].y - out->points[i].y);
			if(sl > best_len) { best_len = sl; best_seg = i; }
		}
		out->label_pos = Vec2f{(out->points[best_seg].x + out->points[best_seg + 1].x) * 0.5f, (out->points[best_seg].y + out->points[best_seg + 1].y) * 0.5f};
		out->src_mult_pos = Vec2f{p0.x + out->src_dir.x * 18.0f, p0.y + out->src_dir.y * 18.0f - 10.0f};
		out->dst_mult_pos = Vec2f{p1.x - out->dst_dir.x * 22.0f, p1.y - out->dst_dir.y * 22.0f - 10.0f};
	} else if(routing == ROUTE_BEZIER) {
		p0 = node_port_point(d, e->src, src_side, e->src_member, slot_offset);
		p1 = node_port_point(d, e->dst, dst_side, e->dst_member, slot_offset);
		Vec2f sn = port_normal(src_side);
		Vec2f dn = port_normal(dst_side);
		f32   dist = sqrtf((p1.x - p0.x) * (p1.x - p0.x) + (p1.y - p0.y) * (p1.y - p0.y));
		f32   ctrl = Clamp(dist * 0.42f, 36.0f, 180.0f);
		out->point_count  = 2;
		out->points[0]    = p0;
		out->points[1]    = p1;
		out->c0           = Vec2f{p0.x + sn.x * ctrl, p0.y + sn.y * ctrl};
		out->c1           = Vec2f{p1.x + dn.x * ctrl, p1.y + dn.y * ctrl};
		out->src_dir      = sn;
		out->dst_dir      = Vec2f{-dn.x, -dn.y};
		// cubic midpoint at t=0.5: (p0 + 3*c0 + 3*c1 + p1) / 8
		out->label_pos    = Vec2f{(p0.x + 3.0f * out->c0.x + 3.0f * out->c1.x + p1.x) * 0.125f,
		                          (p0.y + 3.0f * out->c0.y + 3.0f * out->c1.y + p1.y) * 0.125f};
		out->src_mult_pos = Vec2f{p0.x + sn.x * 20.0f, p0.y + sn.y * 20.0f - 10.0f};
		out->dst_mult_pos = Vec2f{p1.x + dn.x * 24.0f, p1.y + dn.y * 24.0f - 10.0f};
	} else {
		// ORTHOGONAL (Manhattan) routing with clean port stubs
		p0 = node_port_point(d, e->src, src_side, e->src_member, slot_offset);
		p1 = node_port_point(d, e->dst, dst_side, e->dst_member, slot_offset);
		Vec2f sn   = port_normal(src_side);
		Vec2f dn   = port_normal(dst_side);
		f32   stub = 22.0f;
		Vec2f s0   = {p0.x + sn.x * stub, p0.y + sn.y * stub};
		Vec2f s1   = {p1.x + dn.x * stub, p1.y + dn.y * stub};

		b32 src_h = (src_side == PORT_EAST || src_side == PORT_WEST);
		b32 dst_h = (dst_side == PORT_EAST || dst_side == PORT_WEST);

		Vec2f pts[EDGE_PATH_MAX_POINTS];
		u32   n = 0;
		pts[n++] = p0;
		pts[n++] = s0;

		if(src_h && dst_h) {
			b32 facing = (src_side == PORT_EAST && dst_side == PORT_WEST && s0.x <= s1.x) ||
			             (src_side == PORT_WEST && dst_side == PORT_EAST && s0.x >= s1.x);
			if(facing) {
				f32 mx = (s0.x + s1.x) * 0.5f + slot_offset * 0.5f;
				pts[n++] = Vec2f{mx, s0.y};
				pts[n++] = Vec2f{mx, s1.y};
			} else {
				f32 mid_y = (acy <= bcy) ? (Min(a->y, b->y) - 24.0f - fabsf(slot_offset)) : (Max(a->y + a->h, b->y + b->h) + 24.0f + fabsf(slot_offset));
				pts[n++] = Vec2f{s0.x, mid_y};
				pts[n++] = Vec2f{s1.x, mid_y};
			}
		} else if(!src_h && !dst_h) {
			b32 facing = (src_side == PORT_SOUTH && dst_side == PORT_NORTH && s0.y <= s1.y) ||
			             (src_side == PORT_NORTH && dst_side == PORT_SOUTH && s0.y >= s1.y);
			if(facing) {
				f32 my = (s0.y + s1.y) * 0.5f + slot_offset * 0.5f;
				pts[n++] = Vec2f{s0.x, my};
				pts[n++] = Vec2f{s1.x, my};
			} else {
				f32 mid_x = (acx <= bcx) ? (Max(a->x + a->w, b->x + b->w) + 24.0f + fabsf(slot_offset)) : (Min(a->x, b->x) - 24.0f - fabsf(slot_offset));
				pts[n++] = Vec2f{mid_x, s0.y};
				pts[n++] = Vec2f{mid_x, s1.y};
			}
		} else if(src_h && !dst_h) {
			pts[n++] = Vec2f{s1.x, s0.y};
		} else {
			pts[n++] = Vec2f{s0.x, s1.y};
		}

		pts[n++] = s1;
		pts[n++] = p1;

		// simplify collinear / duplicate consecutive points
		u32 clean = 0;
		for(u32 i = 0; i < n; ++i) {
			if(clean > 0 && fabsf(pts[i].x - out->points[clean - 1].x) < 0.5f && fabsf(pts[i].y - out->points[clean - 1].y) < 0.5f) {
				continue;
			}
			if(clean >= 2) {
				Vec2f u = out->points[clean - 2];
				Vec2f v = out->points[clean - 1];
				Vec2f w = pts[i];
				if((fabsf(u.x - v.x) < 0.5f && fabsf(v.x - w.x) < 0.5f) ||
				   (fabsf(u.y - v.y) < 0.5f && fabsf(v.y - w.y) < 0.5f)) {
					out->points[clean - 1] = w;
					continue;
				}
			}
			out->points[clean++] = pts[i];
		}
		if(clean < 2) {
			out->points[0] = p0;
			out->points[1] = p1;
			clean          = 2;
		}
		out->point_count = clean;
		out->src_dir     = sn;
		out->dst_dir     = Vec2f{-dn.x, -dn.y};

		// label sits at midpoint of the longest interior segment
		f32 best_len = -1.0f;
		u32 best_seg = 0;
		for(u32 i = 0; i + 1 < clean; ++i) {
			f32 sl = fabsf(out->points[i + 1].x - out->points[i].x) + fabsf(out->points[i + 1].y - out->points[i].y);
			if(sl > best_len) { best_len = sl; best_seg = i; }
		}
		out->label_pos    = Vec2f{(out->points[best_seg].x + out->points[best_seg + 1].x) * 0.5f,
		                          (out->points[best_seg].y + out->points[best_seg + 1].y) * 0.5f};
		out->src_mult_pos = Vec2f{p0.x + sn.x * 16.0f + (src_h ? 0.0f : 12.0f), p0.y + sn.y * 16.0f + (src_h ? -10.0f : 0.0f)};
		out->dst_mult_pos = Vec2f{p1.x + dn.x * 20.0f + (dst_h ? 0.0f : 12.0f), p1.y + dn.y * 20.0f + (dst_h ? -10.0f : 0.0f)};
	}

	// compute bounding box
	f32 x0 = 1e9f, y0 = 1e9f, x1 = -1e9f, y1 = -1e9f;
	for(u32 i = 0; i < out->point_count; ++i) {
		x0 = Min(x0, out->points[i].x);
		y0 = Min(y0, out->points[i].y);
		x1 = Max(x1, out->points[i].x);
		y1 = Max(y1, out->points[i].y);
	}
	if(out->routing == ROUTE_BEZIER) {
		x0 = Min(x0, Min(out->c0.x, out->c1.x));
		y0 = Min(y0, Min(out->c0.y, out->c1.y));
		x1 = Max(x1, Max(out->c0.x, out->c1.x));
		y1 = Max(y1, Max(out->c0.y, out->c1.y));
	}
	out->bounds = Rect{x0 - 24.0f, y0 - 24.0f, x1 + 24.0f, y1 + 24.0f};
	return true;
}

//- hit testing

internal f32 point_to_segment_dist(f32 px, f32 py, Vec2f a, Vec2f b)
{
	f32 vx = b.x - a.x, vy = b.y - a.y;
	f32 len2 = vx * vx + vy * vy;
	f32 t = (len2 > 1e-6f) ? Clamp(((px - a.x) * vx + (py - a.y) * vy) / len2, 0.0f, 1.0f) : 0.0f;
	f32 cx = a.x + vx * t, cy = a.y + vy * t;
	return sqrtf((px - cx) * (px - cx) + (py - cy) * (py - cy));
}

internal s32 diagram_hit_test_node(Diagram *d, f32 wx, f32 wy)
{
	// NOTE: check regular nodes first (topmost = highest index), then containers behind them
	for(u32 pass = 0; pass < 2; ++pass) {
		b32 want_container = (pass == 1);
		for(s32 i = (s32)d->node_count - 1; i >= 0; --i) {
			DiagramNode *n = &d->nodes[i];
			if(!n->alive || diagram_node_hidden(d, i)) continue;
			b32 is_container = (n->kind == NODE_MODULE);
			if(is_container != want_container) continue;
			if(wx >= n->x && wx <= n->x + n->w && wy >= n->y && wy <= n->y + n->h) {
				// NOTE: a lifeline is only solid at its header and along its line, so messages
				// across its body stay clickable
				if(node_kind_is_lifeline(n->kind) && wy > n->y + n->header_h && fabsf(wx - (n->x + n->w * 0.5f)) > 7.0f) continue;
				return i;
			}
		}
	}
	return -1;
}

internal s32 diagram_hit_test_edge(Diagram *d, f32 wx, f32 wy, f32 tolerance)
{
	s32 best      = -1;
	f32 best_dist = tolerance;
	EdgePath path;
	for(u32 i = 0; i < d->edge_count; ++i) {
		if(!d->edges[i].alive || diagram_edge_hidden(d, (s32)i)) continue;
		if(!diagram_edge_route(d, (s32)i, &path)) continue;
		if(wx < path.bounds.x0 - tolerance || wx > path.bounds.x1 + tolerance ||
		   wy < path.bounds.y0 - tolerance || wy > path.bounds.y1 + tolerance) continue;
		if(path.routing == ROUTE_BEZIER) {
			Vec2f prev = path.points[0];
			for(u32 s = 1; s <= 12; ++s) {
				f32 t = (f32)s / 12.0f, u = 1.0f - t;
				Vec2f cur = {
					u * u * u * path.points[0].x + 3 * u * u * t * path.c0.x + 3 * u * t * t * path.c1.x + t * t * t * path.points[1].x,
					u * u * u * path.points[0].y + 3 * u * u * t * path.c0.y + 3 * u * t * t * path.c1.y + t * t * t * path.points[1].y,
				};
				f32 dist = point_to_segment_dist(wx, wy, prev, cur);
				if(dist < best_dist) { best_dist = dist; best = (s32)i; }
				prev = cur;
			}
		} else {
			for(u32 p = 0; p + 1 < path.point_count; ++p) {
				f32 dist = point_to_segment_dist(wx, wy, path.points[p], path.points[p + 1]);
				if(dist < best_dist) { best_dist = dist; best = (s32)i; }
			}
		}
	}
	return best;
}

internal Rect diagram_bounds(Diagram *d)
{
	f32 x0 = 1e9f, y0 = 1e9f, x1 = -1e9f, y1 = -1e9f;
	u32 found = 0;
	for(u32 i = 0; i < d->node_count; ++i) {
		DiagramNode *n = &d->nodes[i];
		if(!n->alive || diagram_node_hidden(d, (s32)i)) continue;
		x0 = Min(x0, n->x);
		y0 = Min(y0, n->y);
		x1 = Max(x1, n->x + n->w);
		y1 = Max(y1, n->y + n->h);
		++found;
	}
	if(!found) return Rect{-200, -150, 200, 150};
	return Rect{x0, y0, x1, y1};
}

//- bends

// NOTE: auto layout moves nodes, hand placed bends would point at the old places
internal void diagram_clear_bends(Diagram *d, b32 only_unpinned)
{
	for(u32 i = 0; i < d->edge_count; ++i) {
		DiagramEdge *e = &d->edges[i];
		if(!e->alive || !e->waypoint_count) continue;
		if(only_unpinned && e->src >= 0 && e->dst >= 0 && d->nodes[e->src].pinned && d->nodes[e->dst].pinned) continue;
		e->waypoint_count = 0;
	}
}

//- sequence diagrams

// NOTE: lifelines left to right in their current x order, all heads on one line, and the messages
// re-spaced top to bottom in their current order (a self message takes a little more room)
internal void diagram_layout_sequence(Diagram *d, f32 origin_x, f32 origin_y)
{
	ScratchArena scratch = ScratchGet();
	s32 *line = PushArray(scratch.arena, s32, d->node_count + 1);
	u32  lines = 0;
	f32  head = 0.0f;
	for(u32 i = 0; i < d->node_count; ++i) {
		if(!d->nodes[i].alive || !node_kind_is_lifeline(d->nodes[i].kind)) continue;
		diagram_measure_node(d, (s32)i);
		line[lines++] = (s32)i;
		head = Max(head, d->nodes[i].header_h);
	}
	if(lines == 0) { scratch_end(&scratch); return; }
	for(u32 a = 1; a < lines; ++a) {
		s32 key = line[a], b = (s32)a - 1;
		while(b >= 0 && d->nodes[line[b]].x > d->nodes[key].x) { line[b + 1] = line[b]; --b; }
		line[b + 1] = key;
	}
	f32 x = origin_x;
	for(u32 i = 0; i < lines; ++i) {
		DiagramNode *n = &d->nodes[line[i]];
		if(!n->pinned) {
			n->x = x;
			n->y = origin_y + (head - n->header_h); // heads share a bottom line
		}
		x = n->x + n->w + 72.0f;
	}

	s32 *msg = PushArray(scratch.arena, s32, d->edge_count + 1);
	u32  msgs = 0;
	for(u32 e = 0; e < d->edge_count; ++e) if(d->edges[e].alive && diagram_edge_is_message(d, (s32)e)) msg[msgs++] = (s32)e;
	for(u32 a = 1; a < msgs; ++a) {
		s32 key = msg[a], b = (s32)a - 1;
		while(b >= 0 && d->edges[msg[b]].msg_y > d->edges[key].msg_y) { msg[b + 1] = msg[b]; --b; }
		msg[b + 1] = key;
	}
	f32 y = origin_y + head + 48.0f;
	for(u32 i = 0; i < msgs; ++i) {
		DiagramEdge *e = &d->edges[msg[i]];
		e->msg_y = y;
		e->waypoint_count = 0;
		y += (e->src == e->dst) ? 64.0f : 44.0f;
	}
	for(u32 i = 0; i < lines; ++i) d->nodes[line[i]].manual_size = false;
	diagram_fit_lifelines(d);
	d->dirty = true;
	++d->version;
	scratch_end(&scratch);
}

// NOTE: after a structural layout the lifelines go right of everything else (or at the origin)
internal void diagram_layout_lifelines_beside(Diagram *d)
{
	b32 any = false;
	f32 x1 = -1e9f, y0 = 1e9f;
	for(u32 i = 0; i < d->node_count; ++i) {
		DiagramNode *n = &d->nodes[i];
		if(!n->alive || diagram_node_hidden(d, (s32)i)) continue;
		if(node_kind_is_lifeline(n->kind)) { any = true; continue; }
		if(n->parent >= 0) continue;
		x1 = Max(x1, n->x + n->w);
		y0 = Min(y0, n->y);
	}
	if(!any) return;
	if(x1 < -1e8f) diagram_layout_sequence(d, 40.0f, 40.0f);
	else           diagram_layout_sequence(d, x1 + 160.0f, y0);
}

//- auto-layout: layered hierarchical (Sugiyama-style)

internal void diagram_layout_layered(Diagram *d, b32 left_to_right)
{
	u64 t0 = time_now_ns();
	diagram_measure_all(d);
	if(d->live_nodes == 0) return;

	ScratchArena scratch = ScratchGet();
	u32  n    = d->node_count;
	s32 *rank = PushArrayZero(scratch.arena, s32, n);
	f32 *ord  = PushArrayZero(scratch.arena, f32, n);
	b32 *act  = PushArrayZero(scratch.arena, b32, n);

	u32 active_count = 0;
	for(u32 i = 0; i < n; ++i) {
		if(d->nodes[i].alive && d->nodes[i].kind != NODE_MODULE && !node_kind_is_lifeline(d->nodes[i].kind) && !diagram_node_hidden(d, (s32)i)) {
			act[i] = true;
			ord[i] = (f32)(active_count++);
		}
	}
	if(active_count == 0) {
		scratch_end(&scratch);
		diagram_layout_lifelines_beside(d);
		return;
	}

	// Longest-path rank relaxation (bounded to handle cycles cleanly)
	u32 max_passes = Min(active_count, 32u);
	for(u32 pass = 0; pass < max_passes; ++pass) {
		b32 changed = false;
		for(u32 e = 0; e < d->edge_count; ++e) {
			DiagramEdge *ed = &d->edges[e];
			if(!ed->alive || ed->src == ed->dst) continue;
			if(!act[ed->src] || !act[ed->dst]) continue;
			// inheritance/implements point upwards in UML (base on top), others flow src->dst
			s32 u = ed->src, v = ed->dst;
			if(ed->kind == EDGE_INHERITANCE || ed->kind == EDGE_IMPLEMENTS) { u = ed->dst; v = ed->src; }
			if(rank[v] <= rank[u] && rank[u] < 24) {
				rank[v] = rank[u] + 1;
				changed = true;
			}
		}
		if(!changed) break;
	}

	s32 max_rank = 0;
	for(u32 i = 0; i < n; ++i) if(act[i] && rank[i] > max_rank) max_rank = rank[i];

	// Barycenter sweeps to minimize edge crossings (group same-parent nodes slightly)
	for(u32 sweep = 0; sweep < 6; ++sweep) {
		for(u32 i = 0; i < n; ++i) {
			if(!act[i]) continue;
			f32 sum = 0.0f;
			u32 deg = 0;
			for(u32 e = 0; e < d->edge_count; ++e) {
				DiagramEdge *ed = &d->edges[e];
				if(!ed->alive) continue;
				if(ed->src == (s32)i && act[ed->dst]) { sum += ord[ed->dst]; ++deg; }
				else if(ed->dst == (s32)i && act[ed->src]) { sum += ord[ed->src]; ++deg; }
			}
			f32 parent_bias = (d->nodes[i].parent >= 0) ? (f32)(d->nodes[i].parent % 16) * 8.0f : 0.0f;
			if(deg > 0) ord[i] = (sum / (f32)deg) * 0.7f + parent_bias * 0.3f;
			else        ord[i] = ord[i] * 0.5f + parent_bias * 0.5f;
		}
	}

	// Place rank by rank
	s32 *layer = PushArray(scratch.arena, s32, active_count);
	f32  gap_x = 64.0f, gap_y = 72.0f;
	f32  cursor_major = 40.0f;

	for(s32 r = 0; r <= max_rank; ++r) {
		u32 count = 0;
		for(u32 i = 0; i < n; ++i) if(act[i] && rank[i] == r) layer[count++] = (s32)i;
		if(count == 0) continue;

		// insertion sort by (parent, ord) so container children stay contiguous
		for(u32 a = 1; a < count; ++a) {
			s32 key = layer[a], b = (s32)a - 1;
			while(b >= 0) {
				s32 prev = layer[b];
				b32 swap = (d->nodes[prev].parent > d->nodes[key].parent) ||
				           (d->nodes[prev].parent == d->nodes[key].parent && ord[prev] > ord[key]);
				if(!swap) break;
				layer[b + 1] = layer[b];
				--b;
			}
			layer[b + 1] = key;
		}

		f32 major_size = 0.0f;
		f32 total_minor = 0.0f;
		for(u32 i = 0; i < count; ++i) {
			DiagramNode *nd = &d->nodes[layer[i]];
			if(left_to_right) {
				major_size  = Max(major_size, nd->w);
				total_minor += nd->h + (i > 0 ? gap_y : 0.0f);
			} else {
				major_size  = Max(major_size, nd->h);
				total_minor += nd->w + (i > 0 ? gap_x : 0.0f);
			}
		}

		f32 minor = -total_minor * 0.5f;
		for(u32 i = 0; i < count; ++i) {
			DiagramNode *nd = &d->nodes[layer[i]];
			if(!nd->pinned) {
				if(left_to_right) {
					nd->x = cursor_major;
					nd->y = minor;
				} else {
					nd->x = minor;
					nd->y = cursor_major;
				}
			}
			minor += (left_to_right ? (nd->h + gap_y) : (nd->w + gap_x));
		}
		cursor_major += major_size + (left_to_right ? 96.0f : 84.0f);
	}

	diagram_fit_containers(d);
	diagram_clear_bends(d, true);
	diagram_layout_lifelines_beside(d);
	d->dirty = true;
	++d->version;
	d->layout_ms = (f32)((f64)(time_now_ns() - t0) / 1e6);
	scratch_end(&scratch);
}

//- auto-layout: force-directed

internal void diagram_layout_force(Diagram *d, u32 iterations)
{
	u64 t0 = time_now_ns();
	diagram_measure_all(d);
	ScratchArena scratch = ScratchGet();
	u32   n    = d->node_count;
	Vec2f *disp = PushArrayZero(scratch.arena, Vec2f, n);
	b32   *act  = PushArrayZero(scratch.arena, b32, n);

	u32 count = 0;
	for(u32 i = 0; i < n; ++i) {
		if(d->nodes[i].alive && d->nodes[i].kind != NODE_MODULE && !node_kind_is_lifeline(d->nodes[i].kind) && !diagram_node_hidden(d, (s32)i)) {
			act[i] = true;
			++count;
		}
	}
	if(count < 2) {
		scratch_end(&scratch);
		diagram_layout_lifelines_beside(d);
		return;
	}

	f32 k    = 280.0f;
	f32 temp = 160.0f;
	for(u32 iter = 0; iter < iterations; ++iter) {
		MemSetZero(disp, sizeof(Vec2f) * n);
		// pairwise repulsion (capped for huge graphs)
		for(u32 i = 0; i < n; ++i) {
			if(!act[i]) continue;
			DiagramNode *a = &d->nodes[i];
			f32 ax = a->x + a->w * 0.5f, ay = a->y + a->h * 0.5f;
			for(u32 j = i + 1; j < n; ++j) {
				if(!act[j]) continue;
				DiagramNode *b = &d->nodes[j];
				f32 dx = ax - (b->x + b->w * 0.5f);
				f32 dy = ay - (b->y + b->h * 0.5f);
				f32 dist2 = dx * dx + dy * dy + 1.0f;
				f32 min_d = (a->w + b->w) * 0.55f + (a->h + b->h) * 0.25f;
				if(dist2 > (k * 4.5f) * (k * 4.5f)) continue;
				f32 dist  = sqrtf(dist2);
				f32 force = (k * k) / dist;
				if(dist < min_d) force += (min_d - dist) * 14.0f;
				f32 fx = (dx / dist) * force;
				f32 fy = (dy / dist) * force;
				disp[i].x += fx; disp[i].y += fy;
				disp[j].x -= fx; disp[j].y -= fy;
			}
		}
		// edge attraction
		for(u32 e = 0; e < d->edge_count; ++e) {
			DiagramEdge *ed = &d->edges[e];
			if(!ed->alive || ed->src == ed->dst || !act[ed->src] || !act[ed->dst]) continue;
			DiagramNode *a = &d->nodes[ed->src], *b = &d->nodes[ed->dst];
			f32 dx = (a->x + a->w * 0.5f) - (b->x + b->w * 0.5f);
			f32 dy = (a->y + a->h * 0.5f) - (b->y + b->h * 0.5f);
			f32 dist = Max(sqrtf(dx * dx + dy * dy), 1.0f);
			f32 force = (dist * dist) / (k * 1.4f);
			f32 fx = (dx / dist) * force, fy = (dy / dist) * force;
			disp[ed->src].x -= fx; disp[ed->src].y -= fy;
			disp[ed->dst].x += fx; disp[ed->dst].y += fy;
		}
		// integrate with cooling
		for(u32 i = 0; i < n; ++i) {
			if(!act[i] || d->nodes[i].pinned) continue;
			f32 len = sqrtf(disp[i].x * disp[i].x + disp[i].y * disp[i].y);
			if(len > 1e-3f) {
				f32 step = Min(len, temp);
				d->nodes[i].x += (disp[i].x / len) * step;
				d->nodes[i].y += (disp[i].y / len) * step;
			}
		}
		temp *= 0.94f;
	}

	diagram_fit_containers(d);
	diagram_clear_bends(d, true);
	diagram_layout_lifelines_beside(d);
	d->dirty = true;
	++d->version;
	d->layout_ms = (f32)((f64)(time_now_ns() - t0) / 1e6);
	scratch_end(&scratch);
}

//- auto-layout: compact grid

internal void diagram_layout_grid(Diagram *d)
{
	u64 t0 = time_now_ns();
	diagram_measure_all(d);
	u32 count = 0;
	for(u32 i = 0; i < d->node_count; ++i) {
		if(d->nodes[i].alive && d->nodes[i].kind != NODE_MODULE && !node_kind_is_lifeline(d->nodes[i].kind) && !diagram_node_hidden(d, (s32)i)) ++count;
	}
	if(count == 0) { diagram_layout_lifelines_beside(d); return; }

	u32 cols = (u32)ceilf(sqrtf((f32)count * 1.3f));
	cols = Max(cols, 1u);
	f32 x = 40.0f, y = 40.0f, row_h = 0.0f;
	u32 col = 0;
	for(u32 i = 0; i < d->node_count; ++i) {
		DiagramNode *nd = &d->nodes[i];
		if(!nd->alive || nd->kind == NODE_MODULE || node_kind_is_lifeline(nd->kind) || diagram_node_hidden(d, (s32)i)) continue;
		if(!nd->pinned) {
			nd->x = x;
			nd->y = y;
		}
		x += nd->w + 48.0f;
		row_h = Max(row_h, nd->h);
		if(++col >= cols) {
			col = 0;
			x   = 40.0f;
			y += row_h + 56.0f;
			row_h = 0.0f;
		}
	}
	diagram_fit_containers(d);
	diagram_clear_bends(d, true);
	diagram_layout_lifelines_beside(d);
	d->dirty = true;
	++d->version;
	d->layout_ms = (f32)((f64)(time_now_ns() - t0) / 1e6);
}

//- align & distribute

internal void diagram_align_selected(Diagram *d, AlignMode mode)
{
	ScratchArena scratch = ScratchGet();
	s32 *sel = PushArray(scratch.arena, s32, d->node_count + 1);
	u32  n   = 0;
	for(u32 i = 0; i < d->node_count; ++i) {
		if(d->nodes[i].alive && d->nodes[i].selected) sel[n++] = (s32)i;
	}
	if(n < 2) { scratch_end(&scratch); return; }

	f32 min_x = 1e9f, max_x = -1e9f, min_y = 1e9f, max_y = -1e9f;
	for(u32 i = 0; i < n; ++i) {
		DiagramNode *nd = &d->nodes[sel[i]];
		min_x = Min(min_x, nd->x);
		max_x = Max(max_x, nd->x + nd->w);
		min_y = Min(min_y, nd->y);
		max_y = Max(max_y, nd->y + nd->h);
	}
	f32 mid_x = (min_x + max_x) * 0.5f;
	f32 mid_y = (min_y + max_y) * 0.5f;

	if(mode == DISTRIBUTE_H || mode == DISTRIBUTE_V) {
		b32 horiz = (mode == DISTRIBUTE_H);
		for(u32 a = 1; a < n; ++a) {
			s32 key = sel[a], b = (s32)a - 1;
			while(b >= 0 && (horiz ? (d->nodes[sel[b]].x > d->nodes[key].x) : (d->nodes[sel[b]].y > d->nodes[key].y))) {
				sel[b + 1] = sel[b];
				--b;
			}
			sel[b + 1] = key;
		}
		f32 total_span = horiz ? (d->nodes[sel[n - 1]].x - d->nodes[sel[0]].x) : (d->nodes[sel[n - 1]].y - d->nodes[sel[0]].y);
		f32 start      = horiz ? d->nodes[sel[0]].x : d->nodes[sel[0]].y;
		for(u32 i = 1; i + 1 < n; ++i) {
			f32 t = (f32)i / (f32)(n - 1);
			if(horiz) d->nodes[sel[i]].x = start + total_span * t;
			else      d->nodes[sel[i]].y = start + total_span * t;
		}
	} else {
		for(u32 i = 0; i < n; ++i) {
			DiagramNode *nd = &d->nodes[sel[i]];
			switch(mode) {
				case ALIGN_LEFT:     nd->x = min_x; break;
				case ALIGN_CENTER_X: nd->x = mid_x - nd->w * 0.5f; break;
				case ALIGN_RIGHT:    nd->x = max_x - nd->w; break;
				case ALIGN_TOP:      nd->y = min_y; break;
				case ALIGN_CENTER_Y: nd->y = mid_y - nd->h * 0.5f; break;
				case ALIGN_BOTTOM:   nd->y = max_y - nd->h; break;
				default: break;
			}
		}
	}
	diagram_fit_containers(d);
	d->dirty = true;
	++d->version;
	scratch_end(&scratch);
}