vpin 0.23.5

Rust library for working with Visual Pinball VPX files
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
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
//! Ramp mesh generation for expanded VPX export
//!
//! This module ports the ramp mesh generation from Visual Pinball's ramp.cpp.
//! Ramps can be either flat (with optional walls) or wire ramps (1-4 wire types).

use super::super::mesh::{
    RenderVertex3D, compute_normals, detail_level_to_accuracy, get_rg_vertex_3d,
};
use crate::vpx::TableDimensions;
use crate::vpx::gameitem::primitive::VertexWrapper;
use crate::vpx::gameitem::ramp::{Ramp, RampType};
use crate::vpx::gameitem::ramp_image_alignment::RampImageAlignment;
use crate::vpx::math::{Vec2, Vec3, get_rotated_axis};
use crate::vpx::model::Vertex3dNoTex2;
use crate::vpx::obj::VpxFace;

/// Compute the 2D outline vertices of the ramp along with heights and ratios
fn get_ramp_vertex(
    ramp: &Ramp,
    vvertex: &[RenderVertex3D],
    inc_width: bool,
) -> (Vec<Vec2>, Vec<f32>, Vec<f32>) {
    let cvertex = vvertex.len();
    if cvertex == 0 {
        return (vec![], vec![], vec![]);
    }

    let mut rgv_local: Vec<Vec2> = vec![Vec2 { x: 0.0, y: 0.0 }; cvertex * 2];
    let mut rgheight: Vec<f32> = vec![0.0; cvertex];
    let mut rgratio: Vec<f32> = vec![0.0; cvertex];

    // Compute an approximation to the length of the central curve
    let mut totallength = 0.0f32;
    for i in 0..(cvertex - 1) {
        let v1 = &vvertex[i];
        let v2 = &vvertex[i + 1];

        let dx = v1.x - v2.x;
        let dy = v1.y - v2.y;
        totallength += (dx * dx + dy * dy).sqrt();
    }

    let bottom_height = ramp.height_bottom;
    let top_height = ramp.height_top;

    let mut currentlength = 0.0f32;

    for i in 0..cvertex {
        // Clamp next and prev as ramps do not loop
        let vprev = &vvertex[if i > 0 { i - 1 } else { i }];
        let vnext = &vvertex[if i < cvertex - 1 { i + 1 } else { i }];
        let vmiddle = &vvertex[i];

        // Get normal at this point
        let v1normal = Vec2 {
            x: vprev.y - vmiddle.y,
            y: vmiddle.x - vprev.x,
        };
        let v2normal = Vec2 {
            x: vmiddle.y - vnext.y,
            y: vnext.x - vmiddle.x,
        };

        let vnormal = if i == cvertex - 1 {
            v1normal.normalize()
        } else if i == 0 {
            v2normal.normalize()
        } else {
            let v1n = v1normal.normalize();
            let v2n = v2normal.normalize();

            if (v1n.x - v2n.x).abs() < 0.0001 && (v1n.y - v2n.y).abs() < 0.0001 {
                // Two parallel segments
                v1n
            } else {
                // Find intersection of the two edges meeting at this point
                let a = vprev.y - vmiddle.y;
                let b = vmiddle.x - vprev.x;
                let c = a * (v1n.x - vprev.x) + b * (v1n.y - vprev.y);

                let d = vnext.y - vmiddle.y;
                let e = vmiddle.x - vnext.x;
                let f = d * (v2n.x - vnext.x) + e * (v2n.y - vnext.y);

                let det = a * e - b * d;
                let inv_det = if det != 0.0 { 1.0 / det } else { 0.0 };

                let intersectx = (b * f - e * c) * inv_det;
                let intersecty = (c * d - a * f) * inv_det;

                Vec2 {
                    x: vmiddle.x - intersectx,
                    y: vmiddle.y - intersecty,
                }
            }
        };

        // Update current length along the ramp
        {
            let dx = vprev.x - vmiddle.x;
            let dy = vprev.y - vmiddle.y;
            currentlength += (dx * dx + dy * dy).sqrt();
        }

        let percentage = if totallength > 0.0 {
            currentlength / totallength
        } else {
            0.0
        };
        let mut widthcur = percentage * (ramp.width_top - ramp.width_bottom) + ramp.width_bottom;

        rgheight[i] = vmiddle.z + percentage * (top_height - bottom_height) + bottom_height;
        rgratio[i] = 1.0 - percentage;

        // Handle wire ramp widths
        if is_habitrail(ramp) && ramp.ramp_type != RampType::OneWire {
            widthcur = ramp.wire_distance_x;
            if inc_width {
                widthcur += 20.0;
            }
        } else if ramp.ramp_type == RampType::OneWire {
            widthcur = ramp.wire_diameter;
        }

        let vmid = Vec2 {
            x: vmiddle.x,
            y: vmiddle.y,
        };
        rgv_local[i] = vmid + vnormal * (widthcur * 0.5);
        rgv_local[cvertex * 2 - i - 1] = vmid - vnormal * (widthcur * 0.5);
    }

    (rgv_local, rgheight, rgratio)
}

