| Name | Message | Date |
|---|---|---|
| 📄 EdgeAvoidance.cs | 5 days ago | |
| 📄 GraphLayout.cs | 5 days ago | |
| 📄 GraphScene.cs | 5 days ago | |
| 📄 PhysicsLayout.cs | 5 days ago |
📄
ui/src/Graph/PhysicsLayout.cs
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
using System; using System.Collections.Generic; using System.Linq; using System.Threading; using Avalonia; namespace Cfa.Ui.Graph; public sealed record PhysicsNode(int Parent, double Radius); public sealed record PhysicsOptions { // Tick-based limits keep the same input reproducible across machines. public int MaxIterations { get; init; } = 600; public double SpringStrength { get; init; } = 0.025; public double Damping { get; init; } = 0.7; public double MaxSpeed { get; init; } = 20; public double Tolerance { get; init; } = 0.05; public double EdgeCrossingStrength { get; init; } = 0.12; public double EdgeNodeStrength { get; init; } = 0.15; public double EdgeClearance { get; init; } = 40; } /// <summary>Occurrence-tree physics. A node's descendants may enter its radius; all other nodes may not.</summary> public sealed class PhysicsLayout { private readonly PhysicsNode[] nodes; private readonly PhysicsOptions options; private readonly Point[] positions; private readonly Vector[] velocities; private readonly int[] enter; private readonly int[] exit; private readonly double cellSize; public IReadOnlyList<Point> Positions { get; } public int Iterations { get; private set; } public bool IsStable { get; private set; } public double MaximumOverlap { get; private set; } public PhysicsLayout( IReadOnlyList<PhysicsNode> nodes, PhysicsOptions? options = null, IReadOnlyList<Point>? initialPositions = null ) { ArgumentNullException.ThrowIfNull(nodes); this.options = options ?? new PhysicsOptions(); if ( this.options.MaxIterations < 1 || !double.IsFinite(this.options.SpringStrength) || this.options.SpringStrength <= 0 || !double.IsFinite(this.options.Damping) || this.options.Damping < 0 || this.options.Damping >= 1 || !double.IsFinite(this.options.MaxSpeed) || this.options.MaxSpeed <= 0 || !double.IsFinite(this.options.Tolerance) || this.options.Tolerance <= 0 || !double.IsFinite(this.options.EdgeCrossingStrength) || this.options.EdgeCrossingStrength < 0 || !double.IsFinite(this.options.EdgeNodeStrength) || this.options.EdgeNodeStrength < 0 || !double.IsFinite(this.options.EdgeClearance) || this.options.EdgeClearance <= 0 ) throw new ArgumentOutOfRangeException(nameof(options)); this.nodes = nodes.ToArray(); positions = new Point[nodes.Count]; velocities = new Vector[nodes.Count]; enter = new int[nodes.Count]; exit = new int[nodes.Count]; var children = Enumerable.Range(0, nodes.Count).Select(_ => new List<int>()).ToArray(); for (var i = 0; i < nodes.Count; i++) { var node = nodes[i]; if ( node.Parent < -1 || node.Parent >= i || (i > 0 && node.Parent < 0) || !double.IsFinite(node.Radius) || node.Radius <= 0 ) throw new ArgumentException( "Nodes must form one parent-before-child tree with finite positive radii.", nameof(nodes) ); if (i > 0) children[node.Parent].Add(i); } var clock = 0; var traversal = new Stack<(int Id, bool Leaving)>(); if (nodes.Count > 0) traversal.Push((0, false)); while (traversal.TryPop(out var item)) { if (item.Leaving) exit[item.Id] = clock; else { enter[item.Id] = clock++; traversal.Push((item.Id, true)); for (var i = children[item.Id].Count - 1; i >= 0; i--) traversal.Push((children[item.Id][i], false)); } } if (initialPositions is not null) { if ( initialPositions.Count != nodes.Count || initialPositions.Any(point => !double.IsFinite(point.X) || !double.IsFinite(point.Y)) ) throw new ArgumentException("Supply one finite position per node.", nameof(initialPositions)); for (var i = 0; i < nodes.Count; i++) positions[i] = initialPositions[i] - initialPositions[0]; } else { for (var i = 0; i < nodes.Count; i++) { var heading = i == 0 ? 0 : Math.Atan2( positions[i].Y - positions[nodes[i].Parent].Y, positions[i].X - positions[nodes[i].Parent].X ); for (var sibling = 0; sibling < children[i].Count; sibling++) { var child = children[i][sibling]; var angle = heading + ( i == 0 ? sibling * Math.PI * 2 / children[i].Count : (sibling - (children[i].Count - 1) / 2d) * Math.PI / Math.Max(1, children[i].Count) ); positions[child] = positions[i] + new Vector(Math.Cos(angle), Math.Sin(angle)) * MinimumDistance(i, child); } } } cellSize = nodes.Count > 0 ? nodes.Max(node => node.Radius) : 1; Positions = Array.AsReadOnly(positions); } public double MinimumDistance(int first, int second) { ArgumentOutOfRangeException.ThrowIfNegative(first); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(first, nodes.Length); ArgumentOutOfRangeException.ThrowIfNegative(second); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(second, nodes.Length); if (first == second) return 0; var firstRadius = IsDescendant(first, second) ? 0 : nodes[first].Radius; var secondRadius = IsDescendant(second, first) ? 0 : nodes[second].Radius; return Math.Max(firstRadius, secondRadius); } private bool IsDescendant(int ancestor, int child) => enter[ancestor] < enter[child] && enter[child] < exit[ancestor]; public void Settle(CancellationToken cancellationToken = default) { var quietTicks = 0; while (Iterations < options.MaxIterations && quietTicks < 8) { var movement = Tick(cancellationToken); quietTicks = movement < options.Tolerance && MaximumOverlap < options.Tolerance ? quietTicks + 1 : 0; } IsStable = quietTicks >= 8; } public double Tick(CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); var before = (Point[])positions.Clone(); var forces = new Vector[nodes.Length]; for (var i = 1; i < nodes.Length; i++) { var parent = nodes[i].Parent; var delta = positions[i] - positions[parent]; var distance = new Vector(delta.X, delta.Y).Length; var direction = Direction(parent, i, distance); var force = direction * ((distance - MinimumDistance(parent, i) * 1.05) * options.SpringStrength); forces[i] -= force; forces[parent] += force; } EdgeAvoidance.Apply(nodes, positions, forces, options, cancellationToken); for (var i = 1; i < nodes.Length; i++) { velocities[i] = (velocities[i] + forces[i]) * options.Damping; if (velocities[i].Length > options.MaxSpeed) velocities[i] *= options.MaxSpeed / velocities[i].Length; positions[i] += velocities[i]; } // Project exclusion violations after integration. Descendants are exempt only // from their ancestor's radius, not from their own radius facing that ancestor. MaximumOverlap = 0; for (var sweep = 0; sweep < 32; sweep++) { var maximumOverlap = 0d; var cells = new Dictionary<(int X, int Y), List<int>>(); for (var i = 0; i < nodes.Length; i++) { cancellationToken.ThrowIfCancellationRequested(); var key = Cell(positions[i]); for (var x = key.X - 1; x <= key.X + 1; x++) for (var y = key.Y - 1; y <= key.Y + 1; y++) { if (!cells.TryGetValue((x, y), out var neighbors)) continue; foreach (var j in neighbors) { var delta = positions[i] - positions[j]; var distance = new Vector(delta.X, delta.Y).Length; var overlap = MinimumDistance(j, i) - distance; if (overlap <= 0) continue; maximumOverlap = Math.Max(maximumOverlap, overlap); var correction = Direction(j, i, distance) * overlap; positions[i] += correction * (j == 0 ? 1 : 0.5); if (j != 0) positions[j] -= correction * 0.5; } } var updated = Cell(positions[i]); if (!cells.TryGetValue(updated, out var members)) cells[updated] = members = []; members.Add(i); } MaximumOverlap = maximumOverlap; if (maximumOverlap < options.Tolerance / 10) break; } MaximumOverlap = MeasureOverlap(cancellationToken); Iterations++; IsStable = false; var maximum = 0d; for (var i = 1; i < nodes.Length; i++) { var delta = positions[i] - before[i]; maximum = Math.Max(maximum, new Vector(delta.X, delta.Y).Length); } return maximum; } private double MeasureOverlap(CancellationToken cancellationToken) { var maximum = 0d; var cells = new Dictionary<(int X, int Y), List<int>>(); for (var i = 0; i < nodes.Length; i++) { cancellationToken.ThrowIfCancellationRequested(); var key = Cell(positions[i]); for (var x = key.X - 1; x <= key.X + 1; x++) for (var y = key.Y - 1; y <= key.Y + 1; y++) { if (!cells.TryGetValue((x, y), out var neighbors)) continue; foreach (var j in neighbors) { var delta = positions[i] - positions[j]; maximum = Math.Max(maximum, MinimumDistance(j, i) - new Vector(delta.X, delta.Y).Length); } } if (!cells.TryGetValue(key, out var members)) cells[key] = members = []; members.Add(i); } return maximum; } private (int X, int Y) Cell(Point point) => ((int)Math.Floor(point.X / cellSize), (int)Math.Floor(point.Y / cellSize)); private Vector Direction(int first, int second, double distance) { if (distance > 1e-9) return new Vector(positions[second].X - positions[first].X, positions[second].Y - positions[first].Y) / distance; var angle = (first + second + 1) * 2.399963229728653; return new Vector(Math.Cos(angle), Math.Sin(angle)); } }