diff --git a/functions.py b/functions.py index 70ade4a5..8cbea083 100644 --- a/functions.py +++ b/functions.py @@ -827,6 +827,8 @@ def isosurface(data, level): *data* 3D numpy array of scalar values *level* The level at which to generate an isosurface + Returns a list of faces; each face is a list of three vertexes and each vertex is a tuple of three floats. + This function is SLOW; plenty of room for optimization here. """ diff --git a/opengl/MeshData.py b/opengl/MeshData.py index b9f8f1ec..7ac24aeb 100644 --- a/opengl/MeshData.py +++ b/opengl/MeshData.py @@ -1,4 +1,5 @@ from pyqtgraph.Qt import QtGui +import pyqtgraph.functions as fn class MeshData(object): """ @@ -37,6 +38,12 @@ class MeshData(object): else: self._setIndexedFaces(faces, vertexes) + def setMeshColor(self, color): + """Set the color of the entire mesh. This removes any per-face or per-vertex colors.""" + color = fn.Color(color) + self._meshColor = color.glColor() + self._vertexColors = None + self._faceColors = None def _setUnindexedFaces(self, faces): verts = {} @@ -107,7 +114,8 @@ class MeshData(object): for i, face in enumerate(self._faces): ## compute face normal pts = [self._vertexes[vind] for vind in face] - norm = QtGui.QVector3D.crossProduct(pts[1]-pts[0], pts[2]-pts[0]).normalized() + norm = QtGui.QVector3D.crossProduct(pts[1]-pts[0], pts[2]-pts[0]) + norm = norm / norm.length() ## don't use .normalized(); doesn't work for small values. self._faceNormals.append(norm) return self._faceNormals @@ -126,7 +134,7 @@ class MeshData(object): norm = QtGui.QVector3D() for fn in norms: norm += fn - norm.normalize() + norm = norm / norm.length() ## don't use .normalize(); doesn't work for small values. self._vertexNormals.append(norm) return self._vertexNormals @@ -152,3 +160,19 @@ class MeshData(object): """ pass + def save(self): + """Serialize this mesh to a string appropriate for disk storage""" + import pickle + names = ['_vertexes', '_edges', '_faces', '_vertexFaces', '_vertexNormals', '_faceNormals', '_vertexColors', '_edgeColors', '_faceColors', '_meshColor'] + state = {n:getattr(self, n) for n in names} + return pickle.dumps(state) + + def restore(self, state): + """Restore the state of a mesh previously saved using save()""" + import pickle + state = pickle.loads(state) + for k in state: + setattr(self, k, state[k]) + + + \ No newline at end of file diff --git a/opengl/items/GLMeshItem.py b/opengl/items/GLMeshItem.py index b82c4d3d..149baafb 100644 --- a/opengl/items/GLMeshItem.py +++ b/opengl/items/GLMeshItem.py @@ -20,8 +20,11 @@ class GLMeshItem(GLGraphicsItem): """ See MeshData for initialization arguments. """ - self.data = MeshData() - self.data.setFaces(faces, vertexes) + if isinstance(faces, MeshData): + self.data = faces + else: + self.data = MeshData() + self.data.setFaces(faces, vertexes) GLGraphicsItem.__init__(self) def initializeGL(self):