laz 0.12.1

Rust port of Laszip compression. of the LAS 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
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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
/*
===============================================================================

  PROGRAMMERS:

    martin.isenburg@rapidlasso.com  -  http://rapidlasso.com
    uday.karan@gmail.com - Hobu, Inc.

  COPYRIGHT:

    (c) 2007-2014, martin isenburg, rapidlasso - tools to catch reality
    (c) 2014, Uday Verma, Hobu, Inc.
    (c) 2019, Thomas Montaigu

    This is free software; you can redistribute and/or modify it under the
    terms of the Apache Public License 2.0 published by the Apache Software
    Foundation. See the COPYING file for more information.

    This software is distributed WITHOUT ANY WARRANTY and without even the
    implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  CHANGE HISTORY:
    6 June 2019: Translated to Rust
===============================================================================
*/

//! Defines the Point Format 0 ands different version of compressors and decompressors

use crate::packers::Packable;

pub trait LasPoint0 {
    // Non mutable accessors
    fn x(&self) -> i32;
    fn y(&self) -> i32;
    fn z(&self) -> i32;
    fn intensity(&self) -> u16;

    fn bit_fields(&self) -> u8;
    fn number_of_returns_of_given_pulse(&self) -> u8;
    fn scan_direction_flag(&self) -> bool;
    fn edge_of_flight_line(&self) -> bool;
    fn return_number(&self) -> u8;

    fn classification(&self) -> u8;
    fn scan_angle_rank(&self) -> i8;
    fn user_data(&self) -> u8;
    fn point_source_id(&self) -> u16;

    // Mutable accessors

    fn set_x(&mut self, new_val: i32);
    fn set_y(&mut self, new_val: i32);
    fn set_z(&mut self, new_val: i32);
    fn set_intensity(&mut self, new_val: u16);

    fn set_bit_fields(&mut self, new_val: u8);

    fn set_classification(&mut self, new_val: u8);
    fn set_scan_angle_rank(&mut self, new_val: i8);
    fn set_user_data(&mut self, new_val: u8);
    fn set_point_source_id(&mut self, new_val: u16);
}

#[derive(Default, Copy, Clone, PartialEq, Debug)]
pub struct Point0 {
    pub x: i32,
    pub y: i32,
    pub z: i32,
    pub intensity: u16,

    // 3 bits
    pub number_of_returns_of_given_pulse: u8,
    // 3 bits
    pub scan_direction_flag: bool,
    // 1 bit
    pub edge_of_flight_line: bool,
    // 1 bit
    pub return_number: u8,

    // 5 bits for classification the rest are bit flags
    pub classification: u8,

    pub scan_angle_rank: i8,
    pub user_data: u8,
    pub point_source_id: u16,
}

impl Point0 {
    pub const SIZE: usize = 20;

    pub fn populate_bit_fields_from(&mut self, byte: u8) {
        self.return_number = byte & 0x7;
        self.number_of_returns_of_given_pulse = (byte >> 3) & 0x7;
        self.scan_direction_flag = ((byte >> 6) & 0x1) != 0;
        self.edge_of_flight_line = ((byte >> 7) & 0x1) != 0;
    }

    pub fn bit_fields_to_byte(&self) -> u8 {
        let a = self.return_number;
        let b = self.number_of_returns_of_given_pulse;
        let c = self.scan_direction_flag as u8;
        let d = self.edge_of_flight_line as u8;

        ((d & 0x1) << 7) | (c & 0x1) << 6 | (b & 0x7) << 3 | (a & 0x7)
    }
}

impl LasPoint0 for Point0 {
    fn x(&self) -> i32 {
        self.x
    }

    fn y(&self) -> i32 {
        self.y
    }

    fn z(&self) -> i32 {
        self.z
    }

    fn intensity(&self) -> u16 {
        self.intensity
    }

    fn bit_fields(&self) -> u8 {
        self.bit_fields_to_byte()
    }

    fn number_of_returns_of_given_pulse(&self) -> u8 {
        self.number_of_returns_of_given_pulse
    }

    fn scan_direction_flag(&self) -> bool {
        self.scan_direction_flag
    }

    fn edge_of_flight_line(&self) -> bool {
        self.edge_of_flight_line
    }

    fn return_number(&self) -> u8 {
        self.return_number
    }

    fn classification(&self) -> u8 {
        self.classification
    }

    fn scan_angle_rank(&self) -> i8 {
        self.scan_angle_rank
    }

    fn user_data(&self) -> u8 {
        self.user_data
    }

    fn point_source_id(&self) -> u16 {
        self.point_source_id
    }

    fn set_x(&mut self, new_val: i32) {
        self.x = new_val;
    }

    fn set_y(&mut self, new_val: i32) {
        self.y = new_val;
    }

    fn set_z(&mut self, new_val: i32) {
        self.z = new_val;
    }

