source: anuga_core/source/anuga/alpha_shape/test_alpha_shape.py @ 5589

Last change on this file since 5589 was 4663, checked in by duncan, 17 years ago

getting rid of xya more. Cleaning up.

File size: 11.0 KB
Line 
1#!/usr/bin/env python
2
3#TEST
4import sys
5import unittest
6from Numeric import allclose
7
8try:
9    from anuga.alpha_shape.alpha_shape import *
10except ImportError: 
11    from alpha_shape import *
12
13class TestCase(unittest.TestCase):
14
15    def setUp(self):
16        pass
17
18         
19    def tearDown(self):
20        pass
21
22    def test_delaunay(self):
23        #print "test_delaunay"
24        a = [0.0, 0.0]
25        b = [1.0, 0.0]
26        c = [2.0, 0.0]
27        d = [2.0, 2.0]
28        e = [1.0, 2.0]
29        f = [0.0, 2.0]
30
31        alpha = Alpha_Shape([a,b,c,d,e,f])
32        result = alpha.get_delaunay()
33        answer = [(0, 1, 5), (5, 1, 4), (4, 2, 3), (2, 4, 1)]
34        assert allclose(answer, result) 
35
36    def test_3_points_on_line(self):
37        #print "test_delaunay"
38        a = [0.0, 0.0]
39        b = [1.0, 0.0]
40        c = [2.0, 0.0]
41
42        try:
43            alpha = Alpha_Shape([a,b,c])
44        except PointError:
45            pass
46        else:
47            self.failUnless(0==1, \
48                        'point list with 2 points did not raise an error!')
49
50
51
52    def test_alpha_1(self):
53        #print "test_alpha"
54        a = [0.0, 0.0]
55        b = [1.0, 0.0]
56        c = [2.0, 0.0]
57        d = [2.0, 2.0]
58        e = [1.0, 2.0]
59        f = [0.0, 2.0]
60
61        alpha = Alpha_Shape([a,b,c,d,e,f])
62        result = alpha.get_boundary()
63        #print "result",result
64        answer = [(5, 0), (0, 1), (4, 5), (2, 3), (3, 4), (1, 2)]
65        assert allclose(answer, result) 
66
67
68    def test_alpha_2(self):
69        #print "test_alpha"
70        a = [0.0, 0.0]
71        b = [2.0, 0.0]
72        c = [4.0, 0.0]
73        cd = [3.0,2.0]
74        d = [4.0, 4.0]
75        e = [2.0, 4.0]
76        f = [0.0, 4.0]
77
78        alpha = Alpha_Shape([a,b,c,cd,d,e,f])
79        result = alpha.get_boundary()
80        #print "result",result
81        answer = [(0, 3), (3, 6), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
82        assert allclose(answer, result) 
83
84
85    def test_alpha_3(self):
86        #print "test_alpha"
87        a = [0.0, 0.0]
88        b = [1.0, 0.0]
89        c = [2.0, 0.0]
90        d = [2.0, 2.0]
91        e = [1.0, 2.0]
92        f = [0.0, 2.0]
93        alf = 1.5
94
95        alpha = Alpha_Shape([a,b,c,d,e,f],alf)
96        result = alpha.get_boundary()
97        #print "result",result
98        answer = [(5, 0), (0, 1), (4, 5), (2, 3), (3, 4), (1, 2)]
99        assert allclose(answer, result) 
100
101
102    def test_boundary_1(self):
103        a = [0.0, 0.0]
104        b = [2.0, 0.0]
105        c = [4.0, 0.0]
106        cd = [3.0,2.0]
107        d = [4.0, 4.0]
108        e = [2.0, 4.0]
109        f = [0.0, 4.0]
110
111        alpha = Alpha_Shape([a,b,c,cd,d,e,f])
112        alpha.set_boundary_type(0,1,0,0)
113        result = alpha.get_boundary()
114        #print "result",result
115        answer = [(0, 3), (3, 6), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
116        assert allclose(answer, result) 
117   
118
119
120    def test_boundary_2(self):
121        a = [0.0, 0.0]
122        b = [2.0, 0.0]
123        c = [4.0, 0.0]
124        cd = [3.0,2.0]
125        d = [4.0, 4.0]
126        e = [2.0, 4.0]
127        f = [0.0, 4.0]
128
129        alpha = Alpha_Shape([a,b,c,cd,d,e,f])
130        alpha.set_boundary_type(0,0,1,0)
131        result = alpha.get_boundary()
132        #print "result",result
133        answer = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 0)]
134        assert allclose(answer, result) 
135
136
137    def test_boundary_3(self):
138        a = [452.0, -300.0]
139        b = [637.0, -322.0]
140        c = [844.0, -474.0]
141        d = [900.0, -585.0]
142        e = [726.0, -492.0]
143        f = [481.0, -354.0]
144        g = [744.0, -388.0]
145
146        alpha = Alpha_Shape([a,b,c,d,e,f,g])
147        alpha.set_boundary_type(0,0,0,1)
148        result = alpha.get_boundary()
149        answer = [(1, 0), (0, 5), (3, 2), (4, 3), (2, 6), (6, 1), (5, 4)]
150        assert allclose(answer, result)
151
152
153       
154    def test_not_enough_points(self):
155        #print "test_not_enought_points"
156        a = [0.0, 0.0]
157        b = [1.0, 0.0]
158
159        try:
160            alpha = Alpha_Shape([a,b])
161        except PointError:
162            pass
163        else:
164            self.failUnless(0==1,
165                        'point list with 2 points did not raise an error!')
166
167## FIXME?  Running from the Comand line isn't in vogue these days
168#  The test was breaking when test_all at the inundation level was running
169# was running it. issue - not running the test in this directory
170    def Bad_test_alpha_stand_alone(self):
171        #print "test_alpha_stand_alone"
172        import os
173        import tempfile
174
175        fileName = tempfile.mktemp(".csv")
176        file = open(fileName,"w")
177        file.write("x,y\n\
1780.0, 0.0\n\
1791.0, 0.0\n\
1802.0, 0.0\n\
1812.0, 2.0\n\
1821.0, 2.0\n\
1830.0, 2.0\n")
184        file.close()
185
186        output_file_name = tempfile.mktemp(".bnd")
187        #(ho, output_file_name) = tempfile.mkstemp(".bnd")
188        command = sys.executable  + 'alpha_shape'+ os.sep +'alpha_shape.py ' \
189                  + fileName  + ' ' + output_file_name
190        #print "command", command
191        os.system(command)
192        os.remove(fileName)
193       
194        file = open(output_file_name,"r")
195        lFile = file.read().split('\n')
196        file.close()
197        os.remove(output_file_name)
198        self.failUnless(lFile[1] == "5,0" and
199                        lFile[2] == "0,1" and 
200                        lFile[3] == "4,5" and 
201                        lFile[4] == "2,3" and 
202                        lFile[5] == "3,4" and 
203                        lFile[6] == "1,2" and 
204                        lFile[7] == ""
205                        ,
206                        'boundary file is wrong')
207
208
209    def test_expand_pinch(self):
210        a = [1.0, 1.0]
211        b = [1.0, 5.0]
212        c = [4.0, 3.0]
213        d = [8.0, 5.0]
214        e = [8.0, 1.0]
215
216        alpha = Alpha_Shape([a,b,c,d,e])
217        alpha.set_boundary_type(raw_boundary=True,
218                          remove_holes=False,
219                          smooth_indents=False,
220                          expand_pinch=True)
221        result = alpha.get_boundary()
222        #print "result",result
223        answer = [(1, 0), (4, 3), (0, 4), (3, 1)]
224        assert allclose(answer, result)
225 
226    def test_sharp_indents(self):
227        a = [3.0, 1.0]
228        b = [5.0, 3.0]
229        c = [4.0, 4.0]
230        d = [3.0, 5.0]
231        e = [1.0, 3.0]
232
233        alpha = Alpha_Shape([a,b,c,d,e])
234        alpha.set_boundary_type(raw_boundary=True,
235                          remove_holes=False,
236                          smooth_indents=True,
237                          expand_pinch=False)
238        result = alpha.get_boundary()
239        #print "result",result
240        answer = [(3, 4), (2, 3), (0, 1), (1, 2), (4, 0)]
241        assert allclose(answer, result)
242
243       
244    def test_small_islands(self):
245        """
246        I couldn't find a small data set that could test this feature...
247        """
248        alpha = Alpha_Shape([(191.0,92.0), \
249(166.0,79.0), \
250(142.0,63.0), \
251(124.0,44.0), \
252(134.0,19.0), \
253(154.0,-9.0), \
254(182.0,-21.0), \
255(200.0,-3.0), \
256(227.0,13.0), \
257(237.0,59.0), \
258(208.0,86.0), \
259(243.0,34.0), \
260(209.0,35.0), \
261(171.0,15.0), \
262(159.0,41.0), \
263(189.0,56.0), \
264(235.0,-6.0), \
265(221.0,-17.0), \
266(204.0,-30.0), \
267(184.0,-41.0), \
268(160.0,-37.0), \
269(144.0,-24.0), \
270(128.0,-8.0), \
271(113.0,9.0), \
272(102.0,29.0), \
273(95.0,46.0), \
274(112.0,61.0), \
275(127.0,72.0), \
276(145.0,85.0), \
277(164.0,100.0), \
278(187.0,112.0), \
279(210.0,107.0), \
280(227.0,94.0), \
281(244.0,74.0), \
282(261.0,49.0), \
283(266.0,20.0), \
284(256.0,1.0), \
285(244.0,-28.0), \
286(228.0,-40.0), \
287(208.0,-50.0), \
288(192.0,-56.0), \
289(169.0,-64.0), \
290(155.0,-58.0), \
291(141.0,-46.0), \
292(129.0,-35.0), \
293(113.0,-17.0), \
294(103.0,-3.0), \
295(90.0,9.0), \
296(78.0,26.0), \
297(70.0,46.0), \
298(86.0,62.0), \
299(101.0,79.0), \
300(118.0,89.0), \
301(135.0,102.0), \
302(152.0,115.0), \
303(177.0,125.0), \
304(192.0,130.0), \
305(209.0,125.0), \
306(227.0,116.0), \
307(234.0,104.0), \
308(249.0,92.0), \
309(258.0,74.0), \
310(264.0,60.0), \
311(280.0,42.0), \
312(279.0,26.0), \
313(274.0,3.0), \
314(270.0,-19.0), \
315(259.0,-34.0), \
316(244.0,-45.0), \
317(229.0,-60.0), \
318(186.0,4.0), \
319(203.0,20.0), \
320(215.0,75.0), \
321(228.0,49.0), \
322(151.0,21.0), \
323(163.0,60.0), \
324(178.0,70.0), \
325(196.0,45.0), \
326(261.0,37.0), \
327(113.0,36.0), \
328(241.0,12.0), \
329(196.0,75.0), \
330(210.0,58.0), \
331(163.248648097,111.633266713), \
332(150.844951796,94.8282588211), \
333(130.438870783,84.0250394618), \
334(112.033385949,72.8217008669), \
335(98.8294511765,61.2182430364), \
336(184.855086816,-50.4150236771), \
337(171.251032808,-52.0155006192), \
338(156.446621093,-49.2146659705), \
339(148.044117147,-42.8127582019), \
340(264.878933922,-2.80083464873), \
341(260.077503096,-12.4036963015), \
342(255.27607227,-28.0083464873), \
343(163.248648097,-22.0065579543), \
344(128.03815537,6.00178853298), \
345(221.265937249,0.0), \
346(202.060213944,-13.6040540081), \
347(166.449601981,-2.80083464873), \
348(149.244474854,6.80202700405), \
349(182.454371403,29.2087041938), \
350(147.243878676,30.4090619004), \
351(126.437678428,28.0083464873), \
352(133.639824668,50.0149044415), \
353(176.852702105,43.612996673), \
354(123.636843779,-24.4072733675), \
355(73.6219393379,35.2104927268), \
356(212.463314068,-39.2116850822), \
357(254.875953034,66.4197930983), \
358(231.669037373,71.6213431603), \
359(85.6255164039,23.6070348964), \
360(98.4293319409,16.4048886568), \
361(223.266533427,-52.0155006192), \
362(208.062002477,-57.2170506811), \
363(243.672614439,86.425754875), \
364(214.06379101,113.633862891), \
365(205.261167828,115.234339833), \
366(194.057829233,120.836009131), \
367(85.2253971684,50.8151429126), \
368(84.0250394618,34.0101350202), \
369(194.457948469,-47.6141890283), \
370(232.86939508,-20.4060810121), \
371(252.875356856,23.2069156609), \
372(232.86939508,27.2081080162), \
373(214.463910245,13.6040540081), \
374(182.054252167,18.4054848345), \
375(163.248648097,29.6088234294), \
376(218.865221836,42.8127582019), \
377(176.45258287,97.2289742343), \
378(154.04590568,70.8211046892), \
379(200.059617766,110.833028242)])
380        alpha.set_boundary_type(raw_boundary=False ,
381                          remove_holes=True,
382                          smooth_indents=True,
383                          expand_pinch=True)
384        result = alpha.get_boundary()
385        #print "result",result
386        answer = [(45, 106), (44, 106), (46, 47), (44, 43), (48, 111), \
387                  (47, 111), (43, 42), (42, 41), (41, 88), (40, 88), \
388                  (107, 48), (49, 107), (49, 50), (50, 51), (52, 53), \
389                  (51, 52), (53, 54), (55, 83), (54, 83), (40, 114), \
390                  (67, 68), (68, 69), (67, 66), (65, 66), (64, 65), \
391                  (69, 114), (57, 56), (56, 55), (58, 57), (64, 63), \
392                  (61, 62), (62, 63), (59, 60), (58, 59), (60, 61), \
393                  (46, 45)]
394        assert allclose(answer, result)   
395#-------------------------------------------------------------
396if __name__ == "__main__":
397    #print "starting tests \n"
398    suite = unittest.makeSuite(TestCase,'test')
399    runner = unittest.TextTestRunner(verbosity=1)
400    runner.run(suite)
401    #print "finished tests \n"
402   
403
404   
405   
406
407
408
Note: See TracBrowser for help on using the repository browser.