-
Notifications
You must be signed in to change notification settings - Fork 26
/
laszip_dll.cs
2305 lines (1974 loc) · 69.2 KB
/
laszip_dll.cs
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
//===============================================================================
//
// FILE: laszip_dll.cs
//
// CONTENTS:
//
// C# port of a simple DLL interface to LASzip.
//
// PROGRAMMERS:
//
// [email protected] - http://rapidlasso.com
//
// COPYRIGHT:
//
// (c) 2005-2012, martin isenburg, rapidlasso - tools to catch reality
// (c) of the C# port 2014-2017 by Shinta <[email protected]>
//
// This is free software; you can redistribute and/or modify it under the
// terms of the GNU Lesser General Licence as published by the Free 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: omitted for easier Copy&Paste (pls see the original)
//
//===============================================================================
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace laszip.net
{
public class laszip_dll
{
public laszip_header header = new laszip_header();
long p_count;
long npoints;
public laszip_point point = new laszip_point();
Stream streamin;
bool leaveStreamInOpen;
LASreadPoint reader;
Stream streamout;
bool leaveStreamOutOpen;
LASwritePoint writer;
string error;
string warning;
static int laszip_get_version(out byte version_major, out byte version_minor, out ushort version_revision, out uint version_build)
{
version_major = LASzip.VERSION_MAJOR;
version_minor = LASzip.VERSION_MINOR;
version_revision = LASzip.VERSION_REVISION;
version_build = LASzip.VERSION_BUILD_DATE;
return 0;
}
public string laszip_get_error()
{
return error;
}
public string laszip_get_warning()
{
return warning;
}
public static laszip_dll laszip_create()
{
laszip_dll ret = new laszip_dll();
ret.laszip_clean();
return ret;
}
public int laszip_clean()
{
try
{
if (reader != null)
{
error = "cannot clean while reader is open.";
return 1;
}
if (writer != null)
{
error = "cannot clean while writer is open.";
return 1;
}
// zero everything
header.file_source_ID = 0;
header.global_encoding = 0;
header.project_ID_GUID_data_1 = 0;
header.project_ID_GUID_data_2 = 0;
header.project_ID_GUID_data_3 = 0;
Array.Clear(header.project_ID_GUID_data_4, 0, header.project_ID_GUID_data_4.Length);
header.version_major = 0;
header.version_minor = 0;
Array.Clear(header.system_identifier, 0, header.system_identifier.Length);
Array.Clear(header.generating_software, 0, header.generating_software.Length);
header.file_creation_day = 0;
header.file_creation_year = 0;
header.header_size = 0;
header.offset_to_point_data = 0;
header.number_of_variable_length_records = 0;
header.point_data_format = 0;
header.point_data_record_length = 0;
header.number_of_point_records = 0;
Array.Clear(header.number_of_points_by_return, 0, header.number_of_points_by_return.Length);
header.x_scale_factor = 0;
header.y_scale_factor = 0;
header.z_scale_factor = 0;
header.x_offset = 0;
header.y_offset = 0;
header.z_offset = 0;
header.max_x = 0;
header.min_x = 0;
header.max_y = 0;
header.min_y = 0;
header.max_z = 0;
header.min_z = 0;
header.start_of_waveform_data_packet_record = 0;
header.start_of_first_extended_variable_length_record = 0;
header.number_of_extended_variable_length_records = 0;
header.extended_number_of_point_records = 0;
Array.Clear(header.extended_number_of_points_by_return, 0, header.extended_number_of_points_by_return.Length);
header.user_data_in_header_size = 0;
header.user_data_in_header = null;
header.vlrs = null;
header.user_data_after_header_size = 0;
header.user_data_after_header = null;
p_count = 0;
npoints = 0;
point.X = 0;
point.Y = 0;
point.Z = 0;
point.intensity = 0;
point.return_number = 0;// : 3;
point.number_of_returns_of_given_pulse = 0;// : 3;
point.scan_direction_flag = 0;// : 1;
point.edge_of_flight_line = 0;// : 1;
point.classification = 0;
point.scan_angle_rank = 0;
point.user_data = 0;
point.point_source_ID = 0;
point.gps_time = 0;
point.rgb = new ushort[4];
point.wave_packet = new byte[29];
point.extended_point_type = 0;// : 2;
point.extended_scanner_channel = 0;// : 2;
point.extended_classification_flags = 0;// : 4;
point.extended_classification = 0;
point.extended_return_number = 0;// : 4;
point.extended_number_of_returns_of_given_pulse = 0;// : 4;
point.extended_scan_angle = 0;
point.num_extra_bytes = 0;
point.extra_bytes = null;
streamin = null;
reader = null;
streamout = null;
writer = null;
error = null;
warning = null;
// create default header
byte[] generatingSoftware = Encoding.ASCII.GetBytes(string.Format("LASzip.net DLL {0}.{1} r{2} ({3})", LASzip.VERSION_MAJOR, LASzip.VERSION_MINOR, LASzip.VERSION_REVISION, LASzip.VERSION_BUILD_DATE));
Array.Copy(generatingSoftware, header.generating_software, Math.Min(generatingSoftware.Length, 32));
header.version_major = 1;
header.version_minor = 2;
header.header_size = 227;
header.offset_to_point_data = 227;
header.point_data_format = 1;
header.point_data_record_length = 28;
header.x_scale_factor = 0.01;
header.y_scale_factor = 0.01;
header.z_scale_factor = 0.01;
}
catch
{
error = "internal error in laszip_clean";
return 1;
}
return 0;
}
[Obsolete]
public laszip_header laszip_get_header_pointer()
{
return header;
}
[Obsolete]
public laszip_point laszip_get_point_pointer()
{
return point;
}
public int laszip_get_point_count(out long count)
{
count = 0;
if (reader == null && writer == null)
{
error = "getting count before reader or writer was opened";
return 1;
}
count = p_count;
error = null;
return 0;
}
public int laszip_get_number_of_point(out long npoints)
{
npoints = 0;
if (reader == null && writer == null)
{
error = "getting count before reader or writer was opened";
return 1;
}
npoints = this.npoints;
error = null;
return 0;
}
public int laszip_set_header(laszip_header header)
{
if (header == null)
{
error = "laszip_header_struct pointer is zero";
return 1;
}
if (reader != null)
{
error = "cannot set header after reader was opened";
return 1;
}
if (writer != null)
{
error = "cannot set header after writer was opened";
return 1;
}
try
{
this.header.file_source_ID = header.file_source_ID;
this.header.global_encoding = header.global_encoding;
this.header.project_ID_GUID_data_1 = header.project_ID_GUID_data_1;
this.header.project_ID_GUID_data_2 = header.project_ID_GUID_data_2;
this.header.project_ID_GUID_data_3 = header.project_ID_GUID_data_3;
Array.Copy(header.project_ID_GUID_data_4, this.header.project_ID_GUID_data_4, 8);
this.header.version_major = header.version_major;
this.header.version_minor = header.version_minor;
Array.Copy(header.system_identifier, this.header.system_identifier, 32);
Array.Copy(header.generating_software, this.header.generating_software, 32);
this.header.file_creation_day = header.file_creation_day;
this.header.file_creation_year = header.file_creation_year;
this.header.header_size = header.header_size;
this.header.offset_to_point_data = header.offset_to_point_data;
this.header.number_of_variable_length_records = header.number_of_variable_length_records;
this.header.point_data_format = header.point_data_format;
this.header.point_data_record_length = header.point_data_record_length;
this.header.number_of_point_records = header.number_of_point_records;
for (int i = 0; i < 5; i++) this.header.number_of_points_by_return[i] = header.number_of_points_by_return[i];
this.header.x_scale_factor = header.x_scale_factor;
this.header.y_scale_factor = header.y_scale_factor;
this.header.z_scale_factor = header.z_scale_factor;
this.header.x_offset = header.x_offset;
this.header.y_offset = header.y_offset;
this.header.z_offset = header.z_offset;
this.header.max_x = header.max_x;
this.header.min_x = header.min_x;
this.header.max_y = header.max_y;
this.header.min_y = header.min_y;
this.header.max_z = header.max_z;
this.header.min_z = header.min_z;
if (this.header.version_minor >= 3)
{
this.header.start_of_waveform_data_packet_record = header.start_of_first_extended_variable_length_record;
}
if (this.header.version_minor >= 4)
{
this.header.start_of_first_extended_variable_length_record = header.start_of_first_extended_variable_length_record;
this.header.number_of_extended_variable_length_records = header.number_of_extended_variable_length_records;
this.header.extended_number_of_point_records = header.extended_number_of_point_records;
for (int i = 0; i < 15; i++) this.header.extended_number_of_points_by_return[i] = header.extended_number_of_points_by_return[i];
}
this.header.user_data_in_header_size = header.user_data_in_header_size;
this.header.user_data_in_header = null;
if (header.user_data_in_header_size != 0)
{
this.header.user_data_in_header = new byte[header.user_data_in_header_size];
Array.Copy(header.user_data_in_header, this.header.user_data_in_header, header.user_data_in_header_size);
}
this.header.vlrs = new List<laszip_vlr>();
if (header.number_of_variable_length_records != 0)
{
for (int i = 0; i < header.number_of_variable_length_records; i++)
{
this.header.vlrs.Add(new laszip_vlr());
this.header.vlrs[i].reserved = header.vlrs[i].reserved;
Array.Copy(header.vlrs[i].user_id, this.header.vlrs[i].user_id, 16);
this.header.vlrs[i].record_id = header.vlrs[i].record_id;
this.header.vlrs[i].record_length_after_header = header.vlrs[i].record_length_after_header;
Array.Copy(header.vlrs[i].description, this.header.vlrs[i].description, 32);
if (header.vlrs[i].record_length_after_header != 0)
{
this.header.vlrs[i].data = new byte[header.vlrs[i].record_length_after_header];
Array.Copy(header.vlrs[i].data, this.header.vlrs[i].data, header.vlrs[i].record_length_after_header);
}
else
{
this.header.vlrs[i].data = null;
}
}
}
this.header.user_data_after_header_size = header.user_data_after_header_size;
this.header.user_data_after_header = null;
if (header.user_data_after_header_size != 0)
{
this.header.user_data_after_header = new byte[header.user_data_after_header_size];
Array.Copy(header.user_data_after_header, this.header.user_data_after_header, header.user_data_after_header_size);
}
}
catch
{
error = "internal error in laszip_set_header";
return 1;
}
error = null;
return 0;
}
int laszip_check_for_integer_overflow()
{
try
{
// quantize and dequantize the bounding box with current scale_factor and offset
int quant_min_x = MyDefs.I32_QUANTIZE((header.min_x - header.x_offset) / header.x_scale_factor);
int quant_max_x = MyDefs.I32_QUANTIZE((header.max_x - header.x_offset) / header.x_scale_factor);
int quant_min_y = MyDefs.I32_QUANTIZE((header.min_y - header.y_offset) / header.y_scale_factor);
int quant_max_y = MyDefs.I32_QUANTIZE((header.max_y - header.y_offset) / header.y_scale_factor);
int quant_min_z = MyDefs.I32_QUANTIZE((header.min_z - header.z_offset) / header.z_scale_factor);
int quant_max_z = MyDefs.I32_QUANTIZE((header.max_z - header.z_offset) / header.z_scale_factor);
double dequant_min_x = header.x_scale_factor * quant_min_x + header.x_offset;
double dequant_max_x = header.x_scale_factor * quant_max_x + header.x_offset;
double dequant_min_y = header.y_scale_factor * quant_min_y + header.y_offset;
double dequant_max_y = header.y_scale_factor * quant_max_y + header.y_offset;
double dequant_min_z = header.z_scale_factor * quant_min_z + header.z_offset;
double dequant_max_z = header.z_scale_factor * quant_max_z + header.z_offset;
// make sure that there is not sign flip (a 32-bit integer overflow) for the bounding box
if ((header.min_x > 0) != (dequant_min_x > 0))
{
error = string.Format("quantization sign flip for min_x from {0} to {1}. set scale factor for x coarser than {2}", header.min_x, dequant_min_x, header.x_scale_factor);
return 1;
}
if ((header.max_x > 0) != (dequant_max_x > 0))
{
error = string.Format("quantization sign flip for max_x from {0} to {1}. set scale factor for x coarser than {2}", header.max_x, dequant_max_x, header.x_scale_factor);
return 1;
}
if ((header.min_y > 0) != (dequant_min_y > 0))
{
error = string.Format("quantization sign flip for min_y from {0} to {1}. set scale factor for y coarser than {2}", header.min_y, dequant_min_y, header.y_scale_factor);
return 1;
}
if ((header.max_y > 0) != (dequant_max_y > 0))
{
error = string.Format("quantization sign flip for max_y from {0} to {1}. set scale factor for y coarser than {2}", header.max_y, dequant_max_y, header.y_scale_factor);
return 1;
}
if ((header.min_z > 0) != (dequant_min_z > 0))
{
error = string.Format("quantization sign flip for min_z from {0} to {1}. set scale factor for z coarser than {2}", header.min_z, dequant_min_z, header.z_scale_factor);
return 1;
}
if ((header.max_z > 0) != (dequant_max_z > 0))
{
error = string.Format("quantization sign flip for max_z from {0} to {1}. set scale factor for z coarser than {2}", header.max_z, dequant_max_z, header.z_scale_factor);
return 1;
}
}
catch
{
error = "internal error in laszip_auto_offset";
return 1;
}
error = null;
return 0;
}
int laszip_auto_offset()
{
try
{
if (reader != null)
{
error = "cannot auto offset after reader was opened";
return 1;
}
if (writer != null)
{
error = "cannot auto offset after writer was opened";
return 1;
}
// check scale factor
double x_scale_factor = header.x_scale_factor;
double y_scale_factor = header.y_scale_factor;
double z_scale_factor = header.z_scale_factor;
if ((x_scale_factor <= 0) || double.IsInfinity(x_scale_factor))
{
error = string.Format("invalid x scale_factor {0} in header", header.x_scale_factor);
return 1;
}
if ((y_scale_factor <= 0) || double.IsInfinity(y_scale_factor))
{
error = string.Format("invalid y scale_factor {0} in header", header.y_scale_factor);
return 1;
}
if ((z_scale_factor <= 0) || double.IsInfinity(z_scale_factor))
{
error = string.Format("invalid z scale_factor {0} in header", header.z_scale_factor);
return 1;
}
double center_bb_x = (header.min_x + header.max_x) / 2;
double center_bb_y = (header.min_y + header.max_y) / 2;
double center_bb_z = (header.min_z + header.max_z) / 2;
if (double.IsInfinity(center_bb_x))
{
error = string.Format("invalid x coordinate at center of bounding box (min: {0} max: {1})", header.min_x, header.max_x);
return 1;
}
if (double.IsInfinity(center_bb_y))
{
error = string.Format("invalid y coordinate at center of bounding box (min: {0} max: {1})", header.min_y, header.max_y);
return 1;
}
if (double.IsInfinity(center_bb_z))
{
error = string.Format("invalid z coordinate at center of bounding box (min: {0} max: {1})", header.min_z, header.max_z);
return 1;
}
double x_offset = header.x_offset;
double y_offset = header.y_offset;
double z_offset = header.z_offset;
header.x_offset = (MyDefs.I64_FLOOR(center_bb_x / x_scale_factor / 10000000)) * 10000000 * x_scale_factor;
header.y_offset = (MyDefs.I64_FLOOR(center_bb_y / y_scale_factor / 10000000)) * 10000000 * y_scale_factor;
header.z_offset = (MyDefs.I64_FLOOR(center_bb_z / z_scale_factor / 10000000)) * 10000000 * z_scale_factor;
if (laszip_check_for_integer_overflow() != 0)
{
header.x_offset = x_offset;
header.y_offset = y_offset;
header.z_offset = z_offset;
return 1;
}
}
catch
{
error = "internal error in laszip_auto_offset";
return 1;
}
error = null;
return 0;
}
public int laszip_set_point(laszip_point point)
{
if (point == null)
{
error = "laszip_point_struct pointer is zero";
return 1;
}
if (reader != null)
{
error = "cannot set point for reader";
return 1;
}
try
{
this.point.classification = point.classification;
this.point.edge_of_flight_line = point.edge_of_flight_line;
this.point.extended_classification = point.extended_classification;
this.point.extended_classification_flags = point.extended_classification_flags;
this.point.extended_number_of_returns_of_given_pulse = point.extended_number_of_returns_of_given_pulse;
this.point.extended_point_type = point.extended_point_type;
this.point.extended_return_number = point.extended_return_number;
this.point.extended_scan_angle = point.extended_scan_angle;
this.point.extended_scanner_channel = point.extended_scanner_channel;
this.point.gps_time = point.gps_time;
this.point.intensity = point.intensity;
this.point.num_extra_bytes = point.num_extra_bytes;
this.point.number_of_returns_of_given_pulse = point.number_of_returns_of_given_pulse;
this.point.point_source_ID = point.point_source_ID;
this.point.return_number = point.return_number;
Array.Copy(point.rgb, this.point.rgb, 4);
this.point.scan_angle_rank = point.scan_angle_rank;
this.point.scan_direction_flag = point.scan_direction_flag;
this.point.user_data = point.user_data;
this.point.X = point.X;
this.point.Y = point.Y;
this.point.Z = point.Z;
Array.Copy(point.wave_packet, this.point.wave_packet, 29);
if (this.point.extra_bytes != null)
{
if (point.extra_bytes != null)
{
if (this.point.num_extra_bytes == point.num_extra_bytes)
{
Array.Copy(point.extra_bytes, this.point.extra_bytes, point.num_extra_bytes);
}
else
{
error = string.Format("target point has {0} extra bytes but source point has {1}", this.point.num_extra_bytes, point.num_extra_bytes);
return 1;
}
}
else
{
error = "target point has extra bytes but source point does not";
return 1;
}
}
else
{
if (point.extra_bytes != null)
{
error = "source point has extra bytes but target point does not";
return 1;
}
}
}
catch
{
error = "internal error in laszip_set_point";
return 1;
}
error = null;
return 0;
}
public int laszip_set_coordinates(double[] coordinates)
{
if (coordinates == null)
{
error = "laszip_F64 coordinates pointer is zero";
return 1;
}
if (reader != null)
{
error = "cannot set coordinates for reader";
return 1;
}
try
{
// set the coordinates
point.X = MyDefs.I32_QUANTIZE((coordinates[0] - header.x_offset) / header.x_scale_factor);
point.Y = MyDefs.I32_QUANTIZE((coordinates[1] - header.y_offset) / header.y_scale_factor);
point.Z = MyDefs.I32_QUANTIZE((coordinates[2] - header.z_offset) / header.z_scale_factor);
}
catch
{
error = "internal error in laszip_set_coordinates";
return 1;
}
error = null;
return 0;
}
public int laszip_get_coordinates(double[] coordinates)
{
if (coordinates == null)
{
error = "laszip_F64 coordinates pointer is zero";
return 1;
}
try
{
// get the coordinates
coordinates[0] = header.x_scale_factor * point.X + header.x_offset;
coordinates[1] = header.y_scale_factor * point.Y + header.y_offset;
coordinates[2] = header.z_scale_factor * point.Z + header.z_offset;
}
catch
{
error = "internal error in laszip_get_coordinates";
return 1;
}
error = null;
return 0;
}
public unsafe int laszip_set_geokeys(ushort number, laszip_geokey[] key_entries)
{
if (number == 0)
{
error = "number of key_entries is zero";
return 1;
}
if (key_entries == null)
{
error = "key_entries pointer is zero";
return 1;
}
if (reader != null)
{
error = "cannot set geokeys after reader was opened";
return 1;
}
if (writer != null)
{
error = "cannot set geokeys after writer was opened";
return 1;
}
try
{
// create the geokey directory
byte[] buffer = new byte[sizeof(laszip_geokey) * (number + 1)];
fixed (byte* pBuffer = buffer)
{
laszip_geokey* key_entries_plus_one = (laszip_geokey*)pBuffer;
key_entries_plus_one[0].key_id = 1; // aka key_directory_version
key_entries_plus_one[0].tiff_tag_location = 1; // aka key_revision
key_entries_plus_one[0].count = 0; // aka minor_revision
key_entries_plus_one[0].value_offset = number; // aka number_of_keys
for (int i = 0; i < number; i++) key_entries_plus_one[i + 1] = key_entries[i];
}
// fill a VLR
laszip_vlr vlr = new laszip_vlr();
vlr.reserved = 0xAABB;
byte[] user_id = Encoding.ASCII.GetBytes("LASF_Projection");
Array.Copy(user_id, vlr.user_id, Math.Min(user_id.Length, 16));
vlr.record_id = 34735;
vlr.record_length_after_header = (ushort)(8 + number * 8);
// description field must be a null-terminate string, so we don't copy more than 31 characters
byte[] v = Encoding.ASCII.GetBytes(string.Format("LASzip.net DLL {0}.{1} r{2} ({3})", LASzip.VERSION_MAJOR, LASzip.VERSION_MINOR, LASzip.VERSION_REVISION, LASzip.VERSION_BUILD_DATE));
Array.Copy(v, vlr.description, Math.Min(v.Length, 31));
vlr.data = buffer;
// add the VLR
if (laszip_add_vlr(vlr) != 0)
{
error = string.Format("setting {0} geokeys", number);
return 1;
}
}
catch
{
error = "internal error in laszip_set_geokey_entries";
return 1;
}
error = null;
return 0;
}
public int laszip_set_geodouble_params(ushort number, double[] geodouble_params)
{
if (number == 0)
{
error = "number of geodouble_params is zero";
return 1;
}
if (geodouble_params == null)
{
error = "geodouble_params pointer is zero";
return 1;
}
if (reader != null)
{
error = "cannot set geodouble_params after reader was opened";
return 1;
}
if (writer != null)
{
error = "cannot set geodouble_params after writer was opened";
return 1;
}
try
{
// fill a VLR
laszip_vlr vlr = new laszip_vlr();
vlr.reserved = 0xAABB;
byte[] user_id = Encoding.ASCII.GetBytes("LASF_Projection");
Array.Copy(user_id, vlr.user_id, Math.Min(user_id.Length, 16));
vlr.record_id = 34736;
vlr.record_length_after_header = (ushort)(number * 8);
// description field must be a null-terminate string, so we don't copy more than 31 characters
byte[] v = Encoding.ASCII.GetBytes(string.Format("LASzip.net DLL {0}.{1} r{2} ({3})", LASzip.VERSION_MAJOR, LASzip.VERSION_MINOR, LASzip.VERSION_REVISION, LASzip.VERSION_BUILD_DATE));
Array.Copy(v, vlr.description, Math.Min(v.Length, 31));
byte[] buffer = new byte[number * 8];
Buffer.BlockCopy(geodouble_params, 0, buffer, 0, number * 8);
vlr.data = buffer;
// add the VLR
if (laszip_add_vlr(vlr) != 0)
{
error = string.Format("setting {0} geodouble_params", number);
return 1;
}
}
catch
{
error = "internal error in laszip_set_geodouble_params";
return 1;
}
error = null;
return 0;
}
public int laszip_set_geoascii_params(ushort number, byte[] geoascii_params)
{
if (number == 0)
{
error = "number of geoascii_params is zero";
return 1;
}
if (geoascii_params == null)
{
error = "geoascii_params pointer is zero";
return 1;
}
if (reader != null)
{
error = "cannot set geoascii_params after reader was opened";
return 1;
}
if (writer != null)
{
error = "cannot set geoascii_params after writer was opened";
return 1;
}
try
{
// fill a VLR
laszip_vlr vlr = new laszip_vlr();
vlr.reserved = 0xAABB;
byte[] user_id = Encoding.ASCII.GetBytes("LASF_Projection");
Array.Copy(user_id, vlr.user_id, Math.Min(user_id.Length, 16));
vlr.record_id = 34737;
vlr.record_length_after_header = number;
// description field must be a null-terminate string, so we don't copy more than 31 characters
byte[] v = Encoding.ASCII.GetBytes(string.Format("LASzip.net DLL {0}.{1} r{2} ({3})", LASzip.VERSION_MAJOR, LASzip.VERSION_MINOR, LASzip.VERSION_REVISION, LASzip.VERSION_BUILD_DATE));
Array.Copy(v, vlr.description, Math.Min(v.Length, 31));
vlr.data = geoascii_params;
// add the VLR
if (laszip_add_vlr(vlr) != 0)
{
error = string.Format("setting {0} geoascii_params", number);
return 1;
}
}
catch
{
error = "internal error in laszip_set_geoascii_params";
return 1;
}
error = null;
return 0;
}
static bool ArrayCompare(byte[] a, byte[] b)
{
int len = Math.Min(a.Length, b.Length);
int i = 0;
for (; i < len; i++)
{
if (a[i] != b[i]) return false;
if (a[i] == 0) break;
}
if (i < len - 1) return true;
return a.Length == b.Length;
}
public int laszip_add_vlr(laszip_vlr vlr)
{
if (vlr == null)
{
error = "laszip_vlr_struct vlr pointer is zero";
return 1;
}
if ((vlr.record_length_after_header > 0) && (vlr.data == null))
{
error = string.Format("VLR has record_length_after_header of {0} but VLR data pointer is zero", vlr.record_length_after_header);
return 1;
}
if (reader != null)
{
error = "cannot add vlr after reader was opened";
return 1;
}
if (writer != null)
{
error = "cannot add vlr after writer was opened";
return 1;
}
try
{
if (header.vlrs.Count > 0)
{
// overwrite existing VLR ?
for (int i = (int)header.number_of_variable_length_records - 1; i >= 0; i--)
{
if (header.vlrs[i].record_id == vlr.record_id && !ArrayCompare(header.vlrs[i].user_id, vlr.user_id))
{
if (header.vlrs[i].record_length_after_header != 0)
header.offset_to_point_data -= header.vlrs[i].record_length_after_header;
header.vlrs.RemoveAt(i);
}
}
}
header.vlrs.Add(vlr);
header.number_of_variable_length_records = (uint)header.vlrs.Count;
header.offset_to_point_data += 54;
// copy the VLR
header.offset_to_point_data += vlr.record_length_after_header;
}
catch
{
error = "internal error in laszip_add_vlr";
return 1;
}
error = null;
return 0;
}
static int CheckHeaderAndSetup(laszip_header header, bool compress, out LASzip laszip, ref laszip_point point, out uint laszip_vrl_payload_size, out string error)
{
laszip = null;
laszip_vrl_payload_size = 0;
error = null;
#region check header and prepare point
uint vlrs_size = 0;
if (header.version_major != 1)
{
error = string.Format("unknown LAS version {0}.{1}", header.version_major, header.version_minor);
return 1;
}
if (compress && (header.point_data_format > 5))
{
error = string.Format("compressor does not yet support point data format {1}", header.point_data_format);
return 1;
}
if (header.number_of_variable_length_records != 0)
{
if (header.vlrs == null)
{
error = string.Format("number_of_variable_length_records is {0} but vlrs pointer is zero", header.number_of_variable_length_records);
return 1;
}
for (int i = 0; i < header.number_of_variable_length_records; i++)
{
vlrs_size += 54;
if (header.vlrs[i].record_length_after_header != 0)
{
if (header.vlrs == null)
{
error = string.Format("vlrs[{0}].record_length_after_header is {1} but vlrs[{0}].data pointer is zero", i, header.vlrs[i].record_length_after_header);
return 1;
}
vlrs_size += header.vlrs[i].record_length_after_header;
}
}
}
if ((vlrs_size + header.header_size + header.user_data_after_header_size) != header.offset_to_point_data)
{
error = string.Format("header_size ({0}) plus vlrs_size ({1}) plus user_data_after_header_size ({2}) does not equal offset_to_point_data ({3})", header.header_size, vlrs_size, header.user_data_after_header_size, header.offset_to_point_data);
return 1;
}
try
{
laszip = new LASzip();
}
catch
{
error = "could not alloc LASzip";
return 1;
}
if (!laszip.setup(header.point_data_format, header.point_data_record_length, LASzip.COMPRESSOR_NONE))
{
error = string.Format("invalid combination of point_data_format {0} and point_data_record_length {1}", header.point_data_format, header.point_data_record_length);
return 1;
}
#region create point's item pointers
for (uint i = 0; i < laszip.num_items; i++)
{
switch (laszip.items[i].type)
{
case LASitem.Type.POINT14:
case LASitem.Type.POINT10:
case LASitem.Type.GPSTIME11:
case LASitem.Type.RGBNIR14:
case LASitem.Type.RGB12:
case LASitem.Type.WAVEPACKET13: break;
case LASitem.Type.BYTE:
point.num_extra_bytes = laszip.items[i].size;
point.extra_bytes = new byte[point.num_extra_bytes];
break;
default:
error = string.Format("unknown LASitem type {0}", laszip.items[i].type);
return 1;
}
}
#endregion
if (compress)
{
if (!laszip.setup(header.point_data_format, header.point_data_record_length, LASzip.COMPRESSOR_DEFAULT))
{