    fn set_intensity(&mut self, new_val: u16) {
        self.intensity = new_val
    }

    fn set_bit_fields(&mut self, new_val: u8) {
        self.populate_bit_fields_from(new_val);
    }

    fn set_classification(&mut self, new_val: u8) {
        self.classification = new_val;
    }

    fn set_scan_angle_rank(&mut self, new_val: i8) {
        self.scan_angle_rank = new_val;
    }

    fn set_user_data(&mut self, new_val: u8) {
        self.user_data = new_val;
    }

    fn set_point_source_id(&mut self, new_val: u16) {
        self.point_source_id = new_val;
    }
}

impl Packable for Point0 {
    fn unpack_from(input: &[u8]) -> Self {
        assert!(
            input.len() >= Point0::SIZE,
            "Point10::unpack_from expected buffer of 20 bytes"
        );
        unsafe { Self::unpack_from_unchecked(input) }
    }

    fn pack_into(&self, output: &mut [u8]) {
        assert!(
            output.len() >= Point0::SIZE,
            "Point10::pack_into expected buffer of 20 bytes"
        );
        unsafe { self.pack_into_unchecked(output) }
    }

    unsafe fn unpack_from_unchecked(input: &[u8]) -> Self {
        debug_assert!(
            input.len() >= 20,
            "Point10::unpack_from expected buffer of 20 bytes"
        );
        let mut point = Self {
            x: i32::unpack_from_unchecked(input.get_unchecked(..4)),
            y: i32::unpack_from_unchecked(input.get_unchecked(4..8)),
            z: i32::unpack_from_unchecked(input.get_unchecked(8..12)),
            intensity: u16::unpack_from_unchecked(input.get_unchecked(12..14)),
            number_of_returns_of_given_pulse: 0,
            scan_direction_flag: false,
            edge_of_flight_line: false,
            return_number: 0,
            classification: *input.get_unchecked(15),
            scan_angle_rank: *input.get_unchecked(16) as i8,
            user_data: *input.get_unchecked(17),
            point_source_id: u16::unpack_from_unchecked(input.get_unchecked(18..20)),
        };
        point.set_bit_fields(*input.get_unchecked(14));
        point
    }

    unsafe fn pack_into_unchecked(&self, output: &mut [u8]) {
        debug_assert!(
            output.len() >= 20,
            "Point10::pack_into expected buffer of 20 bytes"
        );
        i32::pack_into_unchecked(&self.x, output.get_unchecked_mut(0..4));
        i32::pack_into_unchecked(&self.y, output.get_unchecked_mut(4..8));
        i32::pack_into_unchecked(&self.z, output.get_unchecked_mut(8..12));

        u16::pack_into_unchecked(&self.intensity, output.get_unchecked_mut(12..14));

        *output.get_unchecked_mut(14) = self.bit_fields_to_byte();
        *output.get_unchecked_mut(15) = self.classification;
        *output.get_unchecked_mut(16) = self.scan_angle_rank as u8;
        *output.get_unchecked_mut(17) = self.user_data;
        u16::pack_into_unchecked(&self.point_source_id, output.get_unchecked_mut(18..20));
    }
}

pub mod v1 {
    use std::io::{Read, Write};

    use crate::compressors::{
        IntegerCompressor, IntegerCompressorBuilder, DEFAULT_COMPRESS_CONTEXTS,
    };
    use crate::decoders::ArithmeticDecoder;
    use crate::decompressors::{
        IntegerDecompressor, IntegerDecompressorBuilder, DEFAULT_DECOMPRESS_CONTEXTS,
    };
    use crate::encoders::ArithmeticEncoder;
    use crate::las::point0::LasPoint0;
    use crate::models::{ArithmeticModel, ArithmeticModelBuilder};
    use crate::packers::Packable;
    use crate::record::{FieldCompressor, FieldDecompressor};

    use super::Point0;

    /// find median difference from 3 preceding differences
    fn median_diff(diff_array: &[i32; 3]) -> i32 {
        unsafe {
            if diff_array.get_unchecked(0) < diff_array.get_unchecked(1) {
                if diff_array.get_unchecked(1) < diff_array.get_unchecked(2) {
                    *diff_array.get_unchecked(1)
                } else if diff_array.get_unchecked(0) < diff_array.get_unchecked(2) {
                    *diff_array.get_unchecked(2)
                } else {
                    *diff_array.get_unchecked(0)
                }
            } else if diff_array.get_unchecked(0) < diff_array.get_unchecked(2) {
                *diff_array.get_unchecked(0)
            } else if diff_array.get_unchecked(1) < diff_array.get_unchecked(2) {
                *diff_array.get_unchecked(2)
            } else {
                *diff_array.get_unchecked(1)
            }
        }
    }

    pub struct LasPoint0Decompressor {
        last_point: Point0,
        last_x_diffs: [i32; 3],
        last_y_diffs: [i32; 3],
        last_incr: usize,

