ifc-lite-wasm 2.1.2

WebAssembly bindings for IFC-Lite
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! GPU-ready geometry data structures for zero-copy WebGPU upload
//!
//! This module provides geometry data structures that are pre-processed for direct
//! GPU upload without intermediate copies. Data is:
//! - Interleaved (position + normal per vertex)
//! - Coordinate-converted (Z-up to Y-up)
//! - Stored contiguously for efficient memory access
//!
//! # Zero-Copy Pattern
//!
//! ```javascript
//! // Get GPU-ready geometry from WASM
//! const gpuGeom = api.parseToGpuGeometry(ifcData);
//!
//! // Get WASM memory buffer
//! const memory = api.getMemory();
//!
//! // Create views directly into WASM memory (NO COPY!)
//! const vertexView = new Float32Array(
//!   memory.buffer,
//!   gpuGeom.vertexDataPtr,
//!   gpuGeom.vertexDataLen
//! );
//!
//! // Upload directly to GPU (single copy: WASM → GPU)
//! device.queue.writeBuffer(gpuBuffer, 0, vertexView);
//!
//! // IMPORTANT: Free the geometry when done (allows WASM to reuse memory)
//! gpuGeom.free();
//! ```

use wasm_bindgen::prelude::*;

/// Metadata for a single mesh within the GPU geometry buffer
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct GpuMeshMetadata {
    /// Express ID of the IFC entity
    express_id: u32,
    /// Index into the IFC type string table
    ifc_type_idx: u16,
    /// Offset in vertex_data (in floats, not bytes)
    vertex_offset: u32,
    /// Number of vertices
    vertex_count: u32,
    /// Offset in indices array
    index_offset: u32,
    /// Number of indices
    index_count: u32,
    /// RGBA color
    color: [f32; 4],
}

#[wasm_bindgen]
impl GpuMeshMetadata {
    #[wasm_bindgen(getter, js_name = expressId)]
    pub fn express_id(&self) -> u32 {
        self.express_id
    }

    #[wasm_bindgen(getter, js_name = ifcTypeIdx)]
    pub fn ifc_type_idx(&self) -> u16 {
        self.ifc_type_idx
    }

    #[wasm_bindgen(getter, js_name = vertexOffset)]
    pub fn vertex_offset(&self) -> u32 {
        self.vertex_offset
    }

    #[wasm_bindgen(getter, js_name = vertexCount)]
    pub fn vertex_count(&self) -> u32 {
        self.vertex_count
    }

    #[wasm_bindgen(getter, js_name = indexOffset)]
    pub fn index_offset(&self) -> u32 {
        self.index_offset
    }

    #[wasm_bindgen(getter, js_name = indexCount)]
    pub fn index_count(&self) -> u32 {
        self.index_count
    }

    #[wasm_bindgen(getter)]
    pub fn color(&self) -> Vec<f32> {
        self.color.to_vec()
    }
}

/// GPU-ready geometry stored in WASM linear memory
///
/// Data layout:
/// - vertex_data: Interleaved [px, py, pz, nx, ny, nz, ...] (6 floats per vertex)
/// - indices: Triangle indices [i0, i1, i2, ...]
/// - mesh_metadata: Per-mesh metadata for draw calls
///
/// All coordinates are pre-converted from IFC Z-up to WebGL Y-up
#[wasm_bindgen]
pub struct GpuGeometry {
    /// Interleaved vertex data: [px, py, pz, nx, ny, nz, ...]
    /// Already converted from Z-up to Y-up
    vertex_data: Vec<f32>,

    /// Triangle indices
    indices: Vec<u32>,

    /// Metadata per mesh (for selection, draw call ranges, etc.)
    mesh_metadata: Vec<GpuMeshMetadata>,

    /// IFC type names (deduplicated)
    ifc_type_names: Vec<String>,

    /// RTC (Relative To Center) offset applied to coordinates
    /// Used for models with large world coordinates (>10km from origin)
    rtc_offset_x: f64,
    rtc_offset_y: f64,
    rtc_offset_z: f64,
}

