-
Notifications
You must be signed in to change notification settings - Fork 26
/
LASwriteItemCompressed_POINT10_v2.cs
243 lines (215 loc) · 7.74 KB
/
LASwriteItemCompressed_POINT10_v2.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
//===============================================================================
//
// FILE: laswriteitemcompressed_point10_v2.cs
//
// CONTENTS:
//
// Implementation of LASwriteItemCompressed for POINT10 items (version 2).
//
// PROGRAMMERS:
//
// [email protected] - http://rapidlasso.com
//
// COPYRIGHT:
//
// (c) 2005-2012, martin isenburg, rapidlasso - tools to catch reality
// (c) of the C# port 2014 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.Diagnostics;
using System.Runtime.InteropServices;
namespace laszip.net
{
class LASwriteItemCompressed_POINT10_v2 : LASwriteItemCompressed
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
struct LASpoint10
{
public int x;
public int y;
public int z;
public ushort intensity;
// all these bits combine to flags
//public byte return_number : 3;
//public byte number_of_returns_of_given_pulse : 3;
//public byte scan_direction_flag : 1;
//public byte edge_of_flight_line : 1;
public byte flags;
public byte classification;
public sbyte scan_angle_rank;
public byte user_data;
public ushort point_source_ID;
}
public LASwriteItemCompressed_POINT10_v2(ArithmeticEncoder enc)
{
// set encoder
Debug.Assert(enc!=null);
this.enc=enc;
// create models and integer compressors
ic_dx=new IntegerCompressor(enc, 32, 2); // 32 bits, 2 context
ic_dy=new IntegerCompressor(enc, 32, 22); // 32 bits, 22 contexts
ic_z=new IntegerCompressor(enc, 32, 20); // 32 bits, 20 contexts
ic_intensity=new IntegerCompressor(enc, 16, 4);
m_scan_angle_rank[0]=enc.createSymbolModel(256);
m_scan_angle_rank[1]=enc.createSymbolModel(256);
ic_point_source_ID=new IntegerCompressor(enc, 16);
m_changed_values=enc.createSymbolModel(64);
for(int i=0; i<256; i++)
{
m_bit_byte[i]=null;
m_classification[i]=null;
m_user_data[i]=null;
}
}
public override bool init(laszip_point item)
{
// init state
for(int i=0; i<16; i++)
{
last_x_diff_median5[i].init();
last_y_diff_median5[i].init();
last_intensity[i]=0;
last_height[i/2]=0;
}
// init models and integer compressors
ic_dx.initCompressor();
ic_dy.initCompressor();
ic_z.initCompressor();
ic_intensity.initCompressor();
enc.initSymbolModel(m_scan_angle_rank[0]);
enc.initSymbolModel(m_scan_angle_rank[1]);
ic_point_source_ID.initCompressor();
enc.initSymbolModel(m_changed_values);
for(int i=0; i<256; i++)
{
if(m_bit_byte[i]!=null) enc.initSymbolModel(m_bit_byte[i]);
if(m_classification[i]!=null) enc.initSymbolModel(m_classification[i]);
if(m_user_data[i]!=null) enc.initSymbolModel(m_user_data[i]);
}
// init last item
last.x=item.X;
last.y=item.Y;
last.z=item.Z;
last.intensity=0; // but set intensity to zero
last.flags=item.flags;
last.classification=item.classification;
last.scan_angle_rank=item.scan_angle_rank;
last.user_data=item.user_data;
last.point_source_ID=item.point_source_ID;
return true;
}
public override bool write(laszip_point item)
{
uint r=item.return_number;
uint n=item.number_of_returns_of_given_pulse;
uint m=Laszip_Common_v2.number_return_map[n, r];
uint l=Laszip_Common_v2.number_return_level[n, r];
// compress which other values have changed
uint changed_values=0;
bool needFlags=last.flags!=item.flags; if(needFlags) changed_values|=32; // bit_byte
bool needIntensity=last_intensity[m]!=item.intensity; if(needIntensity) changed_values|=16;
bool needClassification=last.classification!=item.classification; if(needClassification) changed_values|=8;
bool needScanAngleRank=last.scan_angle_rank!=item.scan_angle_rank; if(needScanAngleRank) changed_values|=4;
bool needUserData=last.user_data!=item.user_data; if(needUserData) changed_values|=2;
bool needPointSourceID=last.point_source_ID!=item.point_source_ID; if(needPointSourceID) changed_values|=1;
enc.encodeSymbol(m_changed_values, changed_values);
// compress the bit_byte (edge_of_flight_line, scan_direction_flag, returns, ...) if it has changed
if(needFlags)
{
if(m_bit_byte[last.flags]==null)
{
m_bit_byte[last.flags]=enc.createSymbolModel(256);
enc.initSymbolModel(m_bit_byte[last.flags]);
}
enc.encodeSymbol(m_bit_byte[last.flags], item.flags);
}
// compress the intensity if it has changed
if(needIntensity)
{
ic_intensity.compress(last_intensity[m], item.intensity, (m<3?m:3u));
last_intensity[m]=item.intensity;
}
// compress the classification ... if it has changed
if(needClassification)
{
if(m_classification[last.classification]==null)
{
m_classification[last.classification]=enc.createSymbolModel(256);
enc.initSymbolModel(m_classification[last.classification]);
}
enc.encodeSymbol(m_classification[last.classification], item.classification);
}
// compress the scan_angle_rank ... if it has changed
if(needScanAngleRank)
{
enc.encodeSymbol(m_scan_angle_rank[item.scan_direction_flag], (uint)MyDefs.U8_FOLD(item.scan_angle_rank-last.scan_angle_rank));
}
// compress the user_data ... if it has changed
if(needUserData)
{
if(m_user_data[last.user_data]==null)
{
m_user_data[last.user_data]=enc.createSymbolModel(256);
enc.initSymbolModel(m_user_data[last.user_data]);
}
enc.encodeSymbol(m_user_data[last.user_data], item.user_data);
}
// compress the point_source_ID ... if it has changed
if(needPointSourceID)
{
ic_point_source_ID.compress(last.point_source_ID, item.point_source_ID);
}
// compress x coordinate
int median=last_x_diff_median5[m].get();
int diff=item.X-last.x;
ic_dx.compress(median, diff, n==1?1u:0u);
last_x_diff_median5[m].add(diff);
// compress y coordinate
uint k_bits=ic_dx.getK();
median=last_y_diff_median5[m].get();
diff=item.Y-last.y;
ic_dy.compress(median, diff, (n==1?1u:0u)+(k_bits<20?k_bits&0xFEu:20u)); // &0xFE round k_bits to next even number
last_y_diff_median5[m].add(diff);
// compress z coordinate
k_bits=(ic_dx.getK()+ic_dy.getK())/2;
ic_z.compress(last_height[l], item.Z, (n==1?1u:0u)+(k_bits<18?k_bits&0xFEu:18u)); // &0xFE round k_bits to next even number
last_height[l]=item.Z;
// copy the last point
last.x=item.X;
last.y=item.Y;
last.z=item.Z;
last.intensity=item.intensity;
last.flags=item.flags;
last.classification=item.classification;
last.scan_angle_rank=item.scan_angle_rank;
last.user_data=item.user_data;
last.point_source_ID=item.point_source_ID;
return true;
}
ArithmeticEncoder enc;
LASpoint10 last=new LASpoint10();
ushort[] last_intensity=new ushort[16];
StreamingMedian5[] last_x_diff_median5=new StreamingMedian5[16];
StreamingMedian5[] last_y_diff_median5=new StreamingMedian5[16];
int[] last_height=new int[8];
IntegerCompressor ic_dx;
IntegerCompressor ic_dy;
IntegerCompressor ic_z;
IntegerCompressor ic_intensity;
IntegerCompressor ic_point_source_ID;
ArithmeticModel m_changed_values;
ArithmeticModel[] m_scan_angle_rank=new ArithmeticModel[2];
ArithmeticModel[] m_bit_byte=new ArithmeticModel[256];
ArithmeticModel[] m_classification=new ArithmeticModel[256];
ArithmeticModel[] m_user_data=new ArithmeticModel[256];
}
}