Exemplo n.º 1
0
    def get_x_y_array(self, projection, ras):
        mesh = trimesh.Trimesh(self.vertices, self.triangles)
        contours = intersections.mesh_plane(mesh, self.plane_normals[projection], self.get_plane_origin(ras))
        x_array = [0 for _ in range(len(contours))]
        y_array = [0 for _ in range(len(contours))]

        for s in range(0, len(contours)):
            x_array[s] = contours[s][:, self.x_y_index[projection][0]]
            y_array[s] = contours[s][:, self.x_y_index[projection][1]]

        return x_array, y_array
Exemplo n.º 2
0
    def cut_by_plane(self, projection=SAGITTAL, ras=ORIGIN):
        """
        :param projection:
        :param ras:
        :return: Y_array, X_array
        """
        mesh = Trimesh(self.vertices, self.triangles)
        contours = intersections.mesh_plane(
            mesh, PLANE_NORMALS[projection], self._get_plane_origin(ras))
        x_array = [0] * len(contours)
        y_array = [0] * len(contours)

        for s in range(len(contours)):
            x_array[s] = contours[s][:, X_Y_INDEX[projection][0]]
            y_array[s] = contours[s][:, X_Y_INDEX[projection][1]]

        return x_array, y_array
Exemplo n.º 3
0
    def cut_by_plane(self, projection: str=SAGITTAL, ras: Union[numpy.ndarray, list]=ORIGIN) \
            -> (numpy.ndarray, numpy.ndarray):
        """
        :param projection:
        :param ras:
        :return: Y_array, X_array
        """
        mesh = Trimesh(self.vertices, self.triangles)
        contours = intersections.mesh_plane(
            mesh, PLANE_NORMALS[projection], self._get_plane_origin(ras))
        x_array = [0] * len(contours)
        y_array = [0] * len(contours)

        for s in range(len(contours)):
            x_array[s] = contours[s][:, X_Y_INDEX[projection][0]]
            y_array[s] = contours[s][:, X_Y_INDEX[projection][1]]

        return x_array, y_array
Exemplo n.º 4
0
    def clipPlaneVertices(self, planeIdx, bbox=None):
        """A convenience method for use with overlays being displayed
        in terms of a :class:`.Volume3DOpts` instance.

        Generates vertices for the clipping plane specified by ``planeIdx``
        (an index into the ``Volume3DOpts.clip*`` lists).

        Returns a ``(N, 3)`` ``numpy`` array containing the vertices, and
        a 1D ``numpy`` array containing vertex indices.

        See the :meth:`get3DClipPlane` and :meth:`drawClipPlanes` methods.

        .. note:: This method depends on the ``trimesh`` library - if it is
                  not present, two empty arrays are returned.
        """

        origin, normal = self.get3DClipPlane(planeIdx)
        vertices       = self.generateVertices3D(bbox=bbox)[0]
        indices        = np.arange(vertices.shape[0]).reshape(-1, 3)

        try:
            import trimesh
            import trimesh.intersections as tmint
        except ImportError:
            return np.zeros((0, 3)), np.zeros((0,))

        # Calculate the intersection of the clip
        # plane with the image bounding box
        lines = tmint.mesh_plane(
            trimesh.Trimesh(vertices, indices),
            plane_normal=normal,
            plane_origin=origin)

        # I'm assuming that the
        # returned lines are sorted
        vertices = np.array(lines.reshape(-1, 3), dtype=np.float32)

        if vertices.shape[0] < 3:
            return np.zeros((0, 3)), np.zeros((0,))

        indices = glroutines.polygonIndices(vertices.shape[0])

        return vertices, indices
Exemplo n.º 5
0
    def planeIntersection(self,
                          normal,
                          origin,
                          distances=False):
        """Calculate the intersection of this ``TriangleMesh`` with
        the plane defined by ``normal`` and ``origin``.

        :arg normal:    Vector defining the plane orientation

        :arg origin:    Point defining the plane location

        :arg distances: If ``True``, barycentric coordinates for each
                        intersection line vertex are calculated and returned,
                        giving their respective distance from the intersected
                        triangle vertices.

        :returns:       A tuple containing

                          - A ``(m, 2, 3)`` array containing ``m`` vertices:
                            of a set of lines, defining the plane intersection

                          - A ``(m,)`` array containing the indices of the
                            ``m`` triangles that were intersected.

                          - (if ``distances is True``) A ``(m, 2, 3)`` array
                            containing the barycentric coordinates of each
                            line vertex with respect to its intersected
                            triangle.
        """

        trimesh = self.trimesh

        if trimesh is None:
            return np.zeros((0, 3)), np.zeros((0, 3))

        import trimesh.intersections as tmint
        import trimesh.triangles     as tmtri

        lines, faces = tmint.mesh_plane(
            trimesh,
            plane_normal=normal,
            plane_origin=origin,
            return_faces=True)

        if not distances:
            return lines, faces

        # Calculate the barycentric coordinates
        # (distance from triangle vertices) for
        # each intersection line

        triangles = self.vertices[self.indices[faces]].repeat(2, axis=0)
        points    = lines.reshape((-1, 3))

        if triangles.size > 0:
            dists = tmtri.points_to_barycentric(triangles, points)
            dists = dists.reshape((-1, 2, 3))
        else:
            dists = np.zeros((0, 2, 3))

        return lines, faces, dists