box3d-rust 0.2.1

Pure Rust port of the Box3D 3D physics engine
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
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
//! Port of box3d-cpp-reference/test/test_hull.c

use crate::hull::{
    clone_and_transform_hull, clone_hull, compare_hull_data, create_cylinder, create_hull,
    destroy_hull, is_valid_hull, make_box_hull,
};
use crate::math_functions::{
    abs_float, make_quat_from_axis_angle, sub_mm, Transform, Vec3, PI, QUAT_IDENTITY,
    TRANSFORM_IDENTITY, VEC3_AXIS_Y,
};

const CUBE_CORNERS: [Vec3; 8] = [
    Vec3 {
        x: 1.0,
        y: 1.0,
        z: 1.0,
    },
    Vec3 {
        x: -1.0,
        y: 1.0,
        z: 1.0,
    },
    Vec3 {
        x: -1.0,
        y: -1.0,
        z: 1.0,
    },
    Vec3 {
        x: 1.0,
        y: -1.0,
        z: 1.0,
    },
    Vec3 {
        x: 1.0,
        y: 1.0,
        z: -1.0,
    },
    Vec3 {
        x: -1.0,
        y: 1.0,
        z: -1.0,
    },
    Vec3 {
        x: -1.0,
        y: -1.0,
        z: -1.0,
    },
    Vec3 {
        x: 1.0,
        y: -1.0,
        z: -1.0,
    },
];

fn ensure_small(v: f32, tol: f32) {
    assert!(abs_float(v) <= tol, "expected |{v}| <= {tol}");
}

#[test]
fn create_hull_cube() {
    let hull = create_hull(&CUBE_CORNERS, 8).expect("cube hull");
    assert_eq!(hull.vertex_count, 8);
    assert_eq!(hull.edge_count, 24);
    assert_eq!(hull.face_count, 6);
    assert_eq!(hull.vertex_count - hull.edge_count / 2 + hull.face_count, 2);

    let ref_hull = make_box_hull(1.0, 1.0, 1.0);
    ensure_small(hull.volume - ref_hull.base.volume, 1e-4);
    ensure_small(hull.surface_area - ref_hull.base.surface_area, 1e-4);
    ensure_small(hull.inner_radius - ref_hull.base.inner_radius, f32::EPSILON);
    ensure_small(hull.center.x - ref_hull.base.center.x, 1e-5);
    ensure_small(hull.center.y - ref_hull.base.center.y, 1e-5);
    ensure_small(hull.center.z - ref_hull.base.center.z, 1e-5);

    ensure_small(hull.aabb.lower_bound.x + 1.0, f32::EPSILON);
    ensure_small(hull.aabb.lower_bound.y + 1.0, f32::EPSILON);
    ensure_small(hull.aabb.lower_bound.z + 1.0, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.x - 1.0, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.y - 1.0, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.z - 1.0, f32::EPSILON);

    let d = sub_mm(hull.central_inertia, ref_hull.base.central_inertia);
    ensure_small(d.cx.x, 1e-4);
    ensure_small(d.cy.y, 1e-4);
    ensure_small(d.cz.z, 1e-4);
    ensure_small(d.cx.y, 1e-4);
    ensure_small(d.cx.z, 1e-4);
    ensure_small(d.cy.z, 1e-4);
    ensure_small(d.cy.x, 1e-4);
    ensure_small(d.cz.x, 1e-4);
    ensure_small(d.cz.y, 1e-4);

    destroy_hull(hull);
}

#[test]
fn create_hull_tetrahedron() {
    let points = [
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 1.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 0.0,
            y: 1.0,
            z: 0.0,
        },
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: 1.0,
        },
    ];
    let hull = create_hull(&points, 4).expect("tet hull");
    assert_eq!(hull.vertex_count, 4);
    assert_eq!(hull.edge_count, 12);
    assert_eq!(hull.face_count, 4);
    assert_eq!(hull.vertex_count - hull.edge_count / 2 + hull.face_count, 2);

    let expected_volume = 1.0 / 6.0;
    let expected_surface_area = 1.5 + 0.5 * 3.0f32.sqrt();
    let expected_inner_radius = 0.25 / 3.0f32.sqrt();

    ensure_small(hull.volume - expected_volume, 1e-5);
    ensure_small(hull.surface_area - expected_surface_area, 1e-5);
    ensure_small(hull.inner_radius - expected_inner_radius, 1e-5);
    ensure_small(hull.center.x - 0.25, 1e-5);
    ensure_small(hull.center.y - 0.25, 1e-5);
    ensure_small(hull.center.z - 0.25, 1e-5);

    ensure_small(hull.aabb.lower_bound.x, f32::EPSILON);
    ensure_small(hull.aabb.lower_bound.y, f32::EPSILON);
    ensure_small(hull.aabb.lower_bound.z, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.x - 1.0, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.y - 1.0, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.z - 1.0, f32::EPSILON);

    destroy_hull(hull);
}

#[test]
fn create_hull_determinism() {
    let h1 = create_hull(&CUBE_CORNERS, 8).expect("h1");
    let h2 = create_hull(&CUBE_CORNERS, 8).expect("h2");
    assert_eq!(h1.byte_count, h2.byte_count);
    assert_ne!(h1.hash, 0);
    assert_eq!(h1.hash, h2.hash);
    assert!(compare_hull_data(&h1, &h2));
    destroy_hull(h1);
    destroy_hull(h2);
}

const SPHERE_N: usize = 6;

#[test]
fn create_hull_max_vertex() {
    let mut points = [Vec3 {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    }; SPHERE_N * SPHERE_N];
    let mut index = 0;
    for i in 0..SPHERE_N {
        let theta = PI * i as f32 / (SPHERE_N - 1) as f32;
        for j in 0..SPHERE_N {
            let phi = 2.0 * PI * j as f32 / SPHERE_N as f32;
            // Use libm-style sin/cos via Box3D determinism helpers for theta/phi? C uses sinf/cosf.
            // Sphere sample in the C test uses sinf/cosf (platform), not b3Sin — match that.
            points[index] = Vec3 {
                x: theta.sin() * phi.cos(),
                y: theta.sin() * phi.sin(),
                z: theta.cos(),
            };
            index += 1;
        }
    }
    let _count = (SPHERE_N * SPHERE_N) as i32;

    let h1 = create_hull(&points, 8).expect("h1");
    assert!(h1.vertex_count <= 8);
    destroy_hull(h1);

    let h2 = create_hull(&points, 1).expect("h2");
    assert!(h2.vertex_count >= 4 && h2.vertex_count <= 255);
    destroy_hull(h2);

    let h3 = create_hull(&points, 1000).expect("h3");
    assert!(h3.vertex_count >= 4 && h3.vertex_count <= 255);
    destroy_hull(h3);
}

#[test]
fn create_hull_redundant_input() {
    let points = [
        Vec3 {
            x: 1.0,
            y: 1.0,
            z: 1.0,
        },
        Vec3 {
            x: -1.0,
            y: 1.0,
            z: 1.0,
        },
        Vec3 {
            x: -1.0,
            y: -1.0,
            z: 1.0,
        },
        Vec3 {
            x: 1.0,
            y: -1.0,
            z: 1.0,
        },
        Vec3 {
            x: 1.0,
            y: 1.0,
            z: -1.0,
        },
        Vec3 {
            x: -1.0,
            y: 1.0,
            z: -1.0,
        },
        Vec3 {
            x: -1.0,
            y: -1.0,
            z: -1.0,
        },
        Vec3 {
            x: 1.0,
            y: -1.0,
            z: -1.0,
        },
        Vec3 {
            x: 1.0,
            y: 1.0,
            z: 1.0,
        },
        Vec3 {
            x: 1.0,
            y: 1.0,
            z: 1.0,
        },
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 0.5,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 0.0,
            y: 0.5,
            z: 0.0,
        },
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: 0.5,
        },
        Vec3 {
            x: -0.5,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 0.0,
            y: -0.5,
            z: 0.0,
        },
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: -0.5,
        },
        Vec3 {
            x: 0.25,
            y: 0.25,
            z: 0.25,
        },
        Vec3 {
            x: -0.25,
            y: -0.25,
            z: -0.25,
        },
        Vec3 {
            x: 0.5,
            y: 0.5,
            z: 0.5,
        },
    ];

    let hull = create_hull(&points, 8).expect("redundant");
    assert_eq!(hull.vertex_count, 8);
    assert_eq!(hull.edge_count, 24);
    assert_eq!(hull.face_count, 6);

    let ref_hull = make_box_hull(1.0, 1.0, 1.0);
    ensure_small(hull.volume - ref_hull.base.volume, 1e-4);
    ensure_small(hull.surface_area - ref_hull.base.surface_area, 1e-4);
    ensure_small(hull.inner_radius - ref_hull.base.inner_radius, f32::EPSILON);
    ensure_small(hull.center.x - ref_hull.base.center.x, 1e-5);
    ensure_small(hull.center.y - ref_hull.base.center.y, 1e-5);
    ensure_small(hull.center.z - ref_hull.base.center.z, 1e-5);
    ensure_small(hull.aabb.lower_bound.x + 1.0, f32::EPSILON);
    ensure_small(hull.aabb.lower_bound.y + 1.0, f32::EPSILON);
    ensure_small(hull.aabb.lower_bound.z + 1.0, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.x - 1.0, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.y - 1.0, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.z - 1.0, f32::EPSILON);

    destroy_hull(hull);
}

