2017-01-17 09:47:57 +00:00
|
|
|
from ..basemodel import TextModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
|
2016-12-21 11:16:49 +00:00
|
|
|
from ..mesh import Material, MeshPart
|
|
|
|
|
|
|
|
def is_off(filename):
|
2017-01-18 14:18:31 +00:00
|
|
|
"""Checks that the file is a .off file
|
|
|
|
|
|
|
|
Only checks the extension of the file
|
|
|
|
:param filename: path to the file
|
|
|
|
"""
|
2016-12-21 11:16:49 +00:00
|
|
|
return filename[-4:] == '.off'
|
|
|
|
|
2017-01-17 09:47:57 +00:00
|
|
|
class OFFParser(TextModelParser):
|
2017-01-18 14:18:31 +00:00
|
|
|
"""Parser that parses a .off file
|
|
|
|
"""
|
2016-12-21 11:16:49 +00:00
|
|
|
def __init__(self, up_conversion = None):
|
|
|
|
super().__init__(up_conversion)
|
|
|
|
self.vertex_number = None
|
|
|
|
self.face_number = None
|
|
|
|
self.edge_number = None
|
|
|
|
|
|
|
|
def parse_line(self, string):
|
2017-01-18 14:18:31 +00:00
|
|
|
"""Parses a line of .off file
|
2016-12-21 11:16:49 +00:00
|
|
|
|
2017-01-18 14:18:31 +00:00
|
|
|
:param string: the line to parse
|
|
|
|
"""
|
2016-12-21 11:16:49 +00:00
|
|
|
split = string.split()
|
|
|
|
|
2017-01-18 14:18:31 +00:00
|
|
|
if string == '' or string == 'OFF':
|
2016-12-21 11:16:49 +00:00
|
|
|
pass
|
|
|
|
elif self.vertex_number is None:
|
|
|
|
# The first will be the header
|
|
|
|
self.vertex_number = int(split[0])
|
|
|
|
self.face_number = int(split[1])
|
|
|
|
self.edge_number = int(split[2])
|
|
|
|
elif len(self.vertices) < self.vertex_number:
|
|
|
|
self.add_vertex(Vertex().from_array(split))
|
|
|
|
else:
|
|
|
|
self.add_face(Face(FaceVertex(int(split[1])), FaceVertex(int(split[2])), FaceVertex(int(split[3]))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OFFExporter(Exporter):
|
2017-01-18 14:18:31 +00:00
|
|
|
"""Exporter to .off format
|
|
|
|
"""
|
2016-12-21 11:16:49 +00:00
|
|
|
def __init__(self, model):
|
2017-01-18 14:18:31 +00:00
|
|
|
"""Creates an exporter from the model
|
|
|
|
|
|
|
|
:param model: Model to export
|
|
|
|
"""
|
2016-12-21 11:16:49 +00:00
|
|
|
super().__init__(model)
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-18 14:18:31 +00:00
|
|
|
"""Exports the model
|
|
|
|
"""
|
2016-12-21 11:16:49 +00:00
|
|
|
faces = sum(map(lambda x: x.faces, self.model.parts), [])
|
|
|
|
string = "OFF\n{} {} {}".format(len(self.model.vertices), len(faces), 0) + '\n'
|
|
|
|
|
|
|
|
for vertex in self.model.vertices:
|
|
|
|
string += ' '.join([str(vertex.x), str(vertex.y), str(vertex.z)]) + '\n'
|
|
|
|
|
|
|
|
for face in faces:
|
|
|
|
string += '3 ' + ' '.join([str(face.a.vertex), str(face.b.vertex), str(face.c.vertex)]) + '\n'
|
|
|
|
|
|
|
|
return string
|
|
|
|
|