Exemple #1
0
def createColorCircle(N, R, r, g, b):

    # First vertex at the center
    vertices = [0, 0, 0, r, g, b]
    indices = []

    dtheta = 2 * np.pi / N

    for i in range(N):
        theta = i * dtheta

        vertices += [
            # vertex coordinates
            R * np.cos(theta),
            R * np.sin(theta),
            0,
            r,
            g,
            b
        ]

        # A triangle is created using the center, this and the next vertex
        indices += [0, i, i + 1]

    # The final triangle connects back to the second vertex
    indices += [0, N, 1]

    return bs.Shape(vertices, indices)
def readOBJ(filename, color):

    vertices = []
    normals = []
    textCoords = []
    faces = []

    with open(filename, 'r') as file:
        for line in file.readlines():
            aux = line.strip().split(' ')

            if aux[0] == 'v':
                vertices += [[float(coord) for coord in aux[1:]]]

            elif aux[0] == 'vn':
                normals += [[float(coord) for coord in aux[1:]]]

            elif aux[0] == 'vt':
                assert len(
                    aux[1:]
                ) == 2, "Texture coordinates with different than 2 dimensions are not supported"
                textCoords += [[float(coord) for coord in aux[1:]]]

            elif aux[0] == 'f':
                N = len(aux)
                faces += [[
                    readFaceVertex(faceVertex) for faceVertex in aux[1:4]
                ]]
                for i in range(3, N - 1):
                    faces += [[
                        readFaceVertex(faceVertex)
                        for faceVertex in [aux[i], aux[i + 1], aux[1]]
                    ]]

        vertexData = []
        indices = []
        index = 0

        # Per previous construction, each face is a triangle
        for face in faces:

            # Checking each of the triangle vertices
            for i in range(0, 3):
                vertex = vertices[face[i][0] - 1]
                normal = normals[face[i][2] - 1]

                vertexData += [
                    vertex[0], vertex[1], vertex[2], color[0], color[1],
                    color[2], normal[0], normal[1], normal[2]
                ]

            # Connecting the 3 vertices to create a triangle
            indices += [index, index + 1, index + 2]
            index += 3

        return bs.Shape(vertexData, indices)
Exemple #3
0
def createShape():
    """
    Generating a circle, where the vertices at the border are generated via matrix
    transformations.
    """

    # Adding the vertex at the center, white color to identify it
    #            position       color
    vertices = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]
    indices = []

    # This vector will be used as reference to be transformed
    xt = np.array([1, 0, 0, 1])

    # We iterate generating vertices over the circle border
    for i in range(0, 30):

        # attempt 1: modifying manually each vertex.
        #         positions                                                        colors
        #vertices += [r * np.cos(0.1 *i * np.pi), r * np.sin(0.1 *i * np.pi), 0.0,    1,0,0]

        # attempt 2: using matrix transformations
        transformation = tr.rotationZ(0.1 * i * np.pi)
        xtp = np.matmul(transformation, xt)

        # returning to cartesian coordinates from homogeneous coordinates
        xtr = np.array([xtp[0], xtp[1], xtp[2]]) / xtp[3]

        # Adding the new vertex in clue color
        #            position                color
        vertices += [xtr[0], xtr[1], xtr[2], 0.0, 0.0, 1.0]

        # do not forget the indices!
        indices += [0, i + 1, i + 2]

    # removing the last spare vertex
    indices.pop()

    return bs.Shape(vertices, indices)
def getCharacterShape(char):

    # Getting the unicode code of the character as int
    # Example: ord('a') = 97
    k = ord(char)

    # Vertices are created between 0 and 1 in (x,y,0)
    vertices = [
        # space, texture
        0, 0, 0, k, 8, 0, \
        1, 0, 0, k, 8, 8, \
        1, 1, 0, k, 0, 8, \
        0, 1, 0, k, 0, 0
    ]

    indices = [
        # Bottom right triangle
        0,1,2,\
        # Top left triangle
        2,3,0
    ]

    return bs.Shape(vertices, indices)
