AnalyseMorphologique/utils/files/file_data.py

83 lines
2.3 KiB
Python

"""
This module contains the File class.
"""
class FaceNotGiven(Exception):
"""
Exception raised when no faces was given.
"""
class Object:
"""
This class is used to manage the data of the 3D object.
"""
def __init__(self, vertices, faces=None):
self.vertices = vertices
self.faces = faces
self.x = [vertex[0] for vertex in vertices]
self.y = [vertex[1] for vertex in vertices]
self.z = [vertex[2] for vertex in vertices]
def get_x(self)->list:
"""
Get the x coordinates of the object.
return: x coordinates
"""
return self.x
def get_y(self)->list:
"""
Get the y coordinates of the object.
return: y coordinates
"""
return self.y
def get_z(self)->list:
"""
Get the z coordinates of the object.
return: z coordinates
"""
return self.z
def get_vertices(self, sort:bool = False):
"""
Get the vertices of the object.
:param sort: Sort the vertices by z coordinate
:return: vertices
"""
vertices = self.vertices if not sort else sorted(self.vertices, key=lambda vertex: vertex[2])
return vertices
def get_discrete_vertices(self, step:float = 0.1):
"""
Discretize the vertices of the object.
:param step: Step of the discretization
:return: Discretized vertices
"""
current_interval = int(min(self.get_z()))
splitted_data = [[]]
for line in self.get_vertices(sort=True):
# TODO check distance instead of equality
if line[2] >= current_interval:
splitted_data.append([])
current_interval += step
splitted_data[-1].append(line)
if splitted_data[0] == []:
splitted_data = splitted_data[1:]
return splitted_data
def get_faces(self)->list:
"""
Get the faces of the object.
:return: faces
"""
if self.faces is None:
raise FaceNotGiven('No faces was given')
return self.faces
def get_data(self)->dict:
"""
Get the data of the object.
:return: Data of the object
"""
return {'verticies': self.vertices, 'faces': self.faces, 'x': self.x, 'y': self.y, 'z': self.z}