-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCSG.py
42 lines (35 loc) · 995 Bytes
/
CSG.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
from geom3 import Vector3, Point3, Ray3, dot, unit
from hit import Hit
class Intersection(object):
def __init__(self, objects):
self.objs = objects
def intersect(self, ray):
hit = Hit(self, ray, None, None)
for o in self.objs:
hit = hit.intersection(o.intersect(ray))
if hit == None or hit.miss():
if self.objs.index(o) != 0:
self.objs.remove(o)
self.objs = [o] + self.objs
return None
return hit
class Union(object):
def __init__(self, objects):
self.objs = objects
def intersect(self, ray):
hit = Hit(self, ray, None, None)
for o in self.objs:
hit = hit.union(o.intersect(ray))
if hit.entry < hit.exit:
return hit
return None
class Difference(object):
def __init__(self, objects):
self.objs = objects
def intersect(self, ray):
hit = self.objs[0].intersect(ray)
if hit is not None:
hit = hit.difference(self.objs[1].intersect(ray))
if hit and hit.entry < hit.exit:
return hit
return None