3D mesh updates:

- Can initialize GLMeshItem using MeshData instance
 - MeshData has save/restore methods
This commit is contained in:
Luke Campagnola 2012-04-15 10:21:31 -04:00
parent 727214ca45
commit 5c0c47cda2
3 changed files with 33 additions and 4 deletions

View File

@ -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.
"""

View File

@ -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])

View File

@ -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):