コード例 #1
0
from teapot import load_triangles
from draw_model import draw_model
from math import pi


def polygon_map(transformation, polygons):
    return [[transformation(vertex) for vertex in triangle]
            for triangle in polygons]


def slant_xy(vector):
    x, y, z = vector
    return (x + y, y, z)


####################################################################
#### this code takes a snapshot to reproduce the exact figure
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera
if '--snapshot' in sys.argv:
    camera.default_camera = camera.Camera('fig_4.16_slant_teapot', [0])
####################################################################

draw_model(polygon_map(slant_xy, load_triangles()))
コード例 #2
0
from teapot import load_triangles
from draw_model import draw_model

draw_model(load_triangles())
コード例 #3
0
from draw_model import *
from teapot import load_triangles

teapot = load_triangles()


def matrix(t):
    return ((2, 1, 1), (1, 2, 1), (1, 1, 2))


draw_model(teapot, get_matrix=matrix)
コード例 #4
0
from teapot import load_triangles
from draw_model import draw_model
from camera import Camera
from transforms import *
from math import pi
draw_model(polygon_map(rotate_y_by(pi / 2), load_triangles()),
           camera=Camera('rotate-teapot-y', [0]))
コード例 #5
0
from vectors import scale
from teapot import load_triangles
from draw_model import draw_model


def polygon_map(transformation, polygons):
    return [[transformation(vertex) for vertex in triangle]
            for triangle in polygons]


def scale_by(scalar):
    def new_function(v):
        return scale(scalar, v)

    return new_function


####################################################################
#### this code takes a snapshot to reproduce the exact figure
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera
if '--snapshot' in sys.argv:
    camera.default_camera = camera.Camera('ex_scale_teapot_-1', [0])
####################################################################

draw_model(polygon_map(scale_by(-1), load_triangles()))
コード例 #6
0
from teapot import load_triangles
from draw_model import draw_model
from transforms import polygon_map, multiply_matrix_vector


def translate_3d(translation):
    def new_function(target):  #1
        a, b, c = translation
        x, y, z = target
        matrix = ((1, 0, 0, a), (0, 1, 0, b), (0, 0, 1, c), (0, 0, 0, 1))  #2
        vector = (x, y, z, 1)
        x_out, y_out, z_out, _ = multiply_matrix_vector(matrix, vector)  #3
        return (x_out, y_out, z_out)

    return new_function


####################################################################
#### this code takes a snapshot to reproduce the exact figure
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera
if '--snapshot' in sys.argv:
    camera.default_camera = camera.Camera('fig_5.36_translated_teapot', [0])
####################################################################

draw_model(polygon_map(translate_3d((2, 2, -3)), load_triangles()))
コード例 #7
0
def draw_teapot():
    draw_model(load_triangles())
コード例 #8
0
from teapot import load_triangles
from draw_model import draw_model
from math import pi


def polygon_map(transformation, polygons):
    return [[transformation(vertex) for vertex in triangle]
            for triangle in polygons]


def stretch_y(vector):
    x, y, z = vector
    return (x, 4. * y, z)


####################################################################
#### this code takes a snapshot to reproduce the exact figure
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera
if '--snapshot' in sys.argv:
    camera.default_camera = camera.Camera('fig_4.14_stretch_teapot_y', [0])
####################################################################

draw_model(polygon_map(stretch_y, load_triangles()))
コード例 #9
0
from vectors import scale
from teapot import load_triangles
from draw_model import draw_model
from transforms import rotate_x_by, compose
from math import pi

def polygon_map(transformation, polygons):
    return [
        [transformation(vertex) for vertex in triangle]
        for triangle in polygons
    ]

def scale_by(scalar):
    def new_function(v):
        return scale(scalar, v)
    return new_function

####################################################################
#### this code takes a snapshot to reproduce the exact figure 
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera
for i in range(10):
    if '--snapshot' in sys.argv:
        camera.default_camera = camera.Camera('fig_4_UN_04_%s' % i,[0])
    ####################################################################

    draw_model(polygon_map(compose(rotate_x_by(2*pi*i/10),scale_by(-1)), load_triangles()))
コード例 #10
0
        for triangle in polygons
    ]

def rotate2d(angle, vector):
    l,a = to_polar(vector)
    return to_cartesian((l, a+angle))

def rotate_z(angle, vector):
    x,y,z = vector
    new_x, new_y = rotate2d(angle, (x,y))
    return new_x, new_y, z

def rotate_z_by(angle):
    def new_function(v):
        return rotate_z(angle,v)
    return new_function

####################################################################
#### this code takes a snapshot to reproduce the exact figure 
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera

for d in range(1,46):
    if '--snapshot' in sys.argv:
        camera.default_camera = camera.Camera('fig_4.3_%s' % d,[0])
    ####################################################################

    draw_model(polygon_map(rotate_z_by(d * pi/180), load_triangles()))
コード例 #11
0
from teapot import load_triangles
from matrices import *
from draw_model import *
from camera import Camera
from transforms import polygon_map


def translate_3d(translation):
    def new_function(target):
        a, b, c = translation
        x, y, z = target
        matrix = ((1, 0, 0, a), (0, 1, 0, b), (0, 0, 1, c), (0, 0, 0, 1))
        vector = (x, y, z, 1)
        x_out, y_out, z_out, _ = multiply_matrix_vector(matrix, vector)
        return (x_out, y_out, z_out)

    return new_function


draw_model(load_triangles(), camera=Camera("translate-teapot-pre", [0]))
draw_model(polygon_map(translate_3d((2, 2, -3)), load_triangles()),
           camera=Camera("translate-teapot-post", [0]))
コード例 #12
0
from teapot import load_triangles
from matrices import *
from draw_model import *
from camera import Camera
from transforms import polygon_map
from vector_drawing import *


def transform(v):
    m = ((2, 1, 1), (1, 2, 1), (1, 1, 2))
    return multiply_matrix_vector(m, v)


draw_model(polygon_map(transform, load_triangles()),
           camera=Camera("teapot-matrix-exercise", [0]))

s = Space((-1, -1, -1), (3, 3, 3), "stretched-squeezed-standard-basis")
s.draw_axes()
s.draw_arrow((0, 0, 0), (1, 0, 0), color='black')
s.draw_arrow((0, 0, 0), (0, 1, 0), color='black')
s.draw_arrow((0, 0, 0), (0, 0, 1), color='black')
s.draw_arrow((0, 0, 0), (2, 1, 1), color='blue')
s.draw_arrow((0, 0, 0), (1, 2, 1), color='blue')
s.draw_arrow((0, 0, 0), (1, 1, 2), color='blue')
s.show()
コード例 #13
0
    x,y,z = vector
    return (x, y, 4*z)

def stretch_x(scalar,vector):
    x,y,z = vector
    return (scalar*x, y, z)

def stretch_x_by(scalar):
    def new_function(vector):
        return stretch_x(scalar,vector)
    return new_function
    


t4 = compose(translate_by((-1,-3,-2)), compose(rotate_x_by(3*pi/2), stretch_z))
draw_model(polygon_map(t4, load_triangles()), camera=Camera("stretch_z_rot_trans", [0]))


def cube_stretch_z(vector):
    x,y,z = vector
    return (x, y, 2*z*z*z)


t5 = compose(translate_by((-1,-2,-2)), compose(rotate_x_by(3*pi/2), cube_stretch_z))
draw_model(polygon_map(t5, load_triangles()), camera=Camera("cube_stretch_z_rot_trans", [0]))

def slant_xz(vector):
    x,y,z = vector
    return (x+z, y, z)

t6 = compose(translate_by((-1,-1,0)), compose(rotate_x_by(3*pi/2), slant_xz))
コード例 #14
0
from teapot import load_triangles
from draw_model import draw_model
from camera import Camera
from transforms import *
from math import *
from vectors import add
import os

# draw_model(polygon_map(rotate_z_by(pi/4.), load_triangles()), camera=Camera("teapot_45deg_z", [0]))
# draw_model(polygon_map(rotate_x_by(3*pi/2.), load_triangles()), camera=Camera("teapot_270deg_x", [0]))
#

Ae1 = (1, 1, 1)
Ae2 = (1, 0, -1)
Ae3 = (0, 1, 1)


def apply_A(v):
    return add(scale(v[0], Ae1), scale(v[1], Ae2), scale(v[2], Ae3))


draw_model(polygon_map(apply_A, load_triangles()),
           camera=Camera('teapot-linear-transform', [0]))
コード例 #15
0
from teapot import load_triangles
from draw_model import draw_model


def to_4d_vector(v):
    return (*v, 1)


def translate_matrix(t):
    time = t / 10000
    return (
        (1, 0, 0, min(2, 2 * time)),
        (0, 1, 0, min(2, 2 * time)),
        (0, 0, 1, max(-3, -3 * time)),
        (0, 0, 0, 1),
    )


teapot = [(to_4d_vector(t[0]), to_4d_vector(t[1]), to_4d_vector(t[2]))
          for t in load_triangles()]

draw_model(teapot, get_matrix=translate_matrix)
コード例 #16
0
from teapot import load_triangles
from draw_model_custom_impl import draw_model_anim
from transformations_support import *
from vectors_my_implementation import to_radians


# Basic rendering of Utah teapot, no transformations
#draw_model_anim(load_triangles())

# Rendering of Utah teapot, using non-curried transformation
# orig_triangles = load_triangles()
# scaled_triangles = [
#   [scale(0.5, v) for v in triangle]
#   for triangle in orig_triangles
# ]
# draw_model_anim(scaled_triangles)

# Rendering using currying and polygon_map
# draw_model_anim(polygon_map(translate_by((-1, -1, -1)), load_triangles()))

# Rendering using composition
draw_model_anim(polygon_map(compose(rotate_z_by(to_radians(45)), rotate_y_by(to_radians(45)), rotate_x_by(to_radians(45))), load_triangles()))
コード例 #17
0
    return [[transformation(vertex) for vertex in triangle]
            for triangle in polygons]


def scale2(v):
    return scale(2.0, v)


def translate1left(v):
    return add((-1, 0, 0), v)


def compose(f1, f2):
    def new_function(input):
        return f1(f2(input))

    return new_function


####################################################################
#### this code takes a snapshot to reproduce the exact figure
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera
if '--snapshot' in sys.argv:
    camera.default_camera = camera.Camera('ex_translate_scale', [0])
####################################################################

draw_model(polygon_map(compose(scale2, translate1left), load_triangles()))
コード例 #18
0
from teapot import load_triangles
from draw_model import draw_model
from camera import Camera
from transforms import *
from math import *
from vectors import add


def get_rotation_matrix(t):
    return ((cos(t), 0, -sin(t)), (0, 1, 0), (sin(t), 0, cos(t)))


def get_rotation_matrix(t):
    theta = t / 1000
    return ((cos(theta), 0, -sin(theta)), (0, 1, 0), (sin(theta), 0,
                                                      cos(theta)))


cam = Camera('parametrize_rotation', [1000, 2000, 3000, 4000, 5000],
             comic_strip=5)
draw_model(load_triangles(), get_matrix=get_rotation_matrix, camera=cam)
コード例 #19
0
from math import pi
from teapot import load_triangles
from draw_model import draw_model
from transforms import *

triangles = load_triangles()

f = compose(rotate_x_by(pi / 2), rotate_z_by(pi / 2))
triangles = polygon_map(f, triangles)

draw_model(triangles)
コード例 #20
0
from vectors import scale
from teapot import load_triangles
from draw_model import draw_model

def scale2(v):
    return scale(2.0, v)

original_triangles = load_triangles() #1
scaled_triangles = [
    [scale2(vertex) for vertex in triangle] #21
    for triangle in original_triangles #32
]

####################################################################
#### this code takes a snapshot to reproduce the exact figure 
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera
if '--snapshot' in sys.argv:
    camera.default_camera = camera.Camera('fig_4.5_scale_teapot',[0])
####################################################################

draw_model(scaled_triangles)
コード例 #21
0
def rotate2d(angle, vector):
    l, a = to_polar(vector)
    return to_cartesian((l, a + angle))


def rotate_z(angle, vector):
    x, y, z = vector
    new_x, new_y = rotate2d(angle, (x, y))
    return new_x, new_y, z


def rotate_z_by(angle):
    def new_function(v):
        return rotate_z(angle, v)

    return new_function


####################################################################
#### this code takes a snapshot to reproduce the exact figure
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera
if '--snapshot' in sys.argv:
    camera.default_camera = camera.Camera('fig_4.11_rotate_teapot', [0])
####################################################################

draw_model(polygon_map(rotate_z_by(pi / 4.), load_triangles()))
コード例 #22
0
from vectors import scale, add
from teapot import load_triangles
from draw_model import draw_model

def polygon_map(transformation, polygons):
    return [
        [transformation(vertex) for vertex in triangle]
        for triangle in polygons
    ]

def translate_by(translation):
    def new_function(v):
        return add(translation,v)
    return new_function

####################################################################
#### this code takes a snapshot to reproduce the exact figure 
#### shown in the book as an image saved in the "figs" directory
#### to run it, run this script with command line arg --snapshot
import sys
import camera
if '--snapshot' in sys.argv:
    camera.default_camera = camera.Camera('ex_translate_teapot_down_z',[0])
####################################################################

draw_model(polygon_map(translate_by((0,0,-20)), load_triangles()))
コード例 #23
0
from teapot import load_triangles
from draw_model import draw_model
from camera import Camera
from transforms import *
from math import *
from vectors import add
import os

# draw_model(polygon_map(rotate_z_by(pi/4.), load_triangles()), camera=Camera("teapot_45deg_z", [0]))
# draw_model(polygon_map(rotate_x_by(3*pi/2.), load_triangles()), camera=Camera("teapot_270deg_x", [0]))
#

camera = Camera('teapot-rotation-chapter-intro',
                range(0, 45),
                comic_strip=9,
                dir=os.path.join('figs', 'chapter-intro-teapot'))


def t(ticks):
    rate = 1. / 100.  # 1 degree per 100 ms
    return compose(
        rotate_z_by(ticks * .5 * rate * pi / 180.),
        compose(translate_by((-0.5, -0.5, 3)), rotate_x_by(3 * pi / 2)))


draw_model(load_triangles(), camera=camera, transform=t)