예제 #1
0
 def calcLightPos(light):
     return tuple(
         matrix.transform3(
             matrix.rotx(-objrot[0]) *
             matrix.roty(-objrot[1]) *
             matrix.rotz(-objrot[2]),
             light.position))
예제 #2
0
    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
예제 #3
0
    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
예제 #4
0
 def calcLightPos(light):
     return tuple(
         matrix.transform3(
             matrix.rotx(-objrot[0]) *
             matrix.roty(-objrot[1]) *
             matrix.rotz(-objrot[2]),
             light.position))
예제 #5
0
 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
예제 #6
0
 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
예제 #7
0
파일: view.py 프로젝트: peterbat/rubikon
 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
예제 #8
0
 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()
예제 #9
0
  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 = ' '))