test_collision_polygon.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from panda3d import core
  2. import pytest
  3. def test_collision_polygon_verify_not_enough_points():
  4. # Less than 3 points cannot create a polygon
  5. assert not core.CollisionPolygon.verify_points([])
  6. assert not core.CollisionPolygon.verify_points([core.LPoint3(1, 0, 0)])
  7. assert not core.CollisionPolygon.verify_points([core.LPoint3(1, 0, 0), core.LPoint3(0, 0, 1)])
  8. def test_collision_polygon_verify_repeating_points():
  9. # Repeating points cannot create a polygon
  10. assert not core.CollisionPolygon.verify_points([core.LPoint3(1, 0, 0), core.LPoint3(1, 0, 0), core.LPoint3(0, 0, 1)])
  11. assert not core.CollisionPolygon.verify_points([core.LPoint3(3, 6, 1), core.LPoint3(1, 3, 5), core.LPoint3(9, 1, 2), core.LPoint3(1, 3, 5)])
  12. def test_collision_polygon_verify_colinear_points():
  13. # Colinear points cannot create a polygon
  14. assert not core.CollisionPolygon.verify_points([core.LPoint3(1, 2, 3), core.LPoint3(2, 3, 4), core.LPoint3(3, 4, 5)])
  15. assert not core.CollisionPolygon.verify_points([core.LPoint3(2, 1, 1), core.LPoint3(3, 2, 1), core.LPoint3(4, 3, 1)])
  16. def test_collision_polygon_verify_points():
  17. # Those should be regular, non-colinear points
  18. assert core.CollisionPolygon.verify_points([core.LPoint3(1, 0, 0), core.LPoint3(0, 1, 0), core.LPoint3(0, 0, 1)])
  19. assert core.CollisionPolygon.verify_points([core.LPoint3(10, 2, 8), core.LPoint3(7, 1, 3), core.LPoint3(5, 9, 6)])
  20. assert core.CollisionPolygon.verify_points([core.LPoint3(3, -8, -7), core.LPoint3(9, 10, 8), core.LPoint3(7, 0, 10), core.LPoint3(-6, -2, 3)])
  21. assert core.CollisionPolygon.verify_points([core.LPoint3(-1, -3, -5), core.LPoint3(10, 3, -10), core.LPoint3(-10, 10, -4), core.LPoint3(0, 1, -4), core.LPoint3(-9, -2, 0)])
  22. with pytest.raises(TypeError):
  23. core.CollisionPolygon.verify_points([core.LPoint3(0, 0, 0), None])
  24. def test_collision_polygon_setup_points():
  25. # Create empty collision polygon
  26. polygon = core.CollisionPolygon(core.LVecBase3(0, 0, 0), core.LVecBase3(0, 0, 0), core.LVecBase3(0, 0, 0))
  27. assert not polygon.is_valid()
  28. # Test our setup method against a few test cases
  29. for points in [
  30. [core.LPoint3(-1, -3, -5), core.LPoint3(10, 3, -10), core.LPoint3(-10, 10, -4), core.LPoint3(0, 1, -4), core.LPoint3(-9, -2, 0)],
  31. [core.LPoint3(3, -8, -7), core.LPoint3(9, 10, 8), core.LPoint3(7, 0, 10), core.LPoint3(-6, -2, 3)],
  32. [core.LPoint3(1, 0, 0), core.LPoint3(0, 1, 0), core.LPoint3(0, 0, 1)],
  33. [core.LPoint3(10, 2, 8), core.LPoint3(7, 1, 3), core.LPoint3(5, 9, 6)]
  34. ]:
  35. polygon.setup_points(points)
  36. assert polygon.is_valid()
  37. assert polygon.get_num_points() == len(points)
  38. with pytest.raises(TypeError):
  39. polygon.setup_points([core.LPoint3(0, 0, 0), None, 1])
  40. with pytest.raises(ValueError):
  41. polygon.setup_points([core.LPoint3(0, 0, 0)])