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 | |
---|
100 | def test_not_enough_points(self): |
---|
101 | #print "test_not_enought_points" |
---|
102 | a = [0.0, 0.0] |
---|
103 | b = [1.0, 0.0] |
---|
104 | |
---|
105 | try: |
---|
106 | alpha = Alpha_Shape([a,b]) |
---|
107 | except PointError: |
---|
108 | pass |
---|
109 | else: |
---|
110 | self.failUnless(0==1, |
---|
111 | 'point list with 2 points did not raise an error!') |
---|
112 | |
---|
113 | |
---|
114 | def test_alpha_stand_alone(self): |
---|
115 | #print "test_alpha_stand_alone" |
---|
116 | import os |
---|
117 | import tempfile |
---|
118 | |
---|
119 | fileName = tempfile.mktemp(".xya") |
---|
120 | #(h,fileName) = tempfile.mkstemp(".xya") |
---|
121 | file = open(fileName,"w") |
---|
122 | file.write("title row \n\ |
---|
123 | 0.0, 0.0\n\ |
---|
124 | 1.0, 0.0\n\ |
---|
125 | 2.0, 0.0\n\ |
---|
126 | 2.0, 2.0\n\ |
---|
127 | 1.0, 2.0\n\ |
---|
128 | 0.0, 2.0\n") |
---|
129 | file.close() |
---|
130 | |
---|
131 | output_file_name = tempfile.mktemp(".bnd") |
---|
132 | #(ho, output_file_name) = tempfile.mkstemp(".bnd") |
---|
133 | command = 'python alpha_shape.py ' + fileName + ' ' + output_file_name |
---|
134 | #print "command", command |
---|
135 | os.system(command) |
---|
136 | os.remove(fileName) |
---|
137 | |
---|
138 | file = open(output_file_name,"r") |
---|
139 | lFile = file.read().split('\n') |
---|
140 | file.close() |
---|
141 | os.remove(output_file_name) |
---|
142 | self.failUnless(lFile[1] == "5,0" and |
---|
143 | lFile[2] == "0,1" and |
---|
144 | lFile[3] == "4,5" and |
---|
145 | lFile[4] == "2,3" and |
---|
146 | lFile[5] == "3,4" and |
---|
147 | lFile[6] == "1,2" and |
---|
148 | lFile[7] == "" |
---|
149 | , |
---|
150 | 'boundary file is wrong') |
---|
151 | |
---|
152 | |
---|
153 | #------------------------------------------------------------- |
---|
154 | if __name__ == "__main__": |
---|
155 | #print "starting tests \n" |
---|
156 | suite = unittest.makeSuite(TestCase,'test') |
---|
157 | runner = unittest.TextTestRunner(verbosity=1) |
---|
158 | runner.run(suite) |
---|
159 | #print "finished tests \n" |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | |
---|