Skip to content

Commit 47aeb4e

Browse files
committed
Finish geometry tests
* pygorithm/geometry/polygon2.py - minor documentation changes * tests/test_geometry.py - finish polygon2.Polygon2 tests
1 parent 7a480f8 commit 47aeb4e

File tree

2 files changed

+112
-9
lines changed

2 files changed

+112
-9
lines changed

pygorithm/geometry/polygon2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def find_intersection(poly1, poly2, offset1, offset2, find_mtv = True):
233233
amount of space.
234234
235235
The resulting MTV should be applied to the first polygon (or its offset),
236-
or its negation can be applied to the second polyogn (or its offset).
236+
or its negation can be applied to the second polygon (or its offset).
237237
238238
The MTV will be non-null if overlapping is True and find_mtv is True.
239239
@@ -243,6 +243,10 @@ def find_intersection(poly1, poly2, offset1, offset2, find_mtv = True):
243243
False. It is rarely an improvement to first check without finding
244244
mtv and then to find the mtv.
245245
246+
.. caution::
247+
248+
The first value in the mtv could be negative (used to inverse the direction
249+
of the axis)
246250
247251
:param poly1: the first polygon
248252
:type poly1: :class:`pygorithm.geometry.polygon2.Polygon2`

tests/test_geometry.py

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ def test_constructor_not_convex(self):
594594
with self.assertRaises(ValueError):
595595
poly = polygon2.Polygon2([ (0, 1), (0.5, 0.8), (1, 1), (1, 0), (0, 0) ])
596596

597-
def test_cosntructor_not_clockwise(self):
597+
def test_constructor_not_clockwise(self):
598598
with self.assertRaises(ValueError):
599599
poly = polygon2.Polygon2([ (0, 0), (1, 0), (1, 1), (0, 1) ])
600600

@@ -701,24 +701,123 @@ def test_project_onto_axis(self):
701701
_axis3 = vector2.Vector2(0.70710678118, 0.70710678118)
702702
self._proj_onto_axis_fuzzer(poly.points, _axis3, axisall.AxisAlignedLine(_axis3, 0, 1.41421356236))
703703

704-
704+
def _contains_point_fuzzer(self, points, point, expected_edge, expected_contains):
705+
for i in range(3):
706+
offset = vector2.Vector2(random.randrange(-1000, 1000, 0.01), random.randrange(-1000, 1000, 0.01))
707+
708+
new_points = []
709+
for pt in points:
710+
new_points.append(pt - offset)
711+
712+
new_poly = polygon2.Polygon2(new_points)
713+
714+
edge, cont = polygon2.Polygon2.contains_point(new_poly, offset, point)
715+
716+
help_msg = "points={}, point={}, expected_edge={}, expected_contains={}, edge={}, cont={}".format(points, point, expected_edge, expected_contains, edge, cont)
717+
self.assertEqual(expected_edge, edge. help_msg)
718+
self.assertEqual(expected_contains, cont, help_msg)
719+
705720
def test_contains_point_false(self):
706-
pass
721+
poly = polygon2.Polygon2([ (1, 1), (2, 3), (4, 0) ])
722+
723+
self._contains_point_fuzzer(poly.points, vector2.Vector2(1, 2), False, False)
724+
self._contains_point_fuzzer(poly.points, vector2.Vector2(4, 2), False, False)
725+
self._contains_point_fuzzer(poly.points, vector2.Vector2(3, 0), False, False)
707726

708727
def test_contains_point_edge(self):
709-
pass
728+
poly = polygon2.Polygon2([ (2, 3), (3, 5), (5, 4), (3, 2) ])
729+
730+
self._contains_point_fuzzer(poly.points, vector2.Vector2(4, 3), True, False)
731+
self._contains_point_fuzzer(poly.points, vector2.Vector2(2.5, 2.5), True, False)
732+
self._contains_point_fuzzer(poly.points, vector2.Vector2(4, 4.5), True, False)
710733

711734
def test_contains_point_contained(self):
712-
pass
735+
poly = polygon2.Polygon2([ (-3, -6), (-2, -3), (2, -2), (0, -5) ])
736+
737+
self._contains_point_fuzzer(poly.points, vector2.Vector2(-1, -4), False, True)
738+
self._contains_point_fuzzer(poly.points, vector2.Vector2(-1, -5), False, True)
739+
self._contains_point_fuzzer(poly.points, vector2.Vector2(1, -3), False, True)
740+
741+
def _find_intersection_fuzzer(self, points1, points2, exp_touching, exp_overlap, exp_mtv):
742+
if type(points1) != list:
743+
points1 = points1.points
744+
if type(points2) != list:
745+
points2 = points2.points
713746

