quantized-mesh 0.2.1

Encoder and decoder for Cesium quantized-mesh-1.0 terrain format
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
//! Zero-copy decoder for quantized-mesh format.
//!
//! [`QuantizedMeshView`] borrows directly from the input byte slice and
//! exposes each section (vertices, indices, edges, extensions) as
//! `&[u8]` slices, with iterator helpers that lazily apply the
//! zigzag-delta and high-water-mark decoding without allocating
//! intermediate `Vec`s.
//!
//! For a fully-owned result use [`DecodedMesh::decode`] / [`DecodedMesh::decode_from`],
//! which transparently handle gzip decompression and materialise every
//! section into `Vec`s.

use std::io::{self, Read};

use flate2::read::GzDecoder;

use crate::{
    EdgeIndices, QuantizedMeshHeader, QuantizedVertices, TileMetadata, WaterMask, zigzag_decode,
};

/// Error type for quantized-mesh decoding.
#[derive(Debug)]
pub enum DecodeError {
    /// Input data is too short.
    UnexpectedEof,
    /// Invalid or corrupted data.
    InvalidData(String),
    /// Gzip decompression failed.
    DecompressionError(String),
    /// JSON parsing failed for metadata extension.
    JsonError(String),
    /// IO error.
    IoError(io::Error),
}

impl std::fmt::Display for DecodeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DecodeError::UnexpectedEof => write!(f, "Unexpected end of data"),
            DecodeError::InvalidData(msg) => write!(f, "Invalid data: {msg}"),
            DecodeError::DecompressionError(msg) => write!(f, "Decompression error: {msg}"),
            DecodeError::JsonError(msg) => write!(f, "JSON error: {msg}"),
            DecodeError::IoError(err) => write!(f, "IO error: {err}"),
        }
    }
}

impl std::error::Error for DecodeError {}

impl From<io::Error> for DecodeError {
    fn from(err: io::Error) -> Self {
        if err.kind() == io::ErrorKind::UnexpectedEof {
            DecodeError::UnexpectedEof
        } else {
            DecodeError::IoError(err)
        }
    }
}

/// Result type for quantized-mesh decoding.
pub type DecodeResult<T> = Result<T, DecodeError>;

// ---------------------------------------------------------------------------
// View types (zero-copy)
// ---------------------------------------------------------------------------

/// Triangle / edge indices viewed as borrowed bytes.
///
/// The original quantized-mesh stream stores indices as either u16 or u32
/// depending on whether `vertex_count > 65535`. The view keeps the raw
/// little-endian bytes and decodes lazily via [`Self::iter`].
#[derive(Debug, Clone, Copy)]
pub enum IndicesView<'a> {
    /// 16-bit indices (used when `vertex_count <= 65535`). 2 bytes per index.
    U16(&'a [u8]),
    /// 32-bit indices. 4 bytes per index.
    U32(&'a [u8]),
}

impl<'a> IndicesView<'a> {
    /// Number of indices.
    #[inline]
    pub fn len(&self) -> usize {
        match self {
            Self::U16(b) => b.len() / 2,
            Self::U32(b) => b.len() / 4,
        }
    }

    /// `true` if there are no indices.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Iterate the raw (still high-water-mark encoded) indices.
    pub fn iter_raw(&self) -> RawIndexIter<'a> {
        match self {
            Self::U16(b) => RawIndexIter::U16(b.chunks_exact(2)),
            Self::U32(b) => RawIndexIter::U32(b.chunks_exact(4)),
        }
    }

    /// Iterate the fully decoded triangle indices.
    pub fn iter(&self) -> HighWaterMarkIter<RawIndexIter<'a>> {
        HighWaterMarkIter::new(self.iter_raw())
    }

    /// Decode into an owned `Vec<u32>`.
    pub fn to_vec(&self) -> Vec<u32> {
        let mut out = Vec::with_capacity(self.len());
        out.extend(self.iter());
        out
    }
}

/// Iterator over raw little-endian-encoded indices (u16 or u32) yielding `u32`.
pub enum RawIndexIter<'a> {
    /// 16-bit chunks (2 bytes each).
    U16(std::slice::ChunksExact<'a, u8>),
    /// 32-bit chunks (4 bytes each).
    U32(std::slice::ChunksExact<'a, u8>),
}

impl<'a> Iterator for RawIndexIter<'a> {
    type Item = u32;

    #[inline]
    fn next(&mut self) -> Option<u32> {
        match self {
            Self::U16(it) => it.next().map(|c| u16::from_le_bytes([c[0], c[1]]) as u32),
            Self::U32(it) => it
                .next()
                .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]])),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self {
            Self::U16(it) => it.size_hint(),
            Self::U32(it) => it.size_hint(),
        }
    }
}

