diff options
author | Stan Seibert <stan@mtrr.org> | 2011-09-16 14:27:46 -0400 |
---|---|---|
committer | Stan Seibert <stan@mtrr.org> | 2011-09-16 14:27:46 -0400 |
commit | 084dfd08b714faefaea77cb7dc04d2e93dc04b1d (patch) | |
tree | 5be8c1f1d30dc52d74c70c4964ec54f66294c265 /stl.py | |
parent | cfecff941fc619eb7269128afc62d9c11ae78aff (diff) | |
download | chroma-084dfd08b714faefaea77cb7dc04d2e93dc04b1d.tar.gz chroma-084dfd08b714faefaea77cb7dc04d2e93dc04b1d.tar.bz2 chroma-084dfd08b714faefaea77cb7dc04d2e93dc04b1d.zip |
File reorganization to move toward standard python package layout
Diffstat (limited to 'stl.py')
-rw-r--r-- | stl.py | 91 |
1 files changed, 0 insertions, 91 deletions
@@ -1,91 +0,0 @@ -import numpy as np -import string -import struct -from chroma.geometry import Mesh -import bz2 - -def mesh_from_stl(filename): - "Returns a `chroma.geometry.Mesh` from an STL file." - if filename.endswith('.bz2'): - f = bz2.BZ2File(filename) - else: - f = open(filename) - buf = f.read(200) - f.close() - - for char in buf: - if char not in string.printable: - return mesh_from_binary_stl(filename) - - return mesh_from_ascii_stl(filename) - -def mesh_from_ascii_stl(filename): - "Return a mesh from an ascii stl file." - if filename.endswith('.bz2'): - f = bz2.BZ2File(filename) - else: - f = open(filename) - - vertices = [] - triangles = [] - vertex_map = {} - - while True: - line = f.readline() - - if line == '': - break - - if not line.strip().startswith('vertex'): - continue - - triangle = [None]*3 - for i in range(3): - vertex = tuple([float(s) for s in line.strip().split()[1:]]) - - if vertex not in vertex_map: - vertices.append(vertex) - vertex_map[vertex] = len(vertices) - 1 - - triangle[i] = vertex_map[vertex] - - if i < 3: - line = f.readline() - - triangles.append(triangle) - - f.close() - - return Mesh(np.array(vertices), np.array(triangles, dtype=np.uint32)) - -def mesh_from_binary_stl(filename): - "Return a mesh from a binary stl file." - f = open(filename) - - vertices = [] - triangles = [] - vertex_map = {} - - f.read(80) - ntriangles = struct.unpack('<I', f.read(4))[0] - - for i in range(ntriangles): - normal = tuple(struct.unpack('<fff', f.read(12))) - - triangle = [None]*3 - for j in range(3): - vertex = tuple(struct.unpack('<fff', f.read(12))) - - if vertex not in vertex_map: - vertices.append(vertex) - vertex_map[vertex] = len(vertices) - 1 - - triangle[j] = vertex_map[vertex] - - triangles.append(triangle) - - f.read(2) - - f.close() - - return Mesh(np.array(vertices), np.array(triangles, dtype=np.uint32)) |