-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoordinateConversion.py
More file actions
74 lines (66 loc) · 2.82 KB
/
Copy pathcoordinateConversion.py
File metadata and controls
74 lines (66 loc) · 2.82 KB
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
def read_pcd_file(filepath):
with open(filepath, 'r') as file:
lines = file.readlines()
# Extract the header and point data
header = []
points = []
read_header = True
size_values = None
type_values = None
count_values = None
for line in lines:
if read_header:
if line.startswith('FIELDS'):
# Remove "intensity" from the fields
fields = line.strip().split()
fields = [field for field in fields if field != 'intensity']
line = ' '.join(fields) + '\n'
elif line.startswith('SIZE'):
# Dynamically determine the number of elements and remove the final value
size_values = line.strip().split()
size_values.pop() # Remove the final value
line = ' '.join(size_values) + '\n'
elif line.startswith('TYPE'):
# Dynamically determine the number of elements and remove the final value
type_values = line.strip().split()
type_values.pop() # Remove the final value
line = ' '.join(type_values) + '\n'
elif line.startswith('COUNT'):
# Dynamically determine the number of elements and remove the final value
count_values = line.strip().split()
count_values.pop() # Remove the final value
line =' '.join(count_values) + '\n'
header.append(line)
if line.startswith('DATA'):
read_header = False
else:
# Only keep the first three values (x, y, z)
point_data = line.strip().split()[:3]
if point_data:
points.append(point_data)
return header, points
def write_pcd_file(filepath, header, points):
with open(filepath, 'w') as file:
for line in header:
file.write(line)
for point in points:
file.write(' '.join(point) + '\n')
def convert_pcd_axes(input_filepath, output_filepath):
# Read the PCD file
header, points = read_pcd_file(input_filepath)
# Change the XYZ to ZXY for each point
converted_points = []
for point in points:
if len(point) == 3:
x, y, z = point
elif len(point) == 4:
x, y, z, _ = point
# Reorder to ZXY (x to z, y to x, z to y)
converted_points.append([z, x, y])
# Write the updated PCD file
write_pcd_file(output_filepath, header, converted_points)
print(f"Converted PCD file saved to '{output_filepath}'")
# Use the function to convert a PCD file
input_pcd_file = '/tmp/finalCloud.pcd' # Replace with the path to your input PCD file
output_pcd_file = '/tmp/finalCloud_converted.pcd' # Replace with the desired path for your output PCD file
convert_pcd_axes(input_pcd_file, output_pcd_file)