[vtkusers] Strange behavior of VTK in the computation of polygon area

Niklas Karcher niklas.karcher at navasto.de
Tue Mar 6 07:48:38 EST 2018


Hello,

I am experiencing a strange behavior of VTK using its python interface. I am
using VTK 8.0.1 and Python 2.7.14 on CentOS7.

The base code of my minimum working example is the following:

 

import vtk

 

coneSource = vtk.vtkConeSource()

coneSource.Update()

 

cone = vtk.vtkPolyData()

cone.DeepCopy(coneSource.GetOutput())

 

cone2 = vtk.vtkPolyData()

cone2.DeepCopy(cone)

 

N = 10

for _ in range(N):

                print cone.GetCell(0).ComputeArea(),
cone2.GetCell(0).ComputeArea()

 

What it does:

1. It creates a cone 'cone' from a vtkConeSource 

2. Duplicates it into a 'cone2' and

3. accesses the polygon cell at the base of the cone of 'cone' and 'cone2'
and evaluates the area.

 

The for-loop in step 3 shows the strange behavior. With varying 'N' the
print statements produces different results. Also whether or not I run this
code in PyCharm or in a terminal triggers a different outcome of the print
statements.

In my IDE the results for different N are the following:

 

+----+------+-------+

| N  | Case | Case2 |

+----+------+-------+

|  1 | inf  | OK    |

| 10 | inf  | OK    |

| 80 | OK   | OK    |

+----+------+-------+

 

Strangely enough the result also depends on whether or not the following
import at the top of the script is present even though its functionality is
never used:

from vtk.util.numpy_support import vtk_to_numpy

 

Then the result is the following:

+----+------+-------+

| N  | Case | Case2 |

+----+------+-------+

|  1 | nan  | OK    |

| 10 | inf  | OK    |

| 80 | OK   | OK    |

+----+------+-------+

 

I also varied the way the vtkConeSource returns its result to 'cone'. If I
change the lines 

 

cone = vtk.vtkPolyData()

cone.DeepCopy(coneSource.GetOutput())

 

to

 

cone = coneSource.GetOutput()

 

The result tables look like this:

 

+----+------+-------+

| N  | Case | Case2 |

+----+------+-------+

|  1 | OK   | OK    |

| 10 | (ok) | OK    |

| 80 | (ok) | OK    |

+----+------+-------+

 

with (ok) meaning that the first print of the for loop prints an incorrect
result like inf or nan but the rest of the output is correct.

 

Note that all other cell areas of the cones are computed correctly (my hunch
is that this is due to the fact that the rest of the cells are triangles and
not polygons, even though the vtkPolyData object does not distinguish
between the two types). Also all the results displayed above are 100%
reproduceable on my machine.

 

My question to the mailing list is:
What I am doing wrong in the creation and copying of my two cones? It
appears as if the memory behind the two cones is corrupted in some way. How
can I assure that the area calculation is always successful?

 

Regards

Niklas

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://vtk.org/pipermail/vtkusers/attachments/20180306/07e8012a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5728 bytes
Desc: not available
URL: <https://vtk.org/pipermail/vtkusers/attachments/20180306/07e8012a/attachment.bin>


More information about the vtkusers mailing list