forked from LimeRabbit/PcdReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PcdReader.cs
415 lines (344 loc) · 12.7 KB
/
PcdReader.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
// <auto-generated>
// This file is not auto-generated, but we do not want to elevate warnings for it.
// </auto-generated>
// PCD file format: https://pcl.readthedocs.io/projects/tutorials/en/master/pcd_file_format.html#pcd-file-format
class PcdReader
{
private Header header;
private Point3D[] points;
private int pointCount = 0;
public long FileSizeBytes { get; private set; }
public int PointCount => pointCount;
public Point3D[] Points => points;
public Header Header => header;
private const int MaxHeaderSize = 1024; // 1K
public void ReadPcdFile(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName);
FileSizeBytes = fileInfo.Length;
byte[] bytes;
using (FileStream fileStream = File.OpenRead(fileName))
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
bytes = binaryReader.ReadBytes(MaxHeaderSize);
}
ReadHeader(fileName, out var dataBytesIndex);
string dataType = header.data.Split(' ')[1].TrimEnd(); //储存的数据类型,ascii binary binary_compressed
var sizes = header.size
.Split(' ')
.Skip(1)
.Select(item => int.Parse(item));
var counts = header.count
.Split(' ')
.Skip(1)
.Select(item => int.Parse(item));
var rowSizeBytes = sizes
.Zip(counts)
.Sum(item => item.First * item.Second);
pointCount = Convert.ToInt32(header.points.Split(' ')[1]); //点的数量
points = new Point3D[pointCount];
switch (dataType)
{
case "binary":
ReadBinaryData(fileName, bytes, rowSizeBytes, (int)dataBytesIndex, sizes);
break;
case "binary_compressed":
ReadBinaryCompressedData(bytes, rowSizeBytes, (int)dataBytesIndex);
break;
case "ascii":
throw new NotImplementedException("Sorry, ASCII data type is not currently supported.");
default:
break;
}
}
private void ReadHeader(string fileName, out long dataBytesIndex)
{
var headerLines = new string[10];
var newLineLengthBytes = LineBreakLengthBytes(fileName);
using (var textReader = new StreamReader(fileName))
{
string line;
var headerLinesIndex = 0;
dataBytesIndex = 0;
do
{
line = textReader.ReadLine();
if (line == null)
{
throw new ArgumentException("PCD is not well-formed.");
}
dataBytesIndex += line.Length + newLineLengthBytes;
if (line.StartsWith('#'))
{
continue;
}
headerLines[headerLinesIndex] = line;
headerLinesIndex++;
}
while (!line.StartsWith("DATA"));
}
header = new Header(headerLines);
}
private static int LineBreakLengthBytes(string fileName)
{
// We estimate \n (1 B) is by default; however, \r\n (2 B) could appear too
int newLineBytesLength = 1;
int peek;
using var textReader = new StreamReader(fileName);
while ((peek = textReader.Read()) >= 0)
{
var @char = (char)peek;
if (@char == '\r')
{
peek = textReader.Read();
@char = (char)peek;
if (@char == '\n')
{
newLineBytesLength = 2;
break;
}
}
else if (@char == '\n')
{
newLineBytesLength = 1;
break;
}
}
return newLineBytesLength;
}
private void ReadBinaryCompressedData(byte[] bytes, int rowSizeBytes, int index)
{
Point3D point;
//二进制压缩,先解析压缩前的数据量和压缩后数据量
int[] bys = new int[2];
int dataIndex = 0;
for (int i = 0; i < bys.Length; i++)
{
bys[i] = BitConverter.ToInt32(bytes, index + i * 4);
dataIndex = index + i * 4;
}
dataIndex += 4; //数据开始的索引
int compressedSize = bys[0]; //压缩之后的长度
int decompressedSize = bys[1]; //解压之后的长度
//将压缩后的数据单独拿出来
byte[] compress = new byte[compressedSize];
//将bs,从索引为a开始,复制到compress的[0至compressedSize]区间内
Array.Copy(bytes, dataIndex, compress, 0, compressedSize);
//LZF解压算法
byte[] data = Decompress(compress, decompressedSize);
int type = 0;
var pointIndex = 0;
for (int i = 0; i < data.Length; i += 4)
{
//先读取x坐标
if (type == 0)
{
point = new Point3D();
point.x = BitConverter.ToSingle(data, i);
points[pointIndex] = point;
if ((pointIndex + 1) == pointCount)
{
type++;
}
pointIndex++;
}
else if (type == 1) //y 坐标
{
var point3D = points[i / 4 - pointCount];
point3D.y = BitConverter.ToSingle(data, i);
points[i / 4 - pointCount] = point3D;
if (i / 4 == pointCount * 2 - 1) type++;
}
else if (type == 2) //z 坐标
{
var point3D = points[i / 4 - pointCount * 2];
point3D.z = BitConverter.ToSingle(data, i);
points[i / 4 - pointCount * 2] = point3D;
if (i / 4 == pointCount * 3 - 1) type++;
}
else if (rowSizeBytes == 4) //颜色信息
{
var point3D = points[i / 4 - pointCount * 3];
point3D.colorRGB = BitConverter.ToUInt32(data, i);
points[i / 4 - pointCount * 3] = point3D;
if (i / 4 == pointCount * 4 - 1) break;
}
}
}
private void ReadBinaryData(string fileName, byte[] bytes, int rowSizeBytes, int index, IEnumerable<int> sizes)
{
var fields = header.fields
.Split(' ')
.Skip(1)
.Select(item => item.TrimEnd())
.ToList();
var colorFieldIndex = fields.IndexOf("rgb");
var xFieldIndex = fields.IndexOf("x");
var xFieldOffset = 4 * xFieldIndex;
var restOfFields = fields.Except(new string[] { "x", "y", "z", "rgb" });
var isLabelAvailable = restOfFields.Contains("label");
var labelOffset = GetFieldOffset("label", fields, sizes);
int labelsSize = 0;
if (isLabelAvailable)
labelsSize = sizes.ElementAt(fields.IndexOf("label"));
var pointIndex = 0;
int pointsToRead = 100000;
using (FileStream fileStream = File.OpenRead(fileName))
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
fileStream.Seek(index, SeekOrigin.Begin);
int remainingPoints = this.pointCount;
do
{
var loadPoints = Math.Min(pointsToRead, remainingPoints);
int loadBufferSize = rowSizeBytes * loadPoints;
remainingPoints -= loadPoints;
bytes = binaryReader.ReadBytes(loadBufferSize);
for (int byteId = 0; byteId < bytes.Length;)
{
ref var point = ref points[pointIndex];
point.x = BitConverter.ToSingle(bytes, byteId + xFieldOffset);
point.y = BitConverter.ToSingle(bytes, byteId + xFieldOffset + 4);
point.z = BitConverter.ToSingle(bytes, byteId + xFieldOffset + 8);
if (colorFieldIndex >= 0)
{
point.colorRGB = BitConverter.ToUInt32(bytes, byteId + (4 * colorFieldIndex));
}
if (isLabelAvailable)
{
if (labelsSize == 4)
{
point.label = BitConverter.ToUInt32(bytes, byteId + labelOffset);
}
else if (labelsSize == 1)
{
point.label = bytes[byteId + labelOffset];
}
}
if ((pointIndex + 1) == pointCount)
{
break;
}
byteId += rowSizeBytes;
pointIndex++;
}
} while (remainingPoints > 0);
}
}
private int GetFieldOffset(string field, IList<string> fields, IEnumerable<int> sizes)
{
var fieldIndex = fields.IndexOf(field);
var offset = sizes.Take(fieldIndex).Sum();
return offset;
}
/// <summary>
/// 使用LZF算法解压缩数据
/// </summary>
/// <param name="input">要解压的数据</param>
/// <param name="outputLength">解压之后的长度</param>
/// <returns>返回解压缩之后的内容</returns>
private static byte[] Decompress(byte[] input, int outputLength)
{
uint iidx = 0;
uint oidx = 0;
int inputLength = input.Length;
byte[] output = new byte[outputLength];
do
{
uint ctrl = input[iidx++];
if (ctrl < (1 << 5))
{
ctrl++;
if (oidx + ctrl > outputLength)
{
return null;
}
do
output[oidx++] = input[iidx++];
while ((--ctrl) != 0);
}
else
{
var len = ctrl >> 5;
var reference = (int)(oidx - ((ctrl & 0x1f) << 8) - 1);
if (len == 7)
len += input[iidx++];
reference -= input[iidx++];
if (oidx + len + 2 > outputLength)
{
return null;
}
if (reference < 0)
{
return null;
}
output[oidx++] = output[reference++];
output[oidx++] = output[reference++];
do
output[oidx++] = output[reference++];
while ((--len) != 0);
}
}
while (iidx < inputLength);
return output;
}
}
/// <summary>
/// Pcd 中的点
/// </summary>
struct Point3D
{
public float x;
public float y;
public float z;
public uint colorRGB;
public uint label;
}
/// <summary>
/// Pcd 文件的头部信息
/// </summary>
class Header
{
public string version;
public string fields;
/// <summary>
/// Size of each dimension (<see cref="fields"/>) in bytes.
/// </summary>
public string size;
public string type;
public string count;
public string width;
public string height;
/// <summary>
/// An acquisition viewpoint for the points in the dataset,
/// specified as a translation (tx ty tz) + quaternion (qw qx qy qz).
/// </summary>
public string viewpoint;
public string points;
public string data;
public Header(params string[] headerLines)
{
version = headerLines[0];
fields = headerLines[1];
size = headerLines[2];
type = headerLines[3];
count = headerLines[4];
width = headerLines[5];
height = headerLines[6];
viewpoint = headerLines[7];
points = headerLines[8];
data = headerLines[9];
var viewpointParts = viewpoint.Split(' ').Skip(1);
ViewpointTranslation = (
viewpointParts.ElementAt(0),
viewpointParts.ElementAt(1),
viewpointParts.ElementAt(2));
ViewpointQuaternion = (
viewpointParts.ElementAt(3),
viewpointParts.ElementAt(4),
viewpointParts.ElementAt(5),
viewpointParts.ElementAt(6));
}
public (string x, string y, string z) ViewpointTranslation { get; private set; }
public (string w, string x, string y, string z) ViewpointQuaternion { get; private set; }
}