Skip to content
Snippets Groups Projects
Commit b5186aab authored by Djalim Simaila's avatar Djalim Simaila
Browse files

added the small files i made

parent 8d536efc
No related branches found
No related tags found
1 merge request!1Functionnal main functionnalities
def main():
pass
if __name__ == '__main__':
main()
\ No newline at end of file
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
import numpy as np
def render3D(data:dict):
"""
Render a 3D model using matplotlib poly3dcollection
:param data: A dict with the vertices and faces
"""
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
faces = np.array(data['faces'])
verts = np.array(list(zip(data['x'], data['y'], data['z'])))
mesh = Poly3DCollection(verts[faces], alpha=0.25, edgecolor='none')
ax.add_collection3d(mesh)
plt.show()
\ No newline at end of file
def denormalizeXYZ(filePath:str, output:str):
"""
Denormalize an XYZ file
:param filePath: Path to the XYZ file
:param output: Path to the output file
"""
with open(filePath, 'r') as f:
data = f.readlines()
x = [float(line.split()[0]) for line in data]
y = [float(line.split()[1]) for line in data]
z = [float(line.split()[2]) for line in data]
with open(output, 'w') as f:
for i in range(len(x)):
f.write(f'{x[i]} {y[i]} {z[i]}\n')
\ No newline at end of file
def parseOBJfiles(filePath:str,ratio:float = 1,cornered:bool = False)->dict:
"""
Parse an OBJ file and return a dict with the vertices and faces
:param filePath: Path to the OBJ file
:param ratio: Ratio to apply to the vertices
:param cornered: If True, the vertices will be cornered
:return: A dict with the vertices and faces
"""
with open(filePath, 'r') as f:
x, y, z = [], [], []
triangles = []
data = f.readlines()
for line in data :
if line.startswith('f'):
# Face indices start at 1, not 0
triangles.append([int(line.split()[1])-1, int(line.split()[2])-1, int(line.split()[3])-1])
elif line.startswith('v'):
x.append(float(line.split()[1]) * ratio)
y.append(float(line.split()[2]) * ratio)
z.append(float(line.split()[3]) * ratio)
if cornered:
xmin = min(x)
for i in range(len(x)):
x[i] -= xmin
ymin = min(y)
for i in range(len(y)):
y[i] -= ymin
zmin = min(z)
for i in range(len(z)):
z[i] -= zmin
return {'x':x, 'y':y, 'z':z, 'faces':triangles, "verticies": zip(x,y,z)}
def parseXYZfiles(filePath: str, delimiter: str = ' ') -> dict:
"""
Parses an xyz file and returns a dict containing the coordinates.
:param file: The xyz file to be parsed.
:param delimiter: The delimiter used in the xyz file.
:return: A dictionary containing the coordinates.
"""
x , y , z = [], [], []
with open(filePath, 'r') as f:
data = f.readlines()
x = [float(line.split(delimiter)[0]) for line in data]
y = [float(line.split(delimiter)[1]) for line in data]
z = [float(line.split(delimiter)[2]) for line in data]
return {'x':x, 'y':y, 'z':z, "verticies": zip(x,y,z)}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment