refactored folders and added vispy 3Drender

This commit is contained in:
Djalim Simaila 2023-04-21 11:50:23 +02:00
parent 2729ac5f4a
commit af36b95e09
3 changed files with 38 additions and 4 deletions

View File

@ -6,7 +6,7 @@ import time
import numpy as np
from main import get_discrete_data, get_raw_data
from utils.files import output
from utils.files.input import ScannedObject
from utils.files.input import ScannedObject, parse_result_file
def check_discrete_data(expected_file: str, actual_file: str, ndigits=7, eps=0.00001, silent: bool = False) -> dict:
"""

View File

@ -1,16 +1,17 @@
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
import numpy as np
from utils.files.input import ScannedObject
def render3D(data:dict):
def render3D(obj:ScannedObject):
"""
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'])))
faces = np.array(obj.get_faces())
verts = np.array(obj.get_vertices())
mesh = Poly3DCollection(verts[faces], alpha=0.25, edgecolor='none')
ax.add_collection3d(mesh)
plt.show()

View File

@ -0,0 +1,33 @@
import numpy as np
from vispy import app, scene
from vispy.scene.visuals import Mesh
from vispy.scene import transforms
from vispy.visuals.filters import ShadingFilter, WireframeFilter
from utils.files.input import ScannedObject
def render3D(obj:ScannedObject):
vertices = np.asarray(obj.get_vertices())
faces = np.asarray(obj.get_faces())
canvas = scene.SceneCanvas(keys='interactive', bgcolor='white')
view = canvas.central_widget.add_view()
view.camera = 'arcball'
view.camera.depth_value = 1e3
mesh = Mesh(vertices, faces, color=(.5, .7, .5, 1))
view.add(mesh)
wireframe_filter = WireframeFilter(width=0)
shading_filter = ShadingFilter(shininess=0)
mesh.attach(wireframe_filter)
mesh.attach(shading_filter)
def attach_headlight(view):
light_dir = (0, 1, 0, 0)
shading_filter.light_dir = light_dir[:3]
initial_light_dir = view.camera.transform.imap(light_dir)
@view.scene.transform.changed.connect
def on_transform_change(event):
transform = view.camera.transform
shading_filter.light_dir = transform.map(initial_light_dir)[:3]
attach_headlight(view)
canvas.show()
app.run()