Skip to content

Commit

Permalink
Merge pull request #988 from JeffreyWardman/vedo2madcad
Browse files Browse the repository at this point in the history
feat: vedo2madcad
  • Loading branch information
marcomusy authored Dec 1, 2023
2 parents c4a0a56 + ea5bd27 commit b8c1502
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/other/madcad2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Example of usage of the madcad library
# See https://pymadcad.readthedocs.io/en/latest/index.html
import vedo
import numpy as np
import madcad

##########################################################################
mesh = vedo.Mesh(vedo.dataurl+"bunny.obj")
mesh.compute_normals()

madcad_mesh = vedo.utils.vedo2madcad(mesh)
madcad.thicken(madcad_mesh, thickness=0.1)
madcad.show([madcad_mesh])


##########################################################################
vedo_mesh = vedo.utils.madcad2vedo(madcad_mesh)

arrs = vedo.Arrows(vedo_mesh.vertices, vedo_mesh.vertices + 0.01 * vedo_mesh.pointdata["Normals"])
vedo.show(mesh, arrs, axes=1).close()
28 changes: 28 additions & 0 deletions vedo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,30 @@ def vedo2open3d(vedo_mesh):
# o3d_mesh.vertex_normals= o3d.utility.Vector3dVector(vedo_mesh.pointdata["Normals"])
return o3d_mesh

def vedo2madcad(vedo_mesh):
"""
Convert a `vedo.Mesh` to a `madcad.Mesh`.
"""
try:
import madcad
import numbers
except ModuleNotFoundError:
vedo.logger.error("Need madcad to run:\npip install pymadcad")

points = [madcad.vec3(*pt) for pt in vedo_mesh.vertices]
faces = [madcad.vec3(*fc) for fc in vedo_mesh.cells]

options = {}
for key, val in vedo_mesh.pointdata.items():
vec_type = f"vec{val.shape[-1]}"
is_float = np.issubdtype(val.dtype, np.floating)
madcad_dtype = getattr(madcad, f"f{vec_type}" if is_float else vec_type)
options[key] = [madcad_dtype(v) for v in val]

madcad_mesh = madcad.Mesh(points=points, faces=faces, options=options)

return madcad_mesh


def madcad2vedo(madcad_mesh):
"""
Expand Down Expand Up @@ -2471,6 +2495,10 @@ def madcad2vedo(madcad_mesh):
m.alpha(0.2)
if "color" in options:
m.c(options["color"])

for key, val in options.items():
m.pointdata[key] = val

except AttributeError:
# print("no options")
pass
Expand Down

0 comments on commit b8c1502

Please sign in to comment.