geometric-traits 0.2.0

Trait-based algebraic and graph algorithms for Rust, supporting no_std environments
Documentation
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
//! Force kernels for the ForceAtlas2 layout.
//!
//! All kernels follow the Gephi convention of multiplying the raw
//! center-to-center difference vector by a scalar factor, so a division by
//! the distance is folded into the factor. Every distance-dividing kernel
//! silently skips coincident points, mirroring the zero-distance guards of
//! the Java `ForceFactory` classes.

#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use num_traits::Float;

/// Adds `factor` times the difference vector to `force1` and subtracts it
/// from `force2`.
fn apply_to_pair(
    x_dist: f64,
    y_dist: f64,
    factor: f64,
    force1: &mut [f64; 2],
    force2: &mut [f64; 2],
) {
    force1[0] += x_dist * factor;
    force1[1] += y_dist * factor;
    force2[0] -= x_dist * factor;
    force2[1] -= y_dist * factor;
}

/// Accumulates the degree-weighted linear repulsion between two nodes onto
/// both force accumulators.
///
/// Force magnitude is `coefficient * mass1 * mass2 / d`, pushing the nodes
/// apart. `coefficient` is the scaling ratio `kr`. Coincident nodes exert no
/// force.
pub(super) fn apply_lin_repulsion(
    pos1: [f64; 2],
    pos2: [f64; 2],
    mass1: f64,
    mass2: f64,
    coefficient: f64,
    force1: &mut [f64; 2],
    force2: &mut [f64; 2],
) {
    let x_dist = pos1[0] - pos2[0];
    let y_dist = pos1[1] - pos2[1];
    let squared_distance = x_dist * x_dist + y_dist * y_dist;
    // Zero-distance guard: coincident nodes exert no repulsion.
    if squared_distance > 0.0 {
        // factor = coefficient * m1 * m2 / d^2, applied to the raw
        // difference vector, so the force magnitude is c * m1 * m2 / d.
        let factor = coefficient * mass1 * mass2 / squared_distance;
        apply_to_pair(x_dist, y_dist, factor, force1, force2);
    }
}

/// Accumulates the attraction along one edge onto both force accumulators,
/// in the variant selected by the mode flags. Mirrors the four
/// non-anti-collision classes of the Java `buildAttraction` decision tree.
///
/// In linear mode the factor is `-coefficient * e` (force magnitude
/// `c * e * d`, no distance guard needed because nothing divides by the
/// distance). In LinLog mode it is `-coefficient * e * log(1 + d) / d`
/// (force magnitude `c * e * log(1 + d)`, coincident nodes guarded, which
/// also covers the `log(0)` concern the paper addresses with `log(1 +
/// d)`). Dissuade hubs divides the factor by the source node mass, with
/// `coefficient` carrying the global mean-mass compensation in that mode.
#[allow(clippy::too_many_arguments)]
pub(super) fn apply_attraction(
    lin_log: bool,
    dissuade_hubs: bool,
    pos1: [f64; 2],
    pos2: [f64; 2],
    e: f64,
    coefficient: f64,
    source_mass: f64,
    force1: &mut [f64; 2],
    force2: &mut [f64; 2],
) {
    let x_dist = pos1[0] - pos2[0];
    let y_dist = pos1[1] - pos2[1];
    let mut factor = if lin_log {
        let distance = (x_dist * x_dist + y_dist * y_dist).sqrt();
        // Zero-distance guard: coincident nodes exert no attraction.
        if distance <= 0.0 {
            return;
        }
        -coefficient * e * distance.ln_1p() / distance
    } else {
        -coefficient * e
    };
    if dissuade_hubs {
        factor /= source_mass;
    }
    apply_to_pair(x_dist, y_dist, factor, force1, force2);
}

/// Returns the anti-collision repulsion factor for the given difference
/// vector, or `None` for exactly touching borders.
///
/// The gating distance is border-to-border: `d' = d - size1 - size2`.
/// Separated nodes (`d' > 0`) repulse with `coefficient * m1 * m2 / d'^2`
/// applied to the raw center-to-center vector. Overlapping nodes
/// (`d' < 0`) get the constant overlap kick `100 * coefficient * m1 * m2`
/// (also on the center vector, so the push grows with center separation).
/// All per the Java `linRepulsion_antiCollision` class.
fn anti_collision_repulsion_factor(
    x_dist: f64,
    y_dist: f64,
    mass1: f64,
    mass2: f64,
    size1: f64,
    size2: f64,
    coefficient: f64,
) -> Option<f64> {
    let border_distance = (x_dist * x_dist + y_dist * y_dist).sqrt() - size1 - size2;
    if border_distance > 0.0 {
        Some(coefficient * mass1 * mass2 / border_distance / border_distance)
    } else if border_distance < 0.0 {
        Some(100.0 * coefficient * mass1 * mass2)
    } else {
        None
    }
}

