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.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.InsertNextCell(VTK_POLYGON, cell)
    pd.InsertNextCell(vtkConstants.VTK_POLYGON, cell)

    prod = vtkTrivialProducer()

    extr = vtkLinearExtrusionFilter()
    extr.SetVector(0, 0, extrude)

    pn = vtkPolyDataNormals()

    return pn

Current Output:

Expected Output:


  1. Maybe you can try vtkCutter
  2. Maybe the problem is that your polygon is not convex

Hope it helps

Two quick comments which may or may not be relevant:

  • Rendering non-convex polygons will most likely produce bad results (this is because most rendering libraries often use simple methods to tessellate n-sided polygons which do not take into account concave situations). Render the polygon in wireframe and see if the outline looks correct. If you need to render non-convex polygons, you’re going to have to triangulate them.

  • VTK has no direct support for complex polygons (including those with holes). David Gobbi created a very nice class called vtkContourTriangulator which can triangulate loops with inner loops.

the vtkContourTriangulator works like a charm:

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)

1 Like

I have checked Wireframe and Exported as an GLTF, it is fine.

This is Wireframe

This is GLTF

But there is no UVs in GLTF/OBJ model.
Could you please guide me if I am missing anything?

It would be great uf someine could port Marco’s code to vtk

Sir my main aim is to export these shapes as OBJ, GLB, GLTF etc.
When I export GLTF/OBJ, the shape is fine. There’s no issue with appearance.
The only problem now is there is no UV inside polygon mesh. So my model in other programs doesn’t have textures because there is no UVs
It seems like I’m missing something.
I’m completely new to it, if you have any idea regarding this, it would be really appericiated.

Can you share demo of how can I use this to assign texture and export as a 3D model (GLB, OBJ etc)?

from vedo import Line, Mesh, merge
# ...
l1 = Line(outpts, closed=True)
l2 = Line(inpts,  closed=True)
m = merge(l1, l2).triangulate().lw(0)
sm = m.extrude(50).flat().lighting("default")
# read back and show again:
Mesh("test.obj").texture("Cat03.jpg", tcoords="TCoords").show().close()

you need to:
pip install -U git+https://github.com/marcomusy/vedo.git

that is very easy, just use vtkAppendPolyData and then vtkContourTriangulator like in the line:
merge(l1, l2).triangulate()


Thank you Marco

You habe done a really great work simplifying vtk for newcommers


1 Like

Thank you so much. As you said, This works like a Charm. Great recommendation

1 Like