/// Check if the ramp is a wire ramp (habitrail)
fn is_habitrail(ramp: &Ramp) -> bool {
    matches!(
        ramp.ramp_type,
        RampType::FourWire
            | RampType::OneWire
            | RampType::TwoWire
            | RampType::ThreeWireLeft
            | RampType::ThreeWireRight
    )
}

/// Generate the flat ramp mesh (floor and walls)
fn build_flat_ramp_mesh(
    ramp: &Ramp,
    vvertex: &[RenderVertex3D],
    table_dims: &TableDimensions,
) -> Option<(Vec<VertexWrapper>, Vec<VpxFace>)> {
    let (rgv_local, rgheight, rgratio) = get_ramp_vertex(ramp, vvertex, true);
    let ramp_vertex = vvertex.len();

    if ramp_vertex < 2 {
        return None;
    }

    let num_vertices = ramp_vertex * 2;
    let rgi_offset = (ramp_vertex - 1) * 6;
    let num_indices = rgi_offset * 3; // floor + right wall + left wall

    let mut vertices: Vec<Vertex3dNoTex2> = vec![
        Vertex3dNoTex2 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
            nx: 0.0,
            ny: 0.0,
            nz: 0.0,
            tu: 0.0,
            tv: 0.0,
        };
        num_vertices * 3
    ];
    let mut indices: Vec<u32> = vec![0; num_indices];

    let has_image = !ramp.image.is_empty();

    // For world-aligned textures, we need to normalize coordinates to table dimensions
    // VPinball: rgv3D[0].tu = rgv3D[0].x * inv_tablewidth
    //           rgv3D[0].tv = rgv3D[0].y * inv_tableheight
    let inv_table_width = 1.0 / (table_dims.right - table_dims.left);
    let inv_table_height = 1.0 / (table_dims.bottom - table_dims.top);
    let use_world_coords = ramp.image_alignment == RampImageAlignment::World;

    // Generate floor vertices
    for i in 0..ramp_vertex {
        let offset = i * 2;
        vertices[offset].x = rgv_local[i].x;
        vertices[offset].y = rgv_local[i].y;
        vertices[offset].z = rgheight[i];

        vertices[offset + 1].x = rgv_local[ramp_vertex * 2 - i - 1].x;
        vertices[offset + 1].y = rgv_local[ramp_vertex * 2 - i - 1].y;
        vertices[offset + 1].z = rgheight[i];

        if has_image {
            if use_world_coords {
                // World-aligned texture coordinates (VPinball ramp.cpp line 2175-2180)
                vertices[offset].tu = vertices[offset].x * inv_table_width;
                vertices[offset].tv = vertices[offset].y * inv_table_height;
                vertices[offset + 1].tu = vertices[offset + 1].x * inv_table_width;
                vertices[offset + 1].tv = vertices[offset + 1].y * inv_table_height;
            } else {
                // Ramp-aligned (wrap) texture coordinates
                vertices[offset].tu = 1.0;
                vertices[offset].tv = rgratio[i];
                vertices[offset + 1].tu = 0.0;
                vertices[offset + 1].tv = rgratio[i];
            }
        }

        if i < ramp_vertex - 1 {
            // Floor indices
            let idx_offset = i * 6;
            indices[idx_offset] = (i * 2) as u32;
            indices[idx_offset + 1] = (i * 2 + 1) as u32;
            indices[idx_offset + 2] = (i * 2 + 3) as u32;
            indices[idx_offset + 3] = (i * 2) as u32;
            indices[idx_offset + 4] = (i * 2 + 3) as u32;
            indices[idx_offset + 5] = (i * 2 + 2) as u32;

            // Right wall indices
            let idx_offset_right = rgi_offset + i * 6;
            indices[idx_offset_right] = (i * 2 + num_vertices) as u32;
            indices[idx_offset_right + 1] = (i * 2 + num_vertices + 1) as u32;
            indices[idx_offset_right + 2] = (i * 2 + num_vertices + 3) as u32;
            indices[idx_offset_right + 3] = (i * 2 + num_vertices) as u32;
            indices[idx_offset_right + 4] = (i * 2 + num_vertices + 3) as u32;
            indices[idx_offset_right + 5] = (i * 2 + num_vertices + 2) as u32;

            // Left wall indices
            let idx_offset_left = rgi_offset * 2 + i * 6;
            indices[idx_offset_left] = (i * 2 + num_vertices * 2) as u32;
            indices[idx_offset_left + 1] = (i * 2 + num_vertices * 2 + 1) as u32;
            indices[idx_offset_left + 2] = (i * 2 + num_vertices * 2 + 3) as u32;
            indices[idx_offset_left + 3] = (i * 2 + num_vertices * 2) as u32;
            indices[idx_offset_left + 4] = (i * 2 + num_vertices * 2 + 3) as u32;
            indices[idx_offset_left + 5] = (i * 2 + num_vertices * 2 + 2) as u32;
        }
    }

    // Compute normals for floor
    compute_normals(&mut vertices[..num_vertices], &indices[..rgi_offset]);

    // Copy floor vertices to output buffer (offset 0)
    // Vertices are already in place

    // Generate right wall vertices (if visible)
    if ramp.right_wall_height_visible > 0.0 || ramp.left_wall_height_visible > 0.0 {
        // Right wall
        for i in 0..ramp_vertex {
            let offset = num_vertices + i * 2;
            vertices[offset].x = rgv_local[i].x;
            vertices[offset].y = rgv_local[i].y;
            vertices[offset].z = rgheight[i];

            vertices[offset + 1].x = rgv_local[i].x;
            vertices[offset + 1].y = rgv_local[i].y;
            vertices[offset + 1].z = rgheight[i] + ramp.right_wall_height_visible;

            if has_image && ramp.image_walls {
                vertices[offset].tu = 0.0;
                vertices[offset].tv = rgratio[i];
                vertices[offset + 1].tu = 0.0;
                vertices[offset + 1].tv = rgratio[i];
            }
        }
        compute_normals(
            &mut vertices[num_vertices..num_vertices * 2],
            &indices[..rgi_offset],
        );

        // Left wall
        for i in 0..ramp_vertex {
            let offset = num_vertices * 2 + i * 2;
            vertices[offset].x = rgv_local[ramp_vertex * 2 - i - 1].x;
            vertices[offset].y = rgv_local[ramp_vertex * 2 - i - 1].y;
            vertices[offset].z = rgheight[i];

            vertices[offset + 1].x = rgv_local[ramp_vertex * 2 - i - 1].x;
            vertices[offset + 1].y = rgv_local[ramp_vertex * 2 - i - 1].y;
            vertices[offset + 1].z = rgheight[i] + ramp.left_wall_height_visible;

            if has_image && ramp.image_walls {
                vertices[offset].tu = 0.0;
                vertices[offset].tv = rgratio[i];
                vertices[offset + 1].tu = 0.0;
                vertices[offset + 1].tv = rgratio[i];
            }
        }
        compute_normals(
            &mut vertices[num_vertices * 2..num_vertices * 3],
            &indices[..rgi_offset],
        );
    }

    // Determine which parts to include based on visibility
    let include_floor = true; // Floor is always included for flat ramps
    let include_right = ramp.right_wall_height_visible > 0.0;
    let include_left = ramp.left_wall_height_visible > 0.0;

    // Build final vertex and index lists
    let mut final_vertices = Vec::new();
    let mut final_indices = Vec::new();

    if include_floor {
        let base = final_vertices.len() as u32;
        for v in &vertices[..num_vertices] {
            final_vertices.push((*v).clone());
        }
        for &idx in &indices[..rgi_offset] {
            final_indices.push(base + idx);
        }
    }

    if include_right && include_left {
        let base = final_vertices.len() as u32;
        for v in &vertices[num_vertices..num_vertices * 2] {
            final_vertices.push((*v).clone());
        }
        for i in 0..rgi_offset {
            final_indices.push(base + indices[rgi_offset + i] - num_vertices as u32);
        }

        let base = final_vertices.len() as u32;
        for v in &vertices[num_vertices * 2..num_vertices * 3] {
            final_vertices.push((*v).clone());
        }
        for i in 0..rgi_offset {
            final_indices.push(base + indices[rgi_offset * 2 + i] - (num_vertices * 2) as u32);
        }
    } else if include_right {
        let base = final_vertices.len() as u32;
        for v in &vertices[num_vertices..num_vertices * 2] {
            final_vertices.push((*v).clone());
        }
        for i in 0..rgi_offset {
            final_indices.push(base + indices[rgi_offset + i] - num_vertices as u32);
        }
    } else if include_left {
        let base = final_vertices.len() as u32;
        for v in &vertices[num_vertices * 2..num_vertices * 3] {
            final_vertices.push((*v).clone());
        }
        for i in 0..rgi_offset {
            final_indices.push(base + indices[rgi_offset * 2 + i] - (num_vertices * 2) as u32);
        }
    }

    if final_vertices.is_empty() || final_indices.is_empty() {
        return None;
    }

    let wrapped = final_vertices
        .into_iter()
        .map(|vertex| VertexWrapper::new(vertex.to_vpx_bytes(), vertex))
        .collect();

    let faces = final_indices
        .chunks_exact(3)
        .map(|tri| VpxFace::new(tri[0] as i64, tri[1] as i64, tri[2] as i64))
        .collect();

    Some((wrapped, faces))
}

