コード例 #1
0
ファイル: graphics3d.py プロジェクト: peterbat/rubikon
def move_camera(camera, camera_control, origin, amount = None):
  location = camera['location']
  line_of_sight = camera['line_of_sight']
  parallel = camera['parallel']
  if camera_control in ['backward', 'forward']:
    if amount is None:
      amount = 0.25
    if camera_control == 'forward':
      location = [location[i] + amount * line_of_sight[i] for i in range(len(location))]
    elif camera_control == 'backward':
      location = [location[i] - amount * line_of_sight[i] for i in range(len(location))]
    camera['location'] = location
    return camera
  if amount is None:
    amount = 2.0 * math.pi / 100.0
  if camera_control == 'up':
    rot = matrix.rotv(parallel, -amount)
  elif camera_control == 'down':
    rot = matrix.rotv(parallel, amount)
  elif camera_control == 'left':
    rot = matrix.rotv(matrix.cross(parallel, line_of_sight), -amount)
  elif camera_control == 'right':
    rot = matrix.rotv(matrix.cross(parallel, line_of_sight), amount)
  line_of_sight = rot.operate(line_of_sight, origin = [0., 0., 0.])
  parallel = rot.operate(parallel, origin = [0., 0., 0.])
  location = rot.operate(location, origin = origin)
  camera = {'location': location, 'line_of_sight': line_of_sight, 'parallel': parallel}
  return camera
コード例 #2
0
ファイル: view.py プロジェクト: peterbat/rubikon
  def animate_explosion(self, delay = 0.000):
    import random
    rotation_list = []
    dr_list = []
    bulk_velocity = [random.gauss(0.0, 0.2), random.gauss(0.0, 0.2), 12.0 * abs(random.gauss(0.0, 0.1))]
    bv_norm = math.sqrt(sum([bvi**2 for bvi in bulk_velocity]))
    bulk_velocity = [bvi / bv_norm * 0.2 for bvi in bulk_velocity]
    dtheta = 0.04 * 2.0 * math.pi
    numsteps = 200
    
    for v in self.views:
      centroid = v.compute_centroid()
      displacement = [centroid[0] - self.origin[0], centroid[1] - self.origin[1], centroid[2] - self.origin[2]]
      norm = math.sqrt(sum([di**2 for di in displacement]))
      orientation = [di / norm for di in displacement]
      speed = 0.1 * random.random()
      dr = [speed * ori for ori in orientation]
      dr = [dr[i] + bulk_velocity[i] for i in range(len(dr))]
      dr_list.append(dr)
      rotvec = [random.random(), random.random(), random.random()]
      rotation_list.append(matrix.rotv(rotvec, dtheta + random.gauss(0.0, 0.1)))

    for i in range(numsteps):
      self.erase()
      for v in range(len(self.views)):
        centroid = self.views[v].compute_centroid()
        self.views[v].transform(rotation_list[v], origin = centroid)
        self.views[v].translate(dr_list[v])
      self.draw()
      self.win.refresh()
      time.sleep(delay)
コード例 #3
0
ファイル: cubeview.py プロジェクト: peterbat/rubikon
 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()
コード例 #4
0
ファイル: view.py プロジェクト: peterbat/rubikon
 def animate_synchronous_orbits(self, delay = 0.000):
   steps = 30
   dtheta = 2.0 * math.pi / steps
   rots = []
   origins = []
   for i in range(len(self.views)):
     rotvec = [random.random() - 0.5, \
               random.random() - 0.5, \
               random.random() - 0.5]
     rots.append(matrix.rotv(rotvec, -dtheta))
     radius = 2.0
     center = self.views[i].compute_centroid()
     v = [center[0] + radius * (random.random() - 0.5), \
          center[1] + radius * (random.random() - 0.5), \
          center[2] + radius * (random.random() - 0.5)]
     origins.append(v)
   for s in range(steps):
     self.erase()
     for i in range(len(self.views)):
       self.views[i].transform(rots[i], origins[i])
     self.draw()
     self.win.refresh()
     time.sleep(delay)
コード例 #5
0
ファイル: view.py プロジェクト: peterbat/rubikon
def test_view(screen):
  set_up_curses(screen)
  if curses.can_change_color():
    curses.init_color(curses.COLOR_MAGENTA, 1000, 549, 0)
  curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
  curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_RED)
  curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_YELLOW)
  curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_MAGENTA)
  curses.init_pair(5, curses.COLOR_BLACK, curses.COLOR_BLUE)
  curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_GREEN)
  curses.init_pair(7, curses.COLOR_BLACK, curses.COLOR_CYAN)

  points1 = [[-1.0, 1.0, 3.5], \
            [1.0, 1.0, 3.5], \
            [1.0, -1.0, 3.5], \
            [-1.0, -1.0, 3.5]]
  points2 = [[1.5, 1.0, 4.0], \
             [1.5, 1.0, 6.0], \
             [1.5, -1.0, 6.0], \
             [1.5, -1.0, 4.0]]
  points3 = [[1.0, 1.0, 6.5], \
             [-1.0, 1.0, 6.5], \
             [-1.0, -1.0, 6.5], \
             [1.0, -1.0, 6.5]]
  points4 = [[-1.5, 1.0, 6.0], \
             [-1.5, 1.0, 4.0], \
             [-1.5, -1.0, 4.0], \
             [-1.5, -1.0, 6.0]]
  points5 = [[-1.0, 1.5, 4.0], \
             [1.0, 1.5, 4.0], \
             [1.0, 1.5, 6.0], \
             [-1.0, 1.5, 6.0]]
  points6 = [[-1.0, -1.5, 4.0],
             [1.0, -1.5, 4.0],
             [1.0, -1.5, 6.0],
             [-1.0, -1.5, 6.0]]
  points_list = [points1, points2, points3, points4, points5, points6]
  polygons = [Poly3d(points) for points in points_list]
  polygon_views = [PolyView(screen, polygons[i], deltaz = 1.0, border_color = curses.color_pair(i+1), fill_color = curses.color_pair(i+1)) for i in range(6)]
  pvc = PolyViewCollection(polygon_views, screen)
  thetasteps = 100
  dtheta = 2.0 * math.pi / thetasteps
  #rot = matrix.rotz(dtheta)
  rot = matrix.rotv([1.0, 0.5, 0.0], dtheta)
  origin = [0.0, 0.0, 5.0]
  run_already = None
  delay = 0.005
  while True:
    if run_already is not None:
      pvc.erase()
    pvc.transform(rot, origin)
    pvc.draw()
    screen.refresh()
    time.sleep(delay)
    c = screen.getch()
    if c != -1 and c <= 255:
      if chr(c) == 'q':
        break
      elif chr(c) == 'j':
        pvc.translate([0.0, 0.0, -0.5])
        origin = matrix.translate_point(origin, [0.0, 0.0, -0.5])
      elif chr(c) == 'k':
        pvc.translate([0.0, 0.0, +0.5])
        origin = matrix.translate_point(origin, [0.0, 0.0, +0.5])
      elif chr(c) == 'h':
        delay += 0.005
      elif chr(c) == 'l':
        delay -= 0.005
        if delay < 0.0:
          delay = 0.00
      elif chr(c) == 'p':
        while screen.getch() == -1:
          pass
    run_already = True
  clean_up_and_exit(screen)