-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
162 lines (153 loc) · 4.51 KB
/
data.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from utility import epsilon_equal
class Point(object):
'''
A class to define a point in space.
Attributes:
x --> float
y --> float
z --> float
'''
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __eq__(self, other):
return (epsilon_equal(self.x, other.x)
and epsilon_equal(self.y, other.y)
and epsilon_equal(self.z, other.z))
class Vector(object):
'''
A class to define a vector in space.
Attributes:
x --> float
y --> float
z --> float
'''
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __eq__(self, other):
return (epsilon_equal(self.x, other.x)
and epsilon_equal(self.y, other.y)
and epsilon_equal(self.z, other.z))
class Ray(object):
'''
A class to define a ray in space.
Attributes:
pt --> Point (object)
dir --> Vector (object)
'''
def __init__(self, pt, dir):
self.pt = pt
self.dir = dir
def __eq__(self, other):
return (epsilon_equal(self.pt.x, other.pt.x)
and epsilon_equal(self.pt.y, other.pt.y)
and epsilon_equal(self.pt.z, other.pt.z)
and epsilon_equal(self.dir.x, other.dir.x)
and epsilon_equal(self.dir.y, other.dir.y)
and epsilon_equal(self.dir.z, other.dir.z))
class Sphere(object):
'''
A class to define a sphere in space.
Attributes:
center --> Point (object)
radius --> float
color --> Color (object)
finish --> Finish (object)
'''
def __init__(self, center, radius, color, finish):
self.center = center
self.radius = radius
self.color = color
self.finish = finish
def __eq__(self, other):
return (epsilon_equal(self.center.x, other.center.x)
and epsilon_equal(self.center.y, other.center.y)
and epsilon_equal(self.center.z, other.center.z)
and epsilon_equal(self.radius, other.radius)
and epsilon_equal(self.color.r, other.color.r)
and epsilon_equal(self.color.g, other.color.g)
and epsilon_equal(self.color.b, other.color.b)
and epsilon_equal(self.finish.amb, other.finish.amb)
and epsilon_equal(self.finish.diff, other.finish.diff)
and epsilon_equal(self.finish.spec, other.finish.spec)
and epsilon_equal(self.finish.rough, other.finish.rough))
class Color(object):
'''
A class to define a Color.
Attributes:
r --> float
g --> float
b --> float
'''
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
def __eq__(self, other):
return (epsilon_equal(self.r, other.r)
and epsilon_equal(self.g, other.g)
and epsilon_equal(self.b, other.b))
class Finish(object):
'''
A class to define a finish to spheres
Attirbutes:
amb --> float
diff --> float
spec --> float
rough --> float
'''
def __init__(self, amb, diff, spec, rough):
self.amb = amb
self.diff = diff
self.spec = spec
self.rough = rough
def __eq__(self, other):
return (epsilon_equal(self.amb, other.amb)
and epsilon_equal(self.diff, other.diff)
and epsilon_equal(self.spec, other.spec)
and epsilon_equal(self.rough, other.rough))
class Light(object):
'''
A class to define a light source
Attributes:
pt --> Point (object)
color --> Color (object)
'''
def __init__(self, pt, color):
self.pt = pt
self.color = color
def __eq__(self, other):
return (epsilon_equal(self.pt.x, other.pt.x)
and epsilon_equal(self.pt.y, other.pt.y)
and epsilon_equal(self.pt.z, other.pt.z)
and epsilon_equal(self.color.r, other.color.r)
and epsilon_equal(self.color.g, other.color.g)
and epsilon_equal(self.color.b, other.color.b))
class View(object):
'''
A class to define a view of the 3D world
Attributes:
min_x --> float
max_x --> float
min_y --> float
max_y --> float
width --> float
height --> float
'''
def __init__(self, min_x, max_x, min_y, max_y, width, height):
self.min_x = min_x
self.max_x = max_x
self.min_y = min_y
self.max_y = max_y
self.width = width
self.height = height
def __eq__(self, other):
return (epsilon_equal(self.min_x, other.min_x)
and epsilon_equal(self.max_x, other.max_x)
and epsilon_equal(self.min_y, other.min_y)
and epsilon_equal(self.max_y, other.max_y)
and epsilon_equal(self.width, other.width)
and epsilon_equal(self.height, other.height))