#[test]
fn create_hull_clone() {
    let original = create_hull(&CUBE_CORNERS, 8).expect("original");
    let clone = clone_hull(&original).expect("clone");
    assert_eq!(clone.byte_count, original.byte_count);
    assert!(compare_hull_data(&clone, &original));
    destroy_hull(clone);
    destroy_hull(original);
}

#[test]
fn create_hull_cylinder() {
    let height = 2.0f32;
    let radius = 1.0f32;
    let sides = 8;
    let y_offset = 0.0f32;

    let hull = create_cylinder(height, radius, y_offset, sides).expect("cylinder");
    assert_eq!(hull.vertex_count, 2 * sides);
    assert_eq!(hull.edge_count, 6 * sides);
    assert_eq!(hull.face_count, sides + 2);

    let half_angle = PI / sides as f32;
    let cap_area = sides as f32 * 0.5 * radius * radius * (2.0 * half_angle).sin();
    let chord_len = 2.0 * radius * half_angle.sin();
    let lateral_area = sides as f32 * chord_len * height;
    let expected_volume = cap_area * height;
    let expected_surface_area = 2.0 * cap_area + lateral_area;
    let expected_inner_radius = radius * half_angle.cos();

    ensure_small((hull.volume - expected_volume) / expected_volume, 1e-4);
    ensure_small(
        (hull.surface_area - expected_surface_area) / expected_surface_area,
        1e-4,
    );
    ensure_small(hull.inner_radius - expected_inner_radius, 1e-5);
    ensure_small(hull.center.x, 1e-5);
    ensure_small(hull.center.y - (y_offset + 0.5 * height), 1e-5);
    ensure_small(hull.center.z, 1e-5);

    ensure_small(hull.aabb.lower_bound.x + radius, f32::EPSILON);
    ensure_small(hull.aabb.lower_bound.y - y_offset, f32::EPSILON);
    ensure_small(hull.aabb.lower_bound.z + radius, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.x - radius, f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.y - (y_offset + height), f32::EPSILON);
    ensure_small(hull.aabb.upper_bound.z - radius, f32::EPSILON);

    destroy_hull(hull);
}

fn fill_sphere_sample(points: &mut [Vec3], seed: u32) {
    const RAND_LIMIT_LOCAL: u32 = 32767;
    let mut seed = seed;
    for point in points.iter_mut() {
        let mut u = [0.0f32; 3];
        for k in 0..3 {
            seed ^= seed << 13;
            seed ^= seed >> 17;
            seed ^= seed << 5;
            let mut r = (seed & RAND_LIMIT_LOCAL) as f32;
            r /= RAND_LIMIT_LOCAL as f32;
            u[k] = r;
        }
        let u1 = u[0];
        let u2 = 2.0 * PI * u[1];
        let u3 = 2.0 * PI * u[2];
        let sqrt1_minus_u1 = (1.0 - u1).sqrt();
        let sqrt_u1 = u1.sqrt();
        // C uses sinf/cosf here.
        point.x = sqrt1_minus_u1 * u2.sin();
        point.y = sqrt1_minus_u1 * u2.cos();
        point.z = sqrt_u1 * u3.sin();
    }
}

fn fill_cube_sample(points: &mut [Vec3], seed: u32) {
    const RAND_LIMIT_LOCAL: u32 = 32767;
    let mut seed = seed;
    for point in points.iter_mut() {
        let mut v = [0.0f32; 3];
        for k in 0..3 {
            seed ^= seed << 13;
            seed ^= seed >> 17;
            seed ^= seed << 5;
            let mut r = (seed & RAND_LIMIT_LOCAL) as f32;
            r /= RAND_LIMIT_LOCAL as f32;
            v[k] = 2.0 * r - 1.0;
        }
        point.x = v[0];
        point.y = v[1];
        point.z = v[2];
    }
}

#[test]
fn create_hull_sphere_reduction() {
    let mut points = [Vec3 {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    }; 64];
    fill_sphere_sample(&mut points, 12345);
    let hull = create_hull(&points, 20).expect("sphere reduction");
    assert!(hull.vertex_count >= 4 && hull.vertex_count <= 20);
    assert_eq!(hull.vertex_count - hull.edge_count / 2 + hull.face_count, 2);
    destroy_hull(hull);
}

