]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Tests/Tetrahedron.hs
Rename the spline project to spline3.
[spline3.git] / src / Tests / Tetrahedron.hs
1 module Tests.Tetrahedron
2 where
3
4 import Test.HUnit
5 import Test.QuickCheck
6
7 import Cube
8 import Point
9 import Tests.Cube()
10 import Tetrahedron
11 import ThreeDimensional
12
13 instance Arbitrary Tetrahedron where
14 arbitrary = do
15 rnd_c0 <- arbitrary :: Gen Cube
16 rnd_v0 <- arbitrary :: Gen Point
17 rnd_v1 <- arbitrary :: Gen Point
18 rnd_v2 <- arbitrary :: Gen Point
19 rnd_v3 <- arbitrary :: Gen Point
20 return (Tetrahedron rnd_c0 rnd_v0 rnd_v1 rnd_v2 rnd_v3)
21
22 almost_equals :: Double -> Double -> Bool
23 almost_equals x y = (abs (x - y)) < 0.0001
24
25 (~=) :: Double -> Double -> Bool
26 (~=) = almost_equals
27
28
29 -- HUnit Tests
30
31 -- Since p0, p1, p2 are in clockwise order, we expect the volume here
32 -- to be negative.
33 test_volume1 :: Test
34 test_volume1 =
35 TestCase $ assertEqual "volume is correct" True (vol ~= (-1/3))
36 where
37 p0 = (0, -0.5, 0)
38 p1 = (0, 0.5, 0)
39 p2 = (2, 0, 0)
40 p3 = (1, 0, 1)
41 t = Tetrahedron { cube = empty_cube,
42 v0 = p0,
43 v1 = p1,
44 v2 = p2,
45 v3 = p3 }
46 vol = volume t
47
48
49 -- Now, p0, p1, and p2 are in counter-clockwise order. The volume
50 -- should therefore be positive.
51 test_volume2 :: Test
52 test_volume2 =
53 TestCase $ assertEqual "volume is correct" True (vol ~= (1/3))
54 where
55 p0 = (0, -0.5, 0)
56 p1 = (2, 0, 0)
57 p2 = (0, 0.5, 0)
58 p3 = (1, 0, 1)
59 t = Tetrahedron { cube = empty_cube,
60 v0 = p0,
61 v1 = p1,
62 v2 = p2,
63 v3 = p3 }
64 vol = volume t
65
66 test_contains_point1 :: Test
67 test_contains_point1 =
68 TestCase $ assertEqual "contains an inner point" True (contains_point t inner_point)
69 where
70 p0 = (0, -0.5, 0)
71 p1 = (0, 0.5, 0)
72 p2 = (2, 0, 0)
73 p3 = (1, 0, 1)
74 inner_point = (1, 0, 0.5)
75 t = Tetrahedron { cube = empty_cube,
76 v0 = p0,
77 v1 = p1,
78 v2 = p2,
79 v3 = p3 }
80
81
82 test_doesnt_contain_point1 :: Test
83 test_doesnt_contain_point1 =
84 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
85 where
86 p0 = (0, -0.5, 0)
87 p1 = (0, 0.5, 0)
88 p2 = (2, 0, 0)
89 p3 = (1, 0, 1)
90 exterior_point = (5, 2, -9.0212)
91 c_empty = empty_cube
92 t = Tetrahedron { cube = c_empty,
93 v0 = p0,
94 v1 = p1,
95 v2 = p2,
96 v3 = p3 }
97
98
99 test_doesnt_contain_point2 :: Test
100 test_doesnt_contain_point2 =
101 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
102 where
103 p0 = (0, 1, 1)
104 p1 = (1, 1, 1)
105 p2 = (0.5, 0.5, 1)
106 p3 = (0.5, 0.5, 0.5)
107 exterior_point = (0, 0, 0)
108 c_empty = empty_cube
109 t = Tetrahedron { cube = c_empty,
110 v0 = p0,
111 v1 = p1,
112 v2 = p2,
113 v3 = p3 }
114
115 test_doesnt_contain_point3 :: Test
116 test_doesnt_contain_point3 =
117 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
118 where
119 p0 = (1, 1, 1)
120 p1 = (1, 0, 1)
121 p2 = (0.5, 0.5, 1)
122 p3 = (0.5, 0.5, 0.5)
123 exterior_point = (0, 0, 0)
124 c_empty = empty_cube
125 t = Tetrahedron { cube = c_empty,
126 v0 = p0,
127 v1 = p1,
128 v2 = p2,
129 v3 = p3 }
130
131 test_doesnt_contain_point4 :: Test
132 test_doesnt_contain_point4 =
133 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
134 where
135 p0 = (1, 0, 1)
136 p1 = (0, 0, 1)
137 p2 = (0.5, 0.5, 1)
138 p3 = (0.5, 0.5, 0.5)
139 exterior_point = (0, 0, 0)
140 c_empty = empty_cube
141 t = Tetrahedron { cube = c_empty,
142 v0 = p0,
143 v1 = p1,
144 v2 = p2,
145 v3 = p3 }
146
147 test_doesnt_contain_point5 :: Test
148 test_doesnt_contain_point5 =
149 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
150 where
151 p0 = (0, 0, 1)
152 p1 = (0, 1, 1)
153 p2 = (0.5, 0.5, 1)
154 p3 = (0.5, 0.5, 0.5)
155 exterior_point = (0, 0, 0)
156 c_empty = empty_cube
157 t = Tetrahedron { cube = c_empty,
158 v0 = p0,
159 v1 = p1,
160 v2 = p2,
161 v3 = p3 }
162
163 tetrahedron_tests :: [Test]
164 tetrahedron_tests = [test_volume1,
165 test_volume2,
166 test_contains_point1,
167 test_doesnt_contain_point1,
168 test_doesnt_contain_point2,
169 test_doesnt_contain_point3,
170 test_doesnt_contain_point4,
171 test_doesnt_contain_point5 ]
172
173 prop_b0_v0_always_unity :: Tetrahedron -> Property
174 prop_b0_v0_always_unity t =
175 (volume t) > 0 ==> (b0 t) (v0 t) ~= 1.0
176
177 prop_b0_v1_always_zero :: Tetrahedron -> Property
178 prop_b0_v1_always_zero t =
179 (volume t) > 0 ==> (b0 t) (v1 t) ~= 0
180
181 prop_b0_v2_always_zero :: Tetrahedron -> Property
182 prop_b0_v2_always_zero t =
183 (volume t) > 0 ==> (b0 t) (v2 t) ~= 0
184
185 prop_b0_v3_always_zero :: Tetrahedron -> Property
186 prop_b0_v3_always_zero t =
187 (volume t) > 0 ==> (b0 t) (v3 t) ~= 0
188
189 prop_b1_v1_always_unity :: Tetrahedron -> Property
190 prop_b1_v1_always_unity t =
191 (volume t) > 0 ==> (b1 t) (v1 t) ~= 1.0
192
193 prop_b1_v0_always_zero :: Tetrahedron -> Property
194 prop_b1_v0_always_zero t =
195 (volume t) > 0 ==> (b1 t) (v0 t) ~= 0
196
197 prop_b1_v2_always_zero :: Tetrahedron -> Property
198 prop_b1_v2_always_zero t =
199 (volume t) > 0 ==> (b1 t) (v2 t) ~= 0
200
201 prop_b1_v3_always_zero :: Tetrahedron -> Property
202 prop_b1_v3_always_zero t =
203 (volume t) > 0 ==> (b1 t) (v3 t) ~= 0
204
205 prop_b2_v2_always_unity :: Tetrahedron -> Property
206 prop_b2_v2_always_unity t =
207 (volume t) > 0 ==> (b2 t) (v2 t) ~= 1.0
208
209 prop_b2_v0_always_zero :: Tetrahedron -> Property
210 prop_b2_v0_always_zero t =
211 (volume t) > 0 ==> (b2 t) (v0 t) ~= 0
212
213 prop_b2_v1_always_zero :: Tetrahedron -> Property
214 prop_b2_v1_always_zero t =
215 (volume t) > 0 ==> (b2 t) (v1 t) ~= 0
216
217 prop_b2_v3_always_zero :: Tetrahedron -> Property
218 prop_b2_v3_always_zero t =
219 (volume t) > 0 ==> (b2 t) (v3 t) ~= 0
220
221 prop_b3_v3_always_unity :: Tetrahedron -> Property
222 prop_b3_v3_always_unity t =
223 (volume t) > 0 ==> (b3 t) (v3 t) ~= 1.0
224
225 prop_b3_v0_always_zero :: Tetrahedron -> Property
226 prop_b3_v0_always_zero t =
227 (volume t) > 0 ==> (b3 t) (v0 t) ~= 0
228
229 prop_b3_v1_always_zero :: Tetrahedron -> Property
230 prop_b3_v1_always_zero t =
231 (volume t) > 0 ==> (b3 t) (v1 t) ~= 0
232
233 prop_b3_v2_always_zero :: Tetrahedron -> Property
234 prop_b3_v2_always_zero t =
235 (volume t) > 0 ==> (b3 t) (v2 t) ~= 0
236
237
238 -- Used for convenience in the next few tests.
239 p :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
240 p t i j k l = (polynomial t) (xi t i j k l)
241
242 -- | Given in Sorokina and Zeilfelder, p. 78.
243 prop_c3000_identity :: Tetrahedron -> Property
244 prop_c3000_identity t =
245 (volume t) > 0 ==>
246 c t 3 0 0 0 ~= p t 3 0 0 0
247
248 -- | Given in Sorokina and Zeilfelder, p. 78.
249 prop_c2100_identity :: Tetrahedron -> Property
250 prop_c2100_identity t =
251 (volume t) > 0 ==>
252 c t 2 1 0 0 ~= (term1 - term2 + term3 - term4)
253 where
254 term1 = (1/3)*(p t 0 3 0 0)
255 term2 = (5/6)*(p t 3 0 0 0)
256 term3 = 3*(p t 2 1 0 0)
257 term4 = (3/2)*(p t 1 2 0 0)
258
259 -- | Given in Sorokina and Zeilfelder, p. 78.
260 prop_c1110_identity :: Tetrahedron -> Property
261 prop_c1110_identity t =
262 (volume t) > 0 ==>
263 c t 1 1 1 0 ~= (term1 + term2 - term3 - term4)
264 where
265 term1 = (1/3)*((p t 3 0 0 0) + (p t 0 3 0 0) + (p t 0 0 3 0))
266 term2 = (9/2)*(p t 1 1 1 0)
267 term3 = (3/4)*((p t 2 1 0 0) + (p t 1 2 0 0) + (p t 2 0 1 0))
268 term4 = (3/4)*((p t 1 0 2 0) + (p t 0 2 1 0) + (p t 0 1 2 0))