## 2019年5月27日月曜日

### Python - Cylinders - Intersections, Normal Vector

The Ray Tracer Challenge: A Test-Driven Guide to Your First 3D Renderer (Jamis Buck(著)、Pragmatic Bookshelf)、Chapter 13(Cylinders)のIntersecting a Ray with a Cylinder、Finding the Normal on a Cylinderを取り組んでみる。

コード

cylinders_test.py

```#!/usr/bin/env python3
from unittest import TestCase, main
from cylinders import Cylinder
from tuples import is_equal, Point, Vector
from rays import Ray

class CylinderTest(TestCase):
def setUp(self):
pass

def tearDown(self):
pass

def test_ray_misses(self):
cylinder = Cylinder()
origins = [(1, 0, 0),
(0, 0, 0),
(0, 0, -5)]
directions = [(0, 1, 0),
(0, 1, 0),
(1, 1, 1)]
for origin, direction in zip(origins, directions):
origin = Point(*origin)
direction = Vector(*direction).normalize()
ray = Ray(origin, direction)
intersections = cylinder.intersect(ray)
self.assertEqual(len(intersections), 0)

def test_ray_strikes(self):
cylinder = Cylinder()
origins = [(1, 0, -5),
(0, 0, -5),
(0.5, 0, -5)]
directions = [(0, 0, 1),
(0, 0, 1),
(0.1, 1, 1)]
ts0 = [5, 4, 6.80798]
ts1 = [5, 6, 7.08872]
for origin, direction, t0, t1 in zip(origins, directions, ts0, ts1):
origin = Point(*origin)
direction = Vector(*direction).normalize()
ray = Ray(origin, direction)
intersections = cylinder.intersect(ray)
self.assertEqual(len(intersections), 2)
self.assertTrue(is_equal(intersections[0].t, t0))
self.assertTrue(is_equal(intersections[1].t, t1))

def test_nomral_vector(self):
cylinder = Cylinder()
points = [(1, 0, 0),
(0, 5, -1),
(0, -2, 1),
(-1, 1, 0)]
normals = [(1, 0, 0),
(0, 0, -1),
(0, 0, 1),
(-1, 0, 0)]
for point, normal in zip(points, normals):
point = Point(*point)
normal = Vector(*normal)
n = cylinder.normal_at(point)
self.assertEqual(n, normal)

if __name__ == '__main__':
main()
```

cylinders.py

```import math
from shapes import Shape
from intersections import Intersection, Intersections
from tuples import is_equal, Vector

class Cylinder(Shape):
def intersect(self, ray):
ray = ray.transform(self.transform.inverse())
a = ray.direction.x ** 2 + ray.direction.z ** 2
if is_equal(a, 0):
return Intersections()
b = 2 * ray.origin.x * ray.direction.x + \
2 * ray.origin.z * ray.direction.z
c = ray.origin.x ** 2 + ray.origin.z ** 2 - 1
disc = b ** 2 - 4 * a * c
if disc < 0:
return Intersections()
t0 = (-b - math.sqrt(disc)) / (2 * a)
t1 = (-b + math.sqrt(disc)) / (2 * a)
return Intersections(*[Intersection(t, self) for t in [t0, t1]])

def normal_at(self, point):
point = self.transform.inverse() * point
return Vector(point.x, 0, point.z)
```

sample1.py

```#!/usr/bin/env python3
import math
import time
from tuples import Point, Vector, Color
from planes import Plane
from cylinders import Cylinder
from materials import Material
from camera import Camera
from lights import Light
from world import World
from transformations import translation, view_transform
from transformations import rotation_x, rotation_y, rotation_z
print('ファイル名, rendering time(秒)')

width = 250
height = 125

wall1 = Plane(material=Material(color=Color(0, 0, 1)),
transform=translation(0, 0, 7) *
rotation_y(-math.pi / 4) *
rotation_x(math.pi / 2))
wall2 = Plane(material=Material(color=Color(1, 0, 0)),
transform=translation(0, 0, 7) *
rotation_y(math.pi / 4) *
rotation_x(math.pi / 2))

floor = Plane(material=Material(Color(0, 1, 0)),
transform=translation(0, -1, 0))

cylinder = Cylinder(material=Material(color=Color(1, 1, 0)))
cylinder1 = Cylinder(transform=rotation_x(math.pi / 2),
material=Material(color=Color(1, 1, 0)))
cylinder2 = Cylinder(transform=rotation_z(math.pi / 2),
material=Material(color=Color(1, 1, 0)))
cylinders = [cylinder, cylinder1, cylinder2]
camera = Camera(width, height, math.pi / 2,
transform=view_transform(Point(0, 1.5, -5), Point(0, 1, 0),
Vector(0, 1, 0)))
world = World([floor, wall1, wall2],
Light(Point(-10, 10, -10), Color(1, 1, 1)))
for i, cyl in enumerate(cylinders, 1):
world.objs.append(cyl)
start = time.time()
canvas = camera.render(world)
s = time.time() - start
with open(f'sample{i}.ppm', 'w') as f:
canvas.to_ppm(f)
print(f'sample{i}.ppm,{s}')
world.objs.remove(cyl)
```

```C:\Users\...>py cyliners_test.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.006s

OK

C:\Users\...>py sample1.py
ファイル名, rendering time(秒)
sample1.ppm,215.65656661987305
sample2.ppm,207.1544578075409
sample3.ppm,202.39941310882568

C:\Users\...>
```