#[wasm_bindgen]
impl GpuGeometry {
    /// Create a new empty GPU geometry container
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self {
            vertex_data: Vec::new(),
            indices: Vec::new(),
            mesh_metadata: Vec::new(),
            ifc_type_names: Vec::new(),
            rtc_offset_x: 0.0,
            rtc_offset_y: 0.0,
            rtc_offset_z: 0.0,
        }
    }

    /// Set the RTC (Relative To Center) offset applied to coordinates
    pub fn set_rtc_offset(&mut self, x: f64, y: f64, z: f64) {
        self.rtc_offset_x = x;
        self.rtc_offset_y = y;
        self.rtc_offset_z = z;
    }

    /// Get X component of RTC offset
    #[wasm_bindgen(getter, js_name = rtcOffsetX)]
    pub fn rtc_offset_x(&self) -> f64 {
        self.rtc_offset_x
    }

    /// Get Y component of RTC offset
    #[wasm_bindgen(getter, js_name = rtcOffsetY)]
    pub fn rtc_offset_y(&self) -> f64 {
        self.rtc_offset_y
    }

    /// Get Z component of RTC offset
    #[wasm_bindgen(getter, js_name = rtcOffsetZ)]
    pub fn rtc_offset_z(&self) -> f64 {
        self.rtc_offset_z
    }

    /// Check if RTC offset is active (non-zero)
    #[wasm_bindgen(getter, js_name = hasRtcOffset)]
    pub fn has_rtc_offset(&self) -> bool {
        self.rtc_offset_x != 0.0 || self.rtc_offset_y != 0.0 || self.rtc_offset_z != 0.0
    }

    /// Get pointer to vertex data for zero-copy view
    ///
    /// SAFETY: View is only valid until next WASM allocation!
    /// Create view, upload to GPU, then discard view immediately.
    #[wasm_bindgen(getter, js_name = vertexDataPtr)]
    pub fn vertex_data_ptr(&self) -> *const f32 {
        self.vertex_data.as_ptr()
    }

    /// Get length of vertex data array (in f32 elements, not bytes)
    #[wasm_bindgen(getter, js_name = vertexDataLen)]
    pub fn vertex_data_len(&self) -> usize {
        self.vertex_data.len()
    }

    /// Get byte length of vertex data (for GPU buffer creation)
    #[wasm_bindgen(getter, js_name = vertexDataByteLength)]
    pub fn vertex_data_byte_length(&self) -> usize {
        self.vertex_data.len() * 4 // f32 = 4 bytes
    }

    /// Get pointer to indices array for zero-copy view
    #[wasm_bindgen(getter, js_name = indicesPtr)]
    pub fn indices_ptr(&self) -> *const u32 {
        self.indices.as_ptr()
    }

    /// Get length of indices array (in u32 elements)
    #[wasm_bindgen(getter, js_name = indicesLen)]
    pub fn indices_len(&self) -> usize {
        self.indices.len()
    }

    /// Get byte length of indices (for GPU buffer creation)
    #[wasm_bindgen(getter, js_name = indicesByteLength)]
    pub fn indices_byte_length(&self) -> usize {
        self.indices.len() * 4 // u32 = 4 bytes
    }

    /// Get number of meshes in this geometry batch
    #[wasm_bindgen(getter, js_name = meshCount)]
    pub fn mesh_count(&self) -> usize {
        self.mesh_metadata.len()
    }

    /// Get total vertex count
    #[wasm_bindgen(getter, js_name = totalVertexCount)]
    pub fn total_vertex_count(&self) -> usize {
        self.vertex_data.len() / 6 // 6 floats per vertex (pos + normal)
    }

    /// Get total triangle count
    #[wasm_bindgen(getter, js_name = totalTriangleCount)]
    pub fn total_triangle_count(&self) -> usize {
        self.indices.len() / 3
    }

    /// Get metadata for a specific mesh
    #[wasm_bindgen(js_name = getMeshMetadata)]
    pub fn get_mesh_metadata(&self, index: usize) -> Option<GpuMeshMetadata> {
        self.mesh_metadata.get(index).cloned()
    }

    /// Get IFC type name by index
    #[wasm_bindgen(js_name = getIfcTypeName)]
    pub fn get_ifc_type_name(&self, index: u16) -> Option<String> {
        self.ifc_type_names.get(index as usize).cloned()
    }

    /// Check if geometry is empty
    #[wasm_bindgen(getter, js_name = isEmpty)]
    pub fn is_empty(&self) -> bool {
        self.vertex_data.is_empty()
    }
}

