-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtriangle_mesh.py
39 lines (35 loc) · 1.48 KB
/
triangle_mesh.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
import numpy as np
from ..utils.constants import *
from ..utils.vector3 import vec3
from ..geometry import Primitive, Triangle_Collider
# WORK IN PROGRESS.
# We need to implement a bounding volume hierarchy to make TriangleMesh collision efficient.
# Without a bounding volume hierarchy a model with 200 triangles takes around 3 minutes to be rendered
class TriangleMesh(Primitive):
def __init__(self,file_name, center, material, max_ray_depth,shadow = True):
super().__init__(center, material,max_ray_depth, shadow = shadow)
self.collider_list += []
vs = []
fs = []
with open(file_name, 'r') as f:
r = f.read()
r = r.split('\n')
for i in r:
i = i.split()
if not i:
continue
elif i[0] == 'v':
x = float(i[1])
y = float(i[2])
z = float(i[3])
vs.append(vec3(x, y, z))
elif i[0] == 'f':
f1 = int(i[1].split('/')[0]) - 1
f2 = int(i[2].split('/')[0]) - 1
f3 = int(i[3].split('/')[0]) - 1
fs.append([f1, f2, f3])
for i in fs:
p1 = vs[i[0]] + center
p2 = vs[i[1]] + center
p3 = vs[i[2]] + center
self.collider_list += [Triangle_Collider(assigned_primitive = self, p1 =p1, p2 = p2, p3 = p3)]