-
Notifications
You must be signed in to change notification settings - Fork 7
/
pypcd.py
808 lines (705 loc) · 27.6 KB
/
pypcd.py
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
"""
Read and write PCL .pcd files in python.
[email protected], 2013-2018
- TODO better API for wacky operations.
- TODO add a cli for common operations.
- TODO deal properly with padding
- TODO deal properly with multicount fields
- TODO better support for rgb nonsense
"""
import re
import struct
import copy
from io import StringIO as sio
import numpy as np
import warnings
import lzf
HAS_SENSOR_MSGS = True
try:
from sensor_msgs.msg import PointField
import numpy_pc2 # needs sensor_msgs
except ImportError:
HAS_SENSOR_MSGS = False
__all__ = ['PointCloud',
'point_cloud_to_path',
'point_cloud_to_buffer',
'point_cloud_to_fileobj',
'point_cloud_from_path',
'point_cloud_from_buffer',
'point_cloud_from_fileobj',
'make_xyz_point_cloud',
'make_xyz_rgb_point_cloud',
'make_xyz_label_point_cloud',
'save_txt',
'cat_point_clouds',
'add_fields',
'update_field',
'build_ascii_fmtstr',
'encode_rgb_for_pcl',
'decode_rgb_from_pcl',
'save_point_cloud',
'save_point_cloud_bin',
'save_point_cloud_bin_compressed',
'pcd_type_to_numpy_type',
'numpy_type_to_pcd_type',
]
if HAS_SENSOR_MSGS:
pc2_pcd_type_mappings = [(PointField.INT8, ('I', 1)),
(PointField.UINT8, ('U', 1)),
(PointField.INT16, ('I', 2)),
(PointField.UINT16, ('U', 2)),
(PointField.INT32, ('I', 4)),
(PointField.UINT32, ('U', 4)),
(PointField.FLOAT32, ('F', 4)),
(PointField.FLOAT64, ('F', 8))]
pc2_type_to_pcd_type = dict(pc2_pcd_type_mappings)
pcd_type_to_pc2_type = dict((q, p) for (p, q) in pc2_pcd_type_mappings)
__all__.extend(['pcd_type_to_pc2_type', 'pc2_type_to_pcd_type'])
numpy_pcd_type_mappings = [(np.dtype('float32'), ('F', 4)),
(np.dtype('float64'), ('F', 8)),
(np.dtype('uint8'), ('U', 1)),
(np.dtype('uint16'), ('U', 2)),
(np.dtype('uint32'), ('U', 4)),
(np.dtype('uint64'), ('U', 8)),
(np.dtype('int16'), ('I', 2)),
(np.dtype('int32'), ('I', 4)),
(np.dtype('int64'), ('I', 8))]
numpy_type_to_pcd_type = dict(numpy_pcd_type_mappings)
pcd_type_to_numpy_type = dict((q, p) for (p, q) in numpy_pcd_type_mappings)
def parse_header(lines):
""" Parse header of PCD files.
"""
metadata = {}
for ln in lines:
if ln.startswith('#') or len(ln) < 2:
continue
match = re.match('(\w+)\s+([\w\s\.]+)', str(ln))
if not match:
warnings.warn("warning: can't understand line: %s" % ln)
continue
key, value = match.group(1).lower(), match.group(2)
if key == 'version':
metadata[key] = value
elif key in ('fields', 'type'):
metadata[key] = value.split()
elif key in ('size', 'count'):
#print('found size and count k %s v %s '% (key, value))
metadata[key] = list(map(int, value.split()))
#print(list(map(int,value.split())))
elif key in ('width', 'height', 'points'):
metadata[key] = int(value)
elif key == 'viewpoint':
metadata[key] = map(float, value.split())
elif key == 'data':
metadata[key] = value.strip().lower()
# TODO apparently count is not required?
# add some reasonable defaults
if 'count' not in metadata:
metadata['count'] = [1]*len(metadata['fields'])
if 'viewpoint' not in metadata:
metadata['viewpoint'] = [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]
if 'version' not in metadata:
metadata['version'] = '.7'
return metadata
def write_header(metadata, rename_padding=False):
""" Given metadata as dictionary, return a string header.
"""
template = """\
VERSION {version}
FIELDS {fields}
SIZE {size}
TYPE {type}
COUNT {count}
WIDTH {width}
HEIGHT {height}
VIEWPOINT {viewpoint}
POINTS {points}
DATA {data}
"""
str_metadata = metadata.copy()
if not rename_padding:
str_metadata['fields'] = ' '.join(metadata['fields'])
else:
new_fields = []
for f in metadata['fields']:
if f == '_':
new_fields.append('padding')
else:
new_fields.append(f)
str_metadata['fields'] = ' '.join(new_fields)
str_metadata['size'] = ' '.join(map(str, metadata['size']))
str_metadata['type'] = ' '.join(metadata['type'])
str_metadata['count'] = ' '.join(map(str, metadata['count']))
str_metadata['width'] = str(metadata['width'])
str_metadata['height'] = str(metadata['height'])
str_metadata['viewpoint'] = ' '.join(map(str, metadata['viewpoint']))
str_metadata['points'] = str(metadata['points'])
tmpl = template.format(**str_metadata)
return tmpl
def _metadata_is_consistent(metadata):
""" Sanity check for metadata. Just some basic checks.
"""
checks = []
required = ('version', 'fields', 'size', 'width', 'height', 'points',
'viewpoint', 'data')
for f in required:
if f not in metadata:
print('%s required' % f)
checks.append((lambda m: all([k in m for k in required]),
'missing field'))
checks.append((lambda m: len(m['type']) == len(list(m['count'])) ==
len(m['fields']),
'length of type, count and fields must be equal'))
checks.append((lambda m: m['height'] > 0,
'height must be greater than 0'))
checks.append((lambda m: m['width'] > 0,
'width must be greater than 0'))
checks.append((lambda m: m['points'] > 0,
'points must be greater than 0'))
checks.append((lambda m: m['data'].lower() in ('ascii', 'binary',
'binary_compressed'),
'unknown data type:'
'should be ascii/binary/binary_compressed'))
ok = True
for check, msg in checks:
if not check(metadata):
print('error:', msg)
ok = False
return ok
# def pcd_type_to_numpy(pcd_type, pcd_sz):
# """ convert from a pcd type string and size to numpy dtype."""
# typedict = {'F' : { 4:np.float32, 8:np.float64 },
# 'I' : { 1:np.int8, 2:np.int16, 4:np.int32, 8:np.int64 },
# 'U' : { 1:np.uint8, 2:np.uint16, 4:np.uint32 , 8:np.uint64 }}
# return typedict[pcd_type][pcd_sz]
def _build_dtype(metadata):
""" Build numpy structured array dtype from pcl metadata.
Note that fields with count > 1 are 'flattened' by creating multiple
single-count fields.
*TODO* allow 'proper' multi-count fields.
"""
fieldnames = []
typenames = []
for f, c, t, s in zip(metadata['fields'],
metadata['count'],
metadata['type'],
metadata['size']):
np_type = pcd_type_to_numpy_type[(t, s)]
if c == 1:
fieldnames.append(f)
typenames.append(np_type)
else:
fieldnames.extend(['%s_%04d' % (f, i) for i in xrange(c)])
typenames.extend([np_type]*c)
print(set(zip(fieldnames, typenames)))
dtype = np.dtype(list(zip(fieldnames, typenames)))
return dtype
def build_ascii_fmtstr(pc):
""" Make a format string for printing to ascii.
Note %.8f is minimum for rgb.
"""
fmtstr = []
for t, cnt in zip(pc.type, pc.count):
if t == 'F':
fmtstr.extend(['%.10f']*cnt)
elif t == 'I':
fmtstr.extend(['%d']*cnt)
elif t == 'U':
fmtstr.extend(['%u']*cnt)
else:
raise ValueError("don't know about type %s" % t)
return fmtstr
def parse_ascii_pc_data(f, dtype, metadata):
""" Use numpy to parse ascii pointcloud data.
"""
return np.loadtxt(f, dtype=dtype, delimiter=' ')
def parse_binary_pc_data(f, dtype, metadata):
rowstep = metadata['points']*dtype.itemsize
# for some reason pcl adds empty space at the end of files
buf = f.read(rowstep)
return np.fromstring(buf, dtype=dtype)
def parse_binary_compressed_pc_data(f, dtype, metadata):
""" Parse lzf-compressed data.
Format is undocumented but seems to be:
- compressed size of data (uint32)
- uncompressed size of data (uint32)
- compressed data
- junk
"""
fmt = 'II'
compressed_size, uncompressed_size =\
struct.unpack(fmt, f.read(struct.calcsize(fmt)))
compressed_data = f.read(compressed_size)
# TODO what to use as second argument? if buf is None
# (compressed > uncompressed)
# should we read buf as raw binary?
buf = lzf.decompress(compressed_data, uncompressed_size)
if len(buf) != uncompressed_size:
raise IOError('Error decompressing data')
# the data is stored field-by-field
pc_data = np.zeros(metadata['width'], dtype=dtype)
ix = 0
for dti in range(len(dtype)):
dt = dtype[dti]
bytes = dt.itemsize * metadata['width']
column = np.fromstring(buf[ix:(ix+bytes)], dt)
pc_data[dtype.names[dti]] = column
ix += bytes
return pc_data
def point_cloud_from_fileobj(f):
""" Parse pointcloud coming from file object f
"""
header = []
while True:
ln = f.readline().strip().decode('ascii')
header.append(ln)
if ln.startswith('DATA'):
print('starts with Data %s' % ln)
metadata = parse_header(header)
dtype = _build_dtype(metadata)
break
if metadata['data'] == 'ascii':
pc_data = parse_ascii_pc_data(f, dtype, metadata)
elif metadata['data'] == 'binary':
pc_data = parse_binary_pc_data(f, dtype, metadata)
elif metadata['data'] == 'binary_compressed':
pc_data = parse_binary_compressed_pc_data(f, dtype, metadata)
else:
print('DATA field is neither "ascii" or "binary" or\
"binary_compressed"')
return PointCloud(metadata, pc_data)
def point_cloud_from_path(fname):
""" load point cloud in binary format
"""
with open(fname, 'rb') as f:
pc = point_cloud_from_fileobj(f)
return pc
def point_cloud_from_buffer(buf):
fileobj = sio.StringIO(buf)
pc = point_cloud_from_fileobj(fileobj)
fileobj.close() # necessary?
return pc
def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
""" Write pointcloud as .pcd to fileobj.
If data_compression is not None it overrides pc.data.
"""
metadata = pc.get_metadata()
if data_compression is not None:
data_compression = data_compression.lower()
assert(data_compression in ('ascii', 'binary', 'binary_compressed'))
metadata['data'] = data_compression
header = write_header(metadata)
fileobj.write(header)
if metadata['data'].lower() == 'ascii':
fmtstr = build_ascii_fmtstr(pc)
np.savetxt(fileobj, pc.pc_data, fmt=fmtstr)
elif metadata['data'].lower() == 'binary':
fileobj.write(pc.pc_data.tostring('C'))
elif metadata['data'].lower() == 'binary_compressed':
# TODO
# a '_' field is ignored by pcl and breakes compressed point clouds.
# changing '_' to '_padding' or other name fixes this.
# admittedly padding shouldn't be compressed in the first place.
# reorder to column-by-column
uncompressed_lst = []
for fieldname in pc.pc_data.dtype.names:
column = np.ascontiguousarray(pc.pc_data[fieldname]).tostring('C')
uncompressed_lst.append(column)
uncompressed = ''.join(uncompressed_lst)
uncompressed_size = len(uncompressed)
# print("uncompressed_size = %r"%(uncompressed_size))
buf = lzf.compress(uncompressed)
if buf is None:
# compression didn't shrink the file
# TODO what do to do in this case when reading?
buf = uncompressed
compressed_size = uncompressed_size
else:
compressed_size = len(buf)
fmt = 'II'
fileobj.write(struct.pack(fmt, compressed_size, uncompressed_size))
fileobj.write(buf)
else:
raise ValueError('unknown DATA type')
# we can't close because if it's stringio buf then we can't get value after
def point_cloud_to_path(pc, fname):
with open(fname, 'w') as f:
point_cloud_to_fileobj(pc, f)
def point_cloud_to_buffer(pc, data_compression=None):
fileobj = sio.StringIO()
point_cloud_to_fileobj(pc, fileobj, data_compression)
return fileobj.getvalue()
def save_point_cloud(pc, fname):
""" Save pointcloud to fname in ascii format.
"""
with open(fname, 'w') as f:
point_cloud_to_fileobj(pc, f, 'ascii')
def save_point_cloud_bin(pc, fname):
""" Save pointcloud to fname in binary format.
"""
with open(fname, 'w') as f:
point_cloud_to_fileobj(pc, f, 'binary')
def save_point_cloud_bin_compressed(pc, fname):
""" Save pointcloud to fname in binary compressed format.
"""
with open(fname, 'w') as f:
point_cloud_to_fileobj(pc, f, 'binary_compressed')
def save_xyz_label(pc, fname, use_default_lbl=False):
""" Save a simple (x y z label) pointcloud, ignoring all other features.
Label is initialized to 1000, for an obscure program I use.
"""
md = pc.get_metadata()
if not use_default_lbl and ('label' not in md['fields']):
raise Exception('label is not a field in this point cloud')
with open(fname, 'w') as f:
for i in xrange(pc.points):
x, y, z = ['%.4f' % d for d in (
pc.pc_data['x'][i], pc.pc_data['y'][i], pc.pc_data['z'][i]
)]
lbl = '1000' if use_default_lbl else pc.pc_data['label'][i]
f.write(' '.join((x, y, z, lbl))+'\n')
def save_xyz_intensity_label(pc, fname, use_default_lbl=False):
""" Save XYZI point cloud.
"""
md = pc.get_metadata()
if not use_default_lbl and ('label' not in md['fields']):
raise Exception('label is not a field in this point cloud')
if 'intensity' not in md['fields']:
raise Exception('intensity is not a field in this point cloud')
with open(fname, 'w') as f:
for i in xrange(pc.points):
x, y, z = ['%.4f' % d for d in (
pc.pc_data['x'][i], pc.pc_data['y'][i], pc.pc_data['z'][i]
)]
intensity = '%.4f' % pc.pc_data['intensity'][i]
lbl = '1000' if use_default_lbl else pc.pc_data['label'][i]
f.write(' '.join((x, y, z, intensity, lbl))+'\n')
def save_txt(pc, fname, header=True):
""" Save to csv-style text file, separated by spaces.
TODO:
- support multi-count fields.
- other delimiters.
"""
with open(fname, 'w') as f:
if header:
header_lst = []
for field_name, cnt in zip(pc.fields, pc.count):
if cnt == 1:
header_lst.append(field_name)
else:
for c in xrange(cnt):
header_lst.append('%s_%04d' % (field_name, c))
f.write(' '.join(header_lst)+'\n')
fmtstr = build_ascii_fmtstr(pc)
np.savetxt(f, pc.pc_data, fmt=fmtstr)
def update_field(pc, field, pc_data):
""" Updates field in-place.
"""
pc.pc_data[field] = pc_data
return pc
def add_fields(pc, metadata, pc_data):
""" Builds copy of pointcloud with extra fields.
Multi-count fields are sketchy, yet again.
"""
if len(set(metadata['fields']).intersection(set(pc.fields))) > 0:
raise Exception("Fields with that name exist.")
if pc.points != len(pc_data):
raise Exception("Mismatch in number of points.")
new_metadata = pc.get_metadata()
new_metadata['fields'].extend(metadata['fields'])
new_metadata['count'].extend(metadata['count'])
new_metadata['size'].extend(metadata['size'])
new_metadata['type'].extend(metadata['type'])
# parse metadata to add
# TODO factor this
fieldnames, typenames = [], []
for f, c, t, s in zip(metadata['fields'],
metadata['count'],
metadata['type'],
metadata['size']):
np_type = pcd_type_to_numpy_type[(t, s)]
if c == 1:
fieldnames.append(f)
typenames.append(np_type)
else:
fieldnames.extend(['%s_%04d' % (f, i) for i in xrange(c)])
typenames.extend([np_type]*c)
dtype = zip(fieldnames, typenames)
# new dtype. could be inferred?
new_dtype = [(f, pc.pc_data.dtype[f])
for f in pc.pc_data.dtype.names] + dtype
new_data = np.empty(len(pc.pc_data), new_dtype)
for n in pc.pc_data.dtype.names:
new_data[n] = pc.pc_data[n]
for n, n_tmp in zip(fieldnames, pc_data.dtype.names):
new_data[n] = pc_data[n_tmp]
# TODO maybe just all the metadata in the dtype.
# TODO maybe use composite structured arrays for fields with count > 1
newpc = PointCloud(new_metadata, new_data)
return newpc
def cat_point_clouds(pc1, pc2):
""" Concatenate two point clouds into bigger point cloud.
Point clouds must have same metadata.
"""
if len(pc1.fields) != len(pc2.fields):
raise ValueError("Pointclouds must have same fields")
new_metadata = pc1.get_metadata()
new_data = np.concatenate((pc1.pc_data, pc2.pc_data))
# TODO this only makes sense for unstructured pc?
new_metadata['width'] = pc1.width+pc2.width
new_metadata['points'] = pc1.points+pc2.points
pc3 = PointCloud(new_metadata, new_data)
return pc3
def make_xyz_point_cloud(xyz, metadata=None):
""" Make a pointcloud object from xyz array.
xyz array is cast to float32.
"""
md = {'version': .7,
'fields': ['x', 'y', 'z'],
'size': [4, 4, 4],
'type': ['F', 'F', 'F'],
'count': [1, 1, 1],
'width': len(xyz),
'height': 1,
'viewpoint': [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
'points': len(xyz),
'data': 'binary'}
if metadata is not None:
md.update(metadata)
xyz = xyz.astype(np.float32)
pc_data = xyz.view(np.dtype([('x', np.float32),
('y', np.float32),
('z', np.float32)]))
# pc_data = np.rec.fromarrays([xyz[:,0], xyz[:,1], xyz[:,2]], dtype=dt)
# data = np.rec.fromarrays([xyz.T], dtype=dt)
pc = PointCloud(md, pc_data)
return pc
def make_xyz_rgb_point_cloud(xyz_rgb, metadata=None):
""" Make a pointcloud object from xyz array.
xyz array is assumed to be float32.
rgb is assumed to be encoded as float32 according to pcl conventions.
"""
md = {'version': .7,
'fields': ['x', 'y', 'z', 'rgb'],
'count': [1, 1, 1, 1],
'width': len(xyz_rgb),
'height': 1,
'viewpoint': [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
'points': len(xyz_rgb),
'type': ['F', 'F', 'F', 'F'],
'size': [4, 4, 4, 4],
'data': 'binary'}
if xyz_rgb.dtype != np.float32:
raise ValueError('array must be float32')
if metadata is not None:
md.update(metadata)
pc_data = xyz_rgb.view(np.dtype([('x', np.float32),
('y', np.float32),
('z', np.float32),
('rgb', np.float32)])).squeeze()
# pc_data = np.rec.fromarrays([xyz[:,0], xyz[:,1], xyz[:,2]], dtype=dt)
# data = np.rec.fromarrays([xyz.T], dtype=dt)
pc = PointCloud(md, pc_data)
return pc
def encode_rgb_for_pcl(rgb):
""" Encode bit-packed RGB for use with PCL.
:param rgb: Nx3 uint8 array with RGB values.
:rtype: Nx1 float32 array with bit-packed RGB, for PCL.
"""
assert(rgb.dtype == np.uint8)
assert(rgb.ndim == 2)
assert(rgb.shape[1] == 3)
rgb = rgb.astype(np.uint32)
rgb = np.array((rgb[:, 0] << 16) | (rgb[:, 1] << 8) | (rgb[:, 2] << 0),
dtype=np.uint32)
rgb.dtype = np.float32
return rgb
def decode_rgb_from_pcl(rgb):
""" Decode the bit-packed RGBs used by PCL.
:param rgb: An Nx1 array.
:rtype: Nx3 uint8 array with one column per color.
"""
rgb = rgb.copy()
rgb.dtype = np.uint32
r = np.asarray((rgb >> 16) & 255, dtype=np.uint8)
g = np.asarray((rgb >> 8) & 255, dtype=np.uint8)
b = np.asarray(rgb & 255, dtype=np.uint8)
rgb_arr = np.zeros((len(rgb), 3), dtype=np.uint8)
rgb_arr[:, 0] = r
rgb_arr[:, 1] = g
rgb_arr[:, 2] = b
return rgb_arr
def make_xyz_label_point_cloud(xyzl, label_type='f'):
""" Make XYZL point cloud from numpy array.
TODO i labels?
"""
md = {'version': .7,
'fields': ['x', 'y', 'z', 'label'],
'count': [1, 1, 1, 1],
'width': len(xyzl),
'height': 1,
'viewpoint': [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
'points': len(xyzl),
'data': 'ASCII'}
if label_type.lower() == 'f':
md['size'] = [4, 4, 4, 4]
md['type'] = ['F', 'F', 'F', 'F']
elif label_type.lower() == 'u':
md['size'] = [4, 4, 4, 1]
md['type'] = ['F', 'F', 'F', 'U']
else:
raise ValueError('label type must be F or U')
# TODO use .view()
xyzl = xyzl.astype(np.float32)
dt = np.dtype([('x', np.float32), ('y', np.float32), ('z', np.float32),
('label', np.float32)])
pc_data = np.rec.fromarrays([xyzl[:, 0], xyzl[:, 1], xyzl[:, 2],
xyzl[:, 3]], dtype=dt)
pc = PointCloud(md, pc_data)
return pc
class PointCloud(object):
""" Wrapper for point cloud data.
The variable members of this class parallel the ones used by
the PCD metadata (and similar to PCL and ROS PointCloud2 messages),
``pc_data`` holds the actual data as a structured numpy array.
The other relevant metadata variables are:
- ``version``: Version, usually .7
- ``fields``: Field names, e.g. ``['x', 'y' 'z']``.
- ``size.`: Field sizes in bytes, e.g. ``[4, 4, 4]``.
- ``count``: Counts per field e.g. ``[1, 1, 1]``. NB: Multi-count field
support is sketchy.
- ``width``: Number of points, for unstructured point clouds (assumed by
most operations).
- ``height``: 1 for unstructured point clouds (again, what we assume most
of the time.
- ``viewpoint``: A pose for the viewpoint of the cloud, as
x y z qw qx qy qz, e.g. ``[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]``.
- ``points``: Number of points.
- ``type``: Data type of each field, e.g. ``[F, F, F]``.
- ``data``: Data storage format. One of ``ascii``, ``binary`` or ``binary_compressed``.
See `PCL docs <http://pointclouds.org/documentation/tutorials/pcd_file_format.php>`__
for more information.
"""
def __init__(self, metadata, pc_data):
self.metadata_keys = metadata.keys()
self.__dict__.update(metadata)
self.pc_data = pc_data
self.check_sanity()
def get_metadata(self):
""" returns copy of metadata """
metadata = {}
for k in self.metadata_keys:
metadata[k] = copy.copy(getattr(self, k))
return metadata
def check_sanity(self):
# pdb.set_trace()
md = self.get_metadata()
assert(_metadata_is_consistent(md))
assert(len(self.pc_data) == self.points)
assert(self.width*self.height == self.points)
assert(len(self.fields) == len(self.count))
assert(len(self.fields) == len(self.type))
def save(self, fname):
self.save_pcd(fname, 'ascii')
def save_pcd(self, fname, compression=None, **kwargs):
if 'data_compression' in kwargs:
warnings.warn('data_compression keyword is deprecated for'
' compression')
compression = kwargs['data_compression']
with open(fname, 'w') as f:
point_cloud_to_fileobj(self, f, compression)
def save_pcd_to_fileobj(self, fileobj, compression=None, **kwargs):
if 'data_compression' in kwargs:
warnings.warn('data_compression keyword is deprecated for'
' compression')
compression = kwargs['data_compression']
point_cloud_to_fileobj(self, fileobj, compression)
def save_pcd_to_buffer(self, compression=None, **kwargs):
if 'data_compression' in kwargs:
warnings.warn('data_compression keyword is deprecated for'
' compression')
compression = kwargs['data_compression']
return point_cloud_to_buffer(self, compression)
def save_txt(self, fname):
save_txt(self, fname)
def save_xyz_label(self, fname, **kwargs):
save_xyz_label(self, fname, **kwargs)
def save_xyz_intensity_label(self, fname, **kwargs):
save_xyz_intensity_label(self, fname, **kwargs)
def copy(self):
new_pc_data = np.copy(self.pc_data)
new_metadata = self.get_metadata()
return PointCloud(new_metadata, new_pc_data)
def to_msg(self):
if not HAS_SENSOR_MSGS:
raise Exception('ROS sensor_msgs not found')
# TODO is there some metadata we want to attach?
return numpy_pc2.array_to_pointcloud2(self.pc_data)
@staticmethod
def from_path(fname):
return point_cloud_from_path(fname)
@staticmethod
def from_fileobj(fileobj):
return point_cloud_from_fileobj(fileobj)
@staticmethod
def from_buffer(buf):
return point_cloud_from_buffer(buf)
@staticmethod
def from_array(arr):
""" create a PointCloud object from an array.
"""
pc_data = arr.copy()
md = {'version': .7,
'fields': [],
'size': [],
'count': [],
'width': 0,
'height': 1,
'viewpoint': [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
'points': 0,
'type': [],
'data': 'binary_compressed'}
md['fields'] = pc_data.dtype.names
for field in md['fields']:
type_, size_ =\
numpy_type_to_pcd_type[pc_data.dtype.fields[field][0]]
md['type'].append(type_)
md['size'].append(size_)
# TODO handle multicount
md['count'].append(1)
md['width'] = len(pc_data)
md['points'] = len(pc_data)
pc = PointCloud(md, pc_data)
return pc
@staticmethod
def from_msg(msg, squeeze=True):
""" from pointcloud2 msg
squeeze: fix when clouds get 1 as first dim
"""
if not HAS_SENSOR_MSGS:
raise NotImplementedError('ROS sensor_msgs not found')
md = {'version': .7,
'fields': [],
'size': [],
'count': [],
'width': msg.width,
'height': msg.height,
'viewpoint': [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
'points': 0,
'type': [],
'data': 'binary_compressed'}
for field in msg.fields:
md['fields'].append(field.name)
t, s = pc2_type_to_pcd_type[field.datatype]
md['type'].append(t)
md['size'].append(s)
# TODO handle multicount correctly
if field.count > 1:
warnings.warn('fields with count > 1 are not well tested')
md['count'].append(field.count)
pc_array = numpy_pc2.pointcloud2_to_array(msg)
pc_data = pc_array.reshape(-1)
md['height'], md['width'] = pc_array.shape
md['points'] = len(pc_data)
pc = PointCloud(md, pc_data)
return pc