        ic_dx: IntegerDecompressor,
        ic_dy: IntegerDecompressor,
        ic_dz: IntegerDecompressor,
        ic_intensity: IntegerDecompressor,
        ic_scan_angle_rank: IntegerDecompressor,
        ic_point_source_id: IntegerDecompressor,

        changed_values_model: ArithmeticModel,
        // All theses vec have 256 elements
        // all the associated dimensions have 256 elements: [0..255]
        bit_byte_models: Vec<Option<ArithmeticModel>>,
        classification_models: Vec<Option<ArithmeticModel>>,
        user_data_models: Vec<Option<ArithmeticModel>>,
    }

    impl Default for LasPoint0Decompressor {
        fn default() -> Self {
            Self {
                last_point: Default::default(),
                last_x_diffs: [0i32; 3],
                last_y_diffs: [0i32; 3],
                last_incr: 0,
                ic_dx: IntegerDecompressorBuilder::new()
                    .bits(32)
                    .build_initialized(),
                ic_dy: IntegerDecompressorBuilder::new()
                    .bits(32)
                    .contexts(20)
                    .build_initialized(),
                ic_dz: IntegerDecompressorBuilder::new()
                    .bits(32)
                    .contexts(20)
                    .build_initialized(),
                ic_intensity: IntegerDecompressorBuilder::new()
                    .bits(16)
                    .build_initialized(),
                ic_scan_angle_rank: IntegerDecompressorBuilder::new()
                    .bits(8)
                    .contexts(2)
                    .build_initialized(),
                ic_point_source_id: IntegerDecompressorBuilder::new()
                    .bits(16)
                    .build_initialized(),
                changed_values_model: ArithmeticModelBuilder::new(64).build(),
                bit_byte_models: (0..256).map(|_| None).collect(),
                classification_models: (0..256).map(|_| None).collect(),
                user_data_models: (0..256).map(|_| None).collect(),
            }
        }
    }

    impl LasPoint0Decompressor {
        fn median_x_diff(&self) -> i32 {
            median_diff(&self.last_x_diffs)
        }

        fn median_y_diff(&self) -> i32 {
            median_diff(&self.last_y_diffs)
        }
    }

    pub struct LasPoint0Compressor {
        last_point: Point0,
        last_x_diffs: [i32; 3],
        last_y_diffs: [i32; 3],
        last_incr: usize,

        ic_dx: IntegerCompressor,
        ic_dy: IntegerCompressor,
        ic_dz: IntegerCompressor,
        ic_intensity: IntegerCompressor,
        ic_scan_angle_rank: IntegerCompressor,
        ic_point_source_id: IntegerCompressor,

        changed_values_model: ArithmeticModel,
        // All theses vec have 256 elements
        // all the associated dimensions have 256 elements: [0..255]
        bit_byte_models: Vec<Option<ArithmeticModel>>,
        classification_models: Vec<Option<ArithmeticModel>>,
        user_data_models: Vec<Option<ArithmeticModel>>,
    }

    impl Default for LasPoint0Compressor {
        fn default() -> Self {
            Self {
                last_point: Default::default(),
                last_x_diffs: [0i32; 3],
                last_y_diffs: [0i32; 3],
                last_incr: 0,
                ic_dx: IntegerCompressorBuilder::new().bits(32).build_initialized(),
                ic_dy: IntegerCompressorBuilder::new()
                    .bits(32)
                    .contexts(20)
                    .build_initialized(),
                ic_dz: IntegerCompressorBuilder::new()
                    .bits(32)
                    .contexts(20)
                    .build_initialized(),
                ic_intensity: IntegerCompressorBuilder::new().bits(16).build_initialized(),
                ic_scan_angle_rank: IntegerCompressorBuilder::new()
                    .bits(8)
                    .contexts(2)
                    .build_initialized(),
                ic_point_source_id: IntegerCompressorBuilder::new().bits(16).build_initialized(),
                changed_values_model: ArithmeticModelBuilder::new(64).build(),
                bit_byte_models: (0..256).map(|_| None).collect(),
                classification_models: (0..256).map(|_| None).collect(),
                user_data_models: (0..256).map(|_| None).collect(),
            }
        }
    }

    impl<W: Write> FieldCompressor<W> for LasPoint0Compressor {
        fn size_of_field(&self) -> usize {
            20
        }

        fn compress_first(&mut self, dst: &mut W, buf: &[u8]) -> std::io::Result<()> {
            dst.write_all(buf)?;
            self.last_point = Point0::unpack_from(buf);
            Ok(())
        }

