test_triangulator.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from panda3d.core import Triangulator
  2. def triangulate(vertices):
  3. t = Triangulator()
  4. for i, v in enumerate(vertices):
  5. t.add_vertex(v)
  6. t.add_polygon_vertex(i)
  7. t.triangulate()
  8. # Make sure that the result is consistent by starting each triangle with
  9. # the lowest index value. That makes it easier to use predetermined values
  10. # in the test cases.
  11. result = set()
  12. for n in range(t.get_num_triangles()):
  13. # Switch to lowest matching index value in case of duplicates.
  14. v0 = vertices.index(vertices[t.get_triangle_v0(n)])
  15. v1 = vertices.index(vertices[t.get_triangle_v1(n)])
  16. v2 = vertices.index(vertices[t.get_triangle_v2(n)])
  17. if v1 < v0:
  18. v0, v1, v2 = v1, v2, v0
  19. if v1 < v0:
  20. v0, v1, v2 = v1, v2, v0
  21. result.add((v0, v1, v2))
  22. return result
  23. def test_triangulator_degenerate():
  24. assert not triangulate([])
  25. assert not triangulate([(0, 0)])
  26. assert not triangulate([(0, 0), (0, 0)])
  27. assert not triangulate([(0, 0), (1, 0)])
  28. assert not triangulate([(0, 0), (0, 0), (0, 0)])
  29. assert not triangulate([(0, 0), (1, 0), (1, 0)])
  30. assert not triangulate([(1, 0), (1, 0), (1, 0)])
  31. assert not triangulate([(1, 0), (0, 0), (1, 0)])
  32. assert not triangulate([(0, 0), (0, 0), (0, 0), (0, 0)])
  33. def test_triangulator_triangle():
  34. assert triangulate([(0, 0), (1, 0), (1, 1)]) == {(0, 1, 2)}
  35. def test_triangulator_tail():
  36. # This triangle has a long "tail" where the polygon retraces its vertices.
  37. assert triangulate([
  38. (0, -1),
  39. (0, 1),
  40. (1, 0),
  41. (2, 0),
  42. (3, 1),
  43. (4, 0),
  44. (5, 0),
  45. (4, 0),
  46. (3, 1),
  47. (2, 0),
  48. (1, 0),
  49. ]) == {(0, 2, 1)}
  50. def test_triangulator_hourglass():
  51. # Two triangles with touching tips, effectively.
  52. assert triangulate([
  53. (-1, 1),
  54. (-1, -1),
  55. (0, 0),
  56. (1, -1),
  57. (1, 1),
  58. (0, 0),
  59. ]) == {(0, 1, 2), (2, 3, 4)}