copc-core 0.4.1

Shared COPC metadata, hierarchy, streaming point, and error types
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
//! Column-oriented LAS/COPC point data.

use crate::{Error, Result};

use las::point::Format as LasPointFormat;

/// LAS/COPC point dimensions that can be represented as columns.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LasDimension {
    X,
    Y,
    Z,
    Intensity,
    ReturnNumber,
    NumberOfReturns,
    Classification,
    ScanDirectionFlag,
    EdgeOfFlightLine,
    ScanAngleRank,
    UserData,
    PointSourceId,
    Synthetic,
    KeyPoint,
    Withheld,
    Overlap,
    ScanChannel,
    GpsTime,
    Red,
    Green,
    Blue,
    Nir,
    WaveformPacketDescriptorIndex,
    WaveformPacketByteOffset,
    WaveformPacketSize,
    WavePacketReturnPointWaveformLocation,
    ExtraBytes,
}

impl LasDimension {
    /// The default scalar representation for fixed LAS/COPC dimensions.
    pub const fn default_scalar(self) -> Option<ScalarType> {
        match self {
            Self::X | Self::Y | Self::Z | Self::GpsTime => Some(ScalarType::F64),
            Self::WavePacketReturnPointWaveformLocation => Some(ScalarType::F32),
            Self::ScanAngleRank => Some(ScalarType::I16),
            Self::WaveformPacketByteOffset => Some(ScalarType::U64),
            Self::Intensity
            | Self::PointSourceId
            | Self::Red
            | Self::Green
            | Self::Blue
            | Self::Nir => Some(ScalarType::U16),
            Self::WaveformPacketSize => Some(ScalarType::U32),
            Self::ReturnNumber
            | Self::NumberOfReturns
            | Self::Classification
            | Self::UserData
            | Self::ScanChannel
            | Self::WaveformPacketDescriptorIndex => Some(ScalarType::U8),
            Self::ScanDirectionFlag
            | Self::EdgeOfFlightLine
            | Self::Synthetic
            | Self::KeyPoint
            | Self::Withheld
            | Self::Overlap => Some(ScalarType::Bool),
            Self::ExtraBytes => None,
        }
    }

    /// Returns whether `scalar` is the default fixed-width representation for this dimension.
    pub const fn accepts_scalar(self, scalar: ScalarType) -> bool {
        match self.default_scalar() {
            Some(default) => default as u8 == scalar as u8,
            None => true,
        }
    }
}

/// Requested LAS/COPC dimensions for column-oriented reads.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ColumnSelection {
    dimensions: Vec<LasDimension>,
}

impl ColumnSelection {
    pub fn all() -> Self {
        Self::from_dimensions([
            LasDimension::X,
            LasDimension::Y,
            LasDimension::Z,
            LasDimension::Intensity,
            LasDimension::ReturnNumber,
            LasDimension::NumberOfReturns,
            LasDimension::Classification,
            LasDimension::ScanDirectionFlag,
            LasDimension::EdgeOfFlightLine,
            LasDimension::ScanAngleRank,
            LasDimension::UserData,
            LasDimension::PointSourceId,
            LasDimension::Synthetic,
            LasDimension::KeyPoint,
            LasDimension::Withheld,
            LasDimension::Overlap,
            LasDimension::ScanChannel,
            LasDimension::GpsTime,
            LasDimension::Red,
            LasDimension::Green,
            LasDimension::Blue,
            LasDimension::Nir,
            LasDimension::WaveformPacketDescriptorIndex,
            LasDimension::WaveformPacketByteOffset,
            LasDimension::WaveformPacketSize,
            LasDimension::WavePacketReturnPointWaveformLocation,
            LasDimension::ExtraBytes,
        ])
    }

    pub fn xyz() -> Self {
        Self::from_dimensions([LasDimension::X, LasDimension::Y, LasDimension::Z])
    }

    pub fn from_dimensions<I>(dims: I) -> Self
    where
        I: IntoIterator<Item = LasDimension>,
    {
        let mut dimensions = Vec::new();
        for dim in dims {
            if !dimensions.contains(&dim) {
                dimensions.push(dim);
            }
        }
        Self { dimensions }
    }

    pub fn contains(&self, dim: LasDimension) -> bool {
        self.dimensions.contains(&dim)
    }

