def display3d(self, index): self.remove3d() b = self.crop_boxes[index] voxel = np.zeros((b[4] - b[1], b[5] - b[2], b[6] - b[3])) voxel[self.seg_mask[b[1]:b[4], b[2]:b[5], b[3]:b[6]]] = 1 # level=0.5意思是1和0交界,內插在0.5的概念 verts, faces, norm, val = measure.marching_cubes_lewiner( voxel, level=0.5, spacing=(1, 1, 1), gradient_direction='descent', step_size=1, allow_degenerate=True) mesh = MeshData( verts / 4, faces) # scale down - because camera is at a fixed position mesh._vertexNormals = norm self.item = GLMeshItem(meshdata=mesh, color=[1, 1, 1, 1], shader="normalColor", drawEdges=False, smooth=True) t1 = (mesh._vertexes[:, 0].max() + mesh._vertexes[:, 0].min()) / 2 t2 = (mesh._vertexes[:, 1].max() + mesh._vertexes[:, 1].min()) / 2 t3 = (mesh._vertexes[:, 2].max() + mesh._vertexes[:, 2].min()) / 2 self.item.translate(-t1, -t2, -t3, local=True) self.GLViewWidget.addItem(self.item)
def _get_iso_mesh(self, data, iso=None, color=(1, 0.2, 0.2, 1), z_shift=True): if iso is None: iso_val = self.options.get_iso_val() * data.max() else: iso_val = iso vertices, faces = isosurface(data, iso_val) nx, ny, nz = data.shape # Center Isosurface around Origin vertices[:, 0] = vertices[:, 0] - nx / 2 vertices[:, 1] = vertices[:, 1] - ny / 2 if z_shift: vertices[:, 2] = vertices[:, 2] - nz / 2 mesh_data = MeshData(vertexes=vertices, faces=faces) colors = np.zeros((mesh_data.faceCount(), 4), dtype=float) for idx in range(4): colors[:, idx] = color[idx] mesh_data.setFaceColors(colors) mesh = GLMeshItem(meshdata=mesh_data, smooth=True, shader='edgeHilight') mesh.setGLOptions('translucent') return mesh
def _get_iso_mesh(self, data, color, sign): iso_val = sign * self.options.get_iso_val() vertices, faces = isosurface(data, iso_val * data.max()) nx, ny, nz = data.shape # Center Isosurface around Origin vertices[:, 0] = vertices[:, 0] - nx / 2 vertices[:, 1] = vertices[:, 1] - ny / 2 vertices[:, 2] = vertices[:, 2] - nz / 2 mesh_data = MeshData(vertexes=vertices, faces=faces) colors = np.zeros((mesh_data.faceCount(), 4), dtype=float) # Sets color to Red (RGB, 0 = red, 1 = green, 2 = blue) if color == 'red': colors[:, 0] = 1.0 colors[:, 1] = 0.2 colors[:, 2] = 0.2 elif color == 'blue': colors[:, 0] = 0.2 colors[:, 1] = 0.2 colors[:, 2] = 1.0 # Transparency (I guess) colors[:, 3] = 1 mesh_data.setFaceColors(colors) mesh = GLMeshItem(meshdata=mesh_data, smooth=True, shader='edgeHilight') mesh.setGLOptions('translucent') return mesh
def labeling_to_mesh(labeling, labels): for label in labels: vertices, normals, faces = march(where(labeling == label, 2, 0).astype(int).T, 4) data = MeshData(vertices, faces) if normals is not None: data._vertexNormals = normals yield label, data
def plot_letter(self): self.widget.removeItem(self.letter) self.letter = GLMeshItem(meshdata=MeshData(self.vertexes, self.faces), drawFaces=False, edgeColor=QColor(51, 204, 255), drawEdges=True) self.widget.addItem(self.letter)
def make_center(): """ Create a marker for the center and wrap it in a GLMeshItem. """ mesh = MeshData.sphere(10, 10, 1) item = GLMeshItem(meshdata=mesh, color=[0.4, 0.4, 0.4, 1]) return item
def make_arrow(color): """ Create an axis arrow mesh and wrap it in a GLMeshItem. :param Sequence[float] color: the color for the arrow, e.g. [r, g, b, a] """ mesh = MeshData.cylinder(10, 10) item = GLMeshItem(meshdata=mesh, color=color) return item
def build_projections(self): if self.projectXY is not None and self.projectionsVisible: self.widget.removeItem(self.projectXY) if self.projectXZ is not None and self.projectionsVisible: self.widget.removeItem(self.projectXZ) if self.projectYZ is not None and self.projectionsVisible: self.widget.removeItem(self.projectYZ) self.projectionsVisible = False projectXY = [[0.0 for _ in range(self.dim + 1)] for _ in range(self.dim + 1)] for i in range(self.dim + 1): projectXY[i][i] = 1.0 projectYZ = copy.deepcopy(projectXY) projectXZ = copy.deepcopy(projectXY) projectXY[2][2] = 0.0 projectXZ[1][1] = 0.0 projectYZ[0][0] = 0.0 projectXY_vert = [] projectXZ_vert = [] projectYZ_vert = [] for vertex in self.vertexes: projectXY_vert.append(multiply_matvec(projectXY, vertex)) projectXZ_vert.append(multiply_matvec(projectXZ, vertex)) projectYZ_vert.append(multiply_matvec(projectYZ, vertex)) self.projectXY = GLMeshItem(meshdata=MeshData(np.array(projectXY_vert), self.faces), drawFaces=True, edgeColor=QColor(9, 156, 19), drawEdges=False) self.projectXZ = GLMeshItem(meshdata=MeshData(np.array(projectXZ_vert), self.faces), drawFaces=True, edgeColor=QColor(9, 156, 19), drawEdges=False) self.projectYZ = GLMeshItem(meshdata=MeshData(np.array(projectYZ_vert), self.faces), drawFaces=True, edgeColor=QColor(9, 156, 19), drawEdges=False)
def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setupUi(self) self.numberValidator = QDoubleValidator() self.translateX.setValidator(self.numberValidator) self.translateY.setValidator(self.numberValidator) self.translateZ.setValidator(self.numberValidator) self.scaleX.setValidator(self.numberValidator) self.scaleY.setValidator(self.numberValidator) self.scaleZ.setValidator(self.numberValidator) self.rotateX.setValidator(self.numberValidator) self.rotateY.setValidator(self.numberValidator) self.rotateZ.setValidator(self.numberValidator) self.projectionsVisible = False self.projectXY = None self.projectXZ = None self.projectYZ = None self.dim = 3 self.setWindowTitle("3D Letter") self.vertexes = np.array([[20, -20, -10], [20, -20, 10], [10, -20, 10], [10, -20, -10], [20, 10, -10], [20, 10, 10], [10, 10, 10], [10, 10, -10], [20, 20, -10], [20, 20, 10], [10, 20, 10], [10, 20, -10], [0, 0, -10], [0, 10, -10], [0, 0, 10], [0, 10, 10], [-10, -20, -10], [-10, -20, 10], [-20, -20, 10], [-20, -20, -10], [-10, 10, -10], [-10, 10, 10], [-20, 10, 10], [-20, 10, -10], [-10, 20, -10], [-10, 20, 10], [-20, 20, 10], [-20, 20, -10]]) self.faces = np.array([[3, 0, 8], [8, 11, 3], [0, 1, 9], [9, 8, 0], [2, 3, 7], [7, 6, 2], [1, 2, 10], [10, 9, 1], [0, 3, 2], [2, 1, 0], [12, 7, 11], [11, 13, 12], [20, 12, 13], [13, 24, 20], [13, 11, 10], [10, 15, 13], [24, 13, 15], [15, 25, 24], [14, 21, 25], [25, 15, 14], [6, 14, 15], [15, 10, 6], [14, 6, 7], [7, 12, 14], [21, 14, 12], [12, 20, 21], [16, 17, 21], [21, 20, 16], [18, 19, 27], [27, 26, 18], [19, 16, 24], [24, 27, 19], [17, 18, 26], [26, 27, 17], [18, 17, 16], [16, 19, 18], [27, 24, 25], [25, 26, 27]]) self.original_vertexes = copy.deepcopy(self.vertexes) self.letter = GLMeshItem(meshdata=MeshData(self.vertexes, self.faces), drawFaces=False, edgeColor=QColor(51, 204, 255), drawEdges=True) self.pushButton.clicked.connect(self.perform_transformations) self.resetButton.clicked.connect(self.reset) self.projectionButton.clicked.connect(self.show_projections) self.init_plot()
def __init__(self, vertexes=None, indices=None, *args, **kwds): """ Constructor for the MeshObject class. Parameters: vertexes (list): List of vertices used in the mesh. indices (list): List of indices for the faces used in the mesh. rot_quat (glm.quat): Quaternion defining the rotation of the object. rot_vec (vec3): Vector3 defining the rotation of the object. scale (vec3): Scale factor for each axis of the object. Defaults to (1.0, 1.0, 1.0) position (vec3): Position of the object. Defaults to (0.0, 0.0, 0.0) """ GLMeshItem.__init__(self) SpatialObject.__init__(self) # Argument extraction self.opts = { "vertexes": None, "model_verts": None, "indices": None, "position": vec3(0.0), "rot_vec": None, "rot_quat": None, "scale": vec3(1.0), "smooth": True, "computeNormals": False, "drawEdges": False, "drawFaces": True, "shader": None, "color": (1.0, 1.0, 1.0, 1.0), "edgeColor": (0.5, 0.5, 0.5, 1.0), } for k, v in kwds.items(): self.opts[k] = v if(vertexes is not None): self.opts["vertexes"] = vertexes if(indices is not None): self.opts["indices"] = indices self.set_position(self.opts["position"], False) if (self.opts["rot_quat"] is not None): self.rotation = R.from_quat(self.opts["rot_quat"]) if (self.opts["rot_vec"] is not None): self.rotation = R.from_rotvec(self.opts["rot_vec"]) self.set_scale(self.opts["scale"], False) self.opts["meshdata"] = MeshData() self.set_vertexes(self.opts["vertexes"], False) self.set_indices(self.opts["indices"], False) if (self.opts["vertexes"] is not None): self.update_vertices()
import time from pyqtgraph.opengl import GLViewWidget, MeshData from pyqtgraph.opengl.items.GLMeshItem import GLMeshItem from PyQt5.QtGui import QApplication volume = load(os.path.join(os.path.split(__file__)[0], 'data/input/sample.npy')) t0 = time.time() vertices, normals, faces = march(volume, 0) # zero smoothing rounds smooth_vertices, smooth_normals, faces = march(volume, 4) # 4 smoothing rounds t1 = time.time() print("took", t1 - t0, "sec") app = QApplication([]) view = GLViewWidget() mesh = MeshData(vertices / 100.0, faces) # scale down - otherwise camera is misplaced # or mesh = MeshData(smooth_vertices / 100, faces) mesh._vertexNormals = normals # or mesh._vertexNormals = smooth_normals item = GLMeshItem(meshdata=mesh, color=[1, 0, 0, 1], shader="normalColor") view.addItem(item) view.show() app.exec_()
import time from pyqtgraph.opengl import GLViewWidget, MeshData from pyqtgraph.opengl.items.GLMeshItem import GLMeshItem from PyQt5.QtGui import QApplication volume = load(os.path.join( os.path.split(__file__)[0], 'data/input/sample.npy')) t0 = time.time() vertices, normals, faces = march(volume, 0) # zero smoothing rounds smooth_vertices, smooth_normals, faces = march(volume, 4) # 4 smoothing rounds t1 = time.time() print("took", t1 - t0, "sec") app = QApplication([]) view = GLViewWidget() mesh = MeshData(vertices / 100.0, faces) # scale down - otherwise camera is misplaced # or mesh = MeshData(smooth_vertices / 100, faces) mesh._vertexNormals = normals # or mesh._vertexNormals = smooth_normals item = GLMeshItem(meshdata=mesh, color=[1, 0, 0, 1], shader="normalColor") view.addItem(item) view.show() app.exec_()
from marching_cubes import march from numpy import load from pyqtgraph.opengl import GLViewWidget, MeshData from pyqtgraph.opengl.items.GLMeshItem import GLMeshItem from PyQt4.QtGui import QApplication volume = load("test/data/input/sample.npy") # 128x128x128 uint8 volume # extract the mesh where the values are larger than or equal to 1 # everything else is ignored vertices, normals, faces = march(volume, 0) # zero smoothing rounds smooth_vertices, smooth_normals, faces = march(volume, 4) # 4 smoothing rounds app = QApplication([]) view = GLViewWidget() mesh = MeshData(vertices / 100, faces) # scale down - because camera is at a fixed position # or mesh = MeshData(smooth_vertices / 100, faces) mesh._vertexNormals = normals # or mesh._vertexNormals = smooth_normals item = GLMeshItem(meshdata=mesh, color=[1, 0, 0, 1], shader="normalColor") view.addItem(item) view.show() app.exec_()