Esempio n. 1
0
def create_mesh(session, c, transform, rgba, name):

    varray, tarray = mesh_geometry(c)
    if len(tarray) == 0:
        return None

    transform.transform_points(varray, in_place=True)

    from chimerax.core.models import Surface
    s = Surface(name, session)
    s.SESSION_SAVE_DRAWING = True
    s.clip_cap = True  # Cap surface when clipped
    from chimerax.surface import calculate_vertex_normals
    narray = calculate_vertex_normals(varray, tarray)
    s.set_geometry(varray, narray, tarray)
    rgba8 = [int(r * 255) for r in rgba]
    s.color = rgba8

    return s
Esempio n. 2
0
def geometry_node_surfaces(primitives, place, color, materials, colors,
                           session):

    from collada import polylist, triangleset

    splist = []
    for p in primitives:
        if isinstance(p, polylist.Polylist):
            p = p.triangleset()
        if not isinstance(p, triangleset.TriangleSet):
            continue  # Skip line sets.

        t = p.vertex_index  # N by 3 array of vertex indices for triangles
        t = t.copy()  # array from pycollada is not contiguous.
        v = p.vertex  # M by 3 array of floats for vertex positions
        ni = p.normal_index  # N by 3 array of normal indices for triangles
        n = p.normal  # M by 3 array of floats for vertex normals

        # Collada allows different normals on the same vertex in different triangles,
        # but Hydra only allows one normal per vertex.
        if n is None:
            vn = None
        else:
            from numpy import empty
            vn = empty(v.shape, n.dtype)
            vn[t.ravel(), :] = n[ni.ravel(), :]

        vcolors = vertex_colors(p, t, len(v), colors)
        c = material_color(materials.get(p.material), color)

        name = '%d' % (len(splist) + 1)
        from chimerax.core.models import Surface
        sp = Surface(name, session)
        sp.set_geometry(v, vn, t)
        sp.color_list = [c]
        sp.position_list = [place]
        if not vcolors is None:
            sp.vertex_colors = vcolors
        sp.clip_cap = True

        splist.append(sp)

    return splist
Esempio n. 3
0
def calculate_segmentation_surfaces(seg,
                                    where=None,
                                    each=None,
                                    region='all',
                                    step=None,
                                    color=None):
    # Warn if number of surfaces is large.
    if where is None and each is None:
        max_seg_id = _maximum_segment_id(seg)
        if max_seg_id > 100:
            from chimerax.core.errors import UserError
            raise UserError(
                'Segmentation %s (#%s) has %d segments (> 100).'
                ' To create surface for each segment use "each segment" option.'
                % (seg.name, seg.id_string, max_seg_id))

    # Compute surfaces
    conditions = (where if where else []) + ([each] if each else [])
    group, attribute_name = _which_segments(seg, conditions)
    matrix = seg.matrix(step=step, subregion=region)
    from . import segmentation_surfaces
    surfs = segmentation_surfaces(matrix, group)

    # Transform vertices from index to scene units and compute normals.
    geom = []
    tf = seg.matrix_indices_to_xyz_transform(step=step, subregion=region)
    for region_id, va, ta in surfs:
        tf.transform_points(va, in_place=True)
        from chimerax.surface import calculate_vertex_normals
        na = calculate_vertex_normals(va, ta)
        geom.append((region_id, va, na, ta))

    # Determine surface coloring.
    if color is None:
        attr = None if attribute_name == 'segment' else attribute_name
        colors = _attribute_colors(seg, attr).attribute_rgba

    # Create one or more surface models
    from chimerax.core.models import Surface
    from chimerax.surface import combine_geometry_xvnt
    segsurfs = []
    if each is None and len(geom) > 1:
        # Combine multiple surfaces into one.
        va, na, ta = combine_geometry_xvnt(geom)
        name = '%s %d %ss' % (seg.name, len(geom), attribute_name)
        s = Surface(name, seg.session)
        s.clip_cap = True  # Cap surface when clipped
        s.set_geometry(va, na, ta)
        if color is None:
            color_counts = [(colors[region_id], len(sva))
                            for region_id, sva, sna, sta in geom]
            s.vertex_colors = _vertex_colors(len(va), color_counts)
        else:
            s.color = color
        segsurfs.append(s)
    else:
        # Create multiple surface models
        for region_id, va, na, ta in geom:
            name = '%s %s %d' % (seg.name, attribute_name, region_id)
            s = Surface(name, seg.session)
            s.clip_cap = True  # Cap surface when clipped
            s.set_geometry(va, na, ta)
            s.color = colors[region_id] if color is None else color
            segsurfs.append(s)

    return segsurfs