1 |
bh |
1589 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with the software for details. |
7 |
|
|
|
8 |
|
|
"""Test Thuban.UI.hittest""" |
9 |
|
|
|
10 |
|
|
__version__ = "$Revision$" |
11 |
|
|
# $Source$ |
12 |
|
|
# $Id$ |
13 |
|
|
|
14 |
|
|
import unittest |
15 |
|
|
|
16 |
|
|
import support |
17 |
|
|
support.initthuban() |
18 |
|
|
|
19 |
|
|
from Thuban.UI.hittest import line_hit, polygon_hit, arc_hit |
20 |
|
|
|
21 |
|
|
|
22 |
|
|
class TestLineHit(unittest.TestCase): |
23 |
|
|
|
24 |
|
|
def test_inside(self): |
25 |
|
|
"""Test line_hit with point that would be on the 'inside' |
26 |
|
|
|
27 |
|
|
Inside here means that the point is to the right of the line but |
28 |
|
|
not so close that the line itself is hit. |
29 |
|
|
""" |
30 |
|
|
self.assertEquals(line_hit(0, 0, 10, 10, 20, 5), 1) |
31 |
|
|
self.assertEquals(line_hit(10, 10, 0, 0, 20, 5), 1) |
32 |
|
|
self.assertEquals(line_hit(10, 10, 0, 0, 8, 5), 1) |
33 |
|
|
|
34 |
|
|
def test_outside(self): |
35 |
|
|
"""Test line_hit with point that would be on the 'outside' |
36 |
|
|
|
37 |
|
|
Inside here means that the point is to the left of the line but |
38 |
|
|
not so close that the line itself is hit. |
39 |
|
|
""" |
40 |
|
|
self.assertEquals(line_hit(0, 0, 10, 10, -10, 5), 0) |
41 |
|
|
self.assertEquals(line_hit(10, 10, 0, 0, -10, 5), 0) |
42 |
|
|
self.assertEquals(line_hit(10, 10, 0, 0, 2, 5), 0) |
43 |
|
|
|
44 |
|
|
def test_on_line(self): |
45 |
|
|
"""Test line_hit with point that would be on or near the line""" |
46 |
|
|
self.assertEquals(line_hit(0, 0, 10, -10, 5, -5), -1) |
47 |
|
|
self.assertEquals(line_hit(0, 0, 10, -10, 10, -10), -1) |
48 |
|
|
self.assertEquals(line_hit(0, 0, 10, -10, 5, -6), -1) |
49 |
|
|
|
50 |
|
|
def test_horizontal_line(self): |
51 |
|
|
"""Test line_hit with a horizontal line |
52 |
|
|
|
53 |
|
|
For a horizonal line, line_hit will never return 1. It will |
54 |
|
|
return -1 though for points close to or on the line. |
55 |
|
|
""" |
56 |
|
|
self.assertEquals(line_hit(10, 10, 10, 10, -10, 10), 0) |
57 |
|
|
self.assertEquals(line_hit(10, 10, 15, 10, 20, 10), 0) |
58 |
|
|
self.assertEquals(line_hit(10, 10, 15, 10, 12, 10), -1) |
59 |
|
|
|
60 |
|
|
# Hits near the line will also be counted as line hits. |
61 |
|
|
self.assertEquals(line_hit(10, 10, 15, 10, 12, 11), -1) |
62 |
|
|
self.assertEquals(line_hit(10, 10, 15, 10, 12, 9), -1) |
63 |
|
|
|
64 |
|
|
def test_upper_ignored(self): |
65 |
|
|
"""Test line_hit with a point whose ray would hit the upper end point |
66 |
|
|
|
67 |
|
|
The upper end point is not hit to avoid problems with hit |
68 |
|
|
testing for polygons. |
69 |
|
|
""" |
70 |
|
|
self.assertEquals(line_hit(-100, 20, 10, -10, 1000, 20), 0) |
71 |
|
|
self.assertEquals(line_hit(10, -10, -100, 20, 1000, 20), 0) |
72 |
|
|
|
73 |
|
|
|
74 |
|
|
class TestPolygonHit(unittest.TestCase): |
75 |
|
|
|
76 |
|
|
def test_simple_inside(self): |
77 |
|
|
"""Test polygon_hit with simple polygon and inside point""" |
78 |
|
|
self.assertEquals(polygon_hit([[(0, 0), (10, 0), (10, 10), (0, 10), |
79 |
|
|
(0, 0)]], 5, 5), |
80 |
|
|
1) |
81 |
|
|
|
82 |
|
|
def test_simple_outside(self): |
83 |
|
|
"""Test polygon_hit with simple polygon and outside point""" |
84 |
|
|
self.assertEquals(polygon_hit([[(0, 0), (10, 0), (10, 10), (0, 10), |
85 |
|
|
(0, 0)]], 20, 5), |
86 |
|
|
0) |
87 |
|
|
self.assertEquals(polygon_hit([[(0, 0), (10, 0), (10, 10), (0, 10), |
88 |
|
|
(0, 0)]], 20, 100), |
89 |
|
|
0) |
90 |
|
|
|
91 |
|
|
self.assertEquals(polygon_hit([[(0, 0), (10, 0), (10, 10), (0, 10), |
92 |
|
|
(0, 0)]], 20, -10), |
93 |
|
|
0) |
94 |
|
|
|
95 |
|
|
def test_holes_outside(self): |
96 |
|
|
"""Test polygon_hit with polygon with holes and outside point""" |
97 |
|
|
points = [[(0, 0), (100, 0), (100, 100), (0, 100), (0, 0)], |
98 |
|
|
[(20, 20), (80, 20), (80, 80), (20, 80), (20, 20)]] |
99 |
|
|
self.assertEquals(polygon_hit(points, 50, 50), 0) |
100 |
|
|
self.assertEquals(polygon_hit(points, 130, 50), 0) |
101 |
|
|
|
102 |
|
|
def test_holes_inside(self): |
103 |
|
|
"""Test polygon_hit with polygon with holes and inside point""" |
104 |
|
|
points = [[(0, 0), (100, 0), (100, 100), (0, 100), (0, 0)], |
105 |
|
|
[(20, 20), (80, 20), (80, 80), (20, 80), (20, 20)]] |
106 |
|
|
self.assertEquals(polygon_hit(points, 50, 90), 1) |
107 |
|
|
self.assertEquals(polygon_hit(points, 90, 50), 1) |
108 |
|
|
self.assertEquals(polygon_hit(points, 10, 50), 1) |
109 |
|
|
|
110 |
|
|
def test_vertex(self): |
111 |
|
|
"""Test polygon_hit with simple polygon and point whose ray hits corner |
112 |
|
|
""" |
113 |
|
|
points = [[(-10, -5), (-10, 20), (10, 50), (10, 0), (-10, -5)]] |
114 |
|
|
self.assertEquals(polygon_hit(points, 0, 20), 1) |
115 |
|
|
|
116 |
|
|
def test_border(self): |
117 |
|
|
"""Test polygon_hit with simple polygon and point on/near border""" |
118 |
|
|
points = [[(-10, -5), (-10, 20), (10, 50), (10, 0), (-10, -5)]] |
119 |
|
|
self.assertEquals(polygon_hit(points, -9, 20), -1) |
120 |
|
|
self.assertEquals(polygon_hit(points, 0, -2), -1) |
121 |
|
|
self.assertEquals(polygon_hit(points, 0, -4), -1) |
122 |
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
class TestArcHit(unittest.TestCase): |
126 |
|
|
|
127 |
|
|
def test_simple_hit(self): |
128 |
|
|
"""Test arc_hit with simple arc and point on/near arc""" |
129 |
|
|
self.assertEquals(arc_hit([[(0, 0), (10, 0), (10, 10), (20, 20)]], |
130 |
|
|
5, 0), 1) |
131 |
|
|
self.assertEquals(arc_hit([[(0, 0), (10, 0), (10, 10), (20, 20)]], |
132 |
|
|
15, 16), 1) |
133 |
|
|
|
134 |
|
|
def test_simple_not_hit(self): |
135 |
|
|
"""Test arc_hit with simple arc and point not on arc""" |
136 |
|
|
self.assertEquals(arc_hit([[(0, 0), (10, 0), (10, 10), (20, 20)]], |
137 |
|
|
5, 100), 0) |
138 |
|
|
self.assertEquals(arc_hit([[(0, 0), (10, 0), (10, 10), (20, 20)]], |
139 |
|
|
13, 7), 0) |
140 |
|
|
|
141 |
|
|
def test_corner(self): |
142 |
|
|
"""Test arc_hit with point on/near arc's corner""" |
143 |
|
|
self.assertEquals(arc_hit([[(0, 0), (10, 0), (10, 10), (20, 20)]], |
144 |
|
|
11, 10), 1) |
145 |
|
|
self.assertEquals(arc_hit([[(0, 0), (10, 0), (10, 10), (20, 20)]], |
146 |
|
|
15, 10), 0) |
147 |
|
|
|
148 |
|
|
if __name__ == "__main__": |
149 |
|
|
support.run_tests() |