        fn compress_with(
            &mut self,
            encoder: &mut ArithmeticEncoder<W>,
            buf: &[u8],
        ) -> std::io::Result<()> {
            let current_point = Point0::unpack_from(buf);
            let median_x = median_diff(&self.last_x_diffs);
            let median_y = median_diff(&self.last_y_diffs);

            let x_diff = current_point.x() - self.last_point.x();
            let y_diff = current_point.y() - self.last_point.y();

            self.ic_dx
                .compress(encoder, median_x, x_diff, DEFAULT_COMPRESS_CONTEXTS)?;
            let k_bits = self.ic_dx.k();
            self.ic_dy.compress(
                encoder,
                median_y,
                y_diff,
                if k_bits < 19 { k_bits } else { 19 },
            )?;

            let k_bits = (k_bits + self.ic_dy.k()) / 2;
            self.ic_dz.compress(
                encoder,
                self.last_point.z,
                current_point.z,
                if k_bits < 19 { k_bits } else { 19 },
            )?;

            let changed_values: u8 = ((self.last_point.intensity() != current_point.intensity())
                as u8)
                << 5
                | ((self.last_point.bit_fields() != current_point.bit_fields()) as u8) << 4
                | ((self.last_point.classification() != current_point.classification()) as u8) << 3
                | ((self.last_point.scan_angle_rank() != current_point.scan_angle_rank()) as u8)
                    << 2
                | ((self.last_point.user_data() != current_point.user_data()) as u8) << 1
                | (self.last_point.point_source_id() != current_point.point_source_id()) as u8;

            encoder.encode_symbol(&mut self.changed_values_model, u32::from(changed_values))?;

            if changed_values != 0 {
                if (changed_values & 32) != 0 {
                    self.ic_intensity.compress(
                        encoder,
                        i32::from(self.last_point.intensity),
                        i32::from(current_point.intensity),
                        DEFAULT_COMPRESS_CONTEXTS,
                    )?;
                }

                if (changed_values & 16) != 0 {
                    let model = &mut self.bit_byte_models[self.last_point.bit_fields() as usize]
                        .get_or_insert(ArithmeticModelBuilder::new(256).build());
                    encoder.encode_symbol(model, u32::from(current_point.bit_fields()))?;
                }

                if (changed_values & 8) != 0 {
                    let model = &mut self.classification_models
                        [self.last_point.classification() as usize]
                        .get_or_insert(ArithmeticModelBuilder::new(256).build());
                    encoder.encode_symbol(model, u32::from(current_point.classification))?;
                }

                if (changed_values & 4) != 0 {
                    self.ic_scan_angle_rank.compress(
                        encoder,
                        i32::from(self.last_point.scan_angle_rank),
                        i32::from(current_point.scan_angle_rank),
                        (k_bits < 3) as u32,
                    )?;
                }

                if (changed_values & 2) != 0 {
                    let model = self.user_data_models[self.last_point.user_data() as usize]
                        .get_or_insert(ArithmeticModelBuilder::new(256).build());
                    encoder.encode_symbol(model, u32::from(current_point.user_data))?;
                }

                if (changed_values & 1) != 0 {
                    self.ic_point_source_id.compress(
                        encoder,
                        i32::from(self.last_point.point_source_id),
                        i32::from(current_point.point_source_id),
                        DEFAULT_COMPRESS_CONTEXTS,
                    )?;
                }
            }
            self.last_x_diffs[self.last_incr] = x_diff;
            self.last_y_diffs[self.last_incr] = y_diff;
            self.last_incr += 1;
            if self.last_incr > 2 {
                self.last_incr = 0;
            }
            self.last_point = current_point;
            Ok(())
        }
    }

    impl<R: Read> FieldDecompressor<R> for LasPoint0Decompressor {
        fn size_of_field(&self) -> usize {
            20
        }

        fn decompress_first(&mut self, src: &mut R, first_point: &mut [u8]) -> std::io::Result<()> {
            src.read_exact(first_point)?;
            self.last_point = Point0::unpack_from(first_point);
            Ok(())
        }