impl GpuGeometry {
    /// Create with pre-allocated capacity
    pub fn with_capacity(vertex_capacity: usize, index_capacity: usize) -> Self {
        Self {
            vertex_data: Vec::with_capacity(vertex_capacity),
            indices: Vec::with_capacity(index_capacity),
            mesh_metadata: Vec::with_capacity(256),
            ifc_type_names: Vec::with_capacity(64),
            rtc_offset_x: 0.0,
            rtc_offset_y: 0.0,
            rtc_offset_z: 0.0,
        }
    }

    /// Add a mesh with positions and normals, interleaving and converting coordinates
    pub fn add_mesh(
        &mut self,
        express_id: u32,
        ifc_type: &str,
        positions: &[f32],
        normals: &[f32],
        indices: &[u32],
        color: [f32; 4],
    ) {
        let vertex_count = positions.len() / 3;
        if vertex_count == 0 {
            return;
        }

        // Safety: ensure normals match positions length
        if normals.len() < positions.len() {
            return;
        }

        // Get or add IFC type name
        let ifc_type_idx = self.get_or_add_ifc_type(ifc_type);

        // Record current offsets
        let vertex_offset = (self.vertex_data.len() / 6) as u32;
        let index_offset = self.indices.len() as u32;

        // Interleave positions and normals with coordinate conversion
        // Layout: [px, py, pz, nx, ny, nz] per vertex
        self.vertex_data.reserve(vertex_count * 6);

        for i in 0..vertex_count {
            let pi = i * 3;

            // Bounds check (positions.len() may not be exact multiple of 3)
            if pi + 2 >= positions.len() || pi + 2 >= normals.len() {
                break;
            }

            // Position (convert Z-up to Y-up)
            let px = positions[pi];
            let py = positions[pi + 2]; // New Y = old Z
            let pz = -positions[pi + 1]; // New Z = -old Y

            // Normal (convert Z-up to Y-up)
            let nx = normals[pi];
            let ny = normals[pi + 2]; // New Y = old Z
            let nz = -normals[pi + 1]; // New Z = -old Y

            self.vertex_data.push(px);
            self.vertex_data.push(py);
            self.vertex_data.push(pz);
            self.vertex_data.push(nx);
            self.vertex_data.push(ny);
            self.vertex_data.push(nz);
        }

        // Add indices (offset by current vertex count)
        self.indices.reserve(indices.len());
        for &idx in indices {
            self.indices.push(idx + vertex_offset);
        }

        // Add metadata
        self.mesh_metadata.push(GpuMeshMetadata {
            express_id,
            ifc_type_idx,
            vertex_offset,
            vertex_count: vertex_count as u32,
            index_offset,
            index_count: indices.len() as u32,
            color,
        });
    }

    /// Get or add an IFC type name to the string table
    fn get_or_add_ifc_type(&mut self, ifc_type: &str) -> u16 {
        // Check if already exists
        for (i, name) in self.ifc_type_names.iter().enumerate() {
            if name == ifc_type {
                return i as u16;
            }
        }

        // Add new
        let idx = self.ifc_type_names.len() as u16;
        self.ifc_type_names.push(ifc_type.to_string());
        idx
    }

    /// Clear all data (for reuse)
    pub fn clear(&mut self) {
        self.vertex_data.clear();
        self.indices.clear();
        self.mesh_metadata.clear();
        // Keep ifc_type_names for reuse
    }
}

impl Default for GpuGeometry {
    fn default() -> Self {
        Self::new()
    }
}

/// GPU-ready instanced geometry for efficient rendering of repeated shapes
///
/// Data layout:
/// - vertex_data: Interleaved [px, py, pz, nx, ny, nz, ...] (shared geometry)
/// - indices: Triangle indices (shared geometry)
/// - instance_data: [transform (16 floats) + color (4 floats)] per instance = 20 floats
#[wasm_bindgen]
pub struct GpuInstancedGeometry {
    /// Geometry ID (hash of the geometry for deduplication)
    geometry_id: u64,

