From 357ba7af3d3e352633c91f6493c391783fe81648 Mon Sep 17 00:00:00 2001 From: Faruk D Date: Mon, 22 Jan 2018 14:50:24 +0100 Subject: [PATCH 1/3] - update example.ply in testdata - read_ply.py now also reads comments - added verbosity to read function --- laserchicken/read_ply.py | 43 +++++++++++++++++++++++++------ laserchicken/testdata/example.ply | 12 +++++---- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/laserchicken/read_ply.py b/laserchicken/read_ply.py index 2f4bd53..429fa94 100644 --- a/laserchicken/read_ply.py +++ b/laserchicken/read_ply.py @@ -3,7 +3,7 @@ import numpy as np -def read(path): +def read(path, verbose=False): if not os.path.exists(path): raise IOError('File not found: {}'.format(path)) @@ -18,34 +18,61 @@ def read(path): index = read_header(ply) result = {block['type']: read_block(block, ply) for block in index} + if verbose: + print('Header:') + print('-'*10) + for i, header_field in enumerate(index): + print(header_field) + print(index[i]) + print + print('\nData:') + print('-'*10) + for data_field in result: + print(data_field) + print(result['%s' % data_field]) + print return result def read_header(ply): index = [] + comments = [] line = ply.readline() while line.strip() != 'end_header': - if line[:8] == 'element ': + if line.startswith('element'): element_type = line.split()[1] number_of_elements = int(line.split()[2]) current_properties = [] index.append( {'type': element_type, 'number_of_elements': number_of_elements, 'properties': current_properties}) - if line[:9] == 'property ': + if line.startswith('property'): property_type, property_name = line[9:].strip('\n').split(' ') current_properties.append({'type': property_type, 'name': property_name}) + + if line.startswith('comment'): + comment_line = line.strip('\n').split(' ', 1)[1] + comments.append(comment_line) + line = ply.readline() + + if len(comments) > 0: + index.append({'type': 'comment', 'data': comments}) + return index def read_block(block, ply_body): - properties, property_names = get_properties(block) - block_type = block['type'] - number_of_elements = block['number_of_elements'] + if block['type'] == 'comment': + return block['data'] + else: + properties, property_names = get_properties(block) + block_type = block['type'] + number_of_elements = block['number_of_elements'] + + read_elements(ply_body, properties, property_names, block_type, number_of_elements) - read_elements(ply_body, properties, property_names, block_type, number_of_elements) - return properties + return properties def cast(value, value_type): diff --git a/laserchicken/testdata/example.ply b/laserchicken/testdata/example.ply index 3c8ac90..d860d83 100644 --- a/laserchicken/testdata/example.ply +++ b/laserchicken/testdata/example.ply @@ -1,7 +1,9 @@ ply format ascii 1.0 -comment log0000 Processed by module load -comment log0001 Processed by module filter using parameters(x,y,z) +comment [ +comment {'time': '2018-01-18 16:01', 'module': 'load', 'parameters': [], 'version': '0.9.2'} +comment {'time': '2018-01-18 16:01', 'module': 'filter', 'parameters': [('z', 'gt', '1.5')], 'version': '0.9.2'} +comment ] element vertex 3 property float x property float y @@ -10,7 +12,7 @@ property int return element pointcloud 1 property double offset end_header -0.11 0.12 0.13 1 -0.21 0.22 0.23 1 -0.31 0.32 0.33 2 +0.1 0.1 0.1 1 +0.2 0.2 0.2 1 +0.3 0.3 0.3 2 12.1 From ae94cf054cd25bbcc2a07721500218c0956cb6ce Mon Sep 17 00:00:00 2001 From: Faruk D Date: Mon, 22 Jan 2018 17:57:21 +0100 Subject: [PATCH 2/3] fixed the issue with list elements --- laserchicken/read_ply.py | 16 ++++++++++++---- laserchicken/testdata/example.ply | 2 -- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/laserchicken/read_ply.py b/laserchicken/read_ply.py index 429fa94..8d2af01 100644 --- a/laserchicken/read_ply.py +++ b/laserchicken/read_ply.py @@ -1,4 +1,6 @@ import os +import json +import ast import numpy as np @@ -30,7 +32,10 @@ def read(path, verbose=False): for data_field in result: print(data_field) print(result['%s' % data_field]) - print + + print('***************************') + print(result) + return result @@ -57,14 +62,17 @@ def read_header(ply): line = ply.readline() if len(comments) > 0: - index.append({'type': 'comment', 'data': comments}) + for i, comment_line in enumerate(comments): + comments[i] = ast.literal_eval(comment_line) + + index.append({'type': 'log', 'log': comments}) return index def read_block(block, ply_body): - if block['type'] == 'comment': - return block['data'] + if block['type'] == 'log': + return block['log'] else: properties, property_names = get_properties(block) block_type = block['type'] diff --git a/laserchicken/testdata/example.ply b/laserchicken/testdata/example.ply index d860d83..3ead0d2 100644 --- a/laserchicken/testdata/example.ply +++ b/laserchicken/testdata/example.ply @@ -1,9 +1,7 @@ ply format ascii 1.0 -comment [ comment {'time': '2018-01-18 16:01', 'module': 'load', 'parameters': [], 'version': '0.9.2'} comment {'time': '2018-01-18 16:01', 'module': 'filter', 'parameters': [('z', 'gt', '1.5')], 'version': '0.9.2'} -comment ] element vertex 3 property float x property float y From 1610d18914c918f14bdec96b5a547b2634c8a453 Mon Sep 17 00:00:00 2001 From: Faruk D Date: Mon, 22 Jan 2018 18:03:46 +0100 Subject: [PATCH 3/3] update the example dataset --- laserchicken/testdata/example.ply | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/laserchicken/testdata/example.ply b/laserchicken/testdata/example.ply index 3ead0d2..5033a0a 100644 --- a/laserchicken/testdata/example.ply +++ b/laserchicken/testdata/example.ply @@ -10,7 +10,7 @@ property int return element pointcloud 1 property double offset end_header -0.1 0.1 0.1 1 -0.2 0.2 0.2 1 -0.3 0.3 0.3 2 +0.11 0.12 0.13 1 +0.21 0.22 0.23 1 +0.31 0.32 0.33 2 12.1