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 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): 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 calcLightPos(light): return tuple( matrix.transform3( matrix.rotx(-objrot[0]) * matrix.roty(-objrot[1]) * matrix.rotz(-objrot[2]), light.position))
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 animate_synchronous_flips(self, delay = 0.000): steps = 30 dtheta = 2.0 * math.pi / steps rot = matrix.roty(-dtheta) for s in range(steps): self.erase() for v in self.views: v.transform(rot, v.compute_centroid()) self.draw() self.win.refresh() time.sleep(delay)
def animate_beamup(self, delay = 0.000): steps = 50 dy = 0.15 dtheta = 2.0 * math.pi / 100.0 rot = matrix.roty(-dtheta) for s in range(steps): self.erase() for v in self.views: v.transform(rot, self.origin) v.translate([0.0, -dy, 0.0]) self.draw() self.win.refresh() time.sleep(delay)
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 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()