impl<'a> ExactSizeIterator for RawIndexIter<'a> {}

/// Iterator that applies inverse zigzag-delta decoding to a stream of raw
/// little-endian u16 bytes, yielding decoded `u16` values.
pub struct ZigzagDeltaIter<'a> {
    chunks: std::slice::ChunksExact<'a, u8>,
    state: i32,
}

impl<'a> ZigzagDeltaIter<'a> {
    /// Wrap a byte slice (length must be a multiple of 2). Trailing odd
    /// bytes are silently ignored.
    #[inline]
    pub fn new(bytes: &'a [u8]) -> Self {
        Self {
            chunks: bytes.chunks_exact(2),
            state: 0,
        }
    }
}

impl<'a> Iterator for ZigzagDeltaIter<'a> {
    type Item = u16;

    #[inline]
    fn next(&mut self) -> Option<u16> {
        let chunk = self.chunks.next()?;
        let enc = u16::from_le_bytes([chunk[0], chunk[1]]) as u32;
        let delta = zigzag_decode(enc);
        self.state = self.state.wrapping_add(delta);
        Some(self.state as u16)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.chunks.size_hint()
    }
}

impl<'a> ExactSizeIterator for ZigzagDeltaIter<'a> {}

/// Iterator that applies inverse high-water-mark decoding to an inner
/// iterator of raw `u32` codes.
pub struct HighWaterMarkIter<I> {
    inner: I,
    highest: u32,
}

impl<I> HighWaterMarkIter<I> {
    /// Wrap a code iterator.
    #[inline]
    pub fn new(inner: I) -> Self {
        Self { inner, highest: 0 }
    }
}

impl<I: Iterator<Item = u32>> Iterator for HighWaterMarkIter<I> {
    type Item = u32;

    #[inline]
    fn next(&mut self) -> Option<u32> {
        let code = self.inner.next()?;
        let index = self.highest.wrapping_sub(code);
        if code == 0 {
            self.highest = self.highest.wrapping_add(1);
        }
        Some(index)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<I: Iterator<Item = u32> + ExactSizeIterator> ExactSizeIterator for HighWaterMarkIter<I> {}

/// Water mask viewed as borrowed bytes.
#[derive(Debug, Clone, Copy)]
pub enum WaterMaskView<'a> {
    /// Uniform mask: single byte (0 = land, 255 = water).
    Uniform(u8),
    /// 256×256 byte grid borrowed from the source buffer.
    Grid(&'a [u8; 256 * 256]),
}

impl<'a> WaterMaskView<'a> {
    /// Convert to the owned [`WaterMask`] form (copies the 64 KiB grid).
    pub fn to_owned(self) -> WaterMask {
        match self {
            Self::Uniform(v) => WaterMask::Uniform(v),
            Self::Grid(g) => {
                let mut owned = Box::new([0u8; 256 * 256]);
                owned.copy_from_slice(g.as_ref());
                WaterMask::Grid(owned)
            }
        }
    }
}

/// Extensions viewed as borrowed slices into the source buffer.
#[derive(Debug, Clone, Copy, Default)]
pub struct ExtensionsView<'a> {
    /// Oct-encoded normals — 2 bytes per vertex. Use [`oct_decode_normal`]
    /// or [`iter_oct_normals`] to decode.
    pub normals_oct: Option<&'a [u8]>,
    /// Water mask.
    pub water_mask: Option<WaterMaskView<'a>>,
    /// Raw metadata JSON. Use [`TileMetadata`]::from a `serde_json::from_str`
    /// to materialise.
    pub metadata_json: Option<&'a str>,
}

impl<'a> ExtensionsView<'a> {
    /// Materialise into the owned [`DecodedExtensions`] form.
    pub fn to_owned(&self) -> DecodeResult<DecodedExtensions> {
        Ok(DecodedExtensions {
            normals: self
                .normals_oct
                .map(|b| iter_oct_normals(b).collect::<Vec<_>>()),
            water_mask: self.water_mask.map(|w| w.to_owned()),
            metadata: self
                .metadata_json
                .map(serde_json::from_str::<TileMetadata>)
                .transpose()
                .map_err(|e| DecodeError::JsonError(e.to_string()))?,
        })
    }
}

/// Iterator over oct-encoded normals (2 bytes each), yielding `[f32; 3]`.
pub fn iter_oct_normals(bytes: &[u8]) -> impl Iterator<Item = [f32; 3]> + '_ {
    bytes
        .chunks_exact(2)
        .map(|c| oct_decode_normal([c[0], c[1]]))
}

