Files

74 lines
1.8 KiB
Python

from vectors import Vector2d, Vector3d, Vector4d
from matrix import Matrix3x3
from triangle import Triangle
class Engine:
def __init__(self, width, height):
self._width = width
self._height = height
@property
def width(self):
return self._width
@width.setter
def width(self, width):
raise ValueError("Not allowed")
class Mesh:
def __init__(self):
self.triangles = []
self.vertices = []
def load_obj_file(self, path):
lines = []
with open(path, 'r') as reader:
lines = reader.readlines()
for line in lines:
if line[0] == 'v':
tmp = line.split(' ')
x = float(tmp[1])
y = float(tmp[2])
z = float(tmp[3])
self.vertices.append(Vector3d(x, y, z))
if line[0] == 'f':
tmp = line.split(' ')
x = tmp[1].split('//')[0]
y = tmp[2].split('//')[0]
z = tmp[3].split('//')[0]
# Subtract one because the obj file isn't zero indexed.
row1 = self.vertices[int(x) - 1]
row2 = self.vertices[int(y) - 1]
row3 = self.vertices[int(z) - 1]
self.triangles.append(Triangle(Matrix3x3(row1, row2, row3)))
def load_csv(self, path):
values = []
x = 0
y = -2
with open(path, 'r') as reader:
values = reader.readlines()
for i in range(0, 60, 3):
tmp = values[i].split(',')
row1 = Vector3d(x, y, float(tmp[6]))
tmp = values[i + 1].split(',')
row2 = Vector3d(x + 0.5, y, float(tmp[6]))
tmp = values[i + 2].split(',')
row3 = Vector3d(x, y + 0.5, float(tmp[6]))
self.triangles.append(Triangle(Matrix3x3(row1, row2, row3)))
self.triangles.append(Triangle(Matrix3x3(row2, row3, Vector3d(x + 0.5, y + 0.5, float(tmp[6])))))#, row2, row3)))
x += 0.5
#y -= 0.5
def __getitem__(self, key):
return self.triangles[key]
def __len__(self):
return len(self.triangles)