Skip to content

sweep_polygon creates twisted/self-intersecting mesh in certain cases #2448

@qixuema

Description

@qixuema

Describe the bug
When using trimesh.creation.sweep_polygon to extrude a polygon along a path, the resulting mesh sometimes contains twisted or self-intersecting faces. This leads to invalid or distorted geometry.


Code to Reproduce

import numpy as np
import trimesh
from shapely.geometry import Polygon

def _regular_ngon_2d(n_sides: int, radius: float, start_angle: float = 0.0) -> np.ndarray:
    angles = np.linspace(0.0 + start_angle, 2*np.pi + start_angle, n_sides, endpoint=False)
    x = radius * np.cos(angles)
    y = radius * np.sin(angles)
    return np.column_stack([x, y])  # (N,2)

polygon_2d = _regular_ngon_2d(n_sides=5, radius=0.1)

polygon_2d = Polygon(polygon_2d)  # Convert numpy array to shapely Polygon

polyline = np.array([
    [-0.4844,  0.541 ,  0.0815],
    [-0.6675,  0.811 ,  0.4648],
    [-0.5557,  0.9077,  0.627 ],
    [-0.3574,  0.977 ,  0.723 ],
    [-0.1116,  1.001 ,  0.7   ],
    [ 0.1598,  0.973 ,  0.5425],
    [ 0.2084,  0.676 ,  0.1472]]
)

mesh = trimesh.creation.sweep_polygon(polygon_2d, path=polyline, cap=True, connect=False)

mesh.export('sweep_test.obj')

Expected behavior
The mesh should follow the sweep path smoothly, with faces aligned, and without twisted or self-intersecting geometry.


Actual behavior

  • The generated mesh is visibly twisted and contains distorted faces.
  • See attached screenshot for visualization:
Image

Environment (please complete the following):

  • OS: Ubuntu 22.04
  • Python version: 3.10
  • trimesh version: 4.7.4

Additional context
It looks like the frame/orientation computation along the sweep path might not be consistent, which causes polygons to flip or rotate unexpectedly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions