summaryrefslogtreecommitdiff
path: root/mesh.py
diff options
context:
space:
mode:
authorAnthony LaTorre <tlatorre9@gmail.com>2011-06-18 00:12:09 -0400
committerAnthony LaTorre <tlatorre9@gmail.com>2011-06-18 00:12:09 -0400
commitab54917bceb4943f6750f589ffe6a032b2770fb1 (patch)
treee76ede76a1344820c424f2c07bc44ef22201864c /mesh.py
parent34ff4d6c734e5adf3aa8a0e7ca89031effdb1489 (diff)
downloadchroma-ab54917bceb4943f6750f589ffe6a032b2770fb1.tar.gz
chroma-ab54917bceb4943f6750f589ffe6a032b2770fb1.tar.bz2
chroma-ab54917bceb4943f6750f589ffe6a032b2770fb1.zip
moved class definitions for Solid, Mesh, Material, and Surface into geometry.py and moved instances of these classes into separate folders. the Solid object no longer contains a rotation, displacement, or id variable; instead, they are passed to a geometry object when calling add_solid().
Diffstat (limited to 'mesh.py')
-rw-r--r--mesh.py110
1 files changed, 0 insertions, 110 deletions
diff --git a/mesh.py b/mesh.py
deleted file mode 100644
index 0f1e8b6..0000000
--- a/mesh.py
+++ /dev/null
@@ -1,110 +0,0 @@
-import numpy as np
-import string
-import struct
-
-class Mesh(object):
- def __init__(self, vertices, triangles):
- vertices = np.asarray(vertices, dtype=np.float32)
- triangles = np.asarray(triangles, dtype=np.int32)
-
- if len(vertices.shape) != 2 or vertices.shape[1] != 3:
- raise ValueError('shape mismatch')
-
- if len(triangles.shape) != 2 or triangles.shape[1] != 3:
- raise ValueError('shape mismatch')
-
- if (triangles < 0).any():
- raise ValueError('indices in `triangles` must be positive.')
-
- if (triangles >= len(vertices)).any():
- raise ValueError('indices in `triangles` must be less than the '
- 'length of the vertex array.')
-
- self.vertices = vertices
- self.triangles = triangles
-
- def build(self):
- return self.vertices[self.triangles]
-
- def __getitem__(self, key):
- return self.vertices[self.triangles[key]]
-
- def __len__(self):
- return len(self.triangles)
-
-def mesh_from_stl(filename):
- 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):
- 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):
- f = open(filename)
-
- vertices = []
- triangles = []
- vertex_map = {}
-
- f.read(80)
- ntriangles = struct.unpack('<I', f.read(4))[0]
-
- for i in range(ntriangles):
- 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))