]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Tests/Tetrahedron.hs
1ec4c8e6a0672e2541d383ae233b1e684b801291
[spline3.git] / src / Tests / Tetrahedron.hs
1 module Tests.Tetrahedron
2 where
3
4 import Test.HUnit
5 import Test.QuickCheck
6
7 import Cardinal
8 import Comparisons
9 import Point
10 import FunctionValues
11 import Tests.FunctionValues()
12 import Tetrahedron
13 import ThreeDimensional
14
15 instance Arbitrary Tetrahedron where
16 arbitrary = do
17 rnd_v0 <- arbitrary :: Gen Point
18 rnd_v1 <- arbitrary :: Gen Point
19 rnd_v2 <- arbitrary :: Gen Point
20 rnd_v3 <- arbitrary :: Gen Point
21 rnd_fv <- arbitrary :: Gen FunctionValues
22 return (Tetrahedron rnd_fv rnd_v0 rnd_v1 rnd_v2 rnd_v3)
23
24 -- HUnit Tests
25
26
27 -- | Check the volume of a particular tetrahedron against the value
28 -- computed by hand. Its vertices are in clockwise order, so the
29 -- volume should be negative.
30 test_volume1 :: Test
31 test_volume1 =
32 TestCase $ assertEqual "volume is correct" True (vol ~= (-1/3))
33 where
34 p0 = (0, -0.5, 0)
35 p1 = (0, 0.5, 0)
36 p2 = (2, 0, 0)
37 p3 = (1, 0, 1)
38 t = Tetrahedron { v0 = p0,
39 v1 = p1,
40 v2 = p2,
41 v3 = p3,
42 fv = empty_values }
43 vol = volume t
44
45
46 -- | Check the volume of a particular tetrahedron against the value
47 -- computed by hand. Its vertices are in counter-clockwise order, so
48 -- the volume should be positive.
49 test_volume2 :: Test
50 test_volume2 =
51 TestCase $ assertEqual "volume is correct" True (vol ~= (1/3))
52 where
53 p0 = (0, -0.5, 0)
54 p1 = (2, 0, 0)
55 p2 = (0, 0.5, 0)
56 p3 = (1, 0, 1)
57 t = Tetrahedron { v0 = p0,
58 v1 = p1,
59 v2 = p2,
60 v3 = p3,
61 fv = empty_values }
62 vol = volume t
63
64
65 -- | Ensure that a tetrahedron contains a particular point chosen to
66 -- be inside of it.
67 test_contains_point1 :: Test
68 test_contains_point1 =
69 TestCase $ assertEqual "contains an inner point" True (contains_point t inner_point)
70 where
71 p0 = (0, -0.5, 0)
72 p1 = (0, 0.5, 0)
73 p2 = (2, 0, 0)
74 p3 = (1, 0, 1)
75 inner_point = (1, 0, 0.5)
76 t = Tetrahedron { v0 = p0,
77 v1 = p1,
78 v2 = p2,
79 v3 = p3,
80 fv = empty_values }
81
82
83 -- | Ensure that a tetrahedron does not contain a particular point chosen to
84 -- be outside of it (first test).
85 test_doesnt_contain_point1 :: Test
86 test_doesnt_contain_point1 =
87 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
88 where
89 p0 = (0, -0.5, 0)
90 p1 = (0, 0.5, 0)
91 p2 = (2, 0, 0)
92 p3 = (1, 0, 1)
93 exterior_point = (5, 2, -9.0212)
94 t = Tetrahedron { v0 = p0,
95 v1 = p1,
96 v2 = p2,
97 v3 = p3,
98 fv = empty_values }
99
100
101 -- | Ensure that a tetrahedron does not contain a particular point chosen to
102 -- be outside of it (second test).
103 test_doesnt_contain_point2 :: Test
104 test_doesnt_contain_point2 =
105 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
106 where
107 p0 = (0, 1, 1)
108 p1 = (1, 1, 1)
109 p2 = (0.5, 0.5, 1)
110 p3 = (0.5, 0.5, 0.5)
111 exterior_point = (0, 0, 0)
112 t = Tetrahedron { v0 = p0,
113 v1 = p1,
114 v2 = p2,
115 v3 = p3,
116 fv = empty_values }
117
118
119 -- | Ensure that a tetrahedron does not contain a particular point chosen to
120 -- be outside of it (third test).
121 test_doesnt_contain_point3 :: Test
122 test_doesnt_contain_point3 =
123 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
124 where
125 p0 = (1, 1, 1)
126 p1 = (1, 0, 1)
127 p2 = (0.5, 0.5, 1)
128 p3 = (0.5, 0.5, 0.5)
129 exterior_point = (0, 0, 0)
130 t = Tetrahedron { v0 = p0,
131 v1 = p1,
132 v2 = p2,
133 v3 = p3,
134 fv = empty_values }
135
136
137 -- | Ensure that a tetrahedron does not contain a particular point chosen to
138 -- be outside of it (fourth test).
139 test_doesnt_contain_point4 :: Test
140 test_doesnt_contain_point4 =
141 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
142 where
143 p0 = (1, 0, 1)
144 p1 = (0, 0, 1)
145 p2 = (0.5, 0.5, 1)
146 p3 = (0.5, 0.5, 0.5)
147 exterior_point = (0, 0, 0)
148 t = Tetrahedron { v0 = p0,
149 v1 = p1,
150 v2 = p2,
151 v3 = p3,
152 fv = empty_values }
153
154
155 -- | Ensure that a tetrahedron does not contain a particular point chosen to
156 -- be outside of it (fifth test).
157 test_doesnt_contain_point5 :: Test
158 test_doesnt_contain_point5 =
159 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
160 where
161 p0 = (0, 0, 1)
162 p1 = (0, 1, 1)
163 p2 = (0.5, 0.5, 1)
164 p3 = (0.5, 0.5, 0.5)
165 exterior_point = (0, 0, 0)
166 t = Tetrahedron { v0 = p0,
167 v1 = p1,
168 v2 = p2,
169 v3 = p3,
170 fv = empty_values }
171
172 -- | A list of all HUnit tests defined in this module.
173 tetrahedron_tests :: [Test]
174 tetrahedron_tests = [test_volume1,
175 test_volume2,
176 test_contains_point1,
177 test_doesnt_contain_point1,
178 test_doesnt_contain_point2,
179 test_doesnt_contain_point3,
180 test_doesnt_contain_point4,
181 test_doesnt_contain_point5 ]
182
183
184 -- | The barycentric coordinate of v0 with respect to itself should
185 -- be one.
186 prop_b0_v0_always_unity :: Tetrahedron -> Property
187 prop_b0_v0_always_unity t =
188 (volume t) > 0 ==> (b0 t) (v0 t) ~= 1.0
189
190 -- | The barycentric coordinate of v1 with respect to v0 should
191 -- be zero.
192 prop_b0_v1_always_zero :: Tetrahedron -> Property
193 prop_b0_v1_always_zero t =
194 (volume t) > 0 ==> (b0 t) (v1 t) ~= 0
195
196 -- | The barycentric coordinate of v2 with respect to v0 should
197 -- be zero.
198 prop_b0_v2_always_zero :: Tetrahedron -> Property
199 prop_b0_v2_always_zero t =
200 (volume t) > 0 ==> (b0 t) (v2 t) ~= 0
201
202 -- | The barycentric coordinate of v3 with respect to v0 should
203 -- be zero.
204 prop_b0_v3_always_zero :: Tetrahedron -> Property
205 prop_b0_v3_always_zero t =
206 (volume t) > 0 ==> (b0 t) (v3 t) ~= 0
207
208 -- | The barycentric coordinate of v1 with respect to itself should
209 -- be one.
210 prop_b1_v1_always_unity :: Tetrahedron -> Property
211 prop_b1_v1_always_unity t =
212 (volume t) > 0 ==> (b1 t) (v1 t) ~= 1.0
213
214 -- | The barycentric coordinate of v0 with respect to v1 should
215 -- be zero.
216 prop_b1_v0_always_zero :: Tetrahedron -> Property
217 prop_b1_v0_always_zero t =
218 (volume t) > 0 ==> (b1 t) (v0 t) ~= 0
219
220 -- | The barycentric coordinate of v2 with respect to v1 should
221 -- be zero.
222 prop_b1_v2_always_zero :: Tetrahedron -> Property
223 prop_b1_v2_always_zero t =
224 (volume t) > 0 ==> (b1 t) (v2 t) ~= 0
225
226 -- | The barycentric coordinate of v3 with respect to v1 should
227 -- be zero.
228 prop_b1_v3_always_zero :: Tetrahedron -> Property
229 prop_b1_v3_always_zero t =
230 (volume t) > 0 ==> (b1 t) (v3 t) ~= 0
231
232 -- | The barycentric coordinate of v2 with respect to itself should
233 -- be one.
234 prop_b2_v2_always_unity :: Tetrahedron -> Property
235 prop_b2_v2_always_unity t =
236 (volume t) > 0 ==> (b2 t) (v2 t) ~= 1.0
237
238 -- | The barycentric coordinate of v0 with respect to v2 should
239 -- be zero.
240 prop_b2_v0_always_zero :: Tetrahedron -> Property
241 prop_b2_v0_always_zero t =
242 (volume t) > 0 ==> (b2 t) (v0 t) ~= 0
243
244 -- | The barycentric coordinate of v1 with respect to v2 should
245 -- be zero.
246 prop_b2_v1_always_zero :: Tetrahedron -> Property
247 prop_b2_v1_always_zero t =
248 (volume t) > 0 ==> (b2 t) (v1 t) ~= 0
249
250 -- | The barycentric coordinate of v3 with respect to v2 should
251 -- be zero.
252 prop_b2_v3_always_zero :: Tetrahedron -> Property
253 prop_b2_v3_always_zero t =
254 (volume t) > 0 ==> (b2 t) (v3 t) ~= 0
255
256 -- | The barycentric coordinate of v3 with respect to itself should
257 -- be one.
258 prop_b3_v3_always_unity :: Tetrahedron -> Property
259 prop_b3_v3_always_unity t =
260 (volume t) > 0 ==> (b3 t) (v3 t) ~= 1.0
261
262 -- | The barycentric coordinate of v0 with respect to v3 should
263 -- be zero.
264 prop_b3_v0_always_zero :: Tetrahedron -> Property
265 prop_b3_v0_always_zero t =
266 (volume t) > 0 ==> (b3 t) (v0 t) ~= 0
267
268 -- | The barycentric coordinate of v1 with respect to v3 should
269 -- be zero.
270 prop_b3_v1_always_zero :: Tetrahedron -> Property
271 prop_b3_v1_always_zero t =
272 (volume t) > 0 ==> (b3 t) (v1 t) ~= 0
273
274 -- | The barycentric coordinate of v2 with respect to v3 should
275 -- be zero.
276 prop_b3_v2_always_zero :: Tetrahedron -> Property
277 prop_b3_v2_always_zero t =
278 (volume t) > 0 ==> (b3 t) (v2 t) ~= 0
279
280
281 -- | Used for convenience in the next few tests; not a test itself.
282 p :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
283 p t i j k l = (polynomial t) (xi t i j k l)
284
285 -- | Given in Sorokina and Zeilfelder, p. 78.
286 prop_c3000_identity :: Tetrahedron -> Property
287 prop_c3000_identity t =
288 (volume t) > 0 ==>
289 c t 3 0 0 0 ~= p t 3 0 0 0
290
291 -- | Given in Sorokina and Zeilfelder, p. 78.
292 prop_c2100_identity :: Tetrahedron -> Property
293 prop_c2100_identity t =
294 (volume t) > 0 ==>
295 c t 2 1 0 0 ~= (term1 - term2 + term3 - term4)
296 where
297 term1 = (1/3)*(p t 0 3 0 0)
298 term2 = (5/6)*(p t 3 0 0 0)
299 term3 = 3*(p t 2 1 0 0)
300 term4 = (3/2)*(p t 1 2 0 0)
301
302 -- | Given in Sorokina and Zeilfelder, p. 78.
303 prop_c1110_identity :: Tetrahedron -> Property
304 prop_c1110_identity t =
305 (volume t) > 0 ==>
306 c t 1 1 1 0 ~= (term1 + term2 - term3 - term4)
307 where
308 term1 = (1/3)*((p t 3 0 0 0) + (p t 0 3 0 0) + (p t 0 0 3 0))
309 term2 = (9/2)*(p t 1 1 1 0)
310 term3 = (3/4)*((p t 2 1 0 0) + (p t 1 2 0 0) + (p t 2 0 1 0))
311 term4 = (3/4)*((p t 1 0 2 0) + (p t 0 2 1 0) + (p t 0 1 2 0))
312
313 prop_x_rotation_doesnt_affect_front :: Tetrahedron -> Bool
314 prop_x_rotation_doesnt_affect_front t =
315 expr1 == expr2
316 where
317 fv0 = Tetrahedron.fv t
318 fv1 = rotate (Tetrahedron.fv t) cwx
319 expr1 = front $ fv0
320 expr2 = front $ fv1
321
322 prop_x_rotation_doesnt_affect_back :: Tetrahedron -> Bool
323 prop_x_rotation_doesnt_affect_back t =
324 expr1 == expr2
325 where
326 fv0 = Tetrahedron.fv t
327 fv1 = rotate (Tetrahedron.fv t) cwx
328 expr1 = back $ fv0
329 expr2 = back $ fv1
330
331
332 prop_y_rotation_doesnt_affect_left :: Tetrahedron -> Bool
333 prop_y_rotation_doesnt_affect_left t =
334 expr1 == expr2
335 where
336 fv0 = Tetrahedron.fv t
337 fv1 = rotate (Tetrahedron.fv t) cwy
338 expr1 = left $ fv0
339 expr2 = left $ fv1
340
341 prop_y_rotation_doesnt_affect_right :: Tetrahedron -> Bool
342 prop_y_rotation_doesnt_affect_right t =
343 expr1 == expr2
344 where
345 fv0 = Tetrahedron.fv t
346 fv1 = rotate (Tetrahedron.fv t) cwy
347 expr1 = right $ fv0
348 expr2 = right $ fv1
349
350
351 prop_z_rotation_doesnt_affect_down :: Tetrahedron -> Bool
352 prop_z_rotation_doesnt_affect_down t =
353 expr1 == expr2
354 where
355 fv0 = Tetrahedron.fv t
356 fv1 = rotate (Tetrahedron.fv t) cwz
357 expr1 = down $ fv0
358 expr2 = down $ fv1
359
360
361 prop_z_rotation_doesnt_affect_top :: Tetrahedron -> Bool
362 prop_z_rotation_doesnt_affect_top t =
363 expr1 == expr2
364 where
365 fv0 = Tetrahedron.fv t
366 fv1 = rotate (Tetrahedron.fv t) cwz
367 expr1 = top $ fv0
368 expr2 = top $ fv1
369
370 prop_swapping_vertices_doesnt_affect_coefficients1 :: Tetrahedron -> Bool
371 prop_swapping_vertices_doesnt_affect_coefficients1 t =
372 c t 0 0 1 2 == c t' 0 0 1 2
373 where
374 t' = t { v0 = (v1 t), v1 = (v0 t) }
375
376 prop_swapping_vertices_doesnt_affect_coefficients2 :: Tetrahedron -> Bool
377 prop_swapping_vertices_doesnt_affect_coefficients2 t =
378 c t 0 1 1 1 == c t' 0 1 1 1
379 where
380 t' = t { v2 = (v3 t), v3 = (v2 t) }
381
382 prop_swapping_vertices_doesnt_affect_coefficients3 :: Tetrahedron -> Bool
383 prop_swapping_vertices_doesnt_affect_coefficients3 t =
384 c t 2 1 0 0 == c t' 2 1 0 0
385 where
386 t' = t { v2 = (v3 t), v3 = (v2 t) }
387
388 prop_swapping_vertices_doesnt_affect_coefficients4 :: Tetrahedron -> Bool
389 prop_swapping_vertices_doesnt_affect_coefficients4 t =
390 c t 2 0 0 1 == c t' 2 0 0 1
391 where
392 t' = t { v0 = (v3 t), v3 = (v0 t) }