def calcLightPos(light): return tuple( matrix.transform3( matrix.rotx(-objrot[0]) * matrix.roty(-objrot[1]) * matrix.rotz(-objrot[2]), light.position))
def sortFaces(self): import numpy as np import matrix camera = G.cameras[0] indices = self.primitives[self.nPrimitives - self.nTransparentPrimitives:] if len(indices) == 0: return m = matrix.translate(-self.translation) m = matrix.rotx(-self.rx) * m m = matrix.roty(-self.ry) * m m = matrix.rotz(-self.rz) * m m = matrix.translate(self.translation) * m cxyz = matrix.transform3(m, camera.eye) # Prepare sorting data verts = self.verts[indices] - cxyz distances = np.sum(verts ** 2, axis = -1) distances = np.amin(distances, axis = -1) distances = -distances # Sort order = np.argsort(distances) indices2 = indices[order,:] indices[...] = indices2
def getModelMatrix(self, obj): """ Calculate model view matrix for this orbital camera Currently ignores the actual model transformation Note that matrix is constructed in reverse order (using post-multiplication) """ # Note: matrix is constructed with post-multiplication in reverse order m = np.matrix(np.identity(4)) # First translate to camera center, then rotate around that center m = m * matrix.translate(self.center) # Move mesh to original position again if not obj.object.lockRotation: if self.verticalInclination != 0: m = m * matrix.rotx(self.verticalInclination) if self.horizontalRotation != 0: m = m * matrix.roty(self.horizontalRotation) # Ignore scale (bounding boxes ignore scale as well, anyway) # if any(x != 1 for x in self.scale): # m = m * matrix.scale(self.scale) center = [-self.center[0], -self.center[1], -self.center[2]] m = m * matrix.translate(center) # Move mesh to its rotation center to apply rotation if obj: m = m * obj.object.transform # Apply object transform first return m
def getModelMatrix(self, obj): """ Calculate model view matrix for this orbital camera Currently ignores the actual model transformation Note that matrix is constructed in reverse order (using post-multiplication) """ # Note: matrix is constructed with post-multiplication in reverse order m = np.matrix(np.identity(4)) # First translate to camera center, then rotate around that center m = m * matrix.translate( self.center) # Move mesh to original position again if not obj.object.lockRotation: if self.verticalInclination != 0: m = m * matrix.rotx(self.verticalInclination) if self.horizontalRotation != 0: m = m * matrix.roty(self.horizontalRotation) # Ignore scale (bounding boxes ignore scale as well, anyway) #if any(x != 1 for x in self.scale): # m = m * matrix.scale(self.scale) center = [-self.center[0], -self.center[1], -self.center[2]] m = m * matrix.translate( center) # Move mesh to its rotation center to apply rotation if obj: m = m * obj.object.transform # Apply object transform first return m
def sortFaces(self): camera = G.cameras[0] indices = self.primitives[self.nPrimitives - self.nTransparentPrimitives:] if len(indices) == 0: return m = matrix.translate(-self.translation) m = matrix.rotx(-self.rx) * m m = matrix.roty(-self.ry) * m m = matrix.rotz(-self.rz) * m m = matrix.translate(self.translation) * m cxyz = matrix.transform3(m, camera.eye) # Prepare sorting data verts = self.verts[indices] - cxyz distances = np.sum(verts**2, axis=-1) distances = np.amin(distances, axis=-1) distances = -distances # Sort order = np.argsort(distances) indices2 = indices[order, :] indices[...] = indices2
def transform(self): m = matrix.translate(self.loc) if any(x != 0 for x in self.rot): m = m * matrix.rotx(self.rx) m = m * matrix.roty(self.ry) m = m * matrix.rotz(self.rz) if any(x != 1 for x in self.scale): m = m * matrix.scale(self.scale) return m
def processargs(args, ext, use): """Process the command-line arguments for a program that does coordinate transformations. :args: The command line arguments without the program name. :ext: The extension of the output file. :use: A function for printing a usage message. :returns: A tuple containing the input file name, the output filename and the transformation matrix. """ validargs = ['x', 'y', 'z', 'X', 'Y', 'Z'] if len(args) < 1: use() sys.exit(0) infile = args[0] if len(args) < 2 or args[1] in validargs: outfile = None del args[:1] outfile = outname(infile, ext) else: outfile = args[1] del args[:2] tr = m.I() while len(args) > 1: if not args[0] in validargs: print("Unknown argument '{}' ignored.".format(args[0])) del args[0] continue try: ang = float(args[1]) if args[0] in ['x', 'X']: add = m.rotx(ang) elif args[0] in ['y', 'Y']: add = m.roty(ang) else: add = m.rotx(ang) del args[:2] tr = m.concat(tr, add) except: print("Argument '{}' is not a number, ignored.".format(args[1])) continue return (infile, outfile, tr)
def addXYTranslation(self, deltaX, deltaY): # Get matrix to transform camera X and Y direction into world space m = np.matrix(np.identity(4)) if self.verticalInclination != 0: m = m * matrix.rotx(self.verticalInclination) if self.horizontalRotation != 0: m = m * matrix.roty(self.horizontalRotation) xDirection = matrix.transform3(m, [1.0, 0.0, 0.0]) yDirection = matrix.transform3(m, [0.0, 1.0, 0.0]) # Translation speed is scaled with zoomFactor deltaX = (-deltaX / 50.0) / self.zoomFactor deltaY = (deltaY / 50.0) / self.zoomFactor offset = (deltaX * xDirection) + (deltaY * yDirection) offset[2] = -offset[2] # Invert Z direction self.addTranslation(0, offset[0]) self.addTranslation(1, offset[1]) self.addTranslation(2, offset[2])
def generate_poses(human, human_name): from bvh import BVH import matrix as m skeleton = human.skeleton bones = skeleton.getBones() b = BVH() b.fromSkeleton(skeleton, None, False) anim = b.createAnimationTrack(skeleton, name=human_name) rotmatrix = m.rotx(30) change = rotmatrix[:3, :4] for boneNr in range(len(bones)): boneName = bones[boneNr].name animData = anim.data[boneNr] if boneName == "upperarm02.L": anim.data[boneNr] = change human.addAnimation(anim) human.setActiveAnimation(anim.name) human.refreshPose() return human
def animate_rotation(self, view_subset, axis, theta, steps, origin = None, delay = 0.001): dtheta = theta / steps rot = None axis = axis.lower() if axis == 'x': rot = matrix.rotx(dtheta) elif axis == 'y': rot = matrix.roty(dtheta) elif axis == 'z': rot = matrix.rotz(dtheta) run_already = True for i in range(steps): if run_already: self.erase() self.transform_subset(view_subset, rot, origin) self.draw() self.win.refresh() if i < (steps - 1): time.sleep(delay) run_already = True
def animate_scramble_in_one_step(self, scramble, steps_per_turn = 20): if steps_per_turn == None: steps = config.STEPS_PER_TURN else: steps = steps_per_turn scramble = scramble.upper() scramble_list = scramble.split() transform_dict = {} for s in scramble_list: affected_tiles = self.cube.get_affected_tiles(s) self.cube.transform_using_string(s) axis, theta = self.get_trans_from_string(s) axis = axis.lower() r = None if axis == 'x': r = matrix.rotx(theta) elif axis == 'y': r = matrix.roty(theta) elif axis == 'z': r = matrix.rotz(theta) if r == None: print "Error: rotation about invalid axis: ", axis return for t in affected_tiles: if t not in transform_dict.keys(): transform_dict[t] = matrix.Matrix(list(r.data)) else: transform_dict[t] = matrix.multiply(matrix.Matrix(list(r.data)), transform_dict[t]) for tile in transform_dict.keys(): total_transform = transform_dict[tile] axis, angle = matrix.get_axis_and_angle_from_rot(total_transform) dtheta = angle / steps transform_dict[tile] = matrix.rotv(axis, dtheta) for s in range(steps): self.pvc.erase() for tile in transform_dict.keys(): self.pvc.views[tile].transform(transform_dict[tile], self.origin) self.display()
skeleton = human.skeleton bones = skeleton.getBones() file = open("/home/sumit/Desktop/testfile.txt", "r+") b = BVH() b.fromSkeleton(skeleton, None, False) anim = b.createAnimationTrack(skeleton, name="test") newData = [] change = np.zeros((3, 4)) change[0][0] = 1.0 change[1][1] = 1.0 change[2][2] = 1.0 change[2][3] = 0.0 #change[1][3] = 10 #rotmatrix = m.rotz(-30) #change = rotmatrix[:3,:4] rotmatrix = m.rotx(-30) change = rotmatrix[:3, :4] for boneNr in range(len(bones)): boneName = bones[boneNr].name animData = anim.data[boneNr] if boneName == "upperarm02.L": pass # <--- here "animData" holds info about the rotations for bone "upperarm02.L" and can be modified anim.data[boneNr] = change newData.append(change) else: newData.append(animData) file.write(str(animData) + "\n") newData = np.array(newData) human.addAnimation(anim)
def init_poly_views(self, win, tilesize, gapsize, origin, deltaz = 1.00): # def make_xz_tile(tile_center, tilesize): # radius = 0.5 * tilesize # n = 4 # dtheta = 2.0 * math.pi / n # points = [] # for i in range(n): # points.append([tile_center[0] - radius * math.cos(i * dtheta), \ # tile_center[1], \ # tile_center[2] + radius * math.sin(i * dtheta)]) # return points def make_xz_tile(tile_center, tilesize): half = 0.5 * tilesize points = [[tile_center[0] - half, tile_center[1], tile_center[2] + half], \ [tile_center[0] + half, tile_center[1], tile_center[2] + half], \ [tile_center[0] + half, tile_center[1], tile_center[2] - half], \ [tile_center[0] - half, tile_center[1], tile_center[2] - half]] return points def make_xy_tile(tile_center, tilesize): half = 0.5 * tilesize points = [[tile_center[0] - half, tile_center[1] - half, tile_center[2]], \ [tile_center[0] + half, tile_center[1] - half, tile_center[2]], \ [tile_center[0] + half, tile_center[1] + half, tile_center[2]], \ [tile_center[0] - half, tile_center[1] + half, tile_center[2]]] return points def make_yz_tile(tile_center, tilesize): half = 0.5 * tilesize points = [[tile_center[0], tile_center[1] - half, tile_center[2] - half], \ [tile_center[0], tile_center[1] - half, tile_center[2] + half], \ [tile_center[0], tile_center[1] + half, tile_center[2] - half], \ [tile_center[0], tile_center[1] + half, tile_center[2] + half]] return points def operate_on_list_of_tiles(trans, tile_list, origin): tile_list_copy = copy.deepcopy(tile_list) for i in range(len(tile_list_copy)): tile_list_copy[i] = trans.list_operate(tile_list_copy[i], origin) return tile_list_copy self.poly_views = [] # make top face: # start at back left corner rotx = matrix.rotx(0.5 * math.pi) rotxi = matrix.rotx(-0.5 * math.pi) rotx2 = matrix.rotx(math.pi) rotz = matrix.rotz(0.5 * math.pi) rotz2 = matrix.rotz(math.pi) #roty = matrix.roty(0.5 * math.pi) #rotyi = matrix.roty(-0.5 * math.pi) tile_center = [origin[0] - tilesize - gapsize, \ origin[1] - 1.5 * tilesize - 2 * gapsize, \ origin[2] + tilesize + gapsize] # construct vertices for top face tile polygons top_points_list = [] tile_points = make_xz_tile(tile_center, tilesize) top_points_list.append(list(tile_points)) tile_points = matrix.translate_points(tile_points, [tilesize + gapsize, 0.0, 0.0]) top_points_list.append(list(tile_points)) tile_points = matrix.translate_points(tile_points, [tilesize + gapsize, 0.0, 0.0]) top_points_list.append(list(tile_points)) tile_points = matrix.translate_points(tile_points, [- 2 * (tilesize + gapsize), 0.0, -(tilesize + gapsize)]) top_points_list.append(list(tile_points)) tile_points = matrix.translate_points(tile_points, [tilesize + gapsize, 0.0, 0.0]) top_points_list.append(list(tile_points)) tile_points = matrix.translate_points(tile_points, [tilesize + gapsize, 0.0, 0.0]) top_points_list.append(list(tile_points)) tile_points = matrix.translate_points(tile_points, [- 2 * (tilesize + gapsize), 0.0, -(tilesize + gapsize)]) top_points_list.append(list(tile_points)) tile_points = matrix.translate_points(tile_points, [tilesize + gapsize, 0.0, 0.0]) top_points_list.append(list(tile_points)) tile_points = matrix.translate_points(tile_points, [tilesize + gapsize, 0.0, 0.0]) top_points_list.append(list(tile_points)) # construct other faces by copying and iteratively transforming top face tile polygons front_points_list = copy.deepcopy(top_points_list) front_points_list = operate_on_list_of_tiles(rotx, front_points_list, origin) bottom_points_list = copy.deepcopy(front_points_list) bottom_points_list = operate_on_list_of_tiles(rotx, bottom_points_list, origin) back_points_list = copy.deepcopy(bottom_points_list) back_points_list = operate_on_list_of_tiles(rotx, back_points_list, origin) back_points_list = operate_on_list_of_tiles(rotz2, back_points_list, origin) right_points_list = copy.deepcopy(top_points_list) right_points_list = operate_on_list_of_tiles(rotz, right_points_list, origin) right_points_list = operate_on_list_of_tiles(rotx, right_points_list, origin) left_points_list = copy.deepcopy(bottom_points_list) left_points_list = operate_on_list_of_tiles(rotz, left_points_list, origin) left_points_list = operate_on_list_of_tiles(rotxi, left_points_list, origin) facedict = { 'U': top_points_list, \ 'F': front_points_list, \ 'R': right_points_list, \ 'B': back_points_list, \ 'L': left_points_list, \ 'D': bottom_points_list } self.pvc = view.PolyViewCollection([], win, origin, deltaz = 0.5, camera = self.camera) for face_name in ['U', 'F', 'R', 'B', 'L', 'D']: face = facedict[face_name] for points in face: if self.border_color == None: bc = self.color_pairs[face_name] # no border else: bc = self.border_color self.pvc.append(view.PolyView(win, graphics3d.Poly3d(points), deltaz = deltaz, \ border_color = bc, \ fill_color = self.color_pairs[face_name], \ char = ' '))