Пример #1
0
    def __init__(self, indices, vertices, normals, texcoords, material):
        """Create a triangle from numpy arrays.

        :Parameters:
          indices
            A (3,) int array with vertex indexes in the vertex array
          vertices
            A (3, 3) float array for points a b c
          normals
            A (3, 3) float array with the normals por points a b c
          texcoords
            A tuple with (3, 2) float arrays with the texcoords for points 
            a b c
          material
            If coming from a not bound set, a symbol (string),
            otherwise, the material object itself

        """
        self.vertices = vertices
        """A (3, 3) float array for points a b c."""
        self.normals = normals
        """A (3, 3) float array with the normals for points a b c."""
        self.texcoords = texcoords
        """A tuple with (3, 2) float arrays with the texcoords."""
        self.material = material
        """Symbol (string) or the material object itself if bound."""
        self.indices = indices

        if self.normals is None:
            #generate normals
            vec1 = numpy.subtract(vertices[0], vertices[1])
            vec2 = numpy.subtract(vertices[2], vertices[0])
            vec3 = toUnitVec(numpy.cross(toUnitVec(vec2), toUnitVec(vec1)))
            self.normals = numpy.array([vec3, vec3, vec3])
Пример #2
0
    def __init__(self, indices, vertices, normals, texcoords, material):
        """A triangle should not be created manually."""

        self.vertices = vertices
        """A (3, 3) float array for points in the triangle"""
        self.normals = normals
        """A (3, 3) float array with the normals for points in the triangle.
        If the triangle didn't have normals, they will be computed."""
        self.texcoords = texcoords
        """A tuple with (3, 2) float arrays with the texture coordinates
          for the points in the triangle"""
        self.material = material
        """If coming from an unbound :class:`collada.triangleset.TriangleSet`, contains a
          string with the material symbol. If coming from a bound
          :class:`collada.triangleset.BoundTriangleSet`, contains the actual
          :class:`collada.material.Effect` the triangle is bound to."""
        self.indices = indices
        """A (3, 3) int array with vertex indexes in the vertex array"""

        if self.normals is None:
            #generate normals
            vec1 = numpy.subtract(vertices[0], vertices[1])
            vec2 = numpy.subtract(vertices[2], vertices[0])
            vec3 = toUnitVec(numpy.cross(toUnitVec(vec2), toUnitVec(vec1)))
            self.normals = numpy.array([vec3, vec3, vec3])
Пример #3
0
    def __init__(self, eye, interest, upvector, xmlnode=None):
        """Creates a lookat transformation
        
        :param numpy.array eye:
          An unshaped numpy array of floats of length 3 containing the position of the eye
        :param numpy.array interest:
          An unshaped numpy array of floats of length 3 containing the point of interest
        :param numpy.array upvector:
          An unshaped numpy array of floats of length 3 containing the up-axis direction
        :param xmlnode:
          When loaded, the xmlnode it comes from
        
        """
        self.eye = eye
        """A numpy array of length 3 containing the position of the eye"""
        self.interest = interest
        """A numpy array of length 3 containing the point of interest"""
        self.upvector = upvector
        """A numpy array of length 3 containing the up-axis direction"""

        if len(eye) != 3 or len(interest) != 3 or len(upvector) != 3:
            raise DaeMalformedError('Corrupted lookat transformation node')
        
        self.matrix = numpy.identity(4, dtype=numpy.float32)
        """The resulting transformation matrix. This will be a numpy.array of size 4x4."""

        front = toUnitVec(numpy.subtract(eye,interest))
        side = numpy.multiply(-1, toUnitVec(numpy.cross(front, upvector)))
        self.matrix[0,0:3] = side
        self.matrix[1,0:3] = upvector
        self.matrix[2,0:3] = front
        self.matrix[3,0:3] = eye

        self.xmlnode = xmlnode
        """ElementTree representation of the transform."""
        if xmlnode is None:
            self.xmlnode = E.lookat(' '.join(map(str, 
                                        numpy.concatenate((self.eye, self.interest, self.upvector)) )))
Пример #4
0
    def load( collada, node ):
        id = node.get('id')
        matrices = [ numpy.identity(4) ]
        nodes = []
        toremove = []
        try:
            for tnode in node:
                if tnode.tag == tag('translate'):
                    m = numpy.identity(4)
                    m[:3,3] = [ float(x) for x in tnode.text.split()]
                    matrices.append(m)
                    toremove.append(tnode)
                elif tnode.tag == tag('rotate'):
                    vx, vy, vz, angle = [ float(x) for x in tnode.text.split()]
                    matrices.append( makeRotationMatrix(vx, vy, vz, angle*numpy.pi/180.0) )
                    toremove.append(tnode)
                elif tnode.tag == tag('scale'):
                    m = numpy.identity(4)
                    t = [ float(x) for x in tnode.text.split()]
                    for i in xrange(3): m[i,i] = t[i]
                    matrices.append(m)
                    toremove.append(tnode)
                elif tnode.tag == tag('matrix'):
                    m = numpy.array([ float(x) for x in tnode.text.split()], dtype=numpy.float32)
                    if len(m) != 16: raise DaeMalformedError('Corrupted transformation node')
                    m.shape = (4, 4)
                    matrices.append(m)
                    toremove.append(tnode)
                elif tnode.tag == tag('lookat'):
                    m = numpy.identity(4)
                    lookat = [ float(x) for x in tnode.text.split() ]
                    
                    position = lookat[0:3]
                    target = lookat[3:6]
                    up = toUnitVec(lookat[6:9])
                    front = toUnitVec(numpy.subtract(position,target))
                    side = numpy.multiply(-1, toUnitVec(numpy.cross(front, up)))

                    m[0][0] = side[0]
                    m[0][1] = side[1]
                    m[0][2] = side[2]
    
                    m[1][0] = up[0]
                    m[1][1] = up[1]
                    m[1][2] = up[2]
    
                    m[2][0] = front[0]
                    m[2][1] = front[1]
                    m[2][2] = front[2]
    
                    m[3][0] = position[0]
                    m[3][1] = position[1]
                    m[3][2] = position[2]
                    
                    matrices.append(m)
                    toremove.append(tnode)
                    # TODO: LOOKAT Transform: is this correct?
        except ValueError, ex: raise DaeMalformedError('Corrupted transformation node')
        while len(matrices) > 1:
            matrices = matrices[:-2] + [ numpy.dot(matrices[-2], matrices[-1]) ]
        for n in toremove: node.remove(n)
        for nodenode in node:
            try: nodes.append( loadNode(collada, nodenode) )
            except DaeError, ex: collada.handleError(ex)
        return TransformNode(id, matrices[0], nodes, xmlnode=node)