def rotate(self, axis, amount): (x, y, z) = self.findCentre() translation_matrix1 = wf.translationMatrix(-x, -y, -z) translation_matrix2 = wf.translationMatrix(x, y, z) if axis == 'x': rotation_matrix = wf.rotateXMatrix(amount) elif axis == 'y': rotation_matrix = wf.rotateYMatrix(amount) elif axis == 'z': rotation_matrix = wf.rotateZMatrix(amount) rotation_matrix = np.dot(np.dot(translation_matrix1, rotation_matrix), translation_matrix2) self.transform(rotation_matrix)
def translateAll(self, vector): """ Translate all wireframes along a given axis by d units. """ matrix = wf.translationMatrix(*vector) for wireframe in [self.hat.base, self.hat.bill, self.hat.lightSensors]: wireframe.transform(matrix)
def recalculate_transform(self): """ Recalculate coordinates of nodes based on the cumulative transformation """ bias_mat = wireframe.translationMatrix(*self.bias) rot_mat = wireframe.rotateMatrix(*self.view_angle) scale_mat = wireframe.scaleMatrix(*self.scale) self.tf_wireframe.nodes = (self.wireframe.nodes - self._center_half) @ \ rot_mat @ scale_mat @ bias_mat
def testTranslate(): """ Example of how to translate a wireframe. Creates a cuboid and translates it by vector (4,3,1). """ cuboid = shape.Cuboid((100,100,10), (20,30,40)) cuboid.outputNodes() print "\n> Translate cuboid along vector [4 3 1]" cuboid.transform(wf.translationMatrix(4, 3, 1)) cuboid.outputNodes()
def rotate(self, axis, amount): """Rotate the "camera" by rotating all other wireframes""" (x, y, z) = self.findCenter() translation_matrix1 = wf.translationMatrix(-x, -y, -z) translation_matrix2 = wf.translationMatrix(x, y, z) axis = axis.lower() if axis == 'x': rotation_matrix = wf.rotateXMatrix(amount) elif axis == 'y': rotation_matrix = wf.rotateYMatrix(amount) elif axis == 'z': rotation_matrix = wf.rotateZMatrix(amount) else: raise Exception("Invalid rotation axis. Valid axes are x, y, and z.") rotation_matrix = np.dot(np.dot(translation_matrix1, rotation_matrix), translation_matrix2) self.transform(rotation_matrix)
def testTranslate(): """ Example of how to translate a wireframe. Creates a cuboid and translates it by vector (4,3,1). """ cuboid = shape.Cuboid((100, 100, 10), (20, 30, 40)) cuboid.outputNodes() print "\n> Translate cuboid along vector [4 3 1]" cuboid.transform(wf.translationMatrix(4, 3, 1)) cuboid.outputNodes()
def setup_viewer(viewer): if scene == "shark": viewer.addEffect(fx.DrawSpeedTween(1, 45, 1, 20)) viewer.addWireframe('spin', obj.loadOBJ("shark.obj")) viewer.wireframes['spin'].transform(wf.scaleMatrix(150)) elif scene == "text": viewer.addEffect(fx.DrawSpeedTween(1, 150, 1, 50)) viewer.addWireframe('spin', obj.loadOBJ("text_test.obj")) viewer.wireframes['spin'].transform(wf.scaleMatrix(3)) viewer.wireframes['spin'].transform(wf.rotateZMatrix(-np.pi)) viewer.wireframes['spin'].transform(wf.rotateYMatrix(-np.pi)) elif scene == "watchdogs": viewer.addEffect(fx.DrawSpeedTween(1, 70, 1, 10)) viewer.addWireframe('spin', obj.loadOBJ("watchdogs.obj")) viewer.wireframes['spin'].transform(wf.scaleMatrix(3)) viewer.wireframes['spin'].transform(wf.rotateZMatrix(-np.pi)) viewer.wireframes['spin'].transform(wf.rotateYMatrix(-np.pi)) elif scene == "milkey": viewer.addEffect(fx.MIDIModulator("milkey.uss")) viewer.addWireframe('head', shape.Spheroid((0,)*3, (150,)*3, resolution=5)) viewer.addWireframe('left_ear', shape.Spheroid((0,)*3, (75,)*3, resolution=3)) viewer.addWireframe('right_ear', shape.Spheroid((0,)*3, (75,)*3, resolution=3)) elif scene == "cube": viewer.addEffect(fx.DrawSpeedTween(1, 250, 2, 50)) viewer.addWireframe('spin', shape.Cuboid((0,)*3, (150,)*3)) elif scene == "sphere": viewer.addEffect(fx.DrawSpeedTween(0.25, 130, 0.25, 20)) viewer.addWireframe('spin', shape.Spheroid((0,)*3, (150,)*3)) if scene != "milkey": viewer.centerWireframe("spin") else: viewer.centerWireframe("head") viewer.centerWireframe("left_ear") viewer.wireframes["left_ear"].transform(wf.translationMatrix(150, -150, 0)) viewer.centerWireframe("right_ear") viewer.wireframes["right_ear"].transform(wf.translationMatrix(-150, -150, 0)) viewer.key_to_function = {} viewer.object_update = object_update
def testRotate(): """ Example of how to rotate a wireframe. Creates a cuboid and rotates about its centre by pi/2 radians. """ cuboid = shape.Cuboid((100,100,10), (20,30,40)) cuboid.outputNodes() # Find rotation matrix (x,y,z) = cuboid.findCentre() translation_matrix = wf.translationMatrix(-x, -y, -z) rotation_matrix = np.dot(translation_matrix, wf.rotateXMatrix(math.pi/2)) rotation_matrix = np.dot(rotation_matrix, -translation_matrix) print "\n> Rotate cuboid around its centre and the x-axis" cuboid.transform(rotation_matrix) cuboid.outputNodes()
def testRotate(): """ Example of how to rotate a wireframe. Creates a cuboid and rotates about its centre by pi/2 radians. """ cuboid = shape.Cuboid((100, 100, 10), (20, 30, 40)) cuboid.outputNodes() # Find rotation matrix (x, y, z) = cuboid.findCentre() translation_matrix = wf.translationMatrix(-x, -y, -z) rotation_matrix = np.dot(translation_matrix, wf.rotateXMatrix(math.pi / 2)) rotation_matrix = np.dot(rotation_matrix, -translation_matrix) print "\n> Rotate cuboid around its centre and the x-axis" cuboid.transform(rotation_matrix) cuboid.outputNodes()
def translateAll(self, vector): for wireframe in self.wireframes.values(): wireframe.transform(wf.translationMatrix(vector[0], vector[1], vector[2]))
import pygame, math import numpy as np import wireframe as wf # Radian rotated by a key event ROTATION_AMOUNT = np.pi/16 MOVEMENT_AMOUNT = 10 key_to_function = { pygame.K_LEFT: (lambda x: x.transform(wf.translationMatrix(dx=-MOVEMENT_AMOUNT))), pygame.K_RIGHT: (lambda x: x.transform(wf.translationMatrix(dx= MOVEMENT_AMOUNT))), pygame.K_UP: (lambda x: x.transform(wf.translationMatrix(dy=-MOVEMENT_AMOUNT))), pygame.K_DOWN: (lambda x: x.transform(wf.translationMatrix(dy= MOVEMENT_AMOUNT))), pygame.K_EQUALS: (lambda x: x.scale(1.25)), pygame.K_MINUS: (lambda x: x.scale(0.8)), pygame.K_q: (lambda x: x.rotate('x', ROTATION_AMOUNT)), pygame.K_w: (lambda x: x.rotate('x',-ROTATION_AMOUNT)), pygame.K_a: (lambda x: x.rotate('y', ROTATION_AMOUNT)), pygame.K_s: (lambda x: x.rotate('y',-ROTATION_AMOUNT)), pygame.K_z: (lambda x: x.rotate('z', ROTATION_AMOUNT)), pygame.K_x: (lambda x: x.rotate('z',-ROTATION_AMOUNT)) } light_movement = { pygame.K_q: (lambda x: x.transform(wf.rotateXMatrix(-ROTATION_AMOUNT))), pygame.K_w: (lambda x: x.transform(wf.rotateXMatrix( ROTATION_AMOUNT))), pygame.K_a: (lambda x: x.transform(wf.rotateYMatrix(-ROTATION_AMOUNT))), pygame.K_s: (lambda x: x.transform(wf.rotateYMatrix( ROTATION_AMOUNT))), pygame.K_z: (lambda x: x.transform(wf.rotateZMatrix(-ROTATION_AMOUNT))), pygame.K_x: (lambda x: x.transform(wf.rotateZMatrix( ROTATION_AMOUNT))) }
def centerWireframe(self, name, z=0): target = self.wireframes[name] center_translate = np.negative(target.findCenter()) + np.array([(self.size / 2), (self.size / 2), z]) target.transform(wf.translationMatrix(center_translate[0], center_translate[1], center_translate[2]))
def __init__(self, size, name="PyScope", line_width=3, fps=None, show_view=("PyPy" not in sys.version), \ run_time=1.0, filename=None): self.size = size self.width = size self.height = size self.time_left = run_time self.total_time = run_time self.last_percent = 0.0 self.has_run = False if show_view: import pygame self.screen = pygame.display.set_mode((size,) * 2) pygame.display.set_caption(name) self.timer = pygame.time.Clock() if filename: if fps: self.wavout = wav.WavOutput(filename=filename, fps=fps) else: self.wavout = wav.WavOutput(filename=filename, fps=60) print "Warning: No FPS provided! Unless you're modifying it with an effect, please set the FPS on init." else: self.wavout = None self.wireframes = {} self.effects = [] self.object_to_update = [] self.perspective = 500.0 self.eyeX = self.width/2 self.eyeY = 100 self.view_vector = np.array([0, 0, -1]) self.background = (0,0,0) self.nodeColour = (0,180,255) self.line_width = line_width self.fixed_dt = fps if self.fixed_dt is not None: self.fixed_dt = 1.0 / float(self.fixed_dt) self.show_view = show_view self.start_time = time.time() self.lastdelta = 0 if show_view: self.rotation_amount = np.pi/32 self.movement_amount = 5 self.key_to_function = { pygame.K_LEFT: (lambda x: x.transform(wf.translationMatrix(dx=-self.movement_amount))), pygame.K_RIGHT: (lambda x: x.transform(wf.translationMatrix(dx= self.movement_amount))), pygame.K_UP: (lambda x: x.transform(wf.translationMatrix(dy=-self.movement_amount))), pygame.K_DOWN: (lambda x: x.transform(wf.translationMatrix(dy= self.movement_amount))), pygame.K_EQUALS: (lambda x: x.scale(1.25)), pygame.K_MINUS: (lambda x: x.scale(0.8)), pygame.K_q: (lambda x: x.rotate('x', self.rotation_amount)), pygame.K_w: (lambda x: x.rotate('x',-self.rotation_amount)), pygame.K_a: (lambda x: x.rotate('y', self.rotation_amount)), pygame.K_s: (lambda x: x.rotate('y',-self.rotation_amount)), pygame.K_z: (lambda x: x.rotate('z', self.rotation_amount)), pygame.K_x: (lambda x: x.rotate('z',-self.rotation_amount)) } else: self.key_to_function = {} self.frame_vectors = []
import pygame, math import numpy as np import wireframe as wf # Radian rotated by a key event ROTATION_AMOUNT = np.pi / 32 MOVEMENT_AMOUNT = 10 key_to_function = { pygame.K_LEFT: (lambda x: x.transform(wf.translationMatrix(dx=-MOVEMENT_AMOUNT))), pygame.K_RIGHT: (lambda x: x.transform(wf.translationMatrix(dx=MOVEMENT_AMOUNT))), pygame.K_UP: (lambda x: x.transform(wf.translationMatrix(dy=-MOVEMENT_AMOUNT))), pygame.K_DOWN: (lambda x: x.transform(wf.translationMatrix(dy=MOVEMENT_AMOUNT))), pygame.K_EQUALS: (lambda x: x.scale(1.25)), pygame.K_MINUS: (lambda x: x.scale(0.8)), pygame.K_q: (lambda x: x.rotate('x', ROTATION_AMOUNT)), pygame.K_w: (lambda x: x.rotate('x', -ROTATION_AMOUNT)), pygame.K_a: (lambda x: x.rotate('y', ROTATION_AMOUNT)), pygame.K_s: (lambda x: x.rotate('y', -ROTATION_AMOUNT)), pygame.K_z: (lambda x: x.rotate('z', ROTATION_AMOUNT)), pygame.K_x: (lambda x: x.rotate('z', -ROTATION_AMOUNT)) } """ light_movement = { pygame.K_q: (lambda x: x.transform(wf.rotateXMatrix(-ROTATION_AMOUNT))), pygame.K_w: (lambda x: x.transform(wf.rotateXMatrix( ROTATION_AMOUNT))), pygame.K_a: (lambda x: x.transform(wf.rotateYMatrix(-ROTATION_AMOUNT))),