747+
for i in range(3):
748+
offset1 = vector2.Vector2(random.randrange(-1000, 1000, 0.01), random.randrange(-1000, 1000, 0.01))
749+
offset2 = vector2.Vector2(random.randrange(-1000, 1000, 0.01), random.randrange(-1000, 1000, 0.01))
750+
751+
new_points1 = []
752+
for pt in points1:
753+
new_points1.append(pt - offset1)
754+
755+
new_points2 = []
756+
for pt in points2:
757+
new_points2.append(pt - offset2)
758+
759+
new_poly1 = polygon2.Polygon2(new_points1)
760+
new_poly2 = polygon2.Polygon2(new_points2)
761+
762+
touch, overlap, mtv = polygon2.Polygon2.find_intersection(new_poly1, new_poly2, offset1, offset2, True)
763+
_invtouch, _invoverlap, _invmtv = polygon2.Polygon2.find_intersection(new_poly2, new_poly1, offset2, offset1, True)
764+
765+
help_msg = "points1={}, points2={}, offset1={}, offset2={}, exp_touching={}, " \
766+
"exp_overlap={}, exp_mtv={}, touch={}, overlap={}, mtv={}".format(points1, points2, offset1,
767+
offset2, exp_touching, exp_overlap, exp_mtv, touch, overlap, mtv)
768+
self.assertEqual(exp_touching, touch, help_msg)
769+
self.assertEqual(exp_overlap, overlap, help_msg)
770+
self.assertEqual(exp_touching, _invtouch, help_msg)
771+
self.assertEqual(exp_overlap, _invoverlap, help_msg)
772+
773+
if exp_mtv is not None:
774+
self.assertIsNotNone(mtv, help_msg)
775+
exp_mult_x = exp_mtv[0] * exp_mtv[1].x
776+
exp_mult_y = exp_mtv[0] * exp_mtv[1].y
777+
mult_x = mtv[0] * mtv[1].x
778+
mult_y = mtv[0] * mtv[1].y
779+
self.assertAlmostEqual(exp_mult_x, mult_x, help_msg)
780+
self.assertAlmostEqual(exp_mult_y, mult_y, help_msg)
781+
782+
self.assertIsNotNone(_invmtv, help_msg)
783+
inv_mult_x = _invmtv[0] * _invmtv[1].x
784+
inv_mult_y = _invmtv[0] * _invmtv[1].y
785+
self.assertAlmostEqual(-exp_mult_x, inv_mult_x, help_msg)
786+
self.assertAlmostEqual(-exp_mult_y, inv_mult_y, help_msg)
787+
else:
788+
self.assertIsNone(mtv, help_msg)
789+
self.assertIsNone(_invmtv, help_msg)
790+
791+
_touch, _overlap, _mtv = polygon2.Polygon2.find_intersection(new_poly1, new_poly2, offset1, offset2, False)
792+
793+
self.assertEqual(exp_touching, _touch, help_msg)
794+
self.assertEqual(exp_overlap, _overlap, help_msg)
795+
self.assertIsNone(_mtv, help_msg)
796+
714797
def test_find_intersection_false(self):
715-
pass
798+
poly1 = polygon2.Polygon2([ (0, 1), (0, 3), (5, 3), (5, 1) ])
799+
poly2 = polygon2.Polygon2([ (3, 4), (2, 6), (7, 5) ])
800+
poly3 = polygon2.Polygon2([ (6, 2), (9, 3), (9, 1) ])
801+
802+
self._find_intersection_fuzzer(poly1, poly2, False, False, None)
803+
self._find_intersection_fuzzer(poly1, poly3, False, False, None)
804+
self._find_intersection_fuzzer(poly2, poly3, False, False, None)
716805

717806
def test_find_intersection_touching(self):
718-
pass
807+
poly1 = polygon2.Polygon2([ (3, 3), (3, 6), (7, 5), (5, 3) ])
808+
poly2 = polygon2.Polygon2([ (4, 3), (8, 2), (6, -1) ])
809+
poly3 = polygon2.Polyogn2([ (5, 5.5), (1, 6.5), (3, 7), (7, 6) ])
810+
811+
self._find_intersection_fuzzer(poly1, poly2, True, False, None)
812+
self._find_intersection_fuzzer(poly1, poly3, True, False, None)
719813

720814
def test_find_intersection_overlapping(self):
721-
pass
815+
poly1 = polygon2.Polygon2([ (2, 1), (4, 3), (6, 3), (6, 1) ])
816+
poly2 = polygon2.Polygon2([ (5, 2), (5, 5), (7, 5) ])
817+
poly3 = polygon2.Polygon2([ (1, 3), (3, 3), (3, 1), (1, 1) ])
818+
819+
self._find_intersection_fuzzer(poly1, poly2, False, True, (1, vector2.Vector2(0, -1)))
820+
self._find_intersection_fuzzer(poly1, poly3, False, True, (0.5, vector2.Vector2(-0.70710678118, 0.70710678118)))
722821

723822
if __name__ == '__main__':
724823
unittest.main()

0 commit comments

Comments
 (0)