Search code examples
3dpolygonvtktriangulation

VTK Extrude Polygon Points and Holes Issue


I am using VTK for creating my meshes. I am new to it and I am facing 2 issues.

  1. I can't find proper documentation to add holes into polygon mesh
  2. Polygon curves have issue

My Code sample is:

def extrude_polygon(vertices, holes, extrude):
    cell = vtkIdList()
    points = [[point[0], point[1]] for point in vertices]
    # points = remove_recursion(points)
    # Create points
    poly_points = vtkPoints()
    poly_points.SetDataTypeToDouble()

    [(poly_points.InsertNextPoint(pt[0], pt[1], 0), cell.InsertNextId(i)) for i, pt in enumerate(points)]

    # Create a PolyData object and add points
    pd = vtkPolyData()
    pd.Allocate(1, 1)
    pd.SetPoints(poly_points)
    # pd.InsertNextCell(VTK_POLYGON, cell)
    pd.InsertNextCell(vtkConstants.VTK_POLYGON, cell)

    prod = vtkTrivialProducer()
    prod.SetOutput(pd)

    extr = vtkLinearExtrusionFilter()
    extr.SetInputConnection(prod.GetOutputPort())
    extr.SetVector(0, 0, extrude)

    pn = vtkPolyDataNormals()
    pn.SetInputConnection(extr.GetOutputPort())
    pn.AutoOrientNormalsOn()

    return pn

Current Output:

enter image description here

Expected Output:

enter image description here


Solution

  • You can try with vtkContourTriangulator, eg using vedo:

    from vedo import Line, merge, show
    
    outpts = [
        (515.5, 146.0),
        (516.1, 429.8),
        (460.0, 440.8),
        (423.3, 489.6),
        (425.2, 534.2),
        (131.6, 532.3),
        (132.2, 337.7),
        (242.7, 339.5),
        (278.7, 323.0),
        (300.7, 304.1),
        (320.2, 268.1),
        (323.9, 236.4),
        (323.9, 146.0),
    ]
        
    inpts = [
        (395.9, 352.9),
        (384.9, 358.4),
        (371.5, 356.0),
        (364.1, 347.4),
        (367.2, 331.6),
        (376.4, 324.8),
        (386.7, 324.2),
        (398.3, 328.5),
    ]
    
    l1 = Line(outpts, lw=3, closed=True)
    l2 = Line(inpts,  lw=3, closed=True)
    m = merge(l1, l2).triangulate().lw(0)
    sm = m.extrude(50).flat().lighting("default")
    
    show(l1, l2, sm, axes=1)
    

    output