/// Create a wire mesh for wire ramps
fn create_wire(
    ramp: &Ramp,
    num_rings: usize,
    num_segments: usize,
    mid_points: &[Vec2],
    heights: &[f32],
) -> Vec<Vertex3dNoTex2> {
    let mut vertices = vec![
        Vertex3dNoTex2 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
            nx: 0.0,
            ny: 0.0,
            nz: 0.0,
            tu: 0.0,
            tv: 0.0,
        };
        num_rings * num_segments
    ];

    let mut prev_binorm = Vec3 {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    };

    let inv_num_rings = 1.0 / num_rings as f32;
    let inv_num_segments = 1.0 / num_segments as f32;

    for i in 0..num_rings {
        let i2 = if i == num_rings - 1 { i } else { i + 1 };
        let height = heights[i];

        let tangent = if i == num_rings - 1 && i > 0 {
            Vec3 {
                x: mid_points[i].x - mid_points[i - 1].x,
                y: mid_points[i].y - mid_points[i - 1].y,
                z: heights[i] - heights[i - 1],
            }
        } else {
            Vec3 {
                x: mid_points[i2].x - mid_points[i].x,
                y: mid_points[i2].y - mid_points[i].y,
                z: heights[i2] - height,
            }
        };

        let (normal, binorm) = if i == 0 {
            let up = Vec3 {
                x: mid_points[i2].x + mid_points[i].x,
                y: mid_points[i2].y + mid_points[i].y,
                z: heights[i2] - height,
            };
            let normal = Vec3::cross(&tangent, &up);
            let binorm = Vec3::cross(&tangent, &normal);
            (normal, binorm)
        } else {
            let normal = Vec3::cross(&prev_binorm, &tangent);
            let binorm = Vec3::cross(&tangent, &normal);
            (normal, binorm)
        };

        let binorm = binorm.normalize();
        let normal = normal.normalize();
        prev_binorm = binorm;

        let u = i as f32 * inv_num_rings;
        for j in 0..num_segments {
            let index = i * num_segments + j;
            let v = (j as f32 + u) * inv_num_segments;
            let angle = j as f32 * 360.0 * inv_num_segments;

            let tmp = get_rotated_axis(angle, &tangent, &normal) * (ramp.wire_diameter * 0.5);

            vertices[index].x = mid_points[i].x + tmp.x;
            vertices[index].y = mid_points[i].y + tmp.y;
            vertices[index].z = height + tmp.z;
            vertices[index].tu = u;
            vertices[index].tv = v;

            // Normal points outward from center
            let n = Vec3 {
                x: vertices[index].x - mid_points[i].x,
                y: vertices[index].y - mid_points[i].y,
                z: vertices[index].z - height,
            };
            let len = n.length();
            if len > 0.0 {
                vertices[index].nx = n.x / len;
                vertices[index].ny = n.y / len;
                vertices[index].nz = n.z / len;
            }
        }
    }

    vertices
}

/// Generate wire ramp mesh
fn build_wire_ramp_mesh(
    ramp: &Ramp,
    vvertex: &[RenderVertex3D],
) -> Option<(Vec<VertexWrapper>, Vec<VpxFace>)> {
    let (rgv_local, rgheight, _) = get_ramp_vertex(ramp, vvertex, false);
    let num_rings = vvertex.len();

    if num_rings < 2 {
        return None;
    }

    // Determine accuracy/segments based on detail level (use 8 as default)
    let num_segments = 8;

    // Get middle points (center of ramp)
    let mut mid_points: Vec<Vec2> = Vec::with_capacity(num_rings);
    for i in 0..num_rings {
        let left_idx = num_rings * 2 - i - 1;
        mid_points.push(Vec2 {
            x: (rgv_local[i].x + rgv_local[left_idx].x) * 0.5,
            y: (rgv_local[i].y + rgv_local[left_idx].y) * 0.5,
        });
    }

    // Get left side points (reversed)
    let mut left_points: Vec<Vec2> = Vec::with_capacity(num_rings);
    for i in 0..num_rings {
        left_points.push(rgv_local[num_rings * 2 - i - 1]);
    }

    let num_vertices_per_wire = num_rings * num_segments;
    let num_indices_per_wire = 6 * (num_rings - 1) * num_segments;

    // Generate wire indices (same for all wires)
    let mut wire_indices: Vec<u32> = vec![0; num_indices_per_wire];
    for i in 0..(num_rings - 1) {
        for j in 0..num_segments {
            let mut quad = [0u32; 4];
            quad[0] = (i * num_segments + j) as u32;
            quad[1] = if j != num_segments - 1 {
                (i * num_segments + j + 1) as u32
            } else {
                (i * num_segments) as u32
            };

            if i != num_rings - 1 {
                quad[2] = ((i + 1) * num_segments + j) as u32;
                quad[3] = if j != num_segments - 1 {
                    ((i + 1) * num_segments + j + 1) as u32
                } else {
                    ((i + 1) * num_segments) as u32
                };
            } else {
                quad[2] = j as u32;
                quad[3] = if j != num_segments - 1 {
                    (j + 1) as u32
                } else {
                    0
                };
            }

            let offs = (i * num_segments + j) * 6;
            wire_indices[offs] = quad[0];
            wire_indices[offs + 1] = quad[1];
            wire_indices[offs + 2] = quad[2];
            wire_indices[offs + 3] = quad[3];
            wire_indices[offs + 4] = quad[2];
            wire_indices[offs + 5] = quad[1];
        }
    }

    // Build mesh based on ramp type
    let (final_vertices, final_indices) = match ramp.ramp_type {
        RampType::OneWire => {
            let vertices = create_wire(ramp, num_rings, num_segments, &mid_points, &rgheight);
            (vertices, wire_indices)
        }
        RampType::TwoWire => {
            let right_wire = create_wire(
                ramp,
                num_rings,
                num_segments,
                &rgv_local[..num_rings],
                &rgheight,
            );
            let left_wire = create_wire(ramp, num_rings, num_segments, &left_points, &rgheight);

            let mut vertices = Vec::with_capacity(num_vertices_per_wire * 2);
            for mut v in right_wire {
                v.z += 3.0; // Raise wire
                vertices.push(v);
            }
            for mut v in left_wire {
                v.z += 3.0;
                vertices.push(v);
            }

            let mut indices = Vec::with_capacity(num_indices_per_wire * 2);
            indices.extend_from_slice(&wire_indices);
            for &idx in &wire_indices {
                indices.push(idx + num_vertices_per_wire as u32);
            }

            (vertices, indices)
        }
        RampType::ThreeWireLeft => {
            let right_wire = create_wire(
                ramp,
                num_rings,
                num_segments,
                &rgv_local[..num_rings],
                &rgheight,
            );
            let left_wire = create_wire(ramp, num_rings, num_segments, &left_points, &rgheight);
            let upper_left = create_wire(ramp, num_rings, num_segments, &left_points, &rgheight);

            let mut vertices = Vec::with_capacity(num_vertices_per_wire * 3);
            for mut v in right_wire {
                v.z += 3.0;
                vertices.push(v);
            }
            for mut v in left_wire {
                v.z += 3.0;
                vertices.push(v);
            }
            for mut v in upper_left {
                v.z += ramp.wire_distance_y * 0.5;
                vertices.push(v);
            }

            let mut indices = Vec::with_capacity(num_indices_per_wire * 3);
            indices.extend_from_slice(&wire_indices);
            for &idx in &wire_indices {
                indices.push(idx + num_vertices_per_wire as u32);
            }
            for &idx in &wire_indices {
                indices.push(idx + (num_vertices_per_wire * 2) as u32);
            }

            (vertices, indices)
        }
        RampType::ThreeWireRight => {
            let right_wire = create_wire(
                ramp,
                num_rings,
                num_segments,
                &rgv_local[..num_rings],
                &rgheight,
            );
            let left_wire = create_wire(ramp, num_rings, num_segments, &left_points, &rgheight);
            let upper_right = create_wire(
                ramp,
                num_rings,
                num_segments,
                &rgv_local[..num_rings],
                &rgheight,
            );

            let mut vertices = Vec::with_capacity(num_vertices_per_wire * 3);
            for mut v in right_wire {
                v.z += 3.0;
                vertices.push(v);
            }
            for mut v in left_wire {
                v.z += 3.0;
                vertices.push(v);
            }
            for mut v in upper_right {
                v.z += ramp.wire_distance_y * 0.5;
                vertices.push(v);
            }

            let mut indices = Vec::with_capacity(num_indices_per_wire * 3);
            indices.extend_from_slice(&wire_indices);
            for &idx in &wire_indices {
                indices.push(idx + num_vertices_per_wire as u32);
            }
            for &idx in &wire_indices {
                indices.push(idx + (num_vertices_per_wire * 2) as u32);
            }

            (vertices, indices)
        }
        RampType::FourWire => {
            let right_wire = create_wire(
                ramp,
                num_rings,
                num_segments,
                &rgv_local[..num_rings],
                &rgheight,
            );
            let left_wire = create_wire(ramp, num_rings, num_segments, &left_points, &rgheight);
            let upper_right = create_wire(
                ramp,
                num_rings,
                num_segments,
                &rgv_local[..num_rings],
                &rgheight,
            );
            let upper_left = create_wire(ramp, num_rings, num_segments, &left_points, &rgheight);

            let mut vertices = Vec::with_capacity(num_vertices_per_wire * 4);
            for mut v in right_wire {
                v.z += 3.0;
                vertices.push(v);
            }
            for mut v in left_wire {
                v.z += 3.0;
                vertices.push(v);
            }
            for mut v in upper_right {
                v.z += ramp.wire_distance_y * 0.5;
                vertices.push(v);
            }
            for mut v in upper_left {
                v.z += ramp.wire_distance_y * 0.5;
                vertices.push(v);
            }

            let mut indices = Vec::with_capacity(num_indices_per_wire * 4);
            indices.extend_from_slice(&wire_indices);
            for &idx in &wire_indices {
                indices.push(idx + num_vertices_per_wire as u32);
            }
            for &idx in &wire_indices {
                indices.push(idx + (num_vertices_per_wire * 2) as u32);
            }
            for &idx in &wire_indices {
                indices.push(idx + (num_vertices_per_wire * 3) as u32);
            }

            (vertices, indices)
        }
        RampType::Flat => {
            // This shouldn't happen as we handle flat ramps separately
            return None;
        }
    };

    if final_vertices.is_empty() || final_indices.is_empty() {
        return None;
    }

    let wrapped = final_vertices
        .into_iter()
        .map(|vertex| VertexWrapper::new(vertex.to_vpx_bytes(), vertex))
        .collect();

    let faces = final_indices
        .chunks_exact(3)
        .map(|tri| VpxFace::new(tri[0] as i64, tri[1] as i64, tri[2] as i64))
        .collect();

    Some((wrapped, faces))
}