        fn decompress_with(
            &mut self,
            mut decoder: &mut ArithmeticDecoder<R>,
            buf: &mut [u8],
        ) -> std::io::Result<()> {
            // Decompress x, y, z
            let median_x = self.median_x_diff();
            let median_y = self.median_y_diff();

            let x_diff =
                self.ic_dx
                    .decompress(&mut decoder, median_x, DEFAULT_DECOMPRESS_CONTEXTS)?;
            self.last_point.x += x_diff;
            // we use the number k of bits corrector bits to switch contexts
            let k_bits = self.ic_dx.k();
            let y_diff = self.ic_dy.decompress(
                &mut decoder,
                median_y,
                if k_bits < 19 { k_bits } else { 19 },
            )?;
            self.last_point.y += y_diff;
            let k_bits = (k_bits + self.ic_dy.k()) / 2;
            self.last_point.z = self.ic_dz.decompress(
                &mut decoder,
                self.last_point.z,
                if k_bits < 19 { k_bits } else { 19 },
            )?;

            let changed_value = decoder.decode_symbol(&mut self.changed_values_model)? as i32;
            if changed_value != 0 {
                if (changed_value & 32) != 0 {
                    self.last_point.intensity = self.ic_intensity.decompress(
                        &mut decoder,
                        self.last_point.intensity as i32,
                        DEFAULT_DECOMPRESS_CONTEXTS,
                    )? as u16;
                }

                if (changed_value & 16) != 0 {
                    let model = self.bit_byte_models[self.last_point.bit_fields() as usize]
                        .get_or_insert_with(|| ArithmeticModelBuilder::new(256).build());
                    self.last_point
                        .set_bit_fields(decoder.decode_symbol(model)? as u8);
                }

                if (changed_value & 8) != 0 {
                    let model = self.classification_models[self.last_point.classification as usize]
                        .get_or_insert_with(|| ArithmeticModelBuilder::new(256).build());
                    self.last_point
                        .set_classification(decoder.decode_symbol(model)? as u8);
                }

                if (changed_value & 4) != 0 {
                    self.last_point
                        .set_scan_angle_rank(self.ic_scan_angle_rank.decompress(
                            &mut decoder,
                            i32::from(self.last_point.scan_angle_rank),
                            (k_bits < 3) as u32,
                        )? as i8);
                }

                if (changed_value & 2) != 0 {
                    let model = self.user_data_models[self.last_point.user_data as usize]
                        .get_or_insert_with(|| ArithmeticModelBuilder::new(256).build());
                    self.last_point
                        .set_user_data(decoder.decode_symbol(model)? as u8);
                }

                if (changed_value & 1) != 0 {
                    self.last_point
                        .set_point_source_id(self.ic_point_source_id.decompress(
                            &mut decoder,
                            i32::from(self.last_point.point_source_id),
                            DEFAULT_DECOMPRESS_CONTEXTS,
                        )? as u16);
                }
            }

            // record the differences
            self.last_x_diffs[self.last_incr] = x_diff;
            self.last_y_diffs[self.last_incr] = y_diff;
            self.last_incr += 1;
            if self.last_incr > 2 {
                self.last_incr = 0;
            }
            self.last_point.pack_into(buf);
            Ok(())
        }
    }

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

        #[test]
        fn median_diff_test_1_elem() {
            let a = [1, 0, 0];
            assert_eq!(median_diff(&a), 0);

            let a = [-1, 0, 0];
            assert_eq!(median_diff(&a), 0);
        }

        #[test]
        fn median_diff_test_2_elem() {
            let a = [3, 1, 0];
            assert_eq!(median_diff(&a), 1);

            let a = [-3, 1, 0];
            assert_eq!(median_diff(&a), 0);
        }

        #[test]
        fn median_diff_test_3_elem() {
            let a = [3, 1, 4];
            assert_eq!(median_diff(&a), 3);

            let a = [-3, 1, -5];
            assert_eq!(median_diff(&a), -3);
        }
    }
}

pub mod v2 {
    use std::io::{Read, Write};

    use crate::compressors::{IntegerCompressor, IntegerCompressorBuilder};
    use crate::decoders::ArithmeticDecoder;
    use crate::decompressors::{IntegerDecompressor, IntegerDecompressorBuilder};
    use crate::encoders::ArithmeticEncoder;
    use crate::las::point0::LasPoint0;
    use crate::las::utils;
    use crate::models::{ArithmeticModel, ArithmeticModelBuilder};
    use crate::packers::Packable;
    use crate::record::{FieldCompressor, FieldDecompressor};

    use super::Point0;

    struct Point10ChangedValues {
        value: i32,
    }

    /// Only valid for version 2 of the compression / decompression
    /// Compared to version 1, the flag bit used for the intensity & bit_fields
    /// have been swapped
    impl Point10ChangedValues {
        pub fn from_points<P: LasPoint0, OP: LasPoint0>(
            current: &P,
            last: &OP,
            last_intensity: u16,
        ) -> Self {
            // This logic here constructs a 5-bit changed value which is basically a bit map of what has changed
            // since the last point, not considering the x, y and z values

            let bit_fields_changed = ((last.return_number() ^ current.return_number()) != 0)
                | ((last.number_of_returns_of_given_pulse()
                    ^ current.number_of_returns_of_given_pulse())
                    != 0)
                | (last.scan_direction_flag() ^ current.scan_direction_flag())
                | (last.edge_of_flight_line() ^ current.edge_of_flight_line());

            let intensity_changed = (last_intensity ^ current.intensity()) != 0;
            let classification_changed = (last.classification() ^ current.classification()) != 0;
            let scan_angle_rank_changed = (last.scan_angle_rank() ^ current.scan_angle_rank()) != 0;
            let user_data_changed = (last.user_data() ^ current.user_data()) != 0;
            let point_source_id_changed = (last.point_source_id() ^ current.point_source_id()) != 0;
            Point10ChangedValues {
                value: (bit_fields_changed as i32) << 5
                    | (intensity_changed as i32) << 4
                    | (classification_changed as i32) << 3
                    | (scan_angle_rank_changed as i32) << 2
                    | (user_data_changed as i32) << 1
                    | (point_source_id_changed as i32),
            }
        }

