diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2020-01-27 18:42:25 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2020-01-27 18:42:25 +0300 |
commit | 84c537e68528177ef982479f5884af07cec322b9 (patch) | |
tree | 25f7f99d75db929e688ea401ebf11d8016c1c637 | |
parent | 1094e560413d38e9834ae563ef248f898cb8b201 (diff) |
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.
-rw-r--r-- | source/blender/python/mathutils/mathutils_geometry.c | 3 | ||||
-rw-r--r-- | tests/python/bl_pyapi_mathutils.py | 39 |
2 files changed, 40 insertions, 2 deletions
diff --git a/source/blender/python/mathutils/mathutils_geometry.c b/source/blender/python/mathutils/mathutils_geometry.c index fcb6a77bf36..859ece61ace 100644 --- a/source/blender/python/mathutils/mathutils_geometry.c +++ b/source/blender/python/mathutils/mathutils_geometry.c @@ -1246,7 +1246,8 @@ PyDoc_STRVAR(M_Geometry_tessellate_polygon_doc, ".. function:: tessellate_polygon(veclist_list)\n" "\n" " Takes a list of polylines (each point a pair or triplet of numbers) and returns " - "the point indices for a polyline filled with triangles.\n" + "the point indices for a polyline filled with triangles. Does not handle degenerate " + "geometry (such as zero-length lines due to consecutive identical points).\n" "\n" " :arg veclist_list: list of polylines\n" " :rtype: list\n"); diff --git a/tests/python/bl_pyapi_mathutils.py b/tests/python/bl_pyapi_mathutils.py index 5aa95f9a5f7..9dfc6c159cc 100644 --- a/tests/python/bl_pyapi_mathutils.py +++ b/tests/python/bl_pyapi_mathutils.py @@ -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 []) |