Exemple #1
0
def read_ply(fname, force_subdivision_mesh=False):

    if not have_readply:
        print('Warning: readply module not found')
        return []

    plymesh = readply(fname, vertex_values_per_loop=False)

    print('%s: %d vertices, %d faces' %
          (fname, plymesh['num_vertices'], plymesh['num_faces']))

    #loop_start = plymesh['loop_start']
    loop_length = plymesh['loop_length']

    mesh_type, minn, maxn = determine_mesh_type(loop_length)

    print('Mesh type: %s' % mesh_type)
    if force_subdivision_mesh:
        print('Forcing subdivision mesh')

    vertices = plymesh['vertices']
    vertices = vertices.reshape((-1, 3))

    indices = plymesh['faces']

    if mesh_type.startswith('pure-') and not force_subdivision_mesh:
        mesh = ospray.Geometry('mesh')
        indices = indices.reshape((-1, minn))
        mesh.set_param('index', ospray.data_constructor_vec(indices))
        # Get a OSPRAY STATUS: ospray::Mesh ignoring 'index' array with wrong element type (should be vec3ui)
        # when passing a pure-quad index array

    elif mesh_type == 'mixed-tris-and-quads' and not force_subdivision_mesh:
        mesh = ospray.Geometry('mesh')

        # Duplicate last index of triangles to get all quads
        new_indices = []
        first = 0
        for n in loop_length:
            if n == 3:
                new_indices.extend(indices[first:first + 3])
                new_indices.append(indices[first + 2])
            else:
                new_indices.extend(indices[first:first + 4])
            first += n

        new_indices = numpy.array(new_indices, dtype=numpy.uint32).reshape(
            (-1, 4))
        mesh.set_param('index', ospray.data_constructor_vec(new_indices))
    else:
        # Use subdivision surface
        mesh = ospray.Geometry('subdivision')
        mesh.set_param('index', ospray.data_constructor(indices))
        mesh.set_param('face', loop_length)

    mesh.set_param('vertex.position', ospray.data_constructor_vec(vertices))

    if 'vertex_colors' in plymesh:
        print('Have vertex colors')
        colors = plymesh['vertex_colors'].reshape((-1, 3))

        # RGB -> RGBA
        n = colors.shape[0]
        alpha = numpy.ones((n, 1), dtype=numpy.float32)
        colors = numpy.hstack((colors, alpha))

        mesh.set_param('vertex.color', ospray.data_constructor_vec(colors))

    mesh.commit()

    return [mesh]
Exemple #2
0
def read_pdb(fname, radius=1):
    """Bare-bones pdb reader, ATOM only"""

    COLORS = {
        'H': (0xff, 0xff, 0xff),
        'C': (0x00, 0x00, 0x00),
        'N': (0x22, 0x33, 0xff),
        'O': (0xff, 0x20, 0x00),
        'CL': (0x1f, 0xf0, 0x1f),
        'F': (0x1f, 0xf0, 0x1f),
        'BR': (0x99, 0x22, 0x00),
        'I': (0x66, 0x00, 0xbb),
        'HE': (0x00, 0xff, 0xff),
        'NE': (0x00, 0xff, 0xff),
        'AR': (0x00, 0xff, 0xff),
        'XE': (0x00, 0xff, 0xff),
        'KR': (0x00, 0xff, 0xff),
        'P': (0xff, 0x99, 0x00),
        'S': (0xff, 0xe5, 0x22),
        'B': (0xff, 0xaa, 0x77),
        'TI': (0x99, 0x99, 0x99),
        'FE': (0xdd, 0x77, 0x00),
    }

    with open(fname, 'rt') as f:

        positions = []
        radii = []
        colors = []

        N = 0
        for line in f:
            if not line.startswith('ATOM '):
                continue

            x = float(line[30:38])
            y = float(line[38:46])
            z = float(line[46:54])
            element = line[76:78].strip()

            positions.append((x, y, z))

            if element in COLORS:
                hexcol = COLORS[element]
            else:
                hexcol = (0xdd, 0x77, 0xff)
                if element != '':
                    print('No color for element %s' % element)

            colors.append((hexcol[0] / 255, hexcol[1] / 255, hexcol[2] / 255))

            radii.append(radius)

            N += 1

        positions = numpy.array(positions, numpy.float32)
        radii = numpy.array(radii, numpy.float32)

        colors = numpy.array(colors, numpy.float32)
        #print(colors.shape, numpy.min(colors), numpy.max(colors))
        opacities = numpy.ones(N, 'float32')
        colors = numpy.c_[colors, opacities]

        positions[:9].tofile('pos9.bin')
        colors[:9].tofile('col9.bin')
        radii[:9].tofile('radii9.bin')

        assert positions.shape[0] == colors.shape[0]

        spheres = ospray.Geometry('sphere')
        spheres.set_param('sphere.position',
                          ospray.data_constructor_vec(positions))
        spheres.set_param('sphere.radius', ospray.data_constructor(radii))
        spheres.commit()

        gmodel = ospray.GeometricModel(spheres)
        gmodel.set_param('color', ospray.data_constructor_vec(colors))
        gmodel.commit()

        return gmodel
