-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_parser.py
More file actions
70 lines (61 loc) · 1.85 KB
/
csv_parser.py
File metadata and controls
70 lines (61 loc) · 1.85 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
import csv
class CSVParser:
"""
```
获取第0行数据
record = parser.get(0)
print(record)
输出类似:
{
'center': {'x': ..., 'y': ..., 'z': ...},
'rectangle': {'width': ..., 'length': ..., 'angle_x_deg': ...},
'normals': [
{'x': ..., 'y': ..., 'z': ...},
{'x': ..., 'y': ..., 'z': ...},
...
]
}
```
"""
def __init__(self, filepath='visualization.csv'):
self.data = []
self.length = 0
self._load_file(filepath)
def _load_file(self, filepath):
with open(filepath, 'r', newline='') as f:
reader = csv.reader(f, delimiter=',')
# 读取前6行字段说明(总共21个字段)
header_lines = [next(reader) for _ in range(6)]
self.field_names = sum(header_lines, []) # 扁平化字段列表
# 读取数据部分
for row in reader:
float_row = [float(val.strip()) for val in row if val.strip()]
self.data.append(float_row)
self.length += 1
def get(self, index):
"""
X_POS Y_POS Z_POS
width length angle
I1_x I1_y I1_z
I2_x I2_y I2_z
I3_x I3_y I3_z
I4_x I4_y I4_z
"""
row = self.data[index]
result = {
'center': {
'x': row[0],
'y': row[1],
'z': row[2],
'width': row[3],
'length': row[4],
'angle': row[5],
},
'impulse': []
}
for i in range(6, len(row), 3):
nx, ny, nz = row[i:i+3]
if nx == 0 and ny == 0 and nz == 0:
continue # 忽略无效冲击
result['impulse'].append({'x': nx, 'y': ny, 'z': nz})
return result