    /// Interleaved vertex data for shared geometry
    vertex_data: Vec<f32>,

    /// Triangle indices for shared geometry
    indices: Vec<u32>,

    /// Instance data: [transform (16 floats) + color (4 floats)] per instance
    instance_data: Vec<f32>,

    /// Express IDs for each instance (for selection)
    instance_express_ids: Vec<u32>,
}

#[wasm_bindgen]
impl GpuInstancedGeometry {
    /// Create new instanced geometry
    #[wasm_bindgen(constructor)]
    pub fn new(geometry_id: u64) -> Self {
        Self {
            geometry_id,
            vertex_data: Vec::new(),
            indices: Vec::new(),
            instance_data: Vec::new(),
            instance_express_ids: Vec::new(),
        }
    }

    #[wasm_bindgen(getter, js_name = geometryId)]
    pub fn geometry_id(&self) -> u64 {
        self.geometry_id
    }

    // Vertex data pointers
    #[wasm_bindgen(getter, js_name = vertexDataPtr)]
    pub fn vertex_data_ptr(&self) -> *const f32 {
        self.vertex_data.as_ptr()
    }

    #[wasm_bindgen(getter, js_name = vertexDataLen)]
    pub fn vertex_data_len(&self) -> usize {
        self.vertex_data.len()
    }

    #[wasm_bindgen(getter, js_name = vertexDataByteLength)]
    pub fn vertex_data_byte_length(&self) -> usize {
        self.vertex_data.len() * 4
    }

    // Indices pointers
    #[wasm_bindgen(getter, js_name = indicesPtr)]
    pub fn indices_ptr(&self) -> *const u32 {
        self.indices.as_ptr()
    }

    #[wasm_bindgen(getter, js_name = indicesLen)]
    pub fn indices_len(&self) -> usize {
        self.indices.len()
    }

    #[wasm_bindgen(getter, js_name = indicesByteLength)]
    pub fn indices_byte_length(&self) -> usize {
        self.indices.len() * 4
    }

    // Instance data pointers
    #[wasm_bindgen(getter, js_name = instanceDataPtr)]
    pub fn instance_data_ptr(&self) -> *const f32 {
        self.instance_data.as_ptr()
    }

    #[wasm_bindgen(getter, js_name = instanceDataLen)]
    pub fn instance_data_len(&self) -> usize {
        self.instance_data.len()
    }

    #[wasm_bindgen(getter, js_name = instanceDataByteLength)]
    pub fn instance_data_byte_length(&self) -> usize {
        self.instance_data.len() * 4
    }

    // Instance express IDs pointer
    #[wasm_bindgen(getter, js_name = instanceExpressIdsPtr)]
    pub fn instance_express_ids_ptr(&self) -> *const u32 {
        self.instance_express_ids.as_ptr()
    }

    #[wasm_bindgen(getter, js_name = instanceCount)]
    pub fn instance_count(&self) -> usize {
        self.instance_express_ids.len()
    }

    #[wasm_bindgen(getter, js_name = vertexCount)]
    pub fn vertex_count(&self) -> usize {
        self.vertex_data.len() / 6
    }

    #[wasm_bindgen(getter, js_name = triangleCount)]
    pub fn triangle_count(&self) -> usize {
        self.indices.len() / 3
    }
}

impl GpuInstancedGeometry {
    /// Set shared geometry with interleaving and coordinate conversion
    pub fn set_geometry(&mut self, positions: &[f32], normals: &[f32], indices: &[u32]) {
        // Always reset first so invalid input cannot keep stale geometry.
        self.vertex_data.clear();
        self.indices.clear();

        // Safety: ensure normals match positions length
        if normals.len() < positions.len() {
            return;
        }

        let vertex_count = positions.len() / 3;
        self.vertex_data.reserve(vertex_count * 6);
        self.indices.reserve(indices.len());

        // Interleave with Z-up to Y-up conversion
        for i in 0..vertex_count {
            let pi = i * 3;

            // Bounds check
            if pi + 2 >= positions.len() || pi + 2 >= normals.len() {
                break;
            }

            // Position (convert Z-up to Y-up)
            self.vertex_data.push(positions[pi]);
            self.vertex_data.push(positions[pi + 2]); // New Y = old Z
            self.vertex_data.push(-positions[pi + 1]); // New Z = -old Y

            // Normal (convert Z-up to Y-up)
            self.vertex_data.push(normals[pi]);
            self.vertex_data.push(normals[pi + 2]); // New Y = old Z
            self.vertex_data.push(-normals[pi + 1]); // New Z = -old Y
        }

        // Copy indices directly
        self.indices.extend_from_slice(indices);
    }

