drufat / triangle

Python bindings to the triangle library
GNU Lesser General Public License v3.0
230 stars 53 forks source link

Bug: Triangualte with 'a' and small values <10 ** -4 #26

Closed icemtel closed 6 years ago

icemtel commented 6 years ago

I don't think this will be fixed any time soon, the source of the problem is probably in C code. But I want to make others aware of this problem. Using triangulate with parameter 'aX' (which sets a restriction on maximum triangle area) with X - a number <10**-4, the Triangle will fully ignore this argument, and maximum triangle area will be unrestricted.

This is quite unexpected, because this means that algorithm will already have problems with triangles of linear size 10-3-10-2, which is not that small.

import scipy as sp
import triangle

boundary_points = sp.array([[-1, -1], [1, -1], [1, 1], [-1, 1]])

max_areas = [10 ** -5, 9 * 10 ** -5, 10 ** -4, 1.1 * 10 ** - 4, 10 ** -2 ]

print('Max area   Num of triangles')
for max_area in max_areas:
    tri_data = {'vertices': boundary_points}
    code = 'a{0}'.format(max_area)
    tri = triangle.triangulate(tri_data, code)

    coords = tri['vertices']
    trias = tri['triangles']

    print('{:.2e}   '.format(max_area), len(trias))

For me this prints

Max area   Num of triangles
1.00e-05    4
9.00e-05    2
1.00e-04    62223
1.10e-04    56758
1.00e-02    628

I could reproduce this problem with circular geometry and for objects of different size, also with additional code 'q' the problem remains.

icemtel commented 6 years ago

UPD: in https://github.com/wo80/Triangle/issues/37 C code of Triangle 1.6 was checked and it works fine. The python bindings use the same version, so the problem has to be with python library.

icemtel commented 6 years ago
code = 'a{0}'.format(max_area)

has to be replaced with

code = 'a{0:.31f}'.format(max_area)

for correct formatting. Otherwise any value below 10 ** -4 will be written in the exponential form.

drufat commented 6 years ago

I have not updated the version of the triangle C library that the python bindings use. If the problem has been fixed recently, these bindings still use the older version of the code. Maybe it would be a good idea to upgrade to the latest version when I have more time.

icemtel commented 6 years ago

The problem was on my side, but some updates where made in C code indeed. See https://github.com/wo80/Triangle/issues/37#issuecomment-400224118 and https://github.com/wo80/Triangle/issues/37#issuecomment-400242694