/// Zero-copy view over a parsed quantized-mesh stream.
///
/// Every byte section (vertex / index / edge / extension data) is a slice
/// borrowed from the input. Use the `iter_*` helpers to decode on the fly,
/// or [`Self::into_owned`] to materialise everything into a [`DecodedMesh`].
#[derive(Debug, Clone, Copy)]
pub struct QuantizedMeshView<'a> {
    /// Parsed 88-byte header (copied — it's small and not aligned in the source).
    pub header: QuantizedMeshHeader,
    /// Number of vertices.
    pub vertex_count: u32,
    /// Whether the index stream uses 32-bit indices (`vertex_count > 65535`).
    pub use_32bit_indices: bool,
    /// Number of triangles.
    pub triangle_count: u32,

    /// Zigzag-delta encoded `u` coordinate bytes (2 bytes per vertex).
    pub encoded_u_bytes: &'a [u8],
    /// Zigzag-delta encoded `v` coordinate bytes.
    pub encoded_v_bytes: &'a [u8],
    /// Zigzag-delta encoded `height` bytes.
    pub encoded_height_bytes: &'a [u8],

    /// High-water-mark encoded triangle indices.
    pub indices: IndicesView<'a>,
    /// West-edge indices (raw little-endian).
    pub edge_west: IndicesView<'a>,
    /// South-edge indices.
    pub edge_south: IndicesView<'a>,
    /// East-edge indices.
    pub edge_east: IndicesView<'a>,
    /// North-edge indices.
    pub edge_north: IndicesView<'a>,

    /// Parsed extension views.
    pub extensions: ExtensionsView<'a>,
}

impl<'a> QuantizedMeshView<'a> {
    /// Parse a raw (already gzip-decompressed) quantized-mesh byte stream.
    pub fn parse(data: &'a [u8]) -> DecodeResult<Self> {
        let mut cur = Cursor::new(data);
        let header = QuantizedMeshHeader::from_bytes(cur.take(88)?)
            .ok_or_else(|| DecodeError::InvalidData("Invalid header".to_string()))?;

        let vertex_count = cur.read_u32()?;
        let use_32bit = vertex_count > 65535;
        let vc = vertex_count as usize;

        let encoded_u_bytes = cur.take(vc * 2)?;
        let encoded_v_bytes = cur.take(vc * 2)?;
        let encoded_height_bytes = cur.take(vc * 2)?;

        cur.align_to(if use_32bit { 4 } else { 2 });

        let triangle_count = cur.read_u32()?;
        let index_stride = if use_32bit { 4 } else { 2 };
        let index_bytes = cur.take(triangle_count as usize * 3 * index_stride)?;
        let indices = if use_32bit {
            IndicesView::U32(index_bytes)
        } else {
            IndicesView::U16(index_bytes)
        };

        let edge_west = read_edge_indices(&mut cur, use_32bit)?;
        let edge_south = read_edge_indices(&mut cur, use_32bit)?;
        let edge_east = read_edge_indices(&mut cur, use_32bit)?;
        let edge_north = read_edge_indices(&mut cur, use_32bit)?;

        let extensions = parse_extensions(&mut cur, vc)?;

        Ok(Self {
            header,
            vertex_count,
            use_32bit_indices: use_32bit,
            triangle_count,
            encoded_u_bytes,
            encoded_v_bytes,
            encoded_height_bytes,
            indices,
            edge_west,
            edge_south,
            edge_east,
            edge_north,
            extensions,
        })
    }

