Exemple #1
0
def show_shape(vertex_set): 
    """  Demonstrate the 3D animated transformations of shapes composed of vertices and edges. 
    """ 
    cycle_period= 100 
    rad_one_deg = math.pi/180.0 
    rad_angle = 0.0 

    for i in range(len(faces_tk)): 
       for j in range(len(faces_tk[i])): 
           faces_tk[i][j] = faces_tk[i][j] * matrix_transforms.T_scaling(50 , 50, 50) 

    # Animation by rotation around 3 axes. 
    for i in range (500): 
        
        # Faces. 
        for i in range(len(faces_tk)): 
            faces_tk[i] = faces_tk[i] * matrix_transforms.T_roty(-rad_angle) 
            faces_tk[i] = faces_tk[i] * matrix_transforms.T_rotx(rad_angle*0.2) 
            faces_tk[i] = faces_tk[i] * matrix_transforms.T_rotz(rad_angle*1.3) 
            view_faces =faces_tk[i] * matrix_transforms.T_translate( 300.0,  300.0, 0.0) 
            fourd_shape_2_twod_line(view_faces , '#ffff00', 1)   
            
        rad_angle = rad_one_deg * 1.0 
        canvas_1.update()                    # This refreshes the drawing on the canvas. 
        canvas_1.after(cycle_period)   # This makes execution pause for 100 milliseconds. 
        canvas_1.delete('lines_1') 
Exemple #2
0
def transform_display(object_vertices, rad_angle, kula):
    """ Transform and then Display. 
            There are two very distinct and separate operations here: 
            A) Rotate the object around the X, Y and Z axes. This alters the position of every vertex permanently. 
            B) Translate and scale each vertex temporarily for viewing. These operations are performed on a separate 
               copy of the rotated object. After each view the copy has no worth - it will be overwritten when the 
               next view is created. 
        """
    #1: Strips: Rotate Assemblage. Any other transforms may be applied to the vertices here. Their effect will be permanent.
    #   For this example only rotation by a common angle is considered.
    for h in range(len(object_vertices)):
        object_vertices[h] = object_vertices[h] * matrix_transforms.T_rotx(
            rad_angle)  # Rotate around x.
        object_vertices[h] = object_vertices[h] * matrix_transforms.T_roty(
            rad_angle)  # Rotate around y.
        object_vertices[h] = object_vertices[h] * matrix_transforms.T_rotz(
            rad_angle)  # Rotate around z.

    perspective_vertices = copy.deepcopy(
        object_vertices)  # Preserve the original and view an independent copy.

    #2: Apply perspective transforms in Z directions - single-vanishing point perspective.
    for h in range(len(object_vertices)):
        for i in range(len(object_vertices[h])):
            perspective_vertices[h][i] = perspective_vertices[h][
                i] * matrix_transforms.T_Z_perspective(
                    perspective_vertices[h][i], f)

    #3: Position and size the strip-grid for convenient viewing.
    view_perspective_vertices = []
    for h in range(len(perspective_vertices)):
        one_component = scale_shift_3d_object(perspective_vertices[h], scale_y,
                                              scale_z, x_shift, y_shift)
        view_perspective_vertices.append(one_component)

    # 4: Display perspective projections.
    for h in range(len(object_vertices)):
        display_matrix_object(view_perspective_vertices[h], kula)
        new_vertex = [new_x, new_y, new_z, new_w]
        homogenous_4d_array.append(new_vertex)
    homogenous_4d_mat = numpy.matrix(homogenous_4d_array)
    return homogenous_4d_mat


#=======================================================================
# CONVERT 2D SHAPES TO 4D MATRICES (NUMPY FORM)
fish_2d24d_mat = twod_shape_2_homogeneous_matrix(tiny_fish)
# Scale the tiny fish up into a big fish
bigfish_mat = fish_2d24d_mat * matrix_transforms.T_scaling(150.0, 200.0, 0.0)

# Dynamic rotation of fish_1 around axes within the shape
bigfish_view = bigfish_mat * matrix_transforms.T_translate(
    300.0, 300.0, 0.0)  # Viewing convenience.
fourd_shape_2_twod_line(bigfish_view, 'yellow', 4)

for i in range(160):
    bigfish_mat = bigfish_mat * matrix_transforms.T_rotx(
        rad_angle)  # Rotation about X axis.
    bigfish_mat = bigfish_mat * matrix_transforms.T_roty(
        rad_angle)  # Rotation about Y axis.
    bigfish_mat = bigfish_mat * matrix_transforms.T_rotz(
        rad_angle)  # Rotation about Z axis.
    bigfish_view = bigfish_mat * matrix_transforms.T_translate(
        300.0, 300.0, 0.0)  # Viewing convenience.
    fourd_shape_2_twod_line(bigfish_view, 'green', 1)
    rad_angle = rad_one_deg * 5.0

root.mainloop()
#========================================================================
# Convert 2D set SET to 4D matrices (NUMPY FORM).
fish_4d_set_mat = []
for set in range(len(fish_set)):
    temp = twod_shape_2_homogeneous_matrix(fish_set[set])
    fish_4d_set_mat.append(temp)

# Move origin to center-of-rotation(cor).
for set in range(len(fish_set)):
    fish_4d_set_mat[
        set] = fish_4d_set_mat[set] * matrix_transforms.T_translate(
            -300.0, -330.0, 0.0)

# Dynamic rotation of fish_1 around axes within the shape
for i in range(100):
    for set in range(len(fish_4d_set_mat)):
        fish_4d_set_mat[set] = fish_4d_set_mat[set] * matrix_transforms.T_rotx(
            rad_angle)  #  X rotation.
        fish_4d_set_mat[set] = fish_4d_set_mat[set] * matrix_transforms.T_roty(
            rad_angle)  #  Y rotation.
        fish_4d_set_mat[set] = fish_4d_set_mat[set] * matrix_transforms.T_rotz(
            rad_angle)  #  Z rotation.
        view_fish_set = fish_4d_set_mat[set] * matrix_transforms.T_translate(
            300.0, 280.0, 0.0)
        fourd_shape_2_twod_line(view_fish_set, kulas[set], 1)

    rad_angle = rad_one_deg * 10.0

root.mainloop()