## 2019年5月30日木曜日

### Python - Cylinders - Capped, 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、Capped Cylindersを取り組んでみる。

コード

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)

def test_intersecting_constrained(self):
cylinder = Cylinder(minimum=1, maximum=2)
points = [(0, 1.5, 0),
(0, 3, -5),
(0, 0, -5),
(0, 2, -5),
(0, 1, -5),
(0, 1.5, -2)]
directions = [(0.1, 1, 0),
(0, 0, 1),
(0, 0, 1),
(0, 0, 1),
(0, 0, 1),
(0, 0, 1)]
counts = [0, 0, 0, 0, 0, 2]
for direction, point, count in zip(directions, points, counts):
direction = Vector(*direction)
direction = direction.normalize()
point = Point(*point)
ray = Ray(point, direction)
intersections = cylinder.intersect(ray)
self.assertEqual(len(intersections), count)

def test_intersecting_caps_closed(self):
cylinder = Cylinder(minimum=1, maximum=2, is_closed=True)
points = [(0, 3, 0),
(0, 3, -2),
(0, 4, -2),
(0, 0, -2),
(0, -1, -2)]
directions = [(0, -1, 0),
(0, -1, 2),
(0, -1, 1),
(0, 1, 2),
(0, 1, 1)]
for direction, point in zip(directions, points):
direction = Vector(*direction).normalize()
point = Point(*point)
ray = Ray(point, direction)
intersections = cylinder.intersect(ray)
self.assertEqual(len(intersections), 2)

def test_normal_vector_cylinder_caps(self):
cylinder = Cylinder(minimum=1, maximum=2, is_closed=True)
points = [(0, 1, 0),
(0.5, 1, 0),
(0, 1, 0.5),
(0, 2, 0),
(0.5, 2, 0),
(0, 2, 0.5)]
normals = [(0, -1, 0)] * 3 + [(0, 1, 0)] * 3
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 EPSILON, is_equal, Vector

class Cylinder(Shape):
def __init__(self, transform=None, material=None,
minimum=-math.inf, maximum=math.inf, is_closed=False):
super().__init__(transform=transform, material=material)
self.minimum = minimum
self.maximum = maximum
self.is_closed = is_closed

def intersect(self, ray):
ray = ray.transform(self.transform.inverse())
intersections = []
if self.is_closed and not is_equal(ray.direction.y, 0):
ts = [(self.minimum - ray.origin.y) / ray.direction.y,
(self.maximum - ray.origin.y) / ray.direction.y]
intersections = [Intersection(t, self) for t in ts
if check_cap(ray, t)]
a = ray.direction.x ** 2 + ray.direction.z ** 2
if is_equal(a, 0):
return Intersections(*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(*intersections)
t0 = (-b - math.sqrt(disc)) / (2 * a)
t1 = (-b + math.sqrt(disc)) / (2 * a)
intersections += [Intersection(t, self) for t in [t0, t1]
if self.minimum <
ray.origin.y + t * ray.direction.y <
self.maximum]
return Intersections(*intersections)

def normal_at(self, point):
point = self.transform.inverse() * point
dist = point.x ** 2 + point.z ** 2
if dist < 1 and point.y >= self.maximum - EPSILON:
return Vector(0, 1, 0)
if dist < 1 and point.y <= self.minimum + EPSILON:
return Vector(0, -1, 0)
return Vector(point.x, 0, point.z)

def check_cap(ray, t):
x = ray.origin.x + t * ray.direction.x
z = ray.origin.z + t * ray.direction.z
return (x ** 2 + z ** 2) <= 1

sample3.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(minimum=1, maximum=3,
transform=rotation_x(math.pi / 2),
material=Material(color=Color(1, 1, 0)),
is_closed=True)
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, cylinder],
Light(Point(-10, 10, -10), Color(1, 1, 1)))
start = time.time()
canvas = camera.render(world)
s = time.time() - start
with open(f'sample7.ppm', 'w') as f:
canvas.to_ppm(f)
print(f'sample7.ppm,{s}')

C:\Users\...>py cyliners_test.py
......
----------------------------------------------------------------------
Ran 6 tests in 0.016s

OK

C:\Users\...>py sample3.py
ファイル名, rendering time(秒)
sample7.ppm,212.1590027809143

C:\Users\...>