1

Could you tell me how do I initialize triangular mesh based on np array with coordinates of vertices and np array with triangles (it contains triples of indices of vertices from first array)?

Could be done in VTK or any other Python library, the final goal is to do decimation on this mesh.

Thank you.

aaxx
  • 55
  • 1
  • 5
  • 1
    what are the shapes? if you have points and edges you can do this by some libraries like meshio or possibly scipy. prepare small arrays of that you have, coords and edges to work on. what should be the output, what you meant by * to do decimation on this mesh*? – Ali_Sh Jun 04 '22 at 14:20
  • @Ali_Sh, thank you for response and pointers to meshio and scipy. I didn't get what you mean by shapes in that case - mesh is triangular if you mean that. The output should be simplified mesh - i.e. mesh roughly maintaining the overal shape of original mesh, but with x2 less triangles. Ideally in the same format as input - array with vertices and array with triangles. – aaxx Jun 05 '22 at 07:18
  • 1
    Does this answer your question? [Create triangular meshing of a non rectangular shape in 2D in Python](https://stackoverflow.com/questions/60927824/create-triangular-meshing-of-a-non-rectangular-shape-in-2d-in-python) – Ali_Sh Jun 05 '22 at 10:22
  • 1
    Another useful link: https://stackoverflow.com/questions/26434726/return-surface-triangle-of-3d-scipy-spatial-delaunay – Ali_Sh Jun 05 '22 at 11:18

2 Answers2

2

A solution using vedo which is based on vtk:

https://github.com/marcomusy/vedo/blob/master/examples/basic/buildmesh.py

from vedo import Mesh, show

verts = [(50,50,50), (70,40,50), (50,40,80), (80,70,50)]
faces = [(0,1,2), (2,1,3), (1,0,3)]
# (the first triangle face is formed by vertex 0, 1 and 2)

# Build the polygonal Mesh object:
mesh = Mesh([verts, faces])
#mesh.decimate()
show(mesh)

enter image description here

mmusy
  • 1,292
  • 9
  • 10
  • Thank you very much! Could you tell me please if there is a way to get the array of triangles and vertices from the decimated mesh? – aaxx Jun 05 '22 at 12:35
  • 1
    Yes, simply use `my_decimated_mesh.points()` and `.faces()` – mmusy Jun 05 '22 at 13:36
  • Thank you. Does the library provides some measure of how much the decimated mesh differs from the original? – aaxx Jun 06 '22 at 09:29
  • well yes, you can compute the distance from the original with `.distanceTo()` method. – mmusy Jun 06 '22 at 09:55
2

You can use several libraries:

Trimesh

Trimesh is one of the bests; It can be used on Colab to show:

import trimesh

verts = [[50,50,50], [70,40,50], [50,40,80], [80,70,50]]
faces = [[0,1,2], [2,1,3], [1,0,3]]

mesh = trimesh.Trimesh(vertices=verts, faces=faces)
mesh.show()

# for facet in mesh.facets:
#     mesh.visual.face_colors[facet] = trimesh.visual.random_color()
# mesh.split()
# mesh.show()

enter image description here

SciPy

1. ConvexHull:

import scipy as sp
from scipy.spatial import ConvexHull
import matplotlib as mpl
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as a3

verts = np.array(verts)

hull = ConvexHull(verts)
indices = hull.simplices
faces = verts[indices]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.set_xlim([35, 85])
ax.set_ylim([35, 85])
ax.set_zlim([35, 85])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

for f in faces:
    face = a3.art3d.Poly3DCollection([f])
    face.set_color(mpl.colors.rgb2hex(np.random.rand(3)))
    face.set_edgecolor('k')
    face.set_alpha(0.5)
    ax.add_collection3d(face)

plt.show()

enter image description here

2. Delaunay:

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay

verts = np.array(verts)
tri = Delaunay(verts)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.azim = 70
ax.plot_trisurf(verts[:, 0], verts[:, 1], verts[:, 2], triangles=tri.simplices, cmap=plt.cm.Spectral)
plt.show()

enter image description here

Ali_Sh
  • 2,667
  • 3
  • 43
  • 66