    /// Lazily decoded `u` coordinates.
    #[inline]
    pub fn iter_u(&self) -> ZigzagDeltaIter<'a> {
        ZigzagDeltaIter::new(self.encoded_u_bytes)
    }

    /// Lazily decoded `v` coordinates.
    #[inline]
    pub fn iter_v(&self) -> ZigzagDeltaIter<'a> {
        ZigzagDeltaIter::new(self.encoded_v_bytes)
    }

    /// Lazily decoded `height` values.
    #[inline]
    pub fn iter_height(&self) -> ZigzagDeltaIter<'a> {
        ZigzagDeltaIter::new(self.encoded_height_bytes)
    }

    /// Materialise into a fully owned [`DecodedMesh`].
    pub fn into_owned(self) -> DecodeResult<DecodedMesh> {
        let vertices = QuantizedVertices {
            u: self.iter_u().collect(),
            v: self.iter_v().collect(),
            height: self.iter_height().collect(),
        };
        let indices = self.indices.to_vec();
        let edge_indices = EdgeIndices {
            west: self.edge_west.iter_raw().collect(),
            south: self.edge_south.iter_raw().collect(),
            east: self.edge_east.iter_raw().collect(),
            north: self.edge_north.iter_raw().collect(),
        };
        let extensions = self.extensions.to_owned()?;
        Ok(DecodedMesh {
            header: self.header,
            vertices,
            indices,
            edge_indices,
            extensions,
        })
    }
}

fn read_edge_indices<'a>(cur: &mut Cursor<'a>, use_32bit: bool) -> DecodeResult<IndicesView<'a>> {
    let count = cur.read_u32()? as usize;
    let stride = if use_32bit { 4 } else { 2 };
    let bytes = cur.take(count * stride)?;
    Ok(if use_32bit {
        IndicesView::U32(bytes)
    } else {
        IndicesView::U16(bytes)
    })
}

fn parse_extensions<'a>(
    cur: &mut Cursor<'a>,
    vertex_count: usize,
) -> DecodeResult<ExtensionsView<'a>> {
    let mut ext = ExtensionsView::default();
    while cur.remaining() >= 5 {
        let id = cur.read_u8()?;
        let length = cur.read_u32()? as usize;
        if cur.remaining() < length {
            // Truncated extension; bail without erroring (matches old behaviour).
            break;
        }
        let payload = cur.take(length)?;
        match id {
            1 => {
                // Oct-encoded normals: 2 bytes per vertex.
                let want = vertex_count * 2;
                let bytes = if payload.len() >= want {
                    &payload[..want]
                } else {
                    payload
                };
                ext.normals_oct = Some(bytes);
            }
            2 => {
                ext.water_mask = Some(match length {
                    1 => WaterMaskView::Uniform(payload[0]),
                    n if n == 256 * 256 => {
                        let arr: &[u8; 256 * 256] = payload
                            .try_into()
                            .map_err(|_| DecodeError::InvalidData("water mask size".into()))?;
                        WaterMaskView::Grid(arr)
                    }
                    _ => WaterMaskView::Uniform(0),
                });
            }
            4 => {
                if payload.len() < 4 {
                    return Err(DecodeError::UnexpectedEof);
                }
                let json_len =
                    u32::from_le_bytes([payload[0], payload[1], payload[2], payload[3]]) as usize;
                if payload.len() < 4 + json_len {
                    return Err(DecodeError::UnexpectedEof);
                }
                let json_bytes = &payload[4..4 + json_len];
                let json_str = std::str::from_utf8(json_bytes)
                    .map_err(|e| DecodeError::JsonError(e.to_string()))?;
                ext.metadata_json = Some(json_str);
            }
            _ => {} // unknown extension — already advanced past it
        }
    }
    Ok(ext)
}

// ---------------------------------------------------------------------------
// Owned form (backward-compatible)
// ---------------------------------------------------------------------------

/// Decoded extensions from quantized-mesh format.
#[derive(Debug, Clone, Default)]
pub struct DecodedExtensions {
    /// Oct-decoded per-vertex normals (if present).
    pub normals: Option<Vec<[f32; 3]>>,
    /// Water mask (if present).
    pub water_mask: Option<WaterMask>,
    /// Metadata (if present).
    pub metadata: Option<TileMetadata>,
}

/// Fully owned decoded quantized-mesh data.
#[derive(Debug, Clone)]
pub struct DecodedMesh {
    /// Header with tile metadata.
    pub header: QuantizedMeshHeader,
    /// Quantized vertex data.
    pub vertices: QuantizedVertices,
    /// Triangle indices.
    pub indices: Vec<u32>,
    /// Edge indices for skirt generation.
    pub edge_indices: EdgeIndices,
    /// Decoded extensions.
    pub extensions: DecodedExtensions,
}

impl DecodedMesh {
    /// Decode a (possibly gzip-compressed) quantized-mesh byte slice into the
    /// fully owned form.
    pub fn decode(data: &[u8]) -> DecodeResult<Self> {
        if is_gzip(data) {
            let mut decompressed = Vec::new();
            GzDecoder::new(data)
                .read_to_end(&mut decompressed)
                .map_err(|e| DecodeError::DecompressionError(e.to_string()))?;
            QuantizedMeshView::parse(&decompressed)?.into_owned()
        } else {
            QuantizedMeshView::parse(data)?.into_owned()
        }
    }