    /// Add an instance with transform and color
    pub fn add_instance(&mut self, express_id: u32, transform: &[f32; 16], color: [f32; 4]) {
        // Add transform (16 floats)
        self.instance_data.extend_from_slice(transform);

        // Add color (4 floats)
        self.instance_data.extend_from_slice(&color);

        // Track express ID
        self.instance_express_ids.push(express_id);
    }
}

/// Collection of GPU-ready instanced geometries
#[wasm_bindgen]
pub struct GpuInstancedGeometryCollection {
    geometries: Vec<GpuInstancedGeometry>,
}

#[wasm_bindgen]
impl GpuInstancedGeometryCollection {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self {
            geometries: Vec::new(),
        }
    }

    #[wasm_bindgen(getter)]
    pub fn length(&self) -> usize {
        self.geometries.len()
    }

    #[wasm_bindgen]
    pub fn get(&self, index: usize) -> Option<GpuInstancedGeometry> {
        self.geometries.get(index).map(|g| GpuInstancedGeometry {
            geometry_id: g.geometry_id,
            vertex_data: g.vertex_data.clone(),
            indices: g.indices.clone(),
            instance_data: g.instance_data.clone(),
            instance_express_ids: g.instance_express_ids.clone(),
        })
    }

    /// Get geometry by index with pointer access over owned buffers.
    /// This avoids exposing references tied to collection lifetime.
    #[wasm_bindgen(js_name = getRef)]
    pub fn get_ref(&self, index: usize) -> Option<GpuInstancedGeometryRef> {
        self.geometries.get(index).map(|g| GpuInstancedGeometryRef {
            geometry_id: g.geometry_id,
            vertex_data: g.vertex_data.clone(),
            indices: g.indices.clone(),
            instance_data: g.instance_data.clone(),
            instance_express_ids: g.instance_express_ids.clone(),
        })
    }
}

impl GpuInstancedGeometryCollection {
    pub fn add(&mut self, geometry: GpuInstancedGeometry) {
        self.geometries.push(geometry);
    }

    pub fn get_mut(&mut self, index: usize) -> Option<&mut GpuInstancedGeometry> {
        self.geometries.get_mut(index)
    }
}

impl Default for GpuInstancedGeometryCollection {
    fn default() -> Self {
        Self::new()
    }
}

/// Pointer-friendly geometry view with owned backing storage.
/// Owning buffers prevents dangling pointers after collection mutation/drop.
#[wasm_bindgen]
pub struct GpuInstancedGeometryRef {
    geometry_id: u64,
    vertex_data: Vec<f32>,
    indices: Vec<u32>,
    instance_data: Vec<f32>,
    instance_express_ids: Vec<u32>,
}

#[wasm_bindgen]
impl GpuInstancedGeometryRef {
    #[wasm_bindgen(getter, js_name = geometryId)]
    pub fn geometry_id(&self) -> u64 {
        self.geometry_id
    }

    #[wasm_bindgen(getter, js_name = vertexDataPtr)]
    pub fn vertex_data_ptr(&self) -> *const f32 {
        self.vertex_data.as_ptr()
    }

    #[wasm_bindgen(getter, js_name = vertexDataLen)]
    pub fn vertex_data_len(&self) -> usize {
        self.vertex_data.len()
    }

    #[wasm_bindgen(getter, js_name = vertexDataByteLength)]
    pub fn vertex_data_byte_length(&self) -> usize {
        self.vertex_data_len() * 4
    }

