예제 #1
0
def main():

    # on crée une image à partir de la librairie Image
    image = Image.new("RGB", (W, H), "black")

    # on crée la scène
    lumiere = Lumiere(Vector(-10, -20, 50), 1000000000)

    origin = Vector(0, 0, 55)
    fov = 90 * 3.14 / 180
    direction = Vector(0, 0, 1).getNormalized
    up = Vector(0, 1, 0).getNormalized
    right = direction.cross(up) * (-1)
    camera = Camera(origin, fov, direction, up, right)

    materiau_opaque1 = Materiau([1, 0, 0], False, 0)  # rouge
    materiau_opaque2 = Materiau([0, 1, 0], False, 0)  # vert
    materiau_opaque3 = Materiau([0, 0, 1], False, 0)  # bleu
    materiau_opaque4 = Materiau([0, 1, 1], False, 0)  # cyan
    materiau_opaque5 = Materiau([1, 1, 1], False, 0)  # blanc
    materiau_opaque6 = Materiau([1, 1, 0], False, 0)  # jaune

    materiau_reflechissant = Materiau([1, 1, 1], True, 0)

    materiau_transparent = Materiau([1, 1, 1], False, 1.5)

    s1 = Sphere(Vector(0, -2, 25), 10, materiau_transparent)
    s2 = Sphere(Vector(0, 0, 1000), 940, materiau_opaque5)  # arrière
    s3 = Sphere(Vector(0, 0, -1000), 940, materiau_opaque5)  # devant
    s4 = Sphere(Vector(1000, 0, 0), 940, materiau_opaque3)  # droite
    s5 = Sphere(Vector(-1000, 0, 0), 940, materiau_opaque1)  # gauche
    s6 = Sphere(Vector(0, 1000, 0), 990, materiau_opaque6)  # dessous
    s7 = Sphere(Vector(0, -1000, 0), 940, materiau_opaque2)  # dessus

    scene = Scene([s1, s2, s3, s4, s5, s6, s7], lumiere, camera)

    # variable pour le multiprocessing
    out_q = Queue()
    imageProcess = []
    imageQuadrant = []

    # on lance des process pour chaque quadran
    for i in xrange(n_quadrants):
        imageProcess.append(Process(target=scene.getImage, args=(image, n_rebonds, i, out_q, n_echantillons, diffus)))
        imageProcess[i].start()

    for i in xrange(n_quadrants):
        imageQuadrant.append(out_q.get())

    for i in xrange(n_quadrants):
        imageProcess[i].join()

        # on reconstruit l'image depuis les quadrans
    image_haute = ImageChops.add(imageQuadrant[0], imageQuadrant[1])
    image_basse = ImageChops.add(imageQuadrant[2], imageQuadrant[3])
    image = ImageChops.add(image_haute, image_basse)

    image.show()
    image.save("image.jpg")
예제 #2
0
def p_vector(p):
    """
    VECTOR : VECTOR ',' TERM
           | TERM
    """
    if len(p) == 4:
        p[0] = p[1] + [p[3]]
    else:
        p[0] = Vector(values=[p[1]])
예제 #3
0
from random import randint, seed
from perceptron import PerceptronTrain, PerceptronTest
from structures import Vector, Scalar, Dataset
from utils import train_test_split, eval_predictions

seed(42)

v = Vector(randint(-100, 100), randint(-100, 100))
xs = [Vector(randint(-100, 100), randint(-100, 100)) for i in range(500)]
ys = [v * x * Scalar(randint(-1, 9)) for x in xs]
# generate vector-label pairs
data = [(vector, label) for vector, label in zip(xs, ys)]
# split data
train, test = train_test_split(data)
# retrieve labels only from a test set
y_true = [entry[1].sign() for entry in test]

# train
weights, bias = PerceptronTrain(train)
# apply perceptron to classify test set
preds = PerceptronTest(weights, bias, test)

# evaluate the performance
score = eval_predictions(preds, y_true)

print(score)
예제 #4
0
"""
One can add two vector to go diagonal:
() ----------------> Horiz
 \ \
 \   \
 \     \
 \       \
 \         \
 \           \
 \|            \
 Vert         Diag

 If one need Diagonal vector, he could add Horiz + Vert.
"""
from structures import Vector, Point


if __name__ == '__main__':
    horizontal = Vector(4,0)
    vertical = Vector(0, -5)
    diagonal = horizontal.add(vertical)

    print("New velocity is: ", diagonal)
예제 #5
0
from random import randint, seed
from perceptron import PerceptronTrain, PerceptronTest
from structures import Vector, Scalar, Dataset
from utils import train_test_split
seed(42)

scores = []
# generate data
for i in range(10000):
    v = Vector(randint(-100, 100), randint(-100, 100))
    xs = [Vector(randint(-100, 100), randint(-100, 100)) for i in range(500)]
    ys = [v * x * Scalar(randint(-1, 9)) for x in xs]
    # generate vector-label pairs
    data = [(vector, label) for vector, label in zip(xs, ys)]
    # split data
    train, test = train_test_split(data)
    weights, bias = PerceptronTrain(train)

    scores.append(
        ((v*weights) / (v.magnitude()*weights.magnitude())).val
    )

print(
    "average (v*w)/Scalar(v.magnitude() * w.magnitude()) is %f" % (sum(scores) / len(scores))
)
예제 #6
0
"""
Dot product could be use to determine, is 2 object watch in a same or different director
1  -> same direction
0  -> 90 degree
-1 -> opposite direction
"""
from structures import Vector, Point

if __name__ == '__main__':
    # Normally view vector is unit length vector, for fun of calculation let's use this one
    blu_player_view_vector = Vector(3, 0)
    red_player_view_vector = Vector(-6, 0)
    print("Dot product of the vectors is: ", blu_player_view_vector.dot_product(red_player_view_vector))
예제 #7
0
"""
Div and mult could be used for increasing or decreasing speed
"""
from structures import Vector, Point


if __name__ == '__main__':
    v = Vector(3, 4)
    print("Initial speed: ", v.length())

    vector_doubled = v.mult(2)
    print("Doubled speed: ", vector_doubled.length())

    vector_halved = v.div(2)
    print("Halved speed: ", vector_halved.length())
예제 #8
0
from structures import Scalar, Vector

x = Scalar(4.0)
y = Scalar(2.0)
a = Vector(1.0, 2.0, 3.0)
b = Vector(3.0, 4.0, 5.0)
# __add__
print(x+y)
# __sub__
print(x - y)
# __mul__ Scalar
print(x * y)
# __mul__ Vec
print(y * a)


# __truediv__
print(x / y)
# __rtruediv__
print(y.__rtruediv__(b))


# sign
print(x.sign())
print(Scalar(-7).sign())
print(Scalar(0).sign())


# Vector + Vector
print(a + b)
# magnitute