# vtkContourTriangulator (Triangulation failed)

**URL:** https://discourse.vtk.org/t/vtkcontourtriangulator-triangulation-failed/11127
**Category:** Development
**Created:** [April 4, 2023, 12:21pm UTC](https://discourse.vtk.org/t/vtkcontourtriangulator-triangulation-failed/11127 "2023-04-04T12:21:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [April 4, 2023, 12:21pm UTC](https://discourse.vtk.org/t/vtkcontourtriangulator-triangulation-failed/11127/1 "2023-04-04T12:21:18Z")

</div>

I found an example where the filter seems to fail (but not clear to me why) - I wonder if there is a fix…:

```python
from vedo import *

pts = np.array([
    [1.96, 2.48, -1.34],
    [1.89, 2.34, -1.33],
    [1.74, 2.41, -1.35],
    [1.68, 2.26, -1.35],
    [1.52, 2.32, -1.34],
    [1.59, 2.48, -1.34],
    [1.66, 2.63, -1.33],
    [1.81, 2.56, -1.34],
])
# pts[:,2] = -1.33 # this fixes it
# pts[:, [1,2]] = pts[:, [2,1]] # no effect in any case

line = Line(pts)
# line.write("line.vtk", binary=False)
mesh = line.clone().triangulate().lw(1).c("blue4")
labels = line.labels2d(c="yellow4", scale=2)
show(line, mesh, labels, bg="gray1", axes=1).close()

```

works fine if:  
`pts[:,2] = -1.33`

 ![Screenshot from 2023-04-04 14-07-21](https://discourse.vtk.org/uploads/default/original/2X/e/ebde60fc5893c838c47ecf0b38d228a6c1853e41.png)

fails without it (non-planar):

 ![Screenshot from 2023-04-04 14-07-39](https://discourse.vtk.org/uploads/default/original/2X/b/b1fdb1a46f73d52e0e630cbf31ed18ced98f6108.png)

with error message:

```plaintext
2023-04-04 14:07:57.010 ( 0.124s) [C1F89180]vtkContourTriangulator.:91 ERR| vtkContourTriangulator (0x26bc1a0): Triangulation failed, output might have holes.

```

Pure VTK to reproduce the issue:

```python
import vtk

reader = vtk.vtkPolyDataReader()
reader.SetFileName("line_bad.vtk")
reader.Update()
polydata = reader.GetOutput()

tf = vtk.vtkContourTriangulator()
tf.TriangulationErrorDisplayOn()
tf.SetInputData(polydata)
tf.Update()
tfpolydata = tf.GetOutput()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(tfpolydata)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().EdgeVisibilityOn()

ren = vtk.vtkRenderer()
ren.AddActor(actor)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.Start()

```

![Screenshot from 2023-04-04 14-08-00](https://discourse.vtk.org/uploads/default/original/2X/e/e7690a572bc25edc0abe6e98b33192bdb020816c.png)

[lines.zip](https://discourse.vtk.org/uploads/short-url/czwFiJ3F9CqmBGcy2cwW1A0Se9I.zip) (699 Bytes)

@dgobbi

Related to:

> [@create separated regions from polydata](https://discourse.vtk.org/t/create-separated-regions-from-polydata/10409/26):
>
> I can reproduce what the vedo code from @marcomusy has proposed with the VTK API. But the split function from the vedo code gives anyway an uncorrect number of regions. I have prepared 3 vtk files to play with. [https://thredds-su.ipsl.fr/thredds/fileServer/ipsl\_thredds/brocksce/tmp/poly\_11.vtk](https://thredds-su.ipsl.fr/thredds/fileServer/ipsl_thredds/brocksce/tmp/poly_11.vtk) [image] [https://thredds-su.ipsl.fr/thredds/fileServer/ipsl\_thredds/brocksce/tmp/poly\_07.vtk](https://thredds-su.ipsl.fr/thredds/fileServer/ipsl_thredds/brocksce/tmp/poly_07.vtk) [image] [https://thredds-su.ipsl.fr/thredds/fileServer/ipsl\_thredds/brocksce/tmp/poly\_03.vtk](https://thredds-su.ipsl.fr/thredds/fileServer/ipsl_thredds/brocksce/tmp/poly_03.vtk) [image] In t…

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [April 4, 2023, 1:52pm UTC](https://discourse.vtk.org/t/vtkcontourtriangulator-triangulation-failed/11127/2 "2023-04-04T13:52:15Z")

</div>

I have confirmed this locally. As you noted in your fix, your points do not lie in a plane. The vtkContourTriangulator was not designed for inputs that are not flat. The description of this filter is: _Fill all 2D contours to create polygons_.

The failure seems to be at the very last stage. One thing that I could do in order to make the filter more robust, is to use the normal to project the points onto a plane before doing the final triangulation. It would increase memory usage and the execution time, so it would have to be an option, not the default.

---

<div class="post-metadata">

### Author: ![PBrockmann](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pbrockmann/32/524_2.png) [@PBrockmann](https://discourse.vtk.org/u/PBrockmann)
#### Post date: [April 4, 2023, 1:59pm UTC](https://discourse.vtk.org/t/vtkcontourtriangulator-triangulation-failed/11127/3 "2023-04-04T13:59:11Z")

</div>

It seems that vtkContourTriangulator() does not handle 3D coordinates when non planar.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/e/e57cdd3f0b9139b32b841c6deeb8944e6d32a40c.jpeg)

The big picture of this problem is how to get a anti-counter clockwise coordinates of polydata made of lines sometimes folded.

See: [create separated regions from polydata](https://discourse.vtk.org/t/create-separated-regions-from-polydata/10409)

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [April 4, 2023, 6:20pm UTC](https://discourse.vtk.org/t/vtkcontourtriangulator-triangulation-failed/11127/4 "2023-04-04T18:20:09Z")

</div>

I’ve used this class a lot and never encountered this kind of problem, yet would be nice to have such an option to make it more robust as you suggest! Maybe it would be also a good idea to have the `TriangulationErrorDisplayOn()` on by default.  
Thanks!

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [April 4, 2023, 6:53pm UTC](https://discourse.vtk.org/t/vtkcontourtriangulator-triangulation-failed/11127/5 "2023-04-04T18:53:36Z")

</div>

indeed projecting on the fitting plane makes it work fine:

```python
from vedo import *

def triangulate_non_planar(line):
    data = line.points()
    dmean = data.mean(axis=0)
    pts = data - dmean
    vv = np.linalg.svd(pts)[2]
    n = np.cross(vv[0], vv[1])
    d = np.dot(pts, n)
    pts -= n * d[:, np.newaxis]
    faces = Line(pts).triangulate().faces()
    return Mesh([data, faces])

pts = np.array([
    [1.96, 2.48, -1.34],
    [1.89, 2.34, -1.33],
    [1.74, 2.41, -1.35],
    [1.68, 2.26, -1.35],
    [1.52, 2.32, -1.34],
    [1.59, 2.48, -1.34],
    [1.66, 2.63, -1.33],
    [1.81, 2.56, -1.34],
])
line = Line(pts)
mesh = triangulate_non_planar(line).lw(1).c("blue4")
labels = line.labels2d(c="yellow4", scale=2)
show(line, mesh, labels, bg="gray1", axes=1).close()

```

 ![Screenshot from 2023-04-04 20-46-15](https://discourse.vtk.org/uploads/default/original/2X/e/ebcefdc2a501b90912a102bc1e0e62aeaad544ac.png)

---

<div class="post-metadata">

### Author: ![PBrockmann](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pbrockmann/32/524_2.png) [@PBrockmann](https://discourse.vtk.org/u/PBrockmann)
#### Post date: [April 5, 2023, 9:04am UTC](https://discourse.vtk.org/t/vtkcontourtriangulator-triangulation-failed/11127/6 "2023-04-05T09:04:45Z")

</div>

Ok. Indeed it works with this set of points.  
But not sure to be able to find a fitting plane with case like captured below.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/6/6d2b1852ec3aae5e4fb51996f46dc06a414e29bd.jpeg)

My purpose is to find all contigous lines that form convex polylines and describe their coordinates anticlockwise.

---

<div class="post-metadata">

### Author: ![PBrockmann](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pbrockmann/32/524_2.png) [@PBrockmann](https://discourse.vtk.org/u/PBrockmann)
#### Post date: [April 6, 2023, 12:54pm UTC](https://discourse.vtk.org/t/vtkcontourtriangulator-triangulation-failed/11127/7 "2023-04-06T12:54:39Z")

</div>

Here is another very basic example.  
Described in [Sort points on line in PolyData - #4 by PBrockmann](https://discourse.vtk.org/t/sort-points-on-line-in-polydata/3851/4)
