source: inundation/alpha_shape/test_alpha_shape.py @ 2267

Last change on this file since 2267 was 2141, checked in by duncan, 19 years ago

compiling triangle using setup.

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