    pub fn dimensions(&self) -> &[LasDimension] {
        &self.dimensions
    }

    pub fn len(&self) -> usize {
        self.dimensions.len()
    }

    pub fn is_empty(&self) -> bool {
        self.dimensions.is_empty()
    }
}

/// Primitive scalar types supported by LAS/COPC column data.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ScalarType {
    F64,
    F32,
    I64,
    I32,
    I16,
    I8,
    U64,
    U32,
    U16,
    U8,
    Bool,
}

/// Declares the LAS/COPC dimension and scalar type for a column.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ColumnSpec {
    pub dimension: LasDimension,
    pub scalar: ScalarType,
    /// For `LasDimension::ExtraBytes`, the fixed byte count stored for each point.
    pub byte_width: Option<usize>,
}

impl ColumnSpec {
    pub const fn new(dimension: LasDimension, scalar: ScalarType) -> Self {
        Self {
            dimension,
            scalar,
            byte_width: None,
        }
    }

    pub const fn extra_bytes(byte_width: usize) -> Self {
        Self {
            dimension: LasDimension::ExtraBytes,
            scalar: ScalarType::U8,
            byte_width: Some(byte_width),
        }
    }

    /// Returns the default fixed LAS/COPC scalar for `dimension`, when it has one.
    pub const fn default_for(dimension: LasDimension) -> Option<Self> {
        match dimension.default_scalar() {
            Some(scalar) => Some(Self {
                dimension,
                scalar,
                byte_width: None,
            }),
            None => None,
        }
    }

    /// Returns whether this specification has the canonical scalar for its dimension.
    pub const fn has_default_scalar(self) -> bool {
        if matches!(self.dimension, LasDimension::ExtraBytes) {
            matches!(self.scalar, ScalarType::U8) && self.byte_width.is_some()
        } else {
            self.byte_width.is_none() && self.dimension.accepts_scalar(self.scalar)
        }
    }

    /// Returns whether `data` has the scalar type declared by this spec.
    pub const fn matches_data(self, data: &ColumnData) -> bool {
        self.scalar as u8 == data.scalar() as u8
    }

    /// Validate the declared scalar against the supplied data.
    pub fn validate_data(self, data: &ColumnData) -> Result<()> {
        if !self.matches_data(data) {
            return Err(Error::InvalidInput(format!(
                "column {:?} declares {:?} data but contains {:?}",
                self.dimension,
                self.scalar,
                data.scalar()
            )));
        }
        if self.dimension == LasDimension::ExtraBytes && self.extra_byte_width().is_none() {
            return Err(Error::InvalidInput(
                "ExtraBytes column requires a non-zero byte width".into(),
            ));
        }
        if self.dimension != LasDimension::ExtraBytes && self.byte_width.is_some() {
            return Err(Error::InvalidInput(format!(
                "column {:?} cannot declare byte width {:?}",
                self.dimension, self.byte_width
            )));
        }
        Ok(())
    }

    /// Validate that this spec uses the fixed LAS/COPC scalar for its dimension.
    pub fn validate_default_scalar(self) -> Result<()> {
        if self.has_default_scalar() {
            Ok(())
        } else {
            Err(Error::InvalidInput(format!(
                "column {:?} declares {:?}, expected {:?}",
                self.dimension,
                self.scalar,
                self.dimension.default_scalar()
            )))
        }
    }

    pub fn extra_byte_width(self) -> Option<usize> {
        match (self.dimension, self.scalar, self.byte_width) {
            (LasDimension::ExtraBytes, ScalarType::U8, Some(width)) if width > 0 => Some(width),
            _ => None,
        }
    }

    pub fn point_count_for_data(self, data: &ColumnData) -> Result<usize> {
        self.validate_data(data)?;
        if self.dimension == LasDimension::ExtraBytes {
            let width = self.extra_byte_width().ok_or_else(|| {
                Error::InvalidInput("ExtraBytes column requires a non-zero byte width".into())
            })?;
            if data.len() % width != 0 {
                return Err(Error::InvalidInput(format!(
                    "ExtraBytes column has {} bytes, which is not divisible by byte width {width}",
                    data.len()
                )));
            }
            Ok(data.len() / width)
        } else {
            Ok(data.len())
        }
    }
}