    /// Decode from a `Read`er. The reader is fully consumed.
    pub fn decode_from<R: Read>(mut reader: R) -> DecodeResult<Self> {
        let mut buf = Vec::new();
        reader.read_to_end(&mut buf)?;
        Self::decode(&buf)
    }
}

/// Check if `data` starts with the gzip magic number.
#[inline]
pub fn is_gzip(data: &[u8]) -> bool {
    data.len() >= 2 && data[0] == 0x1f && data[1] == 0x8b
}

/// Decompress a gzip-compressed quantized-mesh blob. Returns the input unchanged
/// (copied) if it isn't gzip.
pub fn decompress_gzip(data: &[u8]) -> DecodeResult<Vec<u8>> {
    if is_gzip(data) {
        let mut out = Vec::new();
        GzDecoder::new(data)
            .read_to_end(&mut out)
            .map_err(|e| DecodeError::DecompressionError(e.to_string()))?;
        Ok(out)
    } else {
        Ok(data.to_vec())
    }
}

// ---------------------------------------------------------------------------
// Internal cursor
// ---------------------------------------------------------------------------

struct Cursor<'a> {
    data: &'a [u8],
    offset: usize,
}

impl<'a> Cursor<'a> {
    fn new(data: &'a [u8]) -> Self {
        Self { data, offset: 0 }
    }

    #[inline]
    fn remaining(&self) -> usize {
        self.data.len().saturating_sub(self.offset)
    }

    fn take(&mut self, n: usize) -> DecodeResult<&'a [u8]> {
        if self.remaining() < n {
            return Err(DecodeError::UnexpectedEof);
        }
        let slice = &self.data[self.offset..self.offset + n];
        self.offset += n;
        Ok(slice)
    }

    fn read_u8(&mut self) -> DecodeResult<u8> {
        Ok(self.take(1)?[0])
    }

    fn read_u32(&mut self) -> DecodeResult<u32> {
        let b = self.take(4)?;
        Ok(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
    }

    fn align_to(&mut self, alignment: usize) {
        let rem = self.offset % alignment;
        if rem != 0 {
            self.offset += alignment - rem;
        }
    }
}

// ---------------------------------------------------------------------------
// Oct decode (also used by encoder tests)
// ---------------------------------------------------------------------------

