source: inundation/alpha_shape/test_alpha_shape.py @ 1756

Last change on this file since 1756 was 1420, checked in by duncan, 20 years ago

dsg added comments and moved some code around

File size: 10.7 KB
Line 
1#!/usr/bin/env python
2
3#TEST
4
5import unittest
6from Numeric import allclose
7
8from alpha_shape import *
9
10class 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\
1740.0, 0.0\n\
1751.0, 0.0\n\
1762.0, 0.0\n\
1772.0, 2.0\n\
1781.0, 2.0\n\
1790.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    def test_expand_pinch(self):
205        a = [1.0, 1.0]
206        b = [1.0, 5.0]
207        c = [4.0, 3.0]
208        d = [8.0, 5.0]
209        e = [8.0, 1.0]
210
211        alpha = Alpha_Shape([a,b,c,d,e])
212        alpha.set_boundary_type(raw_boundary=True,
213                          remove_holes=False,
214                          smooth_indents=False,
215                          expand_pinch=True)
216        result = alpha.get_boundary()
217        #print "result",result
218        answer = [(1, 0), (4, 3), (0, 4), (3, 1)]
219        assert allclose(answer, result)
220 
221    def test_sharp_indents(self):
222        a = [3.0, 1.0]
223        b = [5.0, 3.0]
224        c = [4.0, 4.0]
225        d = [3.0, 5.0]
226        e = [1.0, 3.0]
227
228        alpha = Alpha_Shape([a,b,c,d,e])
229        alpha.set_boundary_type(raw_boundary=True,
230                          remove_holes=False,
231                          smooth_indents=True,
232                          expand_pinch=False)
233        result = alpha.get_boundary()
234        #print "result",result
235        answer = [(3, 4), (2, 3), (0, 1), (1, 2), (4, 0)]
236        assert allclose(answer, result)
237
238       
239    def test_small_islands(self):
240        """
241        I couldn't find a small data set that could test this feature...
242        """
243        alpha = Alpha_Shape([(191.0,92.0), \
244(166.0,79.0), \
245(142.0,63.0), \
246(124.0,44.0), \
247(134.0,19.0), \
248(154.0,-9.0), \
249(182.0,-21.0), \
250(200.0,-3.0), \
251(227.0,13.0), \
252(237.0,59.0), \
253(208.0,86.0), \
254(243.0,34.0), \
255(209.0,35.0), \
256(171.0,15.0), \
257(159.0,41.0), \
258(189.0,56.0), \
259(235.0,-6.0), \
260(221.0,-17.0), \
261(204.0,-30.0), \
262(184.0,-41.0), \
263(160.0,-37.0), \
264(144.0,-24.0), \
265(128.0,-8.0), \
266(113.0,9.0), \
267(102.0,29.0), \
268(95.0,46.0), \
269(112.0,61.0), \
270(127.0,72.0), \
271(145.0,85.0), \
272(164.0,100.0), \
273(187.0,112.0), \
274(210.0,107.0), \
275(227.0,94.0), \
276(244.0,74.0), \
277(261.0,49.0), \
278(266.0,20.0), \
279(256.0,1.0), \
280(244.0,-28.0), \
281(228.0,-40.0), \
282(208.0,-50.0), \
283(192.0,-56.0), \
284(169.0,-64.0), \
285(155.0,-58.0), \
286(141.0,-46.0), \
287(129.0,-35.0), \
288(113.0,-17.0), \
289(103.0,-3.0), \
290(90.0,9.0), \
291(78.0,26.0), \
292(70.0,46.0), \
293(86.0,62.0), \
294(101.0,79.0), \
295(118.0,89.0), \
296(135.0,102.0), \
297(152.0,115.0), \
298(177.0,125.0), \
299(192.0,130.0), \
300(209.0,125.0), \
301(227.0,116.0), \
302(234.0,104.0), \
303(249.0,92.0), \
304(258.0,74.0), \
305(264.0,60.0), \
306(280.0,42.0), \
307(279.0,26.0), \
308(274.0,3.0), \
309(270.0,-19.0), \
310(259.0,-34.0), \
311(244.0,-45.0), \
312(229.0,-60.0), \
313(186.0,4.0), \
314(203.0,20.0), \
315(215.0,75.0), \
316(228.0,49.0), \
317(151.0,21.0), \
318(163.0,60.0), \
319(178.0,70.0), \
320(196.0,45.0), \
321(261.0,37.0), \
322(113.0,36.0), \
323(241.0,12.0), \
324(196.0,75.0), \
325(210.0,58.0), \
326(163.248648097,111.633266713), \
327(150.844951796,94.8282588211), \
328(130.438870783,84.0250394618), \
329(112.033385949,72.8217008669), \
330(98.8294511765,61.2182430364), \
331(184.855086816,-50.4150236771), \
332(171.251032808,-52.0155006192), \
333(156.446621093,-49.2146659705), \
334(148.044117147,-42.8127582019), \
335(264.878933922,-2.80083464873), \
336(260.077503096,-12.4036963015), \
337(255.27607227,-28.0083464873), \
338(163.248648097,-22.0065579543), \
339(128.03815537,6.00178853298), \
340(221.265937249,0.0), \
341(202.060213944,-13.6040540081), \
342(166.449601981,-2.80083464873), \
343(149.244474854,6.80202700405), \
344(182.454371403,29.2087041938), \
345(147.243878676,30.4090619004), \
346(126.437678428,28.0083464873), \
347(133.639824668,50.0149044415), \
348(176.852702105,43.612996673), \
349(123.636843779,-24.4072733675), \
350(73.6219393379,35.2104927268), \
351(212.463314068,-39.2116850822), \
352(254.875953034,66.4197930983), \
353(231.669037373,71.6213431603), \
354(85.6255164039,23.6070348964), \
355(98.4293319409,16.4048886568), \
356(223.266533427,-52.0155006192), \
357(208.062002477,-57.2170506811), \
358(243.672614439,86.425754875), \
359(214.06379101,113.633862891), \
360(205.261167828,115.234339833), \
361(194.057829233,120.836009131), \
362(85.2253971684,50.8151429126), \
363(84.0250394618,34.0101350202), \
364(194.457948469,-47.6141890283), \
365(232.86939508,-20.4060810121), \
366(252.875356856,23.2069156609), \
367(232.86939508,27.2081080162), \
368(214.463910245,13.6040540081), \
369(182.054252167,18.4054848345), \
370(163.248648097,29.6088234294), \
371(218.865221836,42.8127582019), \
372(176.45258287,97.2289742343), \
373(154.04590568,70.8211046892), \
374(200.059617766,110.833028242)])
375        alpha.set_boundary_type(raw_boundary=False ,
376                          remove_holes=True,
377                          smooth_indents=True,
378                          expand_pinch=True)
379        result = alpha.get_boundary()
380        #print "result",result
381        answer = [(45, 106), (44, 106), (46, 47), (44, 43), (48, 111), \
382                  (47, 111), (43, 42), (42, 41), (41, 88), (40, 88), \
383                  (107, 48), (49, 107), (49, 50), (50, 51), (52, 53), \
384                  (51, 52), (53, 54), (55, 83), (54, 83), (40, 114), \
385                  (67, 68), (68, 69), (67, 66), (65, 66), (64, 65), \
386                  (69, 114), (57, 56), (56, 55), (58, 57), (64, 63), \
387                  (61, 62), (62, 63), (59, 60), (58, 59), (60, 61), \
388                  (46, 45)]
389        assert allclose(answer, result)   
390#-------------------------------------------------------------
391if __name__ == "__main__":
392    #print "starting tests \n"
393    suite = unittest.makeSuite(TestCase,'test')
394    runner = unittest.TextTestRunner(verbosity=1)
395    runner.run(suite)
396    #print "finished tests \n"
397   
398
399   
400   
401
402
403
Note: See TracBrowser for help on using the repository browser.