- Implemented vtk base for viewing
This commit is contained in:
50
mesh_modules/simple_mesh.py
Normal file
50
mesh_modules/simple_mesh.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import numpy as np
|
||||
from scipy.spatial import ConvexHull
|
||||
from stl import mesh
|
||||
|
||||
|
||||
def generate_mesh(points, depth):
|
||||
"""
|
||||
Generate a mesh by extruding a 2D shape along the Z-axis.
|
||||
|
||||
:param points: List of (x, y) tuples representing the 2D shape.
|
||||
:param depth: Extrusion depth along the Z-axis.
|
||||
:return: Tuple of vertices and faces.
|
||||
"""
|
||||
# Convert points to a numpy array
|
||||
points_2d = np.array(points)
|
||||
|
||||
# Get the convex hull of the points to ensure they form a proper polygon
|
||||
hull = ConvexHull(points_2d)
|
||||
hull_points = points_2d[hull.vertices]
|
||||
|
||||
# Generate the top and bottom faces
|
||||
bottom_face = np.hstack((hull_points, np.zeros((hull_points.shape[0], 1))))
|
||||
top_face = np.hstack((hull_points, np.ones((hull_points.shape[0], 1)) * depth))
|
||||
|
||||
# Combine top and bottom vertices
|
||||
vertices_array = np.vstack((bottom_face, top_face))
|
||||
|
||||
# Create faces
|
||||
faces = []
|
||||
|
||||
# Bottom face triangulation (counter-clockwise)
|
||||
for i in range(len(hull_points) - 2):
|
||||
faces.append([0, i + 2, i + 1])
|
||||
|
||||
# Top face triangulation (counter-clockwise, with an offset)
|
||||
top_offset = len(hull_points)
|
||||
for i in range(len(hull_points) - 2):
|
||||
faces.append([top_offset, top_offset + i + 1, top_offset + i + 2])
|
||||
|
||||
# Side faces (ensure counter-clockwise order)
|
||||
for i in range(len(hull_points)):
|
||||
next_i = (i + 1) % len(hull_points)
|
||||
faces.append([i, top_offset + i, top_offset + next_i])
|
||||
faces.append([i, top_offset + next_i, next_i])
|
||||
|
||||
# Convert vertices to the desired format: list of tuples
|
||||
vertices = [tuple(vertex) for vertex in vertices_array]
|
||||
|
||||
return vertices, faces
|
||||
|
||||
Reference in New Issue
Block a user