    #[wasm_bindgen(getter, js_name = indicesPtr)]
    pub fn indices_ptr(&self) -> *const u32 {
        self.indices.as_ptr()
    }

    #[wasm_bindgen(getter, js_name = indicesLen)]
    pub fn indices_len(&self) -> usize {
        self.indices.len()
    }

    #[wasm_bindgen(getter, js_name = indicesByteLength)]
    pub fn indices_byte_length(&self) -> usize {
        self.indices_len() * 4
    }

    #[wasm_bindgen(getter, js_name = instanceDataPtr)]
    pub fn instance_data_ptr(&self) -> *const f32 {
        self.instance_data.as_ptr()
    }

    #[wasm_bindgen(getter, js_name = instanceDataLen)]
    pub fn instance_data_len(&self) -> usize {
        self.instance_data.len()
    }

    #[wasm_bindgen(getter, js_name = instanceDataByteLength)]
    pub fn instance_data_byte_length(&self) -> usize {
        self.instance_data_len() * 4
    }

    #[wasm_bindgen(getter, js_name = instanceExpressIdsPtr)]
    pub fn instance_express_ids_ptr(&self) -> *const u32 {
        self.instance_express_ids.as_ptr()
    }

    #[wasm_bindgen(getter, js_name = instanceCount)]
    pub fn instance_count(&self) -> usize {
        self.instance_express_ids.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gpu_geometry_creation() {
        let geom = GpuGeometry::new();
        assert!(geom.is_empty());
        assert_eq!(geom.mesh_count(), 0);
    }

    #[test]
    fn test_gpu_geometry_add_mesh() {
        let mut geom = GpuGeometry::new();

        // Simple triangle
        let positions = vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5, 0.0, 1.0];
        let normals = vec![0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0];
        let indices = vec![0, 1, 2];
        let color = [1.0, 0.0, 0.0, 1.0];

        geom.add_mesh(123, "IfcWall", &positions, &normals, &indices, color);

        assert!(!geom.is_empty());
        assert_eq!(geom.mesh_count(), 1);
        assert_eq!(geom.total_vertex_count(), 3);
        assert_eq!(geom.total_triangle_count(), 1);

        // Check metadata
        let meta = geom.get_mesh_metadata(0).unwrap();
        assert_eq!(meta.express_id, 123);
        assert_eq!(meta.vertex_count, 3);
        assert_eq!(meta.index_count, 3);
    }

    #[test]
    fn test_coordinate_conversion() {
        let mut geom = GpuGeometry::new();

        // Point at (1, 2, 3) in Z-up should become (1, 3, -2) in Y-up
        let positions = vec![1.0, 2.0, 3.0];
        let normals = vec![0.0, 0.0, 1.0]; // Normal pointing up in Z-up
        let indices = vec![0];
        let color = [1.0, 1.0, 1.0, 1.0];

        geom.add_mesh(1, "Test", &positions, &normals, &indices, color);

        // Vertex data is interleaved: [px, py, pz, nx, ny, nz]
        assert_eq!(geom.vertex_data[0], 1.0); // px unchanged
        assert_eq!(geom.vertex_data[1], 3.0); // py = old z
        assert_eq!(geom.vertex_data[2], -2.0); // pz = -old y

        assert_eq!(geom.vertex_data[3], 0.0); // nx unchanged
        assert_eq!(geom.vertex_data[4], 1.0); // ny = old nz (normal pointing up)
        assert_eq!(geom.vertex_data[5], 0.0); // nz = -old ny
    }

    #[test]
    fn test_instanced_geometry() {
        let mut geom = GpuInstancedGeometry::new(12345);

        let positions = vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5, 0.0, 1.0];
        let normals = vec![0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0];
        let indices = vec![0, 1, 2];

        geom.set_geometry(&positions, &normals, &indices);

        // Identity transform
        let transform = [
            1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
        ];
        let color = [1.0, 0.0, 0.0, 1.0];

        geom.add_instance(100, &transform, color);
        geom.add_instance(101, &transform, color);

        assert_eq!(geom.instance_count(), 2);
        assert_eq!(geom.vertex_count(), 3);
        assert_eq!(geom.triangle_count(), 1);
    }
}