1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | #TEST |
---|
4 | |
---|
5 | import unittest |
---|
6 | from Numeric import allclose |
---|
7 | |
---|
8 | from alpha_shape import * |
---|
9 | |
---|
10 | class TestCase(unittest.TestCase): |
---|
11 | |
---|
12 | def setUp(self): |
---|
13 | pass |
---|
14 | |
---|
15 | |
---|
16 | def tearDown(self): |
---|
17 | pass |
---|
18 | |
---|
19 | def test_delaunay(self): |
---|
20 | #print "test_delaunay" |
---|
21 | a = [0.0, 0.0] |
---|
22 | b = [1.0, 0.0] |
---|
23 | c = [2.0, 0.0] |
---|
24 | d = [2.0, 2.0] |
---|
25 | e = [1.0, 2.0] |
---|
26 | f = [0.0, 2.0] |
---|
27 | |
---|
28 | alpha = Alpha_Shape([a,b,c,d,e,f]) |
---|
29 | result = alpha.get_delaunay() |
---|
30 | answer = [(0, 1, 5), (5, 1, 4), (4, 2, 3), (2, 4, 1)] |
---|
31 | assert allclose(answer, result) |
---|
32 | |
---|
33 | def test_3_points_on_line(self): |
---|
34 | #print "test_delaunay" |
---|
35 | a = [0.0, 0.0] |
---|
36 | b = [1.0, 0.0] |
---|
37 | c = [2.0, 0.0] |
---|
38 | |
---|
39 | try: |
---|
40 | alpha = Alpha_Shape([a,b,c]) |
---|
41 | except PointError: |
---|
42 | pass |
---|
43 | else: |
---|
44 | self.failUnless(0==1, \ |
---|
45 | 'point list with 2 points did not raise an error!') |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | def test_alpha_1(self): |
---|
50 | #print "test_alpha" |
---|
51 | a = [0.0, 0.0] |
---|
52 | b = [1.0, 0.0] |
---|
53 | c = [2.0, 0.0] |
---|
54 | d = [2.0, 2.0] |
---|
55 | e = [1.0, 2.0] |
---|
56 | f = [0.0, 2.0] |
---|
57 | |
---|
58 | alpha = Alpha_Shape([a,b,c,d,e,f]) |
---|
59 | result = alpha.get_boundary() |
---|
60 | #print "result",result |
---|
61 | answer = [(5, 0), (0, 1), (4, 5), (2, 3), (3, 4), (1, 2)] |
---|
62 | assert allclose(answer, result) |
---|
63 | |
---|
64 | |
---|
65 | def test_alpha_2(self): |
---|
66 | #print "test_alpha" |
---|
67 | a = [0.0, 0.0] |
---|
68 | b = [2.0, 0.0] |
---|
69 | c = [4.0, 0.0] |
---|
70 | cd = [3.0,2.0] |
---|
71 | d = [4.0, 4.0] |
---|
72 | e = [2.0, 4.0] |
---|
73 | f = [0.0, 4.0] |
---|
74 | |
---|
75 | alpha = Alpha_Shape([a,b,c,cd,d,e,f]) |
---|
76 | result = alpha.get_boundary() |
---|
77 | #print "result",result |
---|
78 | answer = [(0, 3), (3, 6), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
---|
79 | assert allclose(answer, result) |
---|
80 | |
---|
81 | |
---|
82 | def test_alpha_3(self): |
---|
83 | #print "test_alpha" |
---|
84 | a = [0.0, 0.0] |
---|
85 | b = [1.0, 0.0] |
---|
86 | c = [2.0, 0.0] |
---|
87 | d = [2.0, 2.0] |
---|
88 | e = [1.0, 2.0] |
---|
89 | f = [0.0, 2.0] |
---|
90 | alf = 1.5 |
---|
91 | |
---|
92 | alpha = Alpha_Shape([a,b,c,d,e,f],alf) |
---|
93 | result = alpha.get_boundary() |
---|
94 | #print "result",result |
---|
95 | answer = [(5, 0), (0, 1), (4, 5), (2, 3), (3, 4), (1, 2)] |
---|
96 | assert allclose(answer, result) |
---|
97 | |
---|
98 | |
---|
99 | def test_boundary_1(self): |
---|
100 | a = [0.0, 0.0] |
---|
101 | b = [2.0, 0.0] |
---|
102 | c = [4.0, 0.0] |
---|
103 | cd = [3.0,2.0] |
---|
104 | d = [4.0, 4.0] |
---|
105 | e = [2.0, 4.0] |
---|
106 | f = [0.0, 4.0] |
---|
107 | |
---|
108 | alpha = Alpha_Shape([a,b,c,cd,d,e,f]) |
---|
109 | alpha.set_boundary_type(0,1,0,0) |
---|
110 | result = alpha.get_boundary() |
---|
111 | #print "result",result |
---|
112 | answer = [(0, 3), (3, 6), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
---|
113 | assert allclose(answer, result) |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | def test_boundary_2(self): |
---|
118 | a = [0.0, 0.0] |
---|
119 | b = [2.0, 0.0] |
---|
120 | c = [4.0, 0.0] |
---|
121 | cd = [3.0,2.0] |
---|
122 | d = [4.0, 4.0] |
---|
123 | e = [2.0, 4.0] |
---|
124 | f = [0.0, 4.0] |
---|
125 | |
---|
126 | alpha = Alpha_Shape([a,b,c,cd,d,e,f]) |
---|
127 | alpha.set_boundary_type(0,0,1,0) |
---|
128 | result = alpha.get_boundary() |
---|
129 | #print "result",result |
---|
130 | answer = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 0)] |
---|
131 | assert allclose(answer, result) |
---|
132 | |
---|
133 | |
---|
134 | def test_boundary_3(self): |
---|
135 | a = [452.0, -300.0] |
---|
136 | b = [637.0, -322.0] |
---|
137 | c = [844.0, -474.0] |
---|
138 | d = [900.0, -585.0] |
---|
139 | e = [726.0, -492.0] |
---|
140 | f = [481.0, -354.0] |
---|
141 | g = [744.0, -388.0] |
---|
142 | |
---|
143 | alpha = Alpha_Shape([a,b,c,d,e,f,g]) |
---|
144 | alpha.set_boundary_type(0,0,0,1) |
---|
145 | result = alpha.get_boundary() |
---|
146 | answer = [(1, 0), (0, 5), (3, 2), (4, 3), (2, 6), (6, 1), (5, 4)] |
---|
147 | assert allclose(answer, result) |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | def test_not_enough_points(self): |
---|
152 | #print "test_not_enought_points" |
---|
153 | a = [0.0, 0.0] |
---|
154 | b = [1.0, 0.0] |
---|
155 | |
---|
156 | try: |
---|
157 | alpha = Alpha_Shape([a,b]) |
---|
158 | except PointError: |
---|
159 | pass |
---|
160 | else: |
---|
161 | self.failUnless(0==1, |
---|
162 | 'point list with 2 points did not raise an error!') |
---|
163 | |
---|
164 | |
---|
165 | def test_alpha_stand_alone(self): |
---|
166 | #print "test_alpha_stand_alone" |
---|
167 | import os |
---|
168 | import tempfile |
---|
169 | |
---|
170 | fileName = tempfile.mktemp(".xya") |
---|
171 | #(h,fileName) = tempfile.mkstemp(".xya") |
---|
172 | file = open(fileName,"w") |
---|
173 | file.write("\n\ |
---|
174 | 0.0, 0.0\n\ |
---|
175 | 1.0, 0.0\n\ |
---|
176 | 2.0, 0.0\n\ |
---|
177 | 2.0, 2.0\n\ |
---|
178 | 1.0, 2.0\n\ |
---|
179 | 0.0, 2.0\n") |
---|
180 | file.close() |
---|
181 | |
---|
182 | output_file_name = tempfile.mktemp(".bnd") |
---|
183 | #(ho, output_file_name) = tempfile.mkstemp(".bnd") |
---|
184 | command = 'python alpha_shape.py ' + fileName + ' ' + output_file_name |
---|
185 | #print "command", command |
---|
186 | os.system(command) |
---|
187 | os.remove(fileName) |
---|
188 | |
---|
189 | file = open(output_file_name,"r") |
---|
190 | lFile = file.read().split('\n') |
---|
191 | file.close() |
---|
192 | os.remove(output_file_name) |
---|
193 | self.failUnless(lFile[1] == "5,0" and |
---|
194 | lFile[2] == "0,1" and |
---|
195 | lFile[3] == "4,5" and |
---|
196 | lFile[4] == "2,3" and |
---|
197 | lFile[5] == "3,4" and |
---|
198 | lFile[6] == "1,2" and |
---|
199 | lFile[7] == "" |
---|
200 | , |
---|
201 | 'boundary file is wrong') |
---|
202 | |
---|
203 | |
---|
204 | #------------------------------------------------------------- |
---|
205 | if __name__ == "__main__": |
---|
206 | #print "starting tests \n" |
---|
207 | suite = unittest.makeSuite(TestCase,'test') |
---|
208 | runner = unittest.TextTestRunner(verbosity=1) |
---|
209 | runner.run(suite) |
---|
210 | #print "finished tests \n" |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | |
---|
217 | |
---|