Failure to calculate normal of non-convex planar polygon
While creating some test cases with non-convex polygons (stars in particular) I noticed that vtk is unable to calculate the area of some shapes. The actual failure is in the calculation of the normal which can propagate to the area.
My test script is
import vtk
import math
n = 12
t = 2.0*math.pi/n
s = (1.0 + math.sin(t))/math.cos(t) - 1.0
points = vtk.vtkPoints()
for z,e in enumerate([-1.0e-6,0.0,1.0e-6]):
for i in range(n):
r = 25.0*(1.0 + (i%2)*s + e)
x, y = r*math.cos(i*t), r*math.sin(i*t)
points.InsertNextPoint([x, y, 10.0*z])
nn = [-1,-1,-1]
for ci in range(3):
pts = [ci*n + i for i in range(n)]
vtk.vtkPolygon.ComputeNormal(points, n, pts, nn)
area = vtk.vtkPolygon.ComputeArea(points, n, pts, nn)
print("Cell: %2d Normal: [%5.2f, %5.2f, %5.2f], Area: %g" % (ci, nn[0], nn[1], nn[2], area))
In this, I create a 6-pointed star which is planar and I expect the normal to be (0,0,1)
. The example is contrived to ensure that the areas of the triangles inside the star and outside cancel out. The slight perturbation e
causes the sign of the normal to change.
The output I get is
Cell: 0 Normal: [ 0.00, 0.00, -1.00], Area: 3247.59
Cell: 1 Normal: [ 0.00, 0.00, 0.00], Area: inf
Cell: 2 Normal: [ 0.00, 0.00, 1.00], Area: 3247.6
In fact I think vtk is unable to calculate the normal here and even the value that looks correct is simply due to round-off error.