/// Get the surface height of a ramp at a given (x, y) position.
///
/// Ported from VPinball's `Ramp::GetSurfaceHeight(float x, float y)` in ramp.cpp.
///
/// This finds the closest point on the ramp's central curve to the given position,
/// then interpolates the height based on the distance along the curve.
pub(crate) fn get_ramp_surface_height(ramp: &Ramp, x: f32, y: f32) -> f32 {
    let accuracy = detail_level_to_accuracy(10.0);
    let vvertex = get_rg_vertex_3d(&ramp.drag_points, accuracy);

    if vvertex.len() < 2 {
        return 0.0;
    }

    let result = super::closest_point_on_polyline(&vvertex, x, y);

    let Some((v_out, i_seg)) = result else {
        return 0.0; // Object is not on ramp path
    };

    // Go through vertices counting lengths until iSeg
    let cvertex = vvertex.len();
    let mut totallength = 0.0_f32;
    let mut startlength = 0.0_f32;

    for i2 in 1..cvertex {
        let dx = vvertex[i2].x - vvertex[i2 - 1].x;
        let dy = vvertex[i2].y - vvertex[i2 - 1].y;
        let len = (dx * dx + dy * dy).sqrt();
        if i2 <= i_seg {
            startlength += len;
        }
        totallength += len;
    }

    // Add the distance from the segment start to the closest point
    let dx = v_out.x - vvertex[i_seg].x;
    let dy = v_out.y - vvertex[i_seg].y;
    let len = (dx * dx + dy * dy).sqrt();
    startlength += len;

    let top_height = ramp.height_top;
    let bottom_height = ramp.height_bottom;

    if totallength > 0.0 {
        vvertex[i_seg].z
            + (startlength / totallength) * (top_height - bottom_height)
            + bottom_height
    } else {
        bottom_height
    }
}