/// Decode oct-encoded normal to unit vector.
///
/// Reverses the octahedron encoding used for normal compression.
pub fn oct_decode_normal(encoded: [u8; 2]) -> [f32; 3] {
    let mut x = (encoded[0] as f32 / 255.0) * 2.0 - 1.0;
    let mut y = (encoded[1] as f32 / 255.0) * 2.0 - 1.0;
    let z = 1.0 - x.abs() - y.abs();
    if z < 0.0 {
        let ox = x;
        x = (1.0 - y.abs()) * if ox >= 0.0 { 1.0 } else { -1.0 };
        y = (1.0 - ox.abs()) * if y >= 0.0 { 1.0 } else { -1.0 };
    }
    let len = (x * x + y * y + z * z).sqrt();
    if len > 0.0 {
        [x / len, y / len, z / len]
    } else {
        [0.0, 0.0, 1.0]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{EncodeOptions, QuantizedMeshEncoder, oct_encode_normal};

    fn sample_mesh() -> (
        QuantizedMeshHeader,
        QuantizedVertices,
        Vec<u32>,
        EdgeIndices,
    ) {
        let header = QuantizedMeshHeader::default();
        let vertices = QuantizedVertices {
            u: vec![0, 32767, 0, 32767],
            v: vec![0, 0, 32767, 32767],
            height: vec![0, 0, 0, 0],
        };
        let indices = vec![0, 1, 2, 1, 3, 2];
        let edge_indices = EdgeIndices::from_vertices(&vertices);
        (header, vertices, indices, edge_indices)
    }

    #[test]
    fn test_oct_decode_normal_roundtrip() {
        let test_normals = [
            [0.0f32, 0.0, 1.0],
            [0.0, 0.0, -1.0],
            [1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            [0.577, 0.577, 0.577],
        ];
        for normal in test_normals {
            let encoded = oct_encode_normal(normal);
            let decoded = oct_decode_normal(encoded);
            let dot = normal[0] * decoded[0] + normal[1] * decoded[1] + normal[2] * decoded[2];
            assert!(dot > 0.95, "{normal:?} → {encoded:?} → {decoded:?}");
        }
    }

    #[test]
    fn view_parses_uncompressed_mesh() {
        let (header, vertices, indices, edge_indices) = sample_mesh();
        let encoder = QuantizedMeshEncoder::new(
            header,
            vertices.clone(),
            indices.clone(),
            edge_indices.clone(),
        );
        let encoded = encoder.encode_with_options(&EncodeOptions {
            compression_level: 0,
            ..Default::default()
        });

        let view = QuantizedMeshView::parse(&encoded).expect("parse");
        assert_eq!(view.vertex_count as usize, vertices.u.len());
        assert_eq!(view.triangle_count as usize * 3, indices.len());

        let u: Vec<u16> = view.iter_u().collect();
        let v: Vec<u16> = view.iter_v().collect();
        let h: Vec<u16> = view.iter_height().collect();
        assert_eq!(u, vertices.u);
        assert_eq!(v, vertices.v);
        assert_eq!(h, vertices.height);

        let idx: Vec<u32> = view.indices.iter().collect();
        assert_eq!(idx, indices);
    }

    #[test]
    fn owned_decode_roundtrips() {
        let (header, vertices, indices, edge_indices) = sample_mesh();
        let encoder = QuantizedMeshEncoder::new(
            header,
            vertices.clone(),
            indices.clone(),
            edge_indices.clone(),
        );
        let encoded = encoder.encode_with_options(&EncodeOptions {
            compression_level: 0,
            ..Default::default()
        });

        let decoded = DecodedMesh::decode(&encoded).expect("decode");
        assert_eq!(decoded.header.min_height, header.min_height);
        assert_eq!(decoded.vertices.u, vertices.u);
        assert_eq!(decoded.vertices.v, vertices.v);
        assert_eq!(decoded.vertices.height, vertices.height);
        assert_eq!(decoded.indices, indices);
        assert_eq!(decoded.edge_indices.west, edge_indices.west);
        assert_eq!(decoded.edge_indices.south, edge_indices.south);
        assert_eq!(decoded.edge_indices.east, edge_indices.east);
        assert_eq!(decoded.edge_indices.north, edge_indices.north);
    }

    #[test]
    fn owned_decode_handles_gzip() {
        let (header, vertices, indices, edge_indices) = sample_mesh();
        let encoder =
            QuantizedMeshEncoder::new(header, vertices.clone(), indices.clone(), edge_indices);
        let encoded = encoder.encode_with_options(&EncodeOptions {
            compression_level: 6,
            ..Default::default()
        });
        assert_eq!(&encoded[0..2], &[0x1f, 0x8b]);

        let decoded = DecodedMesh::decode(&encoded).expect("decode");
        assert_eq!(decoded.vertices.u, vertices.u);
        assert_eq!(decoded.indices, indices);
    }

    #[test]
    fn owned_decode_with_extensions() {
        let (header, vertices, indices, edge_indices) = sample_mesh();
        let normals = vec![[0.0_f32, 0.0, 1.0]; 4];
        let encoder = QuantizedMeshEncoder::new(header, vertices.clone(), indices, edge_indices);
        let encoded = encoder.encode_with_options(&EncodeOptions {
            compression_level: 0,
            include_normals: true,
            normals: Some(normals),
            include_water_mask: true,
            water_mask: Some(WaterMask::Uniform(128)),
            ..Default::default()
        });

        let decoded = DecodedMesh::decode(&encoded).expect("decode");
        let ns = decoded.extensions.normals.expect("normals");
        assert_eq!(ns.len(), 4);
        for n in ns {
            assert!(n[2] > 0.9);
        }
        match decoded.extensions.water_mask.expect("water mask") {
            WaterMask::Uniform(v) => assert_eq!(v, 128),
            _ => panic!("expected uniform"),
        }
    }

    #[test]
    fn owned_decode_from_reader_handles_gzip() {
        use std::io::Cursor;
        let (header, vertices, indices, edge_indices) = sample_mesh();
        let encoder =
            QuantizedMeshEncoder::new(header, vertices.clone(), indices.clone(), edge_indices);
        let encoded = encoder.encode_with_options(&EncodeOptions {
            compression_level: 6,
            ..Default::default()
        });
        let decoded = DecodedMesh::decode_from(Cursor::new(encoded)).expect("decode");
        assert_eq!(decoded.vertices.u, vertices.u);
        assert_eq!(decoded.indices, indices);
    }
}