#[test]
fn create_hull_sphere_stress() {
    const N: usize = 512;
    let mut points = [Vec3 {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    }; N];
    let seeds = [12345u32, 1, 0xdeadbeef, 0xcafef00d];
    let m_values = [16, 24, 32, 40];

    for &seed in &seeds {
        fill_sphere_sample(&mut points, seed);
        for &m in &m_values {
            let hull = create_hull(&points, m).expect("sphere stress");
            assert!(hull.vertex_count >= 4 && hull.vertex_count <= m);
            assert_eq!(hull.vertex_count - hull.edge_count / 2 + hull.face_count, 2);
            assert!(hull.face_count >= 4);
            destroy_hull(hull);
        }
    }
}

#[test]
fn create_hull_merge_churn_stress() {
    const N: usize = 4096;
    let mut points = vec![
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        };
        N
    ];
    let seeds = [12345u32, 0xdeadbeef];

    for &seed in &seeds {
        fill_cube_sample(&mut points, seed);
        for c in 0..8 {
            points[N - 8 + c] = Vec3 {
                x: if (c & 1) != 0 { 1.0 } else { -1.0 },
                y: if (c & 2) != 0 { 1.0 } else { -1.0 },
                z: if (c & 4) != 0 { 1.0 } else { -1.0 },
            };
        }
        let hull = create_hull(&points, 64).expect("merge churn");
        assert_eq!(hull.vertex_count, 8);
        assert_eq!(hull.edge_count, 24);
        assert_eq!(hull.face_count, 6);
        destroy_hull(hull);
    }
}

#[test]
fn create_hull_degenerate() {
    let collinear = [
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 1.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 2.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 3.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 4.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 5.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 6.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 7.0,
            y: 0.0,
            z: 0.0,
        },
    ];
    assert!(create_hull(&collinear[..0], 8).is_none());
    assert!(create_hull(&collinear[..3], 8).is_none());

    let coincident = [Vec3 {
        x: 1.0,
        y: 2.0,
        z: 3.0,
    }; 8];
    assert!(create_hull(&coincident, 8).is_none());
    assert!(create_hull(&collinear, 8).is_none());

    let coplanar = [
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 1.0,
            y: 0.0,
            z: 0.0,
        },
        Vec3 {
            x: 0.0,
            y: 1.0,
            z: 0.0,
        },
        Vec3 {
            x: 1.0,
            y: 1.0,
            z: 0.0,
        },
        Vec3 {
            x: 2.0,
            y: 0.5,
            z: 0.0,
        },
        Vec3 {
            x: 0.5,
            y: 2.0,
            z: 0.0,
        },
    ];
    assert!(create_hull(&coplanar, 8).is_none());
}

#[test]
fn clone_and_transform_identity() {
    // Identity transform + unit scale reproduces the source hull's geometry
    // (planes are recomputed via Newell's method but must land on the same faces).
    let original = create_hull(&CUBE_CORNERS, 8).expect("cube");
    let hull = clone_and_transform_hull(
        &original,
        TRANSFORM_IDENTITY,
        Vec3 {
            x: 1.0,
            y: 1.0,
            z: 1.0,
        },
    )
    .expect("identity clone");

    assert!(is_valid_hull(&hull));
    assert_eq!(hull.vertex_count, original.vertex_count);
    assert_eq!(hull.edge_count, original.edge_count);
    assert_eq!(hull.face_count, original.face_count);

    ensure_small(hull.volume - 8.0, 1e-4);
    ensure_small(hull.surface_area - 24.0, 1e-4);
    ensure_small(hull.inner_radius - 1.0, 1e-5);
    ensure_small(hull.center.x, 1e-5);
    ensure_small(hull.center.y, 1e-5);
    ensure_small(hull.center.z, 1e-5);
    ensure_small(hull.aabb.lower_bound.x + 1.0, 1e-5);
    ensure_small(hull.aabb.upper_bound.x - 1.0, 1e-5);

    destroy_hull(hull);
    destroy_hull(original);
}

