forked from cpederkoff/stl-to-voxel
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstl_reader.py
86 lines (69 loc) · 2.38 KB
/
stl_reader.py
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
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 19 06:37:35 2013
@author: Sukhbinder Singh
Reads a Binary file and
Returns Header,Points,Normals,Vertex1,Vertex2,Vertex3
Source: http://sukhbinder.wordpress.com/2013/11/28/binary-stl-file-reader-in-python-powered-by-numpy/
"""
import numpy as np
from struct import unpack
def BinarySTL(fname):
fp = open(fname, 'rb')
Header = fp.read(80)
nn = fp.read(4)
Numtri = unpack('i', nn)[0]
record_dtype = np.dtype([
('normals', np.float32, (3,)),
('Vertex1', np.float32, (3,)),
('Vertex2', np.float32, (3,)),
('Vertex3', np.float32, (3,)),
('atttr', '<i2', (1,) )
])
data = np.fromfile(fp, dtype=record_dtype, count=Numtri)
fp.close()
Normals = data['normals']
Vertex1 = data['Vertex1']
Vertex2 = data['Vertex2']
Vertex3 = data['Vertex3']
p = np.append(Vertex1, Vertex2, axis=0)
p = np.append(p, Vertex3, axis=0) #list(v1)
Points = np.array(list(set(tuple(p1) for p1 in p)))
return Header, Points, Normals, Vertex1, Vertex2, Vertex3
def AsciiSTL(fname):
with open(fname, 'r') as input_data:
# Skips text before the beginning of the interesting block:
init = False
triangles = []
verticies = []
for line in input_data:
if line.strip() == 'outer loop': # Or whatever test is needed
init = True
verticies = []
continue
# Reads text until the end of the block:
elif line.strip() == 'endloop':
init = False
triangles.append(verticies)
continue
elif init:
words = line.strip().split(' ')
words=list(filter(None,words))
assert words[0] == 'vertex'
verticies.append((float(words[1]), float(words[2]), float(words[3])))
return triangles
def IsAsciiStl(fname):
with open(fname,'rb') as input_data:
line = input_data.readline()
if line[:5] == b'solid':
return True
else:
return False
def read_stl_verticies(fname):
if IsAsciiStl(fname):
for (i,j,k) in AsciiSTL(fname):
yield (tuple(i),tuple(j),tuple(k))
else:
head, p, n, v1, v2, v3 = BinarySTL(fname)
for i, j, k in zip(v1, v2, v3):
yield (tuple(i), tuple(j), tuple(k))