1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
import unittest
from numpy import array, allclose
from kiva import agg
class TestPointsInPolygon(unittest.TestCase):
def test_simple_points_in_polygon(self):
polygon = array(((0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)))
points = array(((-1.0, -1.0), (5.0, 5.0), (15.0, 15.0)))
result = agg.points_in_polygon(points, polygon)
self.assertTrue(allclose(array([0,1,0]), result))
return
def test_asymmetric_points_in_polygon(self):
polygon = array(((0.0, 0.0), (20.0, 0.0), (20.0, 10.0), (0.0, 10.0)))
points = array(((5.0, 5.0), (10.0, 5.0), (15.0, 5.0)))
result = agg.points_in_polygon(points, polygon)
self.assertTrue(allclose(array([1,1,1]), result))
return
def test_rectangle(self):
vertices = array(((0,0), (0,10), (10,10), (10,0)))
# Try the lower left.
trial = array(((0,0),))
oe_result = agg.points_in_polygon(trial, vertices)
w_result = agg.points_in_polygon(trial, vertices, True)
self.assertEqual(0, oe_result[0],
"Lower left corner not in polygon. OEF")
self.assertEqual(1, w_result[0],
"Lower left corner not in polygon. Winding")
# Try the center.
trial = array(((5,5),))
oe_result = agg.points_in_polygon(trial, vertices)
w_result = agg.points_in_polygon(trial, vertices, True)
self.assertEqual(1, oe_result[0],
"Center not in polygon. OEF")
self.assertEqual(1, w_result[0],
"Center not in polygon. Winding")
# Try the center.
trial = array(((10,10),))
oe_result = agg.points_in_polygon(trial, vertices)
w_result = agg.points_in_polygon(trial, vertices, True)
self.assertEqual(1, oe_result[0],
"Top-right in polygon. OEF")
self.assertEqual(0, w_result[0],
"Top-right in polygon. Winding")
return
def test_center_removed(self):
# Tests a polygon which resembles the following:
#
# 9------8
# | |
# | 3---+---------2
# | | | |
# | 4---+----5 |
# | | | |
# | 7----6 |
# | |
# 0----------------1
#
# Using the winding rule, the inner square containing the edge (3,4)
# is inside the polygon, while using the odd-even rule, it is outside.
# The inner square with containing the edge (5,6) is outside in both
# cases.
vertices = array(((0,0),
(10, 0),
(10, 8),
(2, 8),
(2, 6),
(8, 6),
(8, 2),
(5, 2),
(5, 10),
(0, 10)))
trial = array(((3,7),))
oe_result = agg.points_in_polygon(trial, vertices)
w_result = agg.points_in_polygon(trial, vertices, True)
self.assertEqual(0, oe_result[0],
"Interior polygon inside: odd-even")
self.assertEqual(1, w_result[0],
"Interior polygon outside: winding")
trial = array(((6,5),))
oe_result = agg.points_in_polygon(trial, vertices)
w_result = agg.points_in_polygon(trial, vertices, True)
self.assertEqual(0, oe_result[0],
"Interior polygon inside: odd-even")
self.assertEqual(0, w_result[0],
"Interior polygon inside: winding")
if __name__ == "__main__":
unittest.main()
#### EOF ######################################################################
|