def lookAtMatrix( camera, target, up ): forward = vector.normalise(target - camera) side = vector.normalise( vector.cross( forward, up ) ) #shifts 'up' to the camera's up up = vector.cross( side, forward ) matrix2 = array( [[ side[0], up[0], -forward[0], 0.0 ], [ side[1], up[1], -forward[1], 0.0 ], [ side[2], up[2], -forward[2], 0.0 ], [ 0.0, 0.0, 0.0, 1.0 ]], dtype = float32) return array(mat4.multiply( mat4.create_from_translation( -camera ), matrix2 ), dtype=float32)
def ray_coincident_ray( ray1, ray2 ): """Check if rays are coincident. Rays must not only be parallel to each other, but reside along the same vector. :param numpy.array ray1, ray2: The rays to check. :rtype: boolean :return: Returns True if the two rays are co-incident. """ # ensure the ray's directions are the same if ray_parallel_ray( ray1, ray2 ): # get the delta between the two ray's start point delta = ray2[ 0 ] - ray1[ 0 ] # get the cross product of the ray delta and # the direction of the rays cross = vector.cross( delta, ray2[ 1 ] ) # if the cross product is zero, the start of the # second ray is in line with the direction of the # first ray if numpy.count_nonzero( cross ) > 0: return False return True return False
def generate_coordinates(self, vertices, normals): # ignore the normals # create an empty texture coord array # dot product expects contiguous memory # so we have 2xN shape instead of Nx2 shape # we reshape after the dot product texture_coords = numpy.empty((2, len(vertices)), dtype=float) # extract our rows tu = texture_coords[0, ...] tv = texture_coords[1, ...] # take our vertices and flatten them against the plane plane_vertices = matrix.apply_direction_scale(vertices, self.plane[plane.normal], 0.0) right = vector.cross(self.plane[plane.normal], self.plane[plane.up]) # get the tu / tv values from our up and right vectors numpy.dot(plane_vertices, right, out=tu) numpy.dot(plane_vertices, self.plane[plane.up], out=tv) # apply our scaling tu /= self.size[0] tv /= self.size[1] # reshape back into our normal Nx2 shape texture_coords = numpy.transpose(texture_coords) return texture_coords
def vector_parallel_vector( v1, v2 ): """Checks if two vectors are parallel. :param numpy.array v1, v2: The vectors to check. :rtype: boolean :return: Returns True if the two vectors are parallel. """ # we cross product the 2 vectors # if the result is 0, then they are parallel cross = vector.cross( v1, v2 ) return 0 == numpy.count_nonzero( cross )
def regenViewMatrix(self): forward = array([ math.cos( self.verticalAngle ) * math.sin( self.horizontalAngle ), math.sin( self.verticalAngle ), math.cos( self.verticalAngle ) * math.cos( self.horizontalAngle ) ]) up = array([0.,-1.,0.,]) side = -vector.normalise( vector.cross( forward, up ) ) #shifts 'up' to the camera's up up = vector.cross( side, forward ) matrix2 = array( [[ side[0], up[0], forward[0], 0.0 ], [ side[1], up[1], forward[1], 0.0 ], [ side[2], up[2], forward[2], 0.0 ], [ 0.0, 0.0, 0.0, 1.0 ]], dtype = float32) self.position += forward * self.xMovement self.position += side * self.yMovement self.xMovement = self.yMovement = 0. self.viewMatrix = array(mat4.multiply( mat4.create_from_translation( self.position ), matrix2 ), dtype=float32)
def single_vector(): vec1 = numpy.array( [1.0, 0.0, 0.0] ) vec2 = numpy.array( [0.0, 1.0, 0.0] ) result = vector.cross( vec1, vec2 ) expected = numpy.cross( vec1, vec2 ) assert numpy.array_equal( expected, vec_cross( vec1, vec2 ) ) self.assertTrue( numpy.array_equal( result, expected ), "Vector cross product incorrect" )
def point_intersect_line( point, line ): """Calculates the intersection point of a point and aline. Performed by checking if the cross-product of the point relative to the line is 0. """ rl = line[ 1 ] - line[ 0 ] rp = point - line[ 0 ] cross = vector.cross( rl, rp ) # check if the cross product is zero if numpy.count_nonzero( cross ) > 0: return None return point
def batch(): vec1 = numpy.array( [1.0, 0.0, 0.0] ) vec2 = numpy.array( [0.0, 1.0, 0.0] ) batch1 = numpy.tile( vec1, (3,1) ) batch2 = numpy.tile( vec2, (3,1) ) result = vector.cross( batch1, batch2 ) expected = numpy.cross( vec1, vec2 ) expected = numpy.tile( expected, (3,1) ) self.assertTrue( numpy.array_equal( result, expected ), "Vector cross product incorrect" )
def generate_coordinates( self, vertices, normals ): # ignore the normals # create an empty texture coord array # dot product expects contiguous memory # so we have 2xN shape instead of Nx2 shape # we reshape after the dot product texture_coords = numpy.empty( (2, len(vertices)), dtype = float ) # extract our rows tu = texture_coords[0,...] tv = texture_coords[1,...] # take our vertices and flatten them against the plane plane_vertices = matrix.apply_direction_scale( vertices, self.plane[ plane.normal ], 0.0 ) right = vector.cross( self.plane[ plane.normal ], self.plane[ plane.up ] ) # get the tu / tv values from our up and right vectors numpy.dot( plane_vertices, right, out = tu ) numpy.dot( plane_vertices, self.plane[ plane.up ], out = tv ) # apply our scaling tu /= self.size[ 0 ] tv /= self.size[ 1 ] # reshape back into our normal Nx2 shape texture_coords = numpy.transpose( texture_coords ) return texture_coords
def point_intersect_line_segment( point, line ): """Calculates the intersection point of a point and a line segment. Performed by checking if the cross-product of the point relative to the line is 0 and if the dot product of the point relative to the line start AND the end point relative to the line start is less than the segment's squared length. """ rl = line[ 1 ] - line[ 0 ] rp = point - line[ 0 ] cross = vector.cross( rl, rp ) dot = vector.dot( rp, rl ) squared_length = vector.squared_length( rl ) if numpy.count_nonzero( cross ) > 0: return None if \ dot < 0.0 or \ dot > squared_length: return None return point