Update formats

This commit is contained in:
Thomas FORGIONE 2017-01-18 15:18:31 +01:00
parent 525ec741d9
commit 913583aeba
No known key found for this signature in database
GPG Key ID: 2A210FFC062E00C3
3 changed files with 63 additions and 2 deletions

View File

@ -2,9 +2,16 @@ from ..basemodel import TextModelParser, Exporter, Vertex, TexCoord, Normal, Fac
from ..mesh import Material, MeshPart from ..mesh import Material, MeshPart
def is_off(filename): def is_off(filename):
"""Checks that the file is a .off file
Only checks the extension of the file
:param filename: path to the file
"""
return filename[-4:] == '.off' return filename[-4:] == '.off'
class OFFParser(TextModelParser): class OFFParser(TextModelParser):
"""Parser that parses a .off file
"""
def __init__(self, up_conversion = None): def __init__(self, up_conversion = None):
super().__init__(up_conversion) super().__init__(up_conversion)
self.vertex_number = None self.vertex_number = None
@ -12,10 +19,13 @@ class OFFParser(TextModelParser):
self.edge_number = None self.edge_number = None
def parse_line(self, string): def parse_line(self, string):
"""Parses a line of .off file
:param string: the line to parse
"""
split = string.split() split = string.split()
if string == 'OFF': if string == '' or string == 'OFF':
pass pass
elif self.vertex_number is None: elif self.vertex_number is None:
# The first will be the header # The first will be the header
@ -30,11 +40,18 @@ class OFFParser(TextModelParser):
class OFFExporter(Exporter): class OFFExporter(Exporter):
"""Exporter to .off format
"""
def __init__(self, model): def __init__(self, model):
"""Creates an exporter from the model
:param model: Model to export
"""
super().__init__(model) super().__init__(model)
def __str__(self): def __str__(self):
"""Exports the model
"""
faces = sum(map(lambda x: x.faces, self.model.parts), []) faces = sum(map(lambda x: x.faces, self.model.parts), [])
string = "OFF\n{} {} {}".format(len(self.model.vertices), len(faces), 0) + '\n' string = "OFF\n{} {} {}".format(len(self.model.vertices), len(faces), 0) + '\n'

View File

@ -9,10 +9,19 @@ class UnkownTypeError(Exception):
self.message = message self.message = message
def is_ply(filename): def is_ply(filename):
"""Checks that the file is a .ply file
Only checks the extension of the file
:param filename: path to the file
"""
return filename[-4:] == '.ply' return filename[-4:] == '.ply'
# List won't work with this function # List won't work with this function
def _ply_type_size(type): def _ply_type_size(type):
"""Returns the size of a ply property
:param type: a string that is in a ply element
""""
if type == 'char' or type == 'uchar': if type == 'char' or type == 'uchar':
return 1 return 1
elif type == 'short' or type == 'ushort': elif type == 'short' or type == 'ushort':
@ -27,6 +36,10 @@ def _ply_type_size(type):
raise UnkownTypeError('Type ' + type + ' is unknown') raise UnkownTypeError('Type ' + type + ' is unknown')
def ply_type_size(type): def ply_type_size(type):
"""Returns the list containing the sizes of the elements
:param type: a string that is in a ply element
"""
split = type.split() split = type.split()
if len(split) == 1: if len(split) == 1:
@ -40,6 +53,12 @@ def ply_type_size(type):
def bytes_to_element(type, bytes, byteorder = 'little'): def bytes_to_element(type, bytes, byteorder = 'little'):
"""Returns a python object parsed from bytes
:param type: the type of the object to parse
:param bytes: the bytes to read
:param byteorder: little or big endian
"""
if type == 'char': if type == 'char':
return ord(struct.unpack('<b', bytes)[0]) return ord(struct.unpack('<b', bytes)[0])
if type == 'uchar': if type == 'uchar':
@ -60,6 +79,8 @@ def bytes_to_element(type, bytes, byteorder = 'little'):
raise UnkownTypeError('Type ' + type + ' is unknown') raise UnkownTypeError('Type ' + type + ' is unknown')
class PLYParser(ModelParser): class PLYParser(ModelParser):
"""Parser that parses a .ply file
"""
def __init__(self, up_conversion = None): def __init__(self, up_conversion = None):
super().__init__(up_conversion) super().__init__(up_conversion)
@ -71,6 +92,8 @@ class PLYParser(ModelParser):
self.header_finished = False self.header_finished = False
def parse_bytes(self, bytes, byte_counter): def parse_bytes(self, bytes, byte_counter):
"""Parses bytes of a .ply file
"""
if self.header_finished: if self.header_finished:
self.inner_parser.parse_bytes(self.beginning_of_line + bytes, byte_counter - len(self.beginning_of_line)) self.inner_parser.parse_bytes(self.beginning_of_line + bytes, byte_counter - len(self.beginning_of_line))
self.beginning_of_line = b'' self.beginning_of_line = b''
@ -92,6 +115,8 @@ class PLYParser(ModelParser):
self.beginning_of_line = current_line self.beginning_of_line = current_line
class PLYHeaderParser: class PLYHeaderParser:
"""Parser that parses the header of a .ply file
"""
def __init__(self, parent): def __init__(self, parent):
self.current_element = None self.current_element = None
self.parent = parent self.parent = parent

View File

@ -4,9 +4,16 @@ from ..mesh import MeshPart
import os.path import os.path
def is_stl(filename): def is_stl(filename):
"""Checks that the file is a .stl file
Only checks the extension of the file
:param filename: path to the file
"""
return filename[-4:] == '.stl' return filename[-4:] == '.stl'
class STLParser(TextModelParser): class STLParser(TextModelParser):
"""Parser that parses a .stl file
"""
def __init__(self, up_conversion = None): def __init__(self, up_conversion = None):
super().__init__(up_conversion) super().__init__(up_conversion)
@ -17,7 +24,10 @@ class STLParser(TextModelParser):
self.face_vertices = None self.face_vertices = None
def parse_line(self, string): def parse_line(self, string):
"""Parses a line of .stl file
:param string: the line to parse
"""
if string == '': if string == '':
return return
@ -66,10 +76,19 @@ class STLParser(TextModelParser):
class STLExporter(Exporter): class STLExporter(Exporter):
"""Exporter to .stl format
"""
def __init__(self, model): def __init__(self, model):
"""Creates an exporter from the model
:param model: Model to export
"""
super().__init__(model)
super().__init__(model) super().__init__(model)
def __str__(self): def __str__(self):
"""Exports the model
"""
string = 'solid {}\n'.format(os.path.basename(self.model.path[:-4])) string = 'solid {}\n'.format(os.path.basename(self.model.path[:-4]))
self.model.generate_face_normals() self.model.generate_face_normals()