This commit is contained in:
2019-04-01 16:02:59 +04:00
commit 36d7f037c3
48 changed files with 1933 additions and 0 deletions

0
tests/__init__.py Normal file
View File

View File

@@ -0,0 +1,30 @@
import numpy as np
from intersections import intersect_plane_ray
from plane import Plane
from ray import Ray
from vector import Vector
def test_intersections():
r = Ray(
Vector.fromargs(0, 0, 1),
Vector.fromargs(-1, -1, -1),
)
p = Plane(
Vector.fromargs(0, 0, 0),
Vector.fromargs(0, 0, 1),
)
assert intersect_plane_ray(p, r) == Vector.fromargs(-1, -1, 0)
np.random.seed(0)
for i in range(10):
p = Plane(Vector.random(), Vector.random())
r = Ray(Vector.random(), Vector.random())
intersection = intersect_plane_ray(p, r)
print(p, r, intersection)
assert p & intersection

37
tests/test_plane.py Normal file
View File

@@ -0,0 +1,37 @@
import numpy as np
from plane import Plane
from ray import Ray
from vector import Vector
def test_plane():
p = Plane(
Vector.fromargs(0, 0, 0),
Vector.fromargs(0, 0, 1),
)
assert (p & Vector.fromargs(0, 0, 0))
assert (p & Vector.fromargs(1, 1, 0))
assert not (p & Vector.fromargs(0, 0, -1))
p = Plane(
Vector.fromargs(1, 1),
Vector.fromargs(1, 1),
)
assert not (p & Vector.fromargs(0, 0))
assert (p & Vector.fromargs(2, 0))
def test_reflection():
p = Plane(
Vector.fromargs(0, 0),
Vector.fromargs(0, 1),
)
r = Ray(
Vector.fromargs(-1, 1),
Vector.fromargs(1, -1),
)
assert p.intersect(r) == np.sqrt(2)

22
tests/test_sphere.py Normal file
View File

@@ -0,0 +1,22 @@
import numpy as np
from ray import Ray
from sphere import Sphere
from vector import Vector
def test_intersect():
sphere = Sphere(
r0=Vector.fromargs(0, 0),
radius=1,
)
assert sphere.intersect(Ray(
r0=Vector.fromargs(-2, 0),
e=Vector.fromargs(1, 0),
)) == 1
assert sphere.intersect(Ray(
r0=Vector.fromargs(-1, -1),
e=Vector.fromargs(1, 1),
)) == np.sqrt(2) - 1

7
tests/test_vector.py Normal file
View File

@@ -0,0 +1,7 @@
from vector import Vector
def test_vector():
v = Vector.fromargs(1, 2)
print(v)
print(-v)