        pub fn bit_fields_changed(&self) -> bool {
            (self.value & (1 << 5)) != 0
        }

        pub fn intensity_changed(&self) -> bool {
            (self.value & (1 << 4)) != 0
        }

        pub fn classification_changed(&self) -> bool {
            (self.value & (1 << 3)) != 0
        }

        pub fn scan_angle_rank_changed(&self) -> bool {
            (self.value & (1 << 2)) != 0
        }

        pub fn user_data_changed(&self) -> bool {
            (self.value & (1 << 1)) != 0
        }

        pub fn point_source_id_changed(&self) -> bool {
            (self.value & 1) != 0
        }
    }

    struct Common {
        last_intensity: [u16; 16],

        // can't have arrays as StreamingMedian is not a copy type
        // 16 elements both
        last_x_diff_median: Vec<utils::StreamingMedian<i32>>,
        last_y_diff_median: Vec<utils::StreamingMedian<i32>>,

        last_height: [i32; 8],

        changed_values: ArithmeticModel,

        // can't have arrays as ArithmeticModel is not a copy type
        scan_angle_rank: Vec<ArithmeticModel>,
        // 2
        bit_byte: Vec<ArithmeticModel>,
        // 256
        classification: Vec<ArithmeticModel>,
        //256
        user_data: Vec<ArithmeticModel>, //256
    }

    impl Common {
        pub fn new() -> Self {
            Self {
                last_intensity: [0u16; 16],
                last_x_diff_median: (0..16)
                    .map(|_i| utils::StreamingMedian::<i32>::new())
                    .collect(),
                last_y_diff_median: (0..16)
                    .map(|_i| utils::StreamingMedian::<i32>::new())
                    .collect(),
                last_height: [0i32; 8],
                changed_values: ArithmeticModelBuilder::new(64).build(),
                scan_angle_rank: (0..2)
                    .map(|_i| ArithmeticModelBuilder::new(256).build())
                    .collect(),
                bit_byte: (0..256)
                    .map(|_i| ArithmeticModelBuilder::new(256).build())
                    .collect(),
                classification: (0..256)
                    .map(|_i| ArithmeticModelBuilder::new(256).build())
                    .collect(),
                user_data: (0..256)
                    .map(|_i| ArithmeticModelBuilder::new(256).build())
                    .collect(),
            }
        }
    }

    pub struct LasPoint0Compressor {
        last_point: Point0,
        ic_intensity: IntegerCompressor,
        ic_point_source_id: IntegerCompressor,
        ic_dx: IntegerCompressor,
        ic_dy: IntegerCompressor,
        ic_z: IntegerCompressor,
        common: Common,
    }

    impl Default for LasPoint0Compressor {
        fn default() -> Self {
            Self {
                last_point: Default::default(),
                ic_intensity: IntegerCompressorBuilder::new()
                    .bits(16)
                    .contexts(4)
                    .build_initialized(),
                ic_point_source_id: IntegerCompressorBuilder::new().bits(16).build_initialized(),
                ic_dx: IntegerCompressorBuilder::new()
                    .bits(32)
                    .contexts(2)
                    .build_initialized(),
                ic_dy: IntegerCompressorBuilder::new()
                    .bits(32)
                    .contexts(22)
                    .build_initialized(),
                ic_z: IntegerCompressorBuilder::new()
                    .bits(32)
                    .contexts(20)
                    .build_initialized(),
                common: Common::new(),
            }
        }
    }

    impl<W: Write> FieldCompressor<W> for LasPoint0Compressor {
        fn size_of_field(&self) -> usize {
            20
        }

        fn compress_first(&mut self, dst: &mut W, buf: &[u8]) -> std::io::Result<()> {
            self.last_point = Point0::unpack_from(buf);
            dst.write_all(buf)
        }

