Commit 5c0c47cd authored by Luke Campagnola's avatar Luke Campagnola
Browse files

3D mesh updates:

 - Can initialize GLMeshItem using MeshData instance
 - MeshData has save/restore methods
parent 727214ca
......@@ -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.
"""
......
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
......@@ -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):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment