-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape_reader.py
More file actions
54 lines (47 loc) · 1.6 KB
/
Copy pathshape_reader.py
File metadata and controls
54 lines (47 loc) · 1.6 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
'''
reads in text file with edge coordinates and optional color field
returns a shape as a list of Edges
text file has the following format on each line:
start point, end point, color(optional)
0 0 0, 1 0 0, 0xFFF000
'''
import numpy as np
import re
from three_d.primitives import Edge
from three_d.model import Model
class ShapeReader(object):
def __init__(self, shape_file):
self.shape_file = shape_file
@staticmethod
def parse_color(s):
result = re.match(r'#([A-Fa-f0-9]{6})', s.strip())
if result is None:
return 0xFFFFFF
return int(result.group(1), 16)
@staticmethod
def parse_point(s):
coords = s.strip().split(' ')
try:
return np.array([float(coords[0]), float(coords[1]),
float(coords[2])])
except ValueError:
return None
def process_file(self):
with open(self.shape_file, 'r') as f:
edges = []
for line in f:
parts = line.split(',')
if len(parts) != 2 and len(parts) != 3:
continue
if len(parts) == 2:
color = 0xFFFFFF
else:
color = ShapeReader.parse_color(parts[2])
start = ShapeReader.parse_point(parts[0])
if start is None:
continue
end = ShapeReader.parse_point(parts[1])
if end is None:
continue
edges.append(Edge(start, end, color))
return Model(np.array([0, 0, 0]), edges)