Document that tessellate_polygon() doesn't handle degenerate geometry

This 'fixes' T68554: 'API mathutils.geometry.tessellate_polygon returns
bad results sometimes' by documenting the limitations of the current
implementation.

I've also added a unit test for the function, so that any change in this
behaviour will get noticed.

No functional changes.
This commit is contained in:
2020-01-27 16:42:25 +01:00
parent 1094e56041
commit 84c537e685
2 changed files with 40 additions and 2 deletions

View File

@@ -3,7 +3,7 @@
# ./blender.bin --background -noaudio --python tests/python/bl_pyapi_mathutils.py -- --verbose
import unittest
from mathutils import Matrix, Vector, Quaternion
from mathutils import kdtree
from mathutils import kdtree, geometry
import math
# keep globals immutable
@@ -488,6 +488,43 @@ class KDTreeTesting(unittest.TestCase):
k.find((0,) * 3, filter=lambda i: None)
class TesselatePolygon(unittest.TestCase):
def test_empty(self):
self.assertEqual([], geometry.tessellate_polygon([]))
def test_2d(self):
polyline = [
Vector((-0.14401324093341827, 0.1266411542892456)),
Vector((-0.14401324093341827, 0.13)),
Vector((0.13532273471355438, 0.1266411542892456)),
Vector((0.13532273471355438, 0.13)),
]
expect = [(0, 1, 2), (0, 3, 2)]
self.assertEqual(expect, geometry.tessellate_polygon([polyline]))
def test_3d(self):
polyline = [
Vector((-0.14401324093341827, 0.1266411542892456, -0.13966798782348633)),
Vector((-0.14401324093341827, 0.1266411542892456, 0.13966798782348633)),
Vector((0.13532273471355438, 0.1266411542892456, 0.13966798782348633)),
Vector((0.13532273471355438, 0.1266411542892456, -0.13966798782348633)),
]
expect = [(2, 3, 0), (2, 0, 1)]
self.assertEqual(expect, geometry.tessellate_polygon([polyline]))
def test_3d_degenerate(self):
polyline = [
Vector((-0.14401324093341827, -0.15269476175308228, -0.13966798782348633)),
Vector((0.13532273471355438, -0.15269476175308228, -0.13966798782348633)),
Vector((0.13532273471355438, -0.15269476175308228, -0.13966798782348633)),
Vector((-0.14401324093341827, -0.15269476175308228, -0.13966798782348633)),
]
# If this returns a proper result, rather than [(0, 0, 0)], it could mean that
# degenerate geometry is handled properly.
expect = [(0, 0, 0)]
self.assertEqual(expect, geometry.tessellate_polygon([polyline]))
if __name__ == '__main__':
import sys
sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])