-
Notifications
You must be signed in to change notification settings - Fork 4
/
world.py
79 lines (62 loc) · 2.99 KB
/
world.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- encoding: utf-8 -*-
#
# The MIT License (MIT)
#
# Copyright © 2021 Maurizio Tomasi
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of
# the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from typing import Union, List, Any
from geometry import Point
from hitrecord import HitRecord
from lights import PointLight
from ray import Ray
from shapes import Shape
class World:
"""A class holding a list of shapes, which make a «world»
You can add shapes to a world using :meth:`.World.add`. Typically, you call
:meth:`.World.ray_intersection` to check whether a light ray intersects any
of the shapes in the world.
"""
shapes: List[Shape]
point_lights: List[PointLight]
def __init__(self):
self.shapes = []
self.point_lights = []
def add_shape(self, shape: Shape):
"""Append a new shape to this world"""
self.shapes.append(shape)
def add_light(self, light: PointLight):
"""Append a new point light to this world"""
self.point_lights.append(light)
def ray_intersection(self, ray: Ray) -> Union[HitRecord, None]:
"""Determine whether a ray intersects any of the objects in this world"""
closest: Union[HitRecord, None] = None
for shape in self.shapes:
intersection = shape.ray_intersection(ray)
if not intersection:
# The ray missed this shape, skip to the next one
continue
if (not closest) or (intersection.t < closest.t):
# There was a hit, and it was closer than any other hit found before
closest = intersection
if closest:
closest.normal = closest.normal.normalize()
return closest
def is_point_visible(self, point: Point, observer_pos: Point):
direction = point - observer_pos
dir_norm = direction.norm()
ray = Ray(origin=observer_pos, dir=direction, tmin=1e-2 / dir_norm, tmax=1.0)
for shape in self.shapes:
if shape.quick_ray_intersection(ray):
return False
return True