added the small files i made
This commit is contained in:
parent
8d536efcdb
commit
b5186aabed
0
requirement.txt
Normal file
0
requirement.txt
Normal file
16
utils/3d/mpl_render.py
Normal file
16
utils/3d/mpl_render.py
Normal file
@ -0,0 +1,16 @@
|
||||
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()
|
||||
14
utils/norm.py
Normal file
14
utils/norm.py
Normal file
@ -0,0 +1,14 @@
|
||||
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')
|
||||
48
utils/parsers.py
Normal file
48
utils/parsers.py
Normal file
@ -0,0 +1,48 @@
|
||||
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)}
|
||||
Loading…
Reference in New Issue
Block a user