Skip to content

Commit f1eac71

Browse files
committed
mannually merge from dimatura#9
1 parent 20b032b commit f1eac71

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ build
55
.eggs/
66
dist
77
_build
8+
bin
9+
lib
10+
include
11+
.Python
12+
.idea
13+
pip-selfcheck.json
14+
.pytest_cache/

pypcd/pypcd.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import re
1313
import struct
1414
import copy
15-
import cStringIO as sio
1615
import numpy as np
1716
import warnings
1817
import lzf
@@ -24,6 +23,11 @@
2423
except ImportError:
2524
HAS_SENSOR_MSGS = False
2625

26+
try:
27+
from io import StringIO as sio
28+
except:
29+
import cStringIO as sio
30+
2731
__all__ = ['PointCloud',
2832
'point_cloud_to_path',
2933
'point_cloud_to_buffer',
@@ -91,11 +95,11 @@ def parse_header(lines):
9195
elif key in ('fields', 'type'):
9296
metadata[key] = value.split()
9397
elif key in ('size', 'count'):
94-
metadata[key] = map(int, value.split())
98+
metadata[key] = list(map(int, value.split()))
9599
elif key in ('width', 'height', 'points'):
96100
metadata[key] = int(value)
97101
elif key == 'viewpoint':
98-
metadata[key] = map(float, value.split())
102+
metadata[key] = list(map(float, value.split()))
99103
elif key == 'data':
100104
metadata[key] = value.strip().lower()
101105
# TODO apparently count is not required?
@@ -205,9 +209,9 @@ def _build_dtype(metadata):
205209
fieldnames.append(f)
206210
typenames.append(np_type)
207211
else:
208-
fieldnames.extend(['%s_%04d' % (f, i) for i in xrange(c)])
212+
fieldnames.extend(['%s_%04d' % (f, i) for i in range(c)])
209213
typenames.extend([np_type]*c)
210-
dtype = np.dtype(zip(fieldnames, typenames))
214+
dtype = np.dtype(list(zip(fieldnames, typenames)))
211215
return dtype
212216

213217

@@ -278,6 +282,8 @@ def point_cloud_from_fileobj(f):
278282
header = []
279283
while True:
280284
ln = f.readline().strip()
285+
if not isinstance(ln, str):
286+
ln = ln.decode('utf-8')
281287
header.append(ln)
282288
if ln.startswith('DATA'):
283289
metadata = parse_header(header)
@@ -320,13 +326,13 @@ def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
320326
assert(data_compression in ('ascii', 'binary', 'binary_compressed'))
321327
metadata['data'] = data_compression
322328

323-
header = write_header(metadata)
329+
header = write_header(metadata).encode('utf-8')
324330
fileobj.write(header)
325331
if metadata['data'].lower() == 'ascii':
326332
fmtstr = build_ascii_fmtstr(pc)
327333
np.savetxt(fileobj, pc.pc_data, fmt=fmtstr)
328334
elif metadata['data'].lower() == 'binary':
329-
fileobj.write(pc.pc_data.tostring('C'))
335+
fileobj.write(pc.pc_data.tostring())
330336
elif metadata['data'].lower() == 'binary_compressed':
331337
# TODO
332338
# a '_' field is ignored by pcl and breakes compressed point clouds.
@@ -335,9 +341,9 @@ def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
335341
# reorder to column-by-column
336342
uncompressed_lst = []
337343
for fieldname in pc.pc_data.dtype.names:
338-
column = np.ascontiguousarray(pc.pc_data[fieldname]).tostring('C')
344+
column = np.ascontiguousarray(pc.pc_data[fieldname]).tostring()
339345
uncompressed_lst.append(column)
340-
uncompressed = ''.join(uncompressed_lst)
346+
uncompressed = b''.join(uncompressed_lst)
341347
uncompressed_size = len(uncompressed)
342348
# print("uncompressed_size = %r"%(uncompressed_size))
343349
buf = lzf.compress(uncompressed)
@@ -357,7 +363,7 @@ def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
357363

358364

359365
def point_cloud_to_path(pc, fname):
360-
with open(fname, 'w') as f:
366+
with open(fname, 'wb') as f:
361367
point_cloud_to_fileobj(pc, f)
362368

363369

@@ -370,21 +376,21 @@ def point_cloud_to_buffer(pc, data_compression=None):
370376
def save_point_cloud(pc, fname):
371377
""" Save pointcloud to fname in ascii format.
372378
"""
373-
with open(fname, 'w') as f:
379+
with open(fname, 'wb') as f:
374380
point_cloud_to_fileobj(pc, f, 'ascii')
375381

376382

377383
def save_point_cloud_bin(pc, fname):
378384
""" Save pointcloud to fname in binary format.
379385
"""
380-
with open(fname, 'w') as f:
386+
with open(fname, 'wb') as f:
381387
point_cloud_to_fileobj(pc, f, 'binary')
382388

383389

384390
def save_point_cloud_bin_compressed(pc, fname):
385391
""" Save pointcloud to fname in binary compressed format.
386392
"""
387-
with open(fname, 'w') as f:
393+
with open(fname, 'wb') as f:
388394
point_cloud_to_fileobj(pc, f, 'binary_compressed')
389395

390396

@@ -481,7 +487,7 @@ def add_fields(pc, metadata, pc_data):
481487
else:
482488
fieldnames.extend(['%s_%04d' % (f, i) for i in xrange(c)])
483489
typenames.extend([np_type]*c)
484-
dtype = zip(fieldnames, typenames)
490+
dtype = list(zip(fieldnames, typenames))
485491
# new dtype. could be inferred?
486492
new_dtype = [(f, pc.pc_data.dtype[f])
487493
for f in pc.pc_data.dtype.names] + dtype
@@ -693,7 +699,7 @@ def save_pcd(self, fname, compression=None, **kwargs):
693699
warnings.warn('data_compression keyword is deprecated for'
694700
' compression')
695701
compression = kwargs['data_compression']
696-
with open(fname, 'w') as f:
702+
with open(fname, 'wb') as f:
697703
point_cloud_to_fileobj(self, f, compression)
698704

699705
def save_pcd_to_fileobj(self, fileobj, compression=None, **kwargs):

pypcd/tests/test_pypcd.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_parse_header():
8484

8585

8686
def test_from_path(pcd_fname):
87-
import pypcd
87+
from pypcd import pypcd
8888
pc = pypcd.PointCloud.from_path(pcd_fname)
8989

9090
fields = 'x y z normal_x normal_y normal_z curvature boundary k vp_x vp_y vp_z principal_curvature_x principal_curvature_y principal_curvature_z pc1 pc2'.split()
@@ -95,7 +95,7 @@ def test_from_path(pcd_fname):
9595

9696

9797
def test_add_fields(pcd_fname):
98-
import pypcd
98+
from pypcd import pypcd
9999
pc = pypcd.PointCloud.from_path(pcd_fname)
100100

101101
old_md = pc.get_metadata()
@@ -114,7 +114,7 @@ def test_add_fields(pcd_fname):
114114

115115

116116
def test_path_roundtrip_ascii(pcd_fname):
117-
import pypcd
117+
from pypcd import pypcd
118118
pc = pypcd.PointCloud.from_path(pcd_fname)
119119
md = pc.get_metadata()
120120

@@ -138,7 +138,7 @@ def test_path_roundtrip_ascii(pcd_fname):
138138

139139

140140
def test_path_roundtrip_binary(pcd_fname):
141-
import pypcd
141+
from pypcd import pypcd
142142
pc = pypcd.PointCloud.from_path(pcd_fname)
143143
md = pc.get_metadata()
144144

@@ -152,7 +152,7 @@ def test_path_roundtrip_binary(pcd_fname):
152152

153153
pc2 = pypcd.PointCloud.from_path(tmp_fname)
154154
md2 = pc2.get_metadata()
155-
for k, v in md2.iteritems():
155+
for k, v in md2.items():
156156
if k == 'data':
157157
assert v == 'binary'
158158
else:
@@ -166,7 +166,7 @@ def test_path_roundtrip_binary(pcd_fname):
166166

167167

168168
def test_path_roundtrip_binary_compressed(pcd_fname):
169-
import pypcd
169+
from pypcd import pypcd
170170
pc = pypcd.PointCloud.from_path(pcd_fname)
171171
md = pc.get_metadata()
172172

@@ -180,7 +180,7 @@ def test_path_roundtrip_binary_compressed(pcd_fname):
180180

181181
pc2 = pypcd.PointCloud.from_path(tmp_fname)
182182
md2 = pc2.get_metadata()
183-
for k, v in md2.iteritems():
183+
for k, v in md2.items():
184184
if k == 'data':
185185
assert v == 'binary_compressed'
186186
else:
@@ -193,7 +193,7 @@ def test_path_roundtrip_binary_compressed(pcd_fname):
193193

194194

195195
def test_cat_pointclouds(pcd_fname):
196-
import pypcd
196+
from pypcd import pypcd
197197
pc = pypcd.PointCloud.from_path(pcd_fname)
198198
pc2 = pc.copy()
199199
pc2.pc_data['x'] += 0.1
@@ -204,7 +204,7 @@ def test_cat_pointclouds(pcd_fname):
204204

205205

206206
def test_ascii_bin1(ascii_pcd_fname, bin_pcd_fname):
207-
import pypcd
207+
from pypcd import pypcd
208208
apc1 = pypcd.point_cloud_from_path(ascii_pcd_fname)
209209
bpc1 = pypcd.point_cloud_from_path(bin_pcd_fname)
210210
am = cloud_centroid(apc1)

0 commit comments

Comments
 (0)