/// Accumulates the anti-collision (prevent overlap) repulsion between two
/// sized nodes onto both force accumulators. See
/// [`anti_collision_repulsion_factor`] for the force law.
#[allow(clippy::too_many_arguments)]
pub(super) fn apply_lin_repulsion_anti_collision(
    pos1: [f64; 2],
    pos2: [f64; 2],
    mass1: f64,
    mass2: f64,
    size1: f64,
    size2: f64,
    coefficient: f64,
    force1: &mut [f64; 2],
    force2: &mut [f64; 2],
) {
    let x_dist = pos1[0] - pos2[0];
    let y_dist = pos1[1] - pos2[1];
    if let Some(factor) =
        anti_collision_repulsion_factor(x_dist, y_dist, mass1, mass2, size1, size2, coefficient)
    {
        apply_to_pair(x_dist, y_dist, factor, force1, force2);
    }
}

/// One-sided variant of [`apply_lin_repulsion_anti_collision`], used at
/// Barnes-Hut leaves when node sizes are active.
#[allow(clippy::too_many_arguments)]
pub(super) fn apply_lin_repulsion_anti_collision_one_sided(
    pos: [f64; 2],
    other_pos: [f64; 2],
    node_mass: f64,
    other_mass: f64,
    node_size: f64,
    other_size: f64,
    coefficient: f64,
    force: &mut [f64; 2],
) {
    let x_dist = pos[0] - other_pos[0];
    let y_dist = pos[1] - other_pos[1];
    if let Some(factor) = anti_collision_repulsion_factor(
        x_dist,
        y_dist,
        node_mass,
        other_mass,
        node_size,
        other_size,
        coefficient,
    ) {
        force[0] += x_dist * factor;
        force[1] += y_dist * factor;
    }
}

/// Accumulates the anti-collision attraction along one edge onto both
/// force accumulators.
///
/// Attraction is gated on the border-to-border distance: overlapping or
/// touching nodes (`d' <= 0`) receive no attraction. Inside the gate the
/// factors match [`apply_attraction`], with the LinLog variant using the
/// border distance (per the Java `*_antiCollision` classes).
#[allow(clippy::too_many_arguments)]
pub(super) fn apply_attraction_anti_collision(
    lin_log: bool,
    dissuade_hubs: bool,
    pos1: [f64; 2],
    pos2: [f64; 2],
    e: f64,
    coefficient: f64,
    source_mass: f64,
    size1: f64,
    size2: f64,
    force1: &mut [f64; 2],
    force2: &mut [f64; 2],
) {
    let x_dist = pos1[0] - pos2[0];
    let y_dist = pos1[1] - pos2[1];
    let border_distance = (x_dist * x_dist + y_dist * y_dist).sqrt() - size1 - size2;
    if border_distance <= 0.0 {
        return;
    }
    let mut factor = if lin_log {
        -coefficient * e * border_distance.ln_1p() / border_distance
    } else {
        -coefficient * e
    };
    if dissuade_hubs {
        factor /= source_mass;
    }
    apply_to_pair(x_dist, y_dist, factor, force1, force2);
}

/// Accumulates the one-sided repulsion of a Barnes-Hut region (a
/// super-node of aggregated mass at the barycenter) onto a node's force
/// accumulator.
///
/// Same formula as [`apply_lin_repulsion`] but only the node moves, the
/// region is an aggregate. Coincident node and barycenter exert no force.
pub(super) fn apply_region_repulsion(
    pos: [f64; 2],
    region_center: [f64; 2],
    node_mass: f64,
    region_mass: f64,
    coefficient: f64,
    force: &mut [f64; 2],
) {
    let x_dist = pos[0] - region_center[0];
    let y_dist = pos[1] - region_center[1];
    let squared_distance = x_dist * x_dist + y_dist * y_dist;
    if squared_distance > 0.0 {
        let factor = coefficient * node_mass * region_mass / squared_distance;
        force[0] += x_dist * factor;
        force[1] += y_dist * factor;
    }
}