max_radius = 0.7 / pow(N, 1 / 3)

numpy.random.seed(123456)
positions = numpy.random.rand(N, 3).astype(numpy.float32)
radii = max_radius * numpy.random.rand(N).astype(numpy.float32)
colors = 0.3 + 0.7 * numpy.random.rand(N, 3).astype(numpy.float32)
opacities = numpy.ones(N, 'float32')
colors = numpy.c_[colors, opacities]
assert (numpy.min(colors) >= 0.3)

#colors = numpy.tile(numpy.array([1,0,0,1],'float32'), (N,1))

spheres = ospray.Geometry('sphere')
spheres.set_param('sphere.position',
                  ospray.data_constructor_vec(positions, True))
spheres.set_param('sphere.radius', ospray.data_constructor(radii, True))
spheres.commit()

gmodel = ospray.GeometricModel(spheres)
# XXX sometimes segfault, plus some spheres are black which should not be possible given
# the colors set
gmodel.set_param('color', ospray.data_constructor_vec(colors))
gmodel.commit()

group = ospray.Group()
group.set_param('geometry', [gmodel])
group.commit()

instance = ospray.Instance(group)
instance.commit()
Exemple #4
0
def read_obj(fname, force_subdivision_mesh=False):

    if not have_tinyobjloader:
        print('Warning: tinyobjloader module not found')
        return []

    r = tinyobjloader.ObjReader()
    r.ParseFromFile(fname)

    attributes = r.GetAttrib()

    vertices = numpy.array(attributes.vertices)
    vertices = vertices.reshape((-1, 3)).astype('float32')

    shapes = r.GetShapes()

    print('%s: %d vertices, %d shapes' %
          (fname, vertices.shape[0], len(shapes)))

    meshes = []

    for s in r.GetShapes():

        mesh = s.mesh

        indices = mesh.numpy_indices()  # [ v0, n0, t0, v1, n1, t1, ... ]
        vertex_indices = indices[::3]

        face_lengths = mesh.numpy_num_face_vertices()

        mesh_type, minn, maxn = determine_mesh_type(face_lengths)

        print('%s (%s): %d faces' % (s.name, mesh_type, face_lengths.shape[0]))

        if mesh_type.startswith('pure-') and not force_subdivision_mesh:
            mesh = ospray.Geometry('mesh')
            vertex_indices = vertex_indices.reshape(
                (-1, minn)).astype('uint32')
            mesh.set_param('index',
                           ospray.data_constructor_vec(vertex_indices))
        elif mesh_type == 'mixed-tris-and-quads' and not force_subdivision_mesh:
            mesh = ospray.Geometry('mesh')

            # Duplicate last index of triangles to get all quads
            new_indices = []
            first = 0
            for n in loop_length:
                if n == 3:
                    new_indices.extend(vertex_indices[first:first + 3])
                    new_indices.append(vertex_indices[first + 2])
                else:
                    new_indices.extend(vertex_indices[first:first + 4])
                first += n

            new_indices = numpy.array(new_indices, dtype=numpy.uint32).reshape(
                (-1, 4))
            mesh.set_param('index', ospray.data_constructor_vec(new_indices))
        else:
            # Use subdivision surface
            mesh = ospray.Geometry('subdivision')
            mesh.set_param('index', ospray.data_constructor(vertex_indices))
            mesh.set_param('face', face_lengths)

        mesh.set_param('vertex.position',
                       ospray.data_constructor_vec(vertices))

        mesh.commit()

        meshes.append(mesh)

    return meshes
Exemple #5
0
if set_value is not None:
    axis, minidx, maxidx, value = set_value
    assert axis in [0,1,2]
    if axis == 0:
        data[minidx:maxidx+1] = value
    elif axis == 1:
        data[:,minidx:maxidx+1] = value
    else:
        data[:,:,minidx:maxidx+1] = value

# Keep a reference to the numpy array around, as it will get
# deallocated otherwise
saved_data = data

data = ospray.data_constructor(data, is_shared=True)

volume = ospray.Volume('structuredRegular')
volume.set_param('gridSpacing', tuple(grid_spacing.tolist()))
volume.set_param('data', data)
volume.commit()

# TF

T = 16

if isovalue is not None:
    # Fixed color and opacity
    tfcolors = numpy.array([[0.8, 0.8, 0.8]], dtype=numpy.float32)
    tfopacities = numpy.array([1], dtype=numpy.float32)