Beispiel #1
0
def volume_to_mesh(vol, smooth_factor=3, level=None, **kwargs):
    """Convert a volume into a mesh with vertices, faces and normals.

    Parameters
    ----------
    vol : array_like
        The volume of shape (N, M, P)
    smooth_factor : int | 3
        The smoothing factor to apply to the volume.
    level : int | None
        Level to extract.
    kwargs : dict | {}
        Optional arguments to pass to convert_meshdata.

    Returns
    -------
    vertices : array_like
        Mesh vertices.
    faces : array_like
        Mesh faces.
    normals : array_like
        Mesh normals.
    """
    # Smooth the volume :
    vol_s = smooth_3d(vol, smooth_factor)
    # Extract vertices and faces :
    if level is None:
        level = .5
    elif isinstance(level, int):
        vol_s[vol_s != level] = 0
        level = .5
    vert_n, faces_n = isosurface(vol_s, level=level)
    # Convert to meshdata :
    vertices, faces, normals = convert_meshdata(vert_n, faces_n, **kwargs)
    return vertices, faces, normals
    def _update_mesh_visual(self):

        if self._data is None or self._level is None:
            return False

        if self._recompute:
            self._vertices_cache, self._faces_cache = isosurface(np.ascontiguousarray(self._data),
                                                                 self._level)
            self._recompute = False
            self._update_meshvisual = True

        if self._update_meshvisual:
            MeshVisual.set_data(self,
                                vertices=self._vertices_cache,
                                faces=self._faces_cache,
                                vertex_colors=self._vertex_colors,
                                face_colors=self._face_colors,
                                color=self._color)
            self._update_meshvisual = False
Beispiel #3
0
 def _select_roi(self, vol, level, smooth):
     if isinstance(level, (int, np.int)):
         condition = vol != level
     elif isinstance(level, float):
         condition = vol < level
     elif isinstance(level, (np.ndarray, list, tuple)):
         condition = np.logical_and.reduce([vol != k for k in level])
     # Set unused ROIs to 0 in the volume :
     vol[condition] = 0
     # Get the list of remaining ROIs :
     unique_vol = np.unique(vol[vol != 0])
     logger.info("    Selected ROI(s) : \n%r" % self.ref.loc[unique_vol])
     # Smooth the volume :
     vol_sm, tf = smooth_3d(vol, smooth, correct=True)
     # Get the isosurface :
     vert, faces = isosurface(vol_sm, level=.5)
     # Mesh correction after smoothing :
     vert = tf.map(vert)[:, 0:-1]
     return vert, faces
Beispiel #4
0
 def _select_roi(self, vol, level, smooth):
     if isinstance(level, (int, np.int)):
         condition = vol != level
     elif isinstance(level, float):
         condition = vol < level
     elif isinstance(level, (np.ndarray, list, tuple)):
         condition = np.logical_and.reduce([vol != k for k in level])
     # Set unused ROIs to 0 in the volume :
     vol[condition] = 0
     # Get the list of remaining ROIs :
     unique_vol = np.unique(vol[vol != 0])
     logger.info("    Selected ROI(s) : \n%r" % self.ref.loc[unique_vol])
     # Smooth the volume :
     vol_sm, tf = smooth_3d(vol, smooth, correct=True)
     # Get the isosurface :
     vert, faces = isosurface(vol_sm, level=.5)
     # Mesh correction after smoothing :
     vert = tf.map(vert)[:, 0:-1]
     return vert, faces
Beispiel #5
0
    def _update_mesh_visual(self):

        if self._data is None or self._level is None:
            return False

        if self._recompute:
            self._vertices_cache, self._faces_cache = isosurface(
                np.ascontiguousarray(self._data), self._level)
            self._recompute = False
            self._update_meshvisual = True

        if self._update_meshvisual:
            MeshVisual.set_data(self,
                                vertices=self._vertices_cache,
                                faces=self._faces_cache,
                                vertex_colors=self._vertex_colors,
                                face_colors=self._face_colors,
                                color=self._color)
            self._update_meshvisual = False
def retrieveIsosurfaceData(surface1, dimx, dimy, dimz):

    vertices, faces = isosurface(surface1._data, surface1._level)
    edges = np.empty((0, 2), int)

    for face in faces:

        edges = np.append(edges, np.array([[face[0], face[1]]]), axis=0)
        edges = np.append(edges, np.array([[face[0], face[2]]]), axis=0)
        edges = np.append(edges, np.array([[face[1], face[2]]]), axis=0)

    #modifying vertices to get an image centered on (0, 0, 0)

    #first, we need to get max and min values on each dimension :

    for vertice in vertices:
        vertice[0] = vertice[0] - (dimx / 2)
        vertice[1] = vertice[1] - (dimy / 2)
        vertice[2] = vertice[2] - (dimz / 2)

    return vertices, edges, faces
Beispiel #7
0
def volume_to_mesh(vol, smooth_factor=3, level=None, **kwargs):
    """Convert a volume into a mesh with vertices, faces and normals.

    Parameters
    ----------
    vol : array_like
        The volume of shape (N, M, P)
    smooth_factor : int | 3
        The smoothing factor to apply to the volume.
    level : int | None
        Level to extract.
    kwargs : dict | {}
        Optional arguments to pass to convert_meshdata.

    Returns
    -------
    vertices : array_like
        Mesh vertices.
    faces : array_like
        Mesh faces.
    normals : array_like
        Mesh normals.
    """
    # Smooth the volume :
    vol_s, tf = smooth_3d(vol, smooth_factor, correct=True)
    # Extract vertices and faces :
    if level is None:
        level = .5
    elif isinstance(level, int):
        vol_s[vol_s != level] = 0
        level = .5
    vert_n, faces_n = isosurface(vol_s, level=level)
    # Smoothing compensation :
    vert_n = tf.map(vert_n)[:, 0:-1]
    # Convert to meshdata :
    vertices, faces, normals = convert_meshdata(vert_n, faces_n, **kwargs)
    return vertices, faces, normals
Beispiel #8
0
    def _get_vertices(self):
        """Get vertices and faces of selected areas and pre-allocate color.

        Description
        """
        # --------------------------------------------------------------------
        # The volume array (self.vol) is composed with integers where each
        # integer encode for a specific area.
        # The isosurface turn a 3D array into a surface mesh compatible.
        # Futhermore, this function use a level parameter in order to get
        # vertices and faces of specific index. Unfortunately, the level can
        # only be >=, it's not possible to only select some specific levels.
        # --------------------------------------------------------------------
        # ============ Unicolor ============
        if self._unicolor:
            if not self._selectAll:
                # Create an empty volume :
                vol = np.zeros_like(self.vol)
                # Build the condition list :
                cd_lst = [
                    '(self.vol==' + str(k) + ')' for k in self._select_roi
                ]
                # Set vol to 1 for selected index :
                vol[eval(' | '.join(cd_lst))] = 1
            else:
                vol = self.vol
            # Extract the vertices / faces of non-zero values :
            self.vert, self.faces = isosurface(self._smooth(vol), level=.5)
            # Turn the unique color tuple into a faces compatible ndarray:
            self.vertex_colors = color2faces(self._color_roi[0],
                                             self.faces.shape[0])
            # Unique color per ROI :
            self._color_idx = np.zeros((self.faces.shape[0], ))

        # ============ Specific selection + specific colors ============
        # This is where problems begin. In this part, there's a specific area
        # selection with each one of them having a specific color. The program
        # below loop over areas, make a copy of the volume, turn all
        # non-desired area index to 0, transform into an isosurface and finally
        # concatenate vertices / faces / color. This is is very slow and it's
        # only because of the color.
        else:
            self.vert, self.faces = np.array([]), np.array([])
            q = 0
            for num, k in enumerate(self._select_roi):
                # Remove unecessary index :
                vol = np.zeros_like(self.vol)
                vol[self.vol == k] = 1
                # Get vertices/faces for this structure :
                vertT, facesT = isosurface(self._smooth(vol), level=.5)
                # Update faces index :
                facesT += (q + 1)
                # Concatenate vertices/faces :
                self.vert = np.concatenate(
                    (self.vert, vertT)) if self.vert.size else vertT
                self.faces = np.concatenate(
                    (self.faces, facesT)) if self.faces.size else facesT
                # Update colors and index :
                idxt = np.full((facesT.shape[0], ), k, dtype=np.int64)
                self._color_idx = np.concatenate(
                    (self._color_idx, idxt)) if self._color_idx.size else idxt
                color = color2faces(self._color_roi[num], facesT.shape[0])
                self.vertex_colors = np.concatenate(
                    (self.vertex_colors,
                     color)) if self.vertex_colors.size else color
                # Update maximum :
                q = self.faces.max()