        fn compress_with(
            &mut self,
            mut encoder: &mut ArithmeticEncoder<W>,
            buf: &[u8],
        ) -> std::io::Result<()> {
            let current_point = Point0::unpack_from(&buf);
            let r = current_point.return_number();
            let n = current_point.number_of_returns_of_given_pulse();
            // According to table  m is in range 0..16
            let m = utils::NUMBER_RETURN_MAP[n as usize][r as usize];
            // According to table l is in range 0..8
            let l = utils::NUMBER_RETURN_LEVEL[n as usize][r as usize];

            let changed_values =
                Point10ChangedValues::from_points(&current_point, &self.last_point, *unsafe {
                    self.common.last_intensity.get_unchecked(m as usize)
                });

            // compress which other values have changed

            encoder.encode_symbol(&mut self.common.changed_values, changed_values.value as u32)?;

            if changed_values.bit_fields_changed() {
                let b = current_point.bit_fields();
                let last_b = self.last_point.bit_fields();
                encoder.encode_symbol(
                    unsafe { self.common.bit_byte.get_unchecked_mut(last_b as usize) },
                    u32::from(b),
                )?;
            }

            if changed_values.intensity_changed() {
                self.ic_intensity.compress(
                    &mut encoder,
                    i32::from(self.common.last_intensity[m as usize]),
                    i32::from(current_point.intensity),
                    if m < 3 { u32::from(m) } else { 3 },
                )?;
                self.common.last_intensity[m as usize] = current_point.intensity;
            }

            if changed_values.classification_changed() {
                encoder.encode_symbol(
                    unsafe {
                        self.common
                            .classification
                            .get_unchecked_mut(self.last_point.classification as usize)
                    },
                    u32::from(current_point.classification),
                )?;
            }

            if changed_values.scan_angle_rank_changed() {
                // the "as u8" before "as u32" is vital
                encoder.encode_symbol(
                    unsafe {
                        self.common
                            .scan_angle_rank
                            .get_unchecked_mut(current_point.scan_direction_flag() as usize)
                    },
                    (current_point
                        .scan_angle_rank
                        .wrapping_sub(self.last_point.scan_angle_rank)) as u8
                        as u32,
                )?;
            }

            if changed_values.user_data_changed() {
                encoder.encode_symbol(
                    unsafe {
                        self.common
                            .user_data
                            .get_unchecked_mut(self.last_point.user_data as usize)
                    },
                    u32::from(current_point.user_data),
                )?;
            }

            if changed_values.point_source_id_changed() {
                self.ic_point_source_id.compress(
                    &mut encoder,
                    i32::from(self.last_point.point_source_id),
                    i32::from(current_point.point_source_id),
                    0,
                )?;
            }

            //compress x coordinates
            let median = unsafe { self.common.last_x_diff_median.get_unchecked(m as usize) }.get();
            let diff = current_point.x.wrapping_sub(self.last_point.x);
            self.ic_dx
                .compress(&mut encoder, median, diff, (n == 1) as u32)?;
            unsafe { self.common.last_x_diff_median.get_unchecked_mut(m as usize) }.add(diff);

            //compress y coordinates
            let k_bits = self.ic_dx.k();
            let median = unsafe {
                self.common
                    .last_y_diff_median
                    .get_unchecked(m as usize)
                    .get()
            };
            let diff = current_point.y.wrapping_sub(self.last_point.y);
            let context = (n == 1) as u32
                + if k_bits < 20 {
                    utils::u32_zero_bit(k_bits)
                } else {
                    20
                };
            self.ic_dy.compress(&mut encoder, median, diff, context)?;
            unsafe {
                self.common
                    .last_y_diff_median
                    .get_unchecked_mut(m as usize)
                    .add(diff);
            }

            //compress z coordinates
            let k_bits = (self.ic_dx.k() + self.ic_dy.k()) / 2;
            let context = (n == 1) as u32
                + if k_bits < 18 {
                    utils::u32_zero_bit(k_bits)
                } else {
                    18
                };
            self.ic_z.compress(
                &mut encoder,
                *unsafe { self.common.last_height.get_unchecked(l as usize) },
                current_point.z(),
                context,
            )?;
            unsafe { *self.common.last_height.get_unchecked_mut(l as usize) = current_point.z() };
            self.last_point = current_point;
            Ok(())
        }
    }

    pub struct LasPoint0Decompressor {
        last_point: Point0,
        ic_intensity: IntegerDecompressor,
        ic_point_source_id: IntegerDecompressor,
        ic_dx: IntegerDecompressor,
        ic_dy: IntegerDecompressor,
        ic_z: IntegerDecompressor,

        common: Common,
    }

    impl Default for LasPoint0Decompressor {
        fn default() -> Self {
            Self {
                last_point: Default::default(),
                ic_intensity: IntegerDecompressorBuilder::new()
                    .bits(16)
                    .contexts(4)
                    .build_initialized(),
                ic_point_source_id: IntegerDecompressorBuilder::new()
                    .bits(16)
                    .build_initialized(),
                ic_dx: IntegerDecompressorBuilder::new()
                    .bits(32)
                    .contexts(2)
                    .build_initialized(),
                ic_dy: IntegerDecompressorBuilder::new()
                    .bits(32)
                    .contexts(22)
                    .build_initialized(),
                ic_z: IntegerDecompressorBuilder::new()
                    .bits(32)
                    .contexts(20)
                    .build_initialized(),
                common: Common::new(),
            }
        }
    }

    impl<R: Read> FieldDecompressor<R> for LasPoint0Decompressor {
        fn size_of_field(&self) -> usize {
            20
        }

