-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTLDetective.py
178 lines (139 loc) · 5.84 KB
/
STLDetective.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#-*- coding:utf-8 -*-
from sys import argv
import os
import struct
import numpy
import scipy.spatial
from typing import Optional
from copy import deepcopy
class STLDetective:
_vertex_count = 0
_vertices = None
_convex_hull_vertices = None
_convex_hull = None
def load_file(self, file_name):
f = open(file_name, "rb")
if not self._loadBinary(f):
f.close()
f = open(file_name, "rt")
try:
self._loadAscii(f)
except UnicodeDecodeError:
return None
f.close()
def _loadAscii(self, f):
num_verts = 0
for lines in f:
for line in lines.split("\r"):
if "vertex" in line:
num_verts += 1
f.seek(0, os.SEEK_SET)
vertex = 0
face = [None, None, None]
for lines in f:
for line in lines.split("\r"):
if "vertex" in line:
face[vertex] = line.split()[1:]
vertex += 1
if vertex == 3:
self._addVertex(float(face[0][0]), float(face[0][2]), -float(face[0][1]))
self._addVertex(float(face[1][0]), float(face[1][2]), -float(face[1][1]))
self._addVertex(float(face[2][0]), float(face[2][2]), -float(face[2][1]))
vertex = 0
def _loadBinary(self, f):
f.read(80)
num_faces = struct.unpack("<I", f.read(4))[0]
if num_faces < 1 or num_faces > 1000000000:
return False
f.seek(0, os.SEEK_END)
file_size = f.tell()
f.seek(84, os.SEEK_SET)
if file_size < num_faces * 50 + 84:
return False
for idx in range(0, num_faces):
data = struct.unpack(b"<ffffffffffffH", f.read(50))
self._addVertex(data[3], data[5], -data[4])
self._addVertex(data[6], data[8], -data[7])
self._addVertex(data[9], data[11], -data[10])
return True
def _addVertex(self, x, y, z):
if self._vertices is None:
self._vertices = numpy.zeros((10, 3), dtype=numpy.float32)
if len(self._vertices) == self._vertex_count:
self._vertices.resize((self._vertex_count * 2, 3))
self._vertices[self._vertex_count, 0] = x
self._vertices[self._vertex_count, 1] = y
self._vertices[self._vertex_count, 2] = z
self._vertex_count += 1
def getModelBBox(self):
self._newVertices = self._immutableNDArray(self._vertices[0: self._vertex_count])
if self._newVertices is None:
return None
data = numpy.pad(self._getConvexHullVertices(), ((0, 0), (0, 1)), "constant", constant_values=(0.0, 1.0))
minPoint = data.min(axis=0)
maxPoint = data.max(axis=0)
width = maxPoint[0] - minPoint[0]
height = maxPoint[1] - minPoint[1]
depth = maxPoint[2] - minPoint[2]
print("%.1f %.1f %.1f" % (width, depth, height))
def _immutableNDArray(self, nda):
if nda is None:
return None
if type(nda) is list:
nda = numpy.array(nda, numpy.float32)
nda.flags.writeable = False
if not nda.flags.writeable:
return nda
copy = deepcopy(nda)
copy.flags.writeable = False
return copy
def _getConvexHullVertices(self) -> numpy.ndarray:
if self._convex_hull_vertices is None:
convex_hull = self._getConvexHull()
self._convex_hull_vertices = numpy.take(convex_hull.points, convex_hull.vertices, axis=0)
return self._convex_hull_vertices
def _getConvexHull(self) -> Optional[scipy.spatial.ConvexHull]:
if self._convex_hull is None:
self._computeConvexHull()
return self._convex_hull
def _computeConvexHull(self):
points = self._getVertices()
if points is None:
return
self._convex_hull = self._approximateConvexHull(points, 1024)
def _getVertices(self) -> numpy.ndarray:
return self._newVertices
def _approximateConvexHull(self, vertex_data: numpy.ndarray, target_count: int) -> Optional[scipy.spatial.ConvexHull]:
input_max = target_count * 50
unit_size = 0.0125
max_unit_size = 0.01
while len(vertex_data) > input_max and unit_size <= max_unit_size:
new_vertex_data = _uniqueVertices(_roundVertexArray(vertex_data, unit_size))
if numpy.amin(new_vertex_data[:, 1]) != numpy.amax(new_vertex_data[:, 1]):
vertex_data = new_vertex_data
else:
break
unit_size *= 2
if len(vertex_data) < 4:
return None
hull_result = scipy.spatial.ConvexHull(vertex_data)
vertex_data = numpy.take(hull_result.points, hull_result.vertices, axis=0)
while len(vertex_data) > target_count and unit_size <= max_unit_size:
vertex_data = _uniqueVertices(_roundVertexArray(vertex_data, unit_size))
hull_result = scipy.spatial.ConvexHull(vertex_data)
vertex_data = numpy.take(hull_result.points, hull_result.vertices, axis=0)
unit_size *= 2
return hull_result
def _uniqueVertices(vertices: numpy.ndarray) -> numpy.ndarray:
vertex_byte_view = numpy.ascontiguousarray(vertices).view(numpy.dtype(numpy.void, vertices.dtype.itemsize * vertices.shape[1]))
_, idx = numpy.unique(vertex_byte_view, return_index=True)
return vertices[idx]
def _roundVertexArray(vertices: numpy.ndarray, unit: float) -> numpy.ndarray:
expanded = vertices / unit
rounded = expanded.round(0)
return rounded * unit
if __name__ == "__main__":
scriptName, stlFullPath = argv
detective = STLDetective()
detective.load_file(stlFullPath)
detective.getModelBBox()