/// Owned column values.
#[derive(Clone, Debug, PartialEq)]
pub enum ColumnData {
    F64(Vec<f64>),
    F32(Vec<f32>),
    I64(Vec<i64>),
    I32(Vec<i32>),
    I16(Vec<i16>),
    I8(Vec<i8>),
    U64(Vec<u64>),
    U32(Vec<u32>),
    U16(Vec<u16>),
    U8(Vec<u8>),
    Bool(Vec<bool>),
}

impl ColumnData {
    pub fn len(&self) -> usize {
        match self {
            Self::F64(values) => values.len(),
            Self::F32(values) => values.len(),
            Self::I64(values) => values.len(),
            Self::I32(values) => values.len(),
            Self::I16(values) => values.len(),
            Self::I8(values) => values.len(),
            Self::U64(values) => values.len(),
            Self::U32(values) => values.len(),
            Self::U16(values) => values.len(),
            Self::U8(values) => values.len(),
            Self::Bool(values) => values.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub const fn scalar(&self) -> ScalarType {
        match self {
            Self::F64(_) => ScalarType::F64,
            Self::F32(_) => ScalarType::F32,
            Self::I64(_) => ScalarType::I64,
            Self::I32(_) => ScalarType::I32,
            Self::I16(_) => ScalarType::I16,
            Self::I8(_) => ScalarType::I8,
            Self::U64(_) => ScalarType::U64,
            Self::U32(_) => ScalarType::U32,
            Self::U16(_) => ScalarType::U16,
            Self::U8(_) => ScalarType::U8,
            Self::Bool(_) => ScalarType::Bool,
        }
    }

    pub const fn scalar_type(&self) -> ScalarType {
        self.scalar()
    }

    pub const fn matches_scalar(&self, scalar: ScalarType) -> bool {
        self.scalar() as u8 == scalar as u8
    }

    pub fn view(&self) -> ColumnView<'_> {
        match self {
            Self::F64(values) => ColumnView::F64(values),
            Self::F32(values) => ColumnView::F32(values),
            Self::I64(values) => ColumnView::I64(values),
            Self::I32(values) => ColumnView::I32(values),
            Self::I16(values) => ColumnView::I16(values),
            Self::I8(values) => ColumnView::I8(values),
            Self::U64(values) => ColumnView::U64(values),
            Self::U32(values) => ColumnView::U32(values),
            Self::U16(values) => ColumnView::U16(values),
            Self::U8(values) => ColumnView::U8(values),
            Self::Bool(values) => ColumnView::Bool(values),
        }
    }
}

/// Borrowed column values.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ColumnView<'a> {
    F64(&'a [f64]),
    F32(&'a [f32]),
    I64(&'a [i64]),
    I32(&'a [i32]),
    I16(&'a [i16]),
    I8(&'a [i8]),
    U64(&'a [u64]),
    U32(&'a [u32]),
    U16(&'a [u16]),
    U8(&'a [u8]),
    Bool(&'a [bool]),
}

impl ColumnView<'_> {
    pub fn len(&self) -> usize {
        match self {
            Self::F64(values) => values.len(),
            Self::F32(values) => values.len(),
            Self::I64(values) => values.len(),
            Self::I32(values) => values.len(),
            Self::I16(values) => values.len(),
            Self::I8(values) => values.len(),
            Self::U64(values) => values.len(),
            Self::U32(values) => values.len(),
            Self::U16(values) => values.len(),
            Self::U8(values) => values.len(),
            Self::Bool(values) => values.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub const fn scalar(&self) -> ScalarType {
        match self {
            Self::F64(_) => ScalarType::F64,
            Self::F32(_) => ScalarType::F32,
            Self::I64(_) => ScalarType::I64,
            Self::I32(_) => ScalarType::I32,
            Self::I16(_) => ScalarType::I16,
            Self::I8(_) => ScalarType::I8,
            Self::U64(_) => ScalarType::U64,
            Self::U32(_) => ScalarType::U32,
            Self::U16(_) => ScalarType::U16,
            Self::U8(_) => ScalarType::U8,
            Self::Bool(_) => ScalarType::Bool,
        }
    }

    pub const fn scalar_type(&self) -> ScalarType {
        self.scalar()
    }
}

/// Returns the column layout available from a LAS point format.
pub fn layout_for_las_format(format: LasPointFormat) -> Vec<ColumnSpec> {
    let mut columns = Vec::with_capacity(27);

    push_default_specs(
        &mut columns,
        [
            LasDimension::X,
            LasDimension::Y,
            LasDimension::Z,
            LasDimension::Intensity,
            LasDimension::ReturnNumber,
            LasDimension::NumberOfReturns,
            LasDimension::Classification,
            LasDimension::ScanDirectionFlag,
            LasDimension::EdgeOfFlightLine,
            LasDimension::ScanAngleRank,
            LasDimension::UserData,
            LasDimension::PointSourceId,
            LasDimension::Synthetic,
            LasDimension::KeyPoint,
            LasDimension::Withheld,
            LasDimension::Overlap,
            LasDimension::ScanChannel,
        ],
    );

    if format.has_gps_time {
        columns.push(default_column_spec(LasDimension::GpsTime));
    }
    if format.has_color {
        push_default_specs(
            &mut columns,
            [LasDimension::Red, LasDimension::Green, LasDimension::Blue],
        );
    }
    if format.has_nir {
        columns.push(default_column_spec(LasDimension::Nir));
    }
    if format.has_waveform {
        push_default_specs(
            &mut columns,
            [
                LasDimension::WaveformPacketDescriptorIndex,
                LasDimension::WaveformPacketByteOffset,
                LasDimension::WaveformPacketSize,
                LasDimension::WavePacketReturnPointWaveformLocation,
            ],
        );
    }
    if format.extra_bytes > 0 {
        columns.push(ColumnSpec::extra_bytes(usize::from(format.extra_bytes)));
    }
    columns
}

/// Converts scan angle degrees into the rank-style column used by existing readers.
pub fn scan_angle_rank_from_degrees(degrees: f32) -> i16 {
    let scaled = (degrees * 180.0 / 90.0).round() as i32;
    scaled.clamp(i16::MIN as i32, i16::MAX as i32) as i16
}

fn push_default_specs<I>(columns: &mut Vec<ColumnSpec>, dims: I)
where
    I: IntoIterator<Item = LasDimension>,
{
    columns.extend(dims.into_iter().map(default_column_spec));
}

fn default_column_spec(dimension: LasDimension) -> ColumnSpec {
    ColumnSpec::default_for(dimension).expect("fixed LAS dimension has a default scalar")
}

/// A column-oriented batch of LAS/COPC point values.
#[derive(Clone, Debug, PartialEq)]
pub struct LasColumnBatch {
    pub len: usize,
    pub columns: Vec<(ColumnSpec, ColumnData)>,
}

impl LasColumnBatch {
    pub fn new(columns: Vec<(ColumnSpec, ColumnData)>) -> Result<Self> {
        let len = match columns.first() {
            Some((spec, data)) => spec.point_count_for_data(data)?,
            None => 0,
        };
        let batch = Self { len, columns };
        batch.validate()?;
        Ok(batch)
    }

    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub fn column(&self, dimension: LasDimension) -> Option<&ColumnData> {
        self.columns
            .iter()
            .find_map(|(spec, data)| (spec.dimension == dimension).then_some(data))
    }

    pub fn column_by_spec(&self, spec: ColumnSpec) -> Option<&ColumnData> {
        self.columns
            .iter()
            .find_map(|(column_spec, data)| (*column_spec == spec).then_some(data))
    }

    pub fn column_view(&self, dimension: LasDimension) -> Option<ColumnView<'_>> {
        self.column(dimension).map(ColumnData::view)
    }

    pub fn column_view_by_spec(&self, spec: ColumnSpec) -> Option<ColumnView<'_>> {
        self.column_by_spec(spec).map(ColumnData::view)
    }

    /// Validate scalar declarations and column lengths for this batch.
    pub fn validate(&self) -> Result<()> {
        for (spec, data) in &self.columns {
            let point_count = spec.point_count_for_data(data)?;
            if point_count != self.len {
                return Err(Error::InvalidInput(format!(
                    "column {:?} has {} points but batch len is {}",
                    spec.dimension, point_count, self.len
                )));
            }
        }
        Ok(())
    }

    /// Validate scalar declarations, fixed LAS/COPC scalar choices, and column lengths.
    pub fn validate_default_scalars(&self) -> Result<()> {
        self.validate()?;
        for (spec, _) in &self.columns {
            spec.validate_default_scalar()?;
        }
        Ok(())
    }
}

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

    fn base_layout_dims() -> Vec<LasDimension> {
        vec![
            LasDimension::X,
            LasDimension::Y,
            LasDimension::Z,
            LasDimension::Intensity,
            LasDimension::ReturnNumber,
            LasDimension::NumberOfReturns,
            LasDimension::Classification,
            LasDimension::ScanDirectionFlag,
            LasDimension::EdgeOfFlightLine,
            LasDimension::ScanAngleRank,
            LasDimension::UserData,
            LasDimension::PointSourceId,
            LasDimension::Synthetic,
            LasDimension::KeyPoint,
            LasDimension::Withheld,
            LasDimension::Overlap,
            LasDimension::ScanChannel,
        ]
    }

    fn assert_layout_dims(format_id: u8, expected: Vec<LasDimension>) {
        let format = LasPointFormat::new(format_id).unwrap();
        let layout = layout_for_las_format(format);
        let dims: Vec<_> = layout.iter().map(|spec| spec.dimension).collect();
        assert_eq!(expected, dims, "format {format_id}");
        for spec in layout {
            spec.validate_default_scalar().unwrap();
        }
    }

    #[test]
    fn data_reports_len_and_scalar() {
        let data = ColumnData::U16(vec![10, 20, 30]);

        assert_eq!(3, data.len());
        assert!(!data.is_empty());
        assert_eq!(ScalarType::U16, data.scalar());
        assert!(data.matches_scalar(ScalarType::U16));
    }

    #[test]
    fn batch_finds_owned_columns_and_views() {
        let batch = LasColumnBatch::new(vec![
            (
                ColumnSpec::new(LasDimension::X, ScalarType::F64),
                ColumnData::F64(vec![1.0, 2.0]),
            ),
            (
                ColumnSpec::new(LasDimension::Intensity, ScalarType::U16),
                ColumnData::U16(vec![100, 200]),
            ),
            (
                ColumnSpec::new(LasDimension::Withheld, ScalarType::Bool),
                ColumnData::Bool(vec![false, true]),
            ),
        ])
        .unwrap();

        assert_eq!(2, batch.len());
        assert!(!batch.is_empty());
        assert_eq!(
            Some(&ColumnData::U16(vec![100, 200])),
            batch.column(LasDimension::Intensity)
        );
        assert_eq!(
            Some(ColumnView::Bool(&[false, true])),
            batch.column_view(LasDimension::Withheld)
        );
    }

    #[test]
    fn batch_rejects_scalar_mismatch() {
        let err = LasColumnBatch::new(vec![(
            ColumnSpec::new(LasDimension::Intensity, ScalarType::U16),
            ColumnData::U8(vec![1, 2]),
        )])
        .unwrap_err();

        assert!(err
            .to_string()
            .contains("declares U16 data but contains U8"));
    }

    #[test]
    fn batch_rejects_len_mismatch() {
        let batch = LasColumnBatch {
            len: 3,
            columns: vec![(
                ColumnSpec::new(LasDimension::X, ScalarType::F64),
                ColumnData::F64(vec![1.0, 2.0]),
            )],
        };

        assert!(batch.validate().is_err());
    }

    #[test]
    fn batch_validates_fixed_width_extra_bytes() {
        let batch = LasColumnBatch::new(vec![(
            ColumnSpec::extra_bytes(3),
            ColumnData::U8(vec![1, 2, 3, 4, 5, 6]),
        )])
        .unwrap();

        assert_eq!(2, batch.len());
        assert_eq!(
            Some(&ColumnData::U8(vec![1, 2, 3, 4, 5, 6])),
            batch.column(LasDimension::ExtraBytes)
        );

        let invalid = LasColumnBatch::new(vec![(
            ColumnSpec::extra_bytes(3),
            ColumnData::U8(vec![1, 2, 3, 4]),
        )]);
        assert!(invalid.is_err());

        let missing_width = LasColumnBatch::new(vec![(
            ColumnSpec::new(LasDimension::ExtraBytes, ScalarType::U8),
            ColumnData::U8(vec![1, 2, 3]),
        )]);
        assert!(missing_width.is_err());
    }

    #[test]
    fn default_scalar_validation_allows_extra_bytes() {
        assert_eq!(
            ColumnSpec::new(LasDimension::GpsTime, ScalarType::F64),
            ColumnSpec::default_for(LasDimension::GpsTime).unwrap()
        );
        assert!(ColumnSpec::extra_bytes(4).has_default_scalar());
        assert!(ColumnSpec::new(LasDimension::ExtraBytes, ScalarType::U8)
            .validate_default_scalar()
            .is_err());
        assert!(
            ColumnSpec::new(LasDimension::ScanAngleRank, ScalarType::F32)
                .validate_default_scalar()
                .is_err()
        );
    }

    #[test]
    fn selection_tracks_requested_dimensions() {
        let xyz = ColumnSelection::xyz();
        assert_eq!(
            &[LasDimension::X, LasDimension::Y, LasDimension::Z],
            xyz.dimensions()
        );
        assert!(xyz.contains(LasDimension::X));
        assert!(!xyz.contains(LasDimension::Intensity));

        let selection = ColumnSelection::from_dimensions([
            LasDimension::Intensity,
            LasDimension::X,
            LasDimension::Intensity,
        ]);
        assert_eq!(
            &[LasDimension::Intensity, LasDimension::X],
            selection.dimensions()
        );
        assert_eq!(2, selection.len());
        assert!(!selection.is_empty());

        let all = ColumnSelection::all();
        assert!(all.contains(LasDimension::WaveformPacketByteOffset));
        assert!(all.contains(LasDimension::ExtraBytes));
    }

    #[test]
    fn scan_angle_rank_uses_engine_conversion() {
        assert_eq!(0, scan_angle_rank_from_degrees(0.0));
        assert_eq!(91, scan_angle_rank_from_degrees(45.25));
        assert_eq!(-91, scan_angle_rank_from_degrees(-45.25));
        assert_eq!(i16::MAX, scan_angle_rank_from_degrees(f32::MAX));
        assert_eq!(i16::MIN, scan_angle_rank_from_degrees(f32::MIN));
    }

    #[test]
    fn layout_for_format_0_has_core_dimensions() {
        assert_layout_dims(0, base_layout_dims());
    }

    #[test]
    fn layout_for_format_3_adds_gps_and_color() {
        let mut expected = base_layout_dims();
        expected.extend([
            LasDimension::GpsTime,
            LasDimension::Red,
            LasDimension::Green,
            LasDimension::Blue,
        ]);

        assert_layout_dims(3, expected);
    }

    #[test]
    fn layout_for_format_6_adds_gps() {
        let mut expected = base_layout_dims();
        expected.push(LasDimension::GpsTime);

        assert_layout_dims(6, expected);
    }

    #[test]
    fn layout_for_format_7_adds_gps_and_color() {
        let mut expected = base_layout_dims();
        expected.extend([
            LasDimension::GpsTime,
            LasDimension::Red,
            LasDimension::Green,
            LasDimension::Blue,
        ]);

        assert_layout_dims(7, expected);
    }

    #[test]
    fn layout_for_format_8_adds_gps_color_and_nir() {
        let mut expected = base_layout_dims();
        expected.extend([
            LasDimension::GpsTime,
            LasDimension::Red,
            LasDimension::Green,
            LasDimension::Blue,
            LasDimension::Nir,
        ]);

        assert_layout_dims(8, expected);
    }

    #[test]
    fn layout_for_format_10_adds_all_optional_las_dimensions() {
        let mut expected = base_layout_dims();
        expected.extend([
            LasDimension::GpsTime,
            LasDimension::Red,
            LasDimension::Green,
            LasDimension::Blue,
            LasDimension::Nir,
            LasDimension::WaveformPacketDescriptorIndex,
            LasDimension::WaveformPacketByteOffset,
            LasDimension::WaveformPacketSize,
            LasDimension::WavePacketReturnPointWaveformLocation,
        ]);

        assert_layout_dims(10, expected);
    }

    #[test]
    fn layout_includes_extra_bytes_with_byte_width_when_format_declares_them() {
        let mut format = LasPointFormat::new(0).unwrap();
        format.extra_bytes = 4;

        let layout = layout_for_las_format(format);

        assert_eq!(Some(&ColumnSpec::extra_bytes(4)), layout.last());
    }
}