        fn decompress_first(&mut self, src: &mut R, first_point: &mut [u8]) -> std::io::Result<()> {
            src.read_exact(first_point)?;
            self.last_point = Point0::unpack_from(first_point);
            self.last_point.intensity = 0;
            Ok(())
        }

        fn decompress_with(
            &mut self,
            mut decoder: &mut ArithmeticDecoder<R>,
            buf: &mut [u8],
        ) -> std::io::Result<()> {
            let changed_value = Point10ChangedValues {
                value: decoder.decode_symbol(&mut self.common.changed_values)? as i32,
            };

            let r;
            let n;
            let m;
            let l;

            unsafe {
                if changed_value.value != 0 {
                    // there was some change in one of the fields (other than x, y and z)

                    if changed_value.bit_fields_changed() {
                        let mut b = self.last_point.bit_fields();
                        b = decoder
                            .decode_symbol(self.common.bit_byte.get_unchecked_mut(b as usize))?
                            as u8;
                        self.last_point.set_bit_fields(b);
                    }

                    r = self.last_point.return_number();
                    n = self.last_point.number_of_returns_of_given_pulse();
                    // According to table m is in range 0..16
                    m = utils::NUMBER_RETURN_MAP[n as usize][r as usize];
                    // According to table l is in range 0..8
                    l = utils::NUMBER_RETURN_LEVEL[n as usize][r as usize];

                    if changed_value.intensity_changed() {
                        self.last_point.intensity = self.ic_intensity.decompress(
                            &mut decoder,
                            i32::from(*self.common.last_intensity.get_unchecked(m as usize)),
                            if m < 3 { u32::from(m) } else { 3 },
                        )? as u16;
                        *self.common.last_intensity.get_unchecked_mut(m as usize) =
                            self.last_point.intensity;
                    } else {
                        self.last_point.intensity =
                            *self.common.last_intensity.get_unchecked(m as usize);
                    }

                    if changed_value.classification_changed() {
                        self.last_point.set_classification(
                            decoder.decode_symbol(
                                self.common
                                    .classification
                                    .get_unchecked_mut(self.last_point.classification as usize),
                            )? as u8,
                        );
                    }

                    if changed_value.scan_angle_rank_changed() {
                        let val = decoder.decode_symbol(
                            self.common
                                .scan_angle_rank
                                .get_unchecked_mut(self.last_point.scan_direction_flag as usize),
                        )? as i8;
                        self.last_point.scan_angle_rank =
                            self.last_point.scan_angle_rank.wrapping_add(val);
                    }

                    if changed_value.user_data_changed() {
                        self.last_point.set_user_data(
                            decoder.decode_symbol(
                                self.common
                                    .user_data
                                    .get_unchecked_mut(self.last_point.user_data as usize),
                            )? as u8,
                        );
                    }

                    if changed_value.point_source_id_changed() {
                        self.last_point
                            .set_point_source_id(self.ic_point_source_id.decompress(
                                &mut decoder,
                                i32::from(self.last_point.point_source_id),
                                0,
                            )? as u16);
                    }
                } else {
                    r = self.last_point.return_number();
                    n = self.last_point.number_of_returns_of_given_pulse();
                    m = utils::NUMBER_RETURN_MAP[n as usize][r as usize];
                    l = utils::NUMBER_RETURN_LEVEL[n as usize][r as usize];
                }

                // decompress x
                let median = self
                    .common
                    .last_x_diff_median
                    .get_unchecked(m as usize)
                    .get();
                let diff = self
                    .ic_dx
                    .decompress(&mut decoder, median, (n == 1) as u32)?;
                self.last_point.x = self.last_point.x.wrapping_add(diff);
                self.common
                    .last_x_diff_median
                    .get_unchecked_mut(m as usize)
                    .add(diff);

                // decompress y
                let median = self
                    .common
                    .last_y_diff_median
                    .get_unchecked(m as usize)
                    .get();
                let k_bits = self.ic_dx.k();
                let context = (n == 1) as u32
                    + if k_bits < 20 {
                        utils::u32_zero_bit(k_bits)
                    } else {
                        20
                    };
                let diff = self.ic_dy.decompress(&mut decoder, median, context)?;
                self.last_point.y = self.last_point.y.wrapping_add(diff);
                self.common
                    .last_y_diff_median
                    .get_unchecked_mut(m as usize)
                    .add(diff);

                // decompress z coordinate
                let k_bits = (self.ic_dx.k() + self.ic_dy.k()) / 2;
                let context = (n == 1) as u32
                    + if k_bits < 18 {
                        utils::u32_zero_bit(k_bits)
                    } else {
                        18
                    };
                self.last_point.z = self.ic_z.decompress(
                    &mut decoder,
                    *self.common.last_height.get_unchecked(l as usize),
                    context,
                )?;
                *self.common.last_height.get_unchecked_mut(l as usize) = self.last_point.z();
            }
            self.last_point.pack_into(buf);
            Ok(())
        }
    }
}