#[test]
fn clone_and_transform_uniform_scale() {
    // Scale by 2: volume ×8, area ×4, inner radius ×2.
    let original = create_hull(&CUBE_CORNERS, 8).expect("cube");
    let hull = clone_and_transform_hull(
        &original,
        TRANSFORM_IDENTITY,
        Vec3 {
            x: 2.0,
            y: 2.0,
            z: 2.0,
        },
    )
    .expect("scaled clone");

    ensure_small((hull.volume - 64.0) / 64.0, 1e-4);
    ensure_small((hull.surface_area - 96.0) / 96.0, 1e-4);
    ensure_small(hull.inner_radius - 2.0, 1e-5);
    ensure_small(hull.aabb.lower_bound.x + 2.0, 1e-5);
    ensure_small(hull.aabb.upper_bound.x - 2.0, 1e-5);

    destroy_hull(hull);
    destroy_hull(original);
}

#[test]
fn clone_and_transform_translation() {
    // Translation shifts center + bounds, leaving intrinsic properties intact.
    let original = create_hull(&CUBE_CORNERS, 8).expect("cube");
    let hull = clone_and_transform_hull(
        &original,
        Transform {
            p: Vec3 {
                x: 3.0,
                y: 0.0,
                z: 0.0,
            },
            q: QUAT_IDENTITY,
        },
        Vec3 {
            x: 1.0,
            y: 1.0,
            z: 1.0,
        },
    )
    .expect("translated clone");

    ensure_small(hull.center.x - 3.0, 1e-4);
    ensure_small(hull.volume - 8.0, 1e-4);
    ensure_small(hull.aabb.lower_bound.x - 2.0, 1e-5);
    ensure_small(hull.aabb.upper_bound.x - 4.0, 1e-5);

    destroy_hull(hull);
    destroy_hull(original);
}

#[test]
fn clone_and_transform_rotation() {
    // 90° about Y: the cube is symmetric, so all intrinsic + bound quantities hold.
    let original = create_hull(&CUBE_CORNERS, 8).expect("cube");
    let q = make_quat_from_axis_angle(VEC3_AXIS_Y, 0.5 * PI);
    let hull = clone_and_transform_hull(
        &original,
        Transform {
            p: Vec3 {
                x: 0.0,
                y: 0.0,
                z: 0.0,
            },
            q,
        },
        Vec3 {
            x: 1.0,
            y: 1.0,
            z: 1.0,
        },
    )
    .expect("rotated clone");

    assert!(is_valid_hull(&hull));
    ensure_small(hull.volume - 8.0, 1e-4);
    ensure_small(hull.surface_area - 24.0, 1e-4);
    ensure_small(hull.inner_radius - 1.0, 1e-5);
    ensure_small(hull.aabb.upper_bound.x - 1.0, 1e-5);
    ensure_small(hull.aabb.upper_bound.z - 1.0, 1e-5);

    destroy_hull(hull);
    destroy_hull(original);
}

#[test]
fn clone_and_transform_reflection_stays_valid() {
    // A negative net scale reflects the hull; the winding-reversal path must keep
    // it outward-facing (positive volume) and a valid convex hull.
    let original = create_hull(&CUBE_CORNERS, 8).expect("cube");
    let hull = clone_and_transform_hull(
        &original,
        TRANSFORM_IDENTITY,
        Vec3 {
            x: -1.0,
            y: 1.0,
            z: 1.0,
        },
    )
    .expect("reflected clone");

    assert!(is_valid_hull(&hull));
    assert_eq!(hull.face_count, original.face_count);
    assert_eq!(hull.edge_count, original.edge_count);
    ensure_small(hull.volume - 8.0, 1e-4);
    ensure_small(hull.surface_area - 24.0, 1e-4);
    ensure_small(hull.inner_radius - 1.0, 1e-5);

    destroy_hull(hull);
    destroy_hull(original);
}

#[test]
fn clone_and_transform_cylinder_matches_original() {
    // Mirrors the C HullTransform sample: clone a cylinder under identity and
    // confirm the intrinsic properties survive the plane/bulk recompute.
    let original = create_cylinder(1.0, 0.5, 0.0, 9).expect("cylinder");
    let hull = clone_and_transform_hull(
        &original,
        TRANSFORM_IDENTITY,
        Vec3 {
            x: 1.0,
            y: 1.0,
            z: 1.0,
        },
    )
    .expect("cylinder clone");

    assert!(is_valid_hull(&hull));
    assert_eq!(hull.vertex_count, original.vertex_count);
    assert_eq!(hull.face_count, original.face_count);
    assert_eq!(hull.edge_count, original.edge_count);
    ensure_small((hull.volume - original.volume) / original.volume, 1e-4);
    ensure_small(
        (hull.surface_area - original.surface_area) / original.surface_area,
        1e-4,
    );
    ensure_small(hull.inner_radius - original.inner_radius, 1e-5);

    destroy_hull(hull);
    destroy_hull(original);
}