/// Accumulates the gravity force toward the origin onto the force
/// accumulator.
///
/// In normal mode the force magnitude is `kg * mass`, independent of the
/// distance. In strong mode it is `kg * mass * d`. Nodes exactly at the
/// origin receive no force.
pub(super) fn apply_gravity(pos: [f64; 2], mass: f64, kg: f64, strong: bool, force: &mut [f64; 2]) {
    let distance = (pos[0] * pos[0] + pos[1] * pos[1]).sqrt();
    // Zero-distance guard: a node at the origin receives no gravity.
    if distance > 0.0 {
        // In Gephi the gravity constant is passed as g / scaling to a force
        // whose coefficient is the scaling ratio, the two cancel. The net
        // factor is mass * kg / d (normal) or mass * kg (strong).
        let factor = if strong { kg * mass } else { kg * mass / distance };
        force[0] -= pos[0] * factor;
        force[1] -= pos[1] * factor;
    }
}

#[cfg(test)]
#[allow(clippy::float_cmp)]
mod tests {
    use super::{
        apply_attraction, apply_attraction_anti_collision, apply_gravity, apply_lin_repulsion,
        apply_lin_repulsion_anti_collision, apply_lin_repulsion_anti_collision_one_sided,
    };

    /// Log attraction between (0, 0) and (3, 4): d = 5, e = 2, coefficient
    /// 1.
    ///
    /// factor = -2 * ln(6) / 5, diff = (-3, -4), so node 1 gains
    /// (6 ln(6) / 5, 8 ln(6) / 5). The magnitude is e * ln(1 + d) =
    /// 2 ln(6).
    #[test]
    fn log_attraction_closed_form() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_attraction(true, false, [0.0, 0.0], [3.0, 4.0], 2.0, 1.0, 1.0, &mut f1, &mut f2);
        let ln6 = 6.0_f64.ln();
        assert!((f1[0] - 6.0 * ln6 / 5.0).abs() < 1e-12);
        assert!((f1[1] - 8.0 * ln6 / 5.0).abs() < 1e-12);
        assert!((f2[0] + 6.0 * ln6 / 5.0).abs() < 1e-12);
        assert!((f2[1] + 8.0 * ln6 / 5.0).abs() < 1e-12);
    }

    /// Log attraction between coincident nodes is a guarded no-op.
    #[test]
    fn log_attraction_coincident_is_noop() {
        let mut f1 = [1.0, 2.0];
        let mut f2 = [3.0, 4.0];
        apply_attraction(true, false, [7.0, -7.0], [7.0, -7.0], 2.0, 1.0, 1.0, &mut f1, &mut f2);
        assert_eq!(f1, [1.0, 2.0]);
        assert_eq!(f2, [3.0, 4.0]);
    }

    /// Log attraction is antisymmetric.
    #[test]
    fn log_attraction_antisymmetric() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_attraction(true, false, [1.0, -2.0], [-0.5, 3.0], 4.0, 2.0, 1.0, &mut f1, &mut f2);
        assert_eq!(f1[0], -f2[0]);
        assert_eq!(f1[1], -f2[1]);
    }

    /// Dissuaded linear attraction divides by the source mass: with mass 4,
    /// compensation coefficient 2 and e = 3, factor = -2 * 3 / 4 = -1.5,
    /// diff = (-3, -4), so node 1 gains (4.5, 6).
    #[test]
    fn lin_attraction_dissuade_closed_form() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_attraction(false, true, [1.0, 1.0], [4.0, 5.0], 3.0, 2.0, 4.0, &mut f1, &mut f2);
        assert!((f1[0] - 4.5).abs() < 1e-12);
        assert!((f1[1] - 6.0).abs() < 1e-12);
        assert!((f2[0] + 4.5).abs() < 1e-12);
        assert!((f2[1] + 6.0).abs() < 1e-12);
    }

    /// Halving the source mass doubles the dissuaded attraction.
    #[test]
    fn lin_attraction_dissuade_mass_scaling() {
        let mut heavy = [0.0; 2];
        let mut light = [0.0; 2];
        let mut sink = [0.0; 2];
        apply_attraction(false, true, [0.0, 0.0], [3.0, 4.0], 1.0, 1.0, 4.0, &mut heavy, &mut sink);
        apply_attraction(false, true, [0.0, 0.0], [3.0, 4.0], 1.0, 1.0, 2.0, &mut light, &mut sink);
        assert!((light[0] - 2.0 * heavy[0]).abs() < 1e-12);
        assert!((light[1] - 2.0 * heavy[1]).abs() < 1e-12);
    }

    /// Dissuaded log attraction between (0, 0) and (3, 4): d = 5, e = 2,
    /// coefficient 1, source mass 4.
    ///
    /// factor = -2 * ln(6) / 5 / 4 and diff = (-3, -4), so node 1 gains
    /// (6 ln(6) / 20, 8 ln(6) / 20).
    #[test]
    fn log_attraction_dissuade_closed_form() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_attraction(true, true, [0.0, 0.0], [3.0, 4.0], 2.0, 1.0, 4.0, &mut f1, &mut f2);
        let ln6 = 6.0_f64.ln();
        assert!((f1[0] - 6.0 * ln6 / 20.0).abs() < 1e-12);
        assert!((f1[1] - 8.0 * ln6 / 20.0).abs() < 1e-12);
    }

    /// Dissuaded log attraction between coincident nodes is a guarded
    /// no-op.
    #[test]
    fn log_attraction_dissuade_coincident_is_noop() {
        let mut f1 = [1.0, 2.0];
        let mut f2 = [3.0, 4.0];
        apply_attraction(true, true, [7.0, -7.0], [7.0, -7.0], 2.0, 1.0, 4.0, &mut f1, &mut f2);
        assert_eq!(f1, [1.0, 2.0]);
        assert_eq!(f2, [3.0, 4.0]);
    }

    /// Anti-collision repulsion with separated borders: nodes at (0, 0)
    /// and (3, 4) with sizes 1 each have border distance 5 - 2 = 3, so
    /// factor = 2 * 2 * 3 / 9 = 4/3 on the raw center vector (-3, -4).
    #[test]
    fn anti_collision_repulsion_separated() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_lin_repulsion_anti_collision(
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            3.0,
            1.0,
            1.0,
            2.0,
            &mut f1,
            &mut f2,
        );
        let factor = 2.0 * 2.0 * 3.0 / 9.0;
        assert!((f1[0] - (-3.0 * factor)).abs() < 1e-12);
        assert!((f1[1] - (-4.0 * factor)).abs() < 1e-12);
        assert!((f2[0] - 3.0 * factor).abs() < 1e-12);
        assert!((f2[1] - 4.0 * factor).abs() < 1e-12);
    }

    /// Anti-collision repulsion with overlap: sizes 3 each give border
    /// distance 5 - 6 = -1, so the exact overlap kick factor is
    /// 100 * 2 * 2 * 3 = 1200 on the raw center vector.
    #[test]
    fn anti_collision_repulsion_overlap_kick() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_lin_repulsion_anti_collision(
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            3.0,
            3.0,
            3.0,
            2.0,
            &mut f1,
            &mut f2,
        );
        assert!((f1[0] - (-3.0 * 1200.0)).abs() < 1e-9);
        assert!((f1[1] - (-4.0 * 1200.0)).abs() < 1e-9);
    }

    /// Exactly touching borders (d' == 0) exert no repulsion.
    #[test]
    fn anti_collision_repulsion_touching_is_noop() {
        let mut f1 = [1.0, 2.0];
        let mut f2 = [3.0, 4.0];
        apply_lin_repulsion_anti_collision(
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            3.0,
            2.5,
            2.5,
            2.0,
            &mut f1,
            &mut f2,
        );
        assert_eq!(f1, [1.0, 2.0]);
        assert_eq!(f2, [3.0, 4.0]);
    }

    /// The one-sided variant moves only the queried node.
    #[test]
    fn anti_collision_one_sided_matches_pairwise() {
        let mut pair_f1 = [0.0; 2];
        let mut pair_f2 = [0.0; 2];
        apply_lin_repulsion_anti_collision(
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            3.0,
            1.0,
            1.0,
            2.0,
            &mut pair_f1,
            &mut pair_f2,
        );
        let mut single = [0.0; 2];
        apply_lin_repulsion_anti_collision_one_sided(
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            3.0,
            1.0,
            1.0,
            2.0,
            &mut single,
        );
        assert_eq!(single, pair_f1);
    }

    /// The one-sided variant also skips exactly touching borders.
    #[test]
    fn anti_collision_one_sided_touching_is_noop() {
        let mut force = [1.0, 2.0];
        apply_lin_repulsion_anti_collision_one_sided(
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            3.0,
            2.5,
            2.5,
            2.0,
            &mut force,
        );
        assert_eq!(force, [1.0, 2.0]);
    }

    /// Anti-collision attraction is gated off for overlapping or touching
    /// nodes.
    #[test]
    fn anti_collision_attraction_gated_on_overlap() {
        for (size1, size2) in [(3.0, 3.0), (2.5, 2.5)] {
            let mut f1 = [1.0, 1.0];
            let mut f2 = [2.0, 2.0];
            apply_attraction_anti_collision(
                false,
                false,
                [0.0, 0.0],
                [3.0, 4.0],
                2.0,
                1.0,
                1.0,
                size1,
                size2,
                &mut f1,
                &mut f2,
            );
            assert_eq!(f1, [1.0, 1.0]);
            assert_eq!(f2, [2.0, 2.0]);
        }
    }

    /// Separated anti-collision linear attraction uses the plain factor
    /// `-coefficient * e` on the raw center vector.
    #[test]
    fn anti_collision_attraction_linear_closed_form() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_attraction_anti_collision(
            false,
            false,
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            1.0,
            1.0,
            1.0,
            1.0,
            &mut f1,
            &mut f2,
        );
        assert!((f1[0] - 6.0).abs() < 1e-12);
        assert!((f1[1] - 8.0).abs() < 1e-12);
    }

    /// Separated anti-collision LinLog attraction uses the BORDER distance
    /// in both the logarithm and the divisor: factor =
    /// -e * ln(1 + 3) / 3 with border distance 3.
    #[test]
    fn anti_collision_attraction_linlog_uses_border_distance() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_attraction_anti_collision(
            true,
            false,
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            1.0,
            1.0,
            1.0,
            1.0,
            &mut f1,
            &mut f2,
        );
        let factor = 2.0 * 4.0_f64.ln() / 3.0;
        assert!((f1[0] - 3.0 * factor).abs() < 1e-12);
        assert!((f1[1] - 4.0 * factor).abs() < 1e-12);
    }

    /// The dissuade hubs division applies inside the anti-collision gate.
    #[test]
    fn anti_collision_attraction_dissuade_divides_by_mass() {
        let mut plain = [0.0; 2];
        let mut dissuaded = [0.0; 2];
        let mut sink = [0.0; 2];
        apply_attraction_anti_collision(
            false,
            false,
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            1.0,
            1.0,
            1.0,
            1.0,
            &mut plain,
            &mut sink,
        );
        apply_attraction_anti_collision(
            false,
            true,
            [0.0, 0.0],
            [3.0, 4.0],
            2.0,
            1.0,
            4.0,
            1.0,
            1.0,
            &mut dissuaded,
            &mut sink,
        );
        assert!((dissuaded[0] - plain[0] / 4.0).abs() < 1e-12);
        assert!((dissuaded[1] - plain[1] / 4.0).abs() < 1e-12);
    }

    /// Repulsion between (0, 0) and (3, 4): d = 5, masses 2 and 3, kr = 2.
    ///
    /// factor = 2 * 2 * 3 / 5^2 = 0.48, diff = (-3, -4), so node 1 gains
    /// (-1.44, -1.92) and node 2 the opposite. The resulting magnitude is
    /// 0.48 * 5 = 2.4 = kr * m1 * m2 / d.
    #[test]
    fn lin_repulsion_closed_form() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_lin_repulsion([0.0, 0.0], [3.0, 4.0], 2.0, 3.0, 2.0, &mut f1, &mut f2);
        assert!((f1[0] - (-1.44)).abs() < 1e-12);
        assert!((f1[1] - (-1.92)).abs() < 1e-12);
        assert!((f2[0] - 1.44).abs() < 1e-12);
        assert!((f2[1] - 1.92).abs() < 1e-12);
    }

    /// Repulsion accumulates on top of existing forces.
    #[test]
    fn lin_repulsion_accumulates() {
        let mut f1 = [10.0, 20.0];
        let mut f2 = [0.0; 2];
        apply_lin_repulsion([0.0, 0.0], [3.0, 4.0], 2.0, 3.0, 2.0, &mut f1, &mut f2);
        assert!((f1[0] - (10.0 - 1.44)).abs() < 1e-12);
        assert!((f1[1] - (20.0 - 1.92)).abs() < 1e-12);
    }

    /// Coincident nodes exert no repulsion (zero-distance guard).
    #[test]
    fn lin_repulsion_coincident_is_noop() {
        let mut f1 = [1.0, 2.0];
        let mut f2 = [3.0, 4.0];
        apply_lin_repulsion([7.0, -7.0], [7.0, -7.0], 2.0, 3.0, 2.0, &mut f1, &mut f2);
        assert_eq!(f1, [1.0, 2.0]);
        assert_eq!(f2, [3.0, 4.0]);
    }

    /// Repulsion is antisymmetric: the same force with opposite signs.
    #[test]
    fn lin_repulsion_antisymmetric() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_lin_repulsion([1.0, -2.0], [-0.5, 3.0], 4.0, 5.0, 2.0, &mut f1, &mut f2);
        assert_eq!(f1[0], -f2[0]);
        assert_eq!(f1[1], -f2[1]);
    }

    /// Attraction between (1, 1) and (4, 5) with e = 2 and coefficient 1.
    ///
    /// factor = -1 * 2 = -2, diff = (-3, -4), so node 1 gains (6, 8) (pulled
    /// toward node 2) and node 2 gains (-6, -8). The magnitude is e * d =
    /// 10.
    #[test]
    fn lin_attraction_closed_form() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_attraction(false, false, [1.0, 1.0], [4.0, 5.0], 2.0, 1.0, 1.0, &mut f1, &mut f2);
        assert!((f1[0] - 6.0).abs() < 1e-12);
        assert!((f1[1] - 8.0).abs() < 1e-12);
        assert!((f2[0] - (-6.0)).abs() < 1e-12);
        assert!((f2[1] - (-8.0)).abs() < 1e-12);
    }

    /// A zero-weight edge exerts no attraction.
    #[test]
    fn lin_attraction_zero_weight_is_noop() {
        let mut f1 = [1.0, 1.0];
        let mut f2 = [2.0, 2.0];
        apply_attraction(false, false, [1.0, 1.0], [4.0, 5.0], 0.0, 1.0, 1.0, &mut f1, &mut f2);
        assert_eq!(f1, [1.0, 1.0]);
        assert_eq!(f2, [2.0, 2.0]);
    }

    /// Attraction between coincident nodes is zero (no NaN).
    #[test]
    fn lin_attraction_coincident_is_noop() {
        let mut f1 = [0.0; 2];
        let mut f2 = [0.0; 2];
        apply_attraction(false, false, [4.0, 5.0], [4.0, 5.0], 2.0, 1.0, 1.0, &mut f1, &mut f2);
        assert_eq!(f1, [0.0, 0.0]);
        assert_eq!(f2, [0.0, 0.0]);
    }

    /// Normal gravity at (3, 4) with mass 2 and kg = 1.5.
    ///
    /// d = 5, factor = 2 * 1.5 / 5 = 0.6, force = -(3, 4) * 0.6 =
    /// (-1.8, -2.4), magnitude mass * kg = 3, independent of d.
    #[test]
    fn gravity_normal_closed_form() {
        let mut f = [0.0; 2];
        apply_gravity([3.0, 4.0], 2.0, 1.5, false, &mut f);
        assert!((f[0] - (-1.8)).abs() < 1e-12);
        assert!((f[1] - (-2.4)).abs() < 1e-12);
    }

    /// Strong gravity at (3, 4) with mass 2 and kg = 1.5.
    ///
    /// factor = 2 * 1.5 = 3, force = -(3, 4) * 3 = (-9, -12), magnitude
    /// mass * kg * d = 15.
    #[test]
    fn gravity_strong_closed_form() {
        let mut f = [0.0; 2];
        apply_gravity([3.0, 4.0], 2.0, 1.5, true, &mut f);
        assert!((f[0] - (-9.0)).abs() < 1e-12);
        assert!((f[1] - (-12.0)).abs() < 1e-12);
    }

    /// A node exactly at the origin receives no gravity (zero-distance
    /// guard).
    #[test]
    fn gravity_at_origin_is_noop() {
        let mut f = [5.0, 6.0];
        apply_gravity([0.0, 0.0], 2.0, 1.5, false, &mut f);
        assert_eq!(f, [5.0, 6.0]);
        apply_gravity([0.0, 0.0], 2.0, 1.5, true, &mut f);
        assert_eq!(f, [5.0, 6.0]);
    }

    /// Zero gravity constant is a no-op in both modes.
    #[test]
    fn gravity_zero_constant_is_noop() {
        let mut f = [0.0; 2];
        apply_gravity([3.0, 4.0], 2.0, 0.0, false, &mut f);
        assert_eq!(f, [0.0, 0.0]);
        apply_gravity([3.0, 4.0], 2.0, 0.0, true, &mut f);
        assert_eq!(f, [0.0, 0.0]);
    }
}