/// Build the complete ramp mesh
pub(crate) fn build_ramp_mesh(
    ramp: &Ramp,
    table_dims: &TableDimensions,
) -> Option<(Vec<VertexWrapper>, Vec<VpxFace>)> {
    // Generate meshes for all ramps, including invisible ones
    // This is useful for tools that need to visualize or process all geometry

    if ramp.width_bottom == 0.0 && ramp.width_top == 0.0 {
        return None;
    }

    // From VPinball mesh.h GetRgVertex: accuracy = 4.0 is highest detail level
    // detail_level_to_accuracy(10.0) = 4.0
    let accuracy = detail_level_to_accuracy(10.0);
    let vvertex = get_rg_vertex_3d(&ramp.drag_points, accuracy);

    if vvertex.len() < 2 {
        return None;
    }

    if is_habitrail(ramp) {
        build_wire_ramp_mesh(ramp, &vvertex)
    } else {
        build_flat_ramp_mesh(ramp, &vvertex, table_dims)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vpx::gameitem::dragpoint::DragPoint;

    #[test]
    fn test_catmull_curve() {
        let v0 = RenderVertex3D {
            x: 0.0,
            y: 0.0,
            z: 0.0,
            smooth: false,
            control_point: true,
            ..Default::default()
        };
        let v1 = RenderVertex3D {
            x: 1.0,
            y: 0.0,
            z: 0.0,
            smooth: false,
            control_point: true,
            ..Default::default()
        };
        let v2 = RenderVertex3D {
            x: 2.0,
            y: 1.0,
            z: 0.0,
            smooth: false,
            control_point: true,
            ..Default::default()
        };
        let v3 = RenderVertex3D {
            x: 3.0,
            y: 1.0,
            z: 0.0,
            smooth: false,
            control_point: true,
            ..Default::default()
        };

        let curve = crate::vpx::mesh::CatmullCurve3D::new(&v0, &v1, &v2, &v3);
        let (x, y, z) = curve.get_point_at(0.0);
        assert!((x - 1.0).abs() < 0.01);
        assert!((y - 0.0).abs() < 0.01);
        assert!((z - 0.0).abs() < 0.01);

        let (x, y, z) = curve.get_point_at(1.0);
        assert!((x - 2.0).abs() < 0.01);
        assert!((y - 1.0).abs() < 0.01);
        assert!((z - 0.0).abs() < 0.01);
    }

    #[test]
    fn test_simple_ramp() {
        let ramp = Ramp {
            drag_points: vec![
                DragPoint {
                    x: 0.0,
                    y: 0.0,
                    z: 0.0,
                    smooth: false,
                    ..Default::default()
                },
                DragPoint {
                    x: 100.0,
                    y: 0.0,
                    z: 0.0,
                    smooth: false,
                    ..Default::default()
                },
            ],
            ..Default::default()
        };

        let result = build_ramp_mesh(&ramp, &TableDimensions::new(0.0, 0.0, 1000.0, 2000.0));
        assert!(result.is_some());

        let (vertices, indices) = result.unwrap();
        assert!(!vertices.is_empty());
        assert!(!indices.is_empty());
    }

    #[test]
    fn test_one_wire_ramp_with_smoothing() {
        // Test a one-wire ramp with smooth corners
        // This ensures the Catmull-Rom smoothing is properly applied
        let ramp = Ramp {
            ramp_type: RampType::OneWire,
            wire_diameter: 6.0,
            drag_points: vec![
                DragPoint {
                    x: 0.0,
                    y: 0.0,
                    z: 0.0,
                    smooth: true,
                    ..Default::default()
                },
                DragPoint {
                    x: 50.0,
                    y: 50.0,
                    z: 10.0,
                    smooth: true,
                    ..Default::default()
                },
                DragPoint {
                    x: 100.0,
                    y: 0.0,
                    z: 20.0,
                    smooth: true,
                    ..Default::default()
                },
            ],
            ..Default::default()
        };

        let result = build_ramp_mesh(&ramp, &TableDimensions::new(0.0, 0.0, 1000.0, 2000.0));
        assert!(result.is_some(), "One-wire ramp should generate mesh");

        let (vertices, indices) = result.unwrap();
        assert!(!vertices.is_empty(), "Should have vertices");
        assert!(!indices.is_empty(), "Should have faces");

        // With smoothing, we should have more than 3 rings (original control points)
        // The exact number depends on accuracy, but should be > 3 due to subdivision
        let num_segments = 8;
        let num_vertices = vertices.len();
        let num_rings = num_vertices / num_segments;
        assert!(
            num_rings > 3,
            "Smoothed one-wire ramp should have more rings than control points due to Catmull-Rom subdivision, got {} rings",
            num_rings
        );
    }

    #[test]
    fn test_one_wire_ramp_without_smoothing() {
        // Test a one-wire ramp with non-smooth corners
        // This should have fewer vertices since no subdivision occurs
        let ramp = Ramp {
            ramp_type: RampType::OneWire,
            wire_diameter: 6.0,
            drag_points: vec![
                DragPoint {
                    x: 0.0,
                    y: 0.0,
                    z: 0.0,
                    smooth: false,
                    ..Default::default()
                },
                DragPoint {
                    x: 50.0,
                    y: 50.0,
                    z: 10.0,
                    smooth: false,
                    ..Default::default()
                },
                DragPoint {
                    x: 100.0,
                    y: 0.0,
                    z: 20.0,
                    smooth: false,
                    ..Default::default()
                },
            ],
            ..Default::default()
        };

        let result = build_ramp_mesh(&ramp, &TableDimensions::new(0.0, 0.0, 1000.0, 2000.0));
        assert!(result.is_some(), "One-wire ramp should generate mesh");

        let (vertices, _) = result.unwrap();
        assert!(!vertices.is_empty(), "Should have vertices");
    }
}