-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplane.py
54 lines (43 loc) · 1.47 KB
/
plane.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""A ray-traceable Plane is a Plane through a given
point with a given normal and surface material.
It needs an intersect method that returns the
point of intersection of a given ray with the
plane and a normal method that returns the normal
at a given point (which is irrelevant for a plane
as the normal is the same everywhere)."""
from geom3 import Vector3, Point3, Ray3, dot, unit
from math import sqrt
from hit import Hit
class Plane(object):
"""A ray-traceable plane"""
def __init__(self, point, normal, material):
"""Create a plane through a given point with given normal
and surface material"""
self.point = point
self.norm = unit(normal)
self.mat = material
def intersect(self, ray):
"""Returns a hit, or None if the ray is parallel to the plane"""
t = None
hit = None
angle = ray.dir.dot(self.norm)
if angle != 0:
t = (self.point - ray.start).dot(self.norm)/angle
if angle < 0:
hit = Hit(self, ray, t, (), self.norm, self.mat)
else :
hit = Hit(self, ray, None, t, self.norm, self.mat)
else:
vector = unit(ray.start - self.point)
if vector.dot(self.norm) < 0:
hit = Hit(self, ray, None, (), self.norm, self.mat)
else:
return None
if self.mat.texture and hit.entry > 0:
hit.texCords = self.texCords(ray.pos(t))
return hit
def texCords(self, point):
vect = point - self.point
u = vect.dx
v = vect.dz
return (u, v)