def createDice():

    # Defining locations and texture coordinates for each vertex of the shape
    vertices = [
        #   positions         texture coordinates
        # Z+: number 1
        -0.5,
        -0.5,
        0.5,
        0,
        1 / 3,
        0.5,
        -0.5,
        0.5,
        1 / 2,
        1 / 3,
        0.5,
        0.5,
        0.5,
        1 / 2,
        0,
        -0.5,
        0.5,
        0.5,
        0,
        0,

        # Z-: number 6
        -0.5,
        -0.5,
        -0.5,
        1 / 2,
        1,
        0.5,
        -0.5,
        -0.5,
        1,
        1,
        0.5,
        0.5,
        -0.5,
        1,
        2 / 3,
        -0.5,
        0.5,
        -0.5,
        1 / 2,
        2 / 3,

        # X+: number 5
        0.5,
        -0.5,
        -0.5,
        0,
        1,
        0.5,
        0.5,
        -0.5,
        1 / 2,
        1,
        0.5,
        0.5,
        0.5,
        1 / 2,
        2 / 3,
        0.5,
        -0.5,
        0.5,
        0,
        2 / 3,

        # X-: number 2
        -0.5,
        -0.5,
        -0.5,
        1 / 2,
        1 / 3,
        -0.5,
        0.5,
        -0.5,
        1,
        1 / 3,
        -0.5,
        0.5,
        0.5,
        1,
        0,
        -0.5,
        -0.5,
        0.5,
        1 / 2,
        0,

        # Y+: number 4
        -0.5,
        0.5,
        -0.5,
        1 / 2,
        2 / 3,
        0.5,
        0.5,
        -0.5,
        1,
        2 / 3,
        0.5,
        0.5,
        0.5,
        1,
        1 / 3,
        -0.5,
        0.5,
        0.5,
        1 / 2,
        1 / 3,

        # Y-: number 3
        -0.5,
        -0.5,
        -0.5,
        0,
        2 / 3,
        0.5,
        -0.5,
        -0.5,
        1 / 2,
        2 / 3,
        0.5,
        -0.5,
        0.5,
        1 / 2,
        1 / 3,
        -0.5,
        -0.5,
        0.5,
        0,
        1 / 3
    ]

    # Defining connections among vertices
    # We have a triangle every 3 indices specified
    indices = [
        0,
        1,
        2,
        2,
        3,
        0,  # Z+
        7,
        6,
        5,
        5,
        4,
        7,  # Z-
        8,
        9,
        10,
        10,
        11,
        8,  # X+
        15,
        14,
        13,
        13,
        12,
        15,  # X-
        19,
        18,
        17,
        17,
        16,
        19,  # Y+
        20,
        21,
        22,
        22,
        23,
        20
    ]  # Y-

    return bs.Shape(vertices, indices)
Exemple #6
0
def createShape(N, maxPerturbationSize, time, normalizedMousePos):
    vertices = createVertices(N, maxPerturbationSize, time, normalizedMousePos)
    indices = createIndices(N)

    return bs.Shape(vertices, indices)
def toShape(mesh, color=None, textured=False, verbose=False):
    assert isinstance(mesh, openmesh.TriMesh)
    assert (color != None) != textured, "The mesh will be colored or textured, only one of these need to be specified."

    # Requesting normals per face
    mesh.request_face_normals()

    # Requesting normals per vertex
    mesh.request_vertex_normals()

    # Computing all requested normals
    mesh.update_normals()

    # You can also update specific normals
    #mesh.update_face_normals()
    #mesh.update_vertex_normals()
    #mesh.update_halfedge_normals()

    # At this point, we are sure we have normals computed for each face.
    assert mesh.has_face_normals()

    vertices = []
    indices = []

    # To understand how iteraors and circulators works in OpenMesh, check the documentation at:
    # https://www.graphics.rwth-aachen.de:9000/OpenMesh/openmesh-python/-/blob/master/docs/iterators.rst

    def extractCoordinates(numpyVector3):
        assert len(numpyVector3) == 3
        x = vertex[0]
        y = vertex[1]
        z = vertex[2]
        return [x,y,z]

    # This is inefficient, but it works!
    # You can always optimize it further :)

    # Checking each face
    for faceIt in mesh.faces():
        faceId = faceIt.idx()
        if verbose: print("face: ", faceId)

        # Checking each vertex of the face
        for faceVertexIt in mesh.fv(faceIt):
            faceVertexId = faceVertexIt.idx()

            # Obtaining the position and normal of each vertex
            vertex = mesh.point(faceVertexIt)
            normal = mesh.normal(faceVertexIt)
            if verbose: print("vertex ", faceVertexId, "-> position: ", vertex, " normal: ", normal)

            x, y, z = extractCoordinates(vertex)
            nx, ny, nz = extractCoordinates(normal)

            if textured:
                assert mesh.has_vertex_texcoords2D()

                texcoords = mesh.texcoord2D(faceVertexIt)
                tx = texcoords[0]
                ty = texcoords[1]
                
                vertices += [x, y, z, tx, ty, nx, ny, nz]
                indices += [len(vertices)//8 - 1]
            else:
                assert color != None

                r = color[0]
                g = color[1]
                b = color[2]

                vertices += [x, y, z, r, g, b, nx, ny, nz]
                indices += [len(vertices)//9 - 1]
        
        if verbose: print()

    return bs.Shape(vertices, indices)