]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Tests/Tetrahedron.hs
393b9602a4783a7a79189038f9293285b0b94d4d
[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 :: Assertion
31 test_volume1 =
32 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 :: Assertion
50 test_volume2 =
51 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 :: Assertion
68 test_contains_point1 =
69 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 :: Assertion
86 test_doesnt_contain_point1 =
87 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 :: Assertion
104 test_doesnt_contain_point2 =
105 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 :: Assertion
122 test_doesnt_contain_point3 =
123 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 :: Assertion
140 test_doesnt_contain_point4 =
141 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 :: Assertion
158 test_doesnt_contain_point5 =
159 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
173 -- | The barycentric coordinate of v0 with respect to itself should
174 -- be one.
175 prop_b0_v0_always_unity :: Tetrahedron -> Property
176 prop_b0_v0_always_unity t =
177 (volume t) > 0 ==> (b0 t) (v0 t) ~= 1.0
178
179 -- | The barycentric coordinate of v1 with respect to v0 should
180 -- be zero.
181 prop_b0_v1_always_zero :: Tetrahedron -> Property
182 prop_b0_v1_always_zero t =
183 (volume t) > 0 ==> (b0 t) (v1 t) ~= 0
184
185 -- | The barycentric coordinate of v2 with respect to v0 should
186 -- be zero.
187 prop_b0_v2_always_zero :: Tetrahedron -> Property
188 prop_b0_v2_always_zero t =
189 (volume t) > 0 ==> (b0 t) (v2 t) ~= 0
190
191 -- | The barycentric coordinate of v3 with respect to v0 should
192 -- be zero.
193 prop_b0_v3_always_zero :: Tetrahedron -> Property
194 prop_b0_v3_always_zero t =
195 (volume t) > 0 ==> (b0 t) (v3 t) ~= 0
196
197 -- | The barycentric coordinate of v1 with respect to itself should
198 -- be one.
199 prop_b1_v1_always_unity :: Tetrahedron -> Property
200 prop_b1_v1_always_unity t =
201 (volume t) > 0 ==> (b1 t) (v1 t) ~= 1.0
202
203 -- | The barycentric coordinate of v0 with respect to v1 should
204 -- be zero.
205 prop_b1_v0_always_zero :: Tetrahedron -> Property
206 prop_b1_v0_always_zero t =
207 (volume t) > 0 ==> (b1 t) (v0 t) ~= 0
208
209 -- | The barycentric coordinate of v2 with respect to v1 should
210 -- be zero.
211 prop_b1_v2_always_zero :: Tetrahedron -> Property
212 prop_b1_v2_always_zero t =
213 (volume t) > 0 ==> (b1 t) (v2 t) ~= 0
214
215 -- | The barycentric coordinate of v3 with respect to v1 should
216 -- be zero.
217 prop_b1_v3_always_zero :: Tetrahedron -> Property
218 prop_b1_v3_always_zero t =
219 (volume t) > 0 ==> (b1 t) (v3 t) ~= 0
220
221 -- | The barycentric coordinate of v2 with respect to itself should
222 -- be one.
223 prop_b2_v2_always_unity :: Tetrahedron -> Property
224 prop_b2_v2_always_unity t =
225 (volume t) > 0 ==> (b2 t) (v2 t) ~= 1.0
226
227 -- | The barycentric coordinate of v0 with respect to v2 should
228 -- be zero.
229 prop_b2_v0_always_zero :: Tetrahedron -> Property
230 prop_b2_v0_always_zero t =
231 (volume t) > 0 ==> (b2 t) (v0 t) ~= 0
232
233 -- | The barycentric coordinate of v1 with respect to v2 should
234 -- be zero.
235 prop_b2_v1_always_zero :: Tetrahedron -> Property
236 prop_b2_v1_always_zero t =
237 (volume t) > 0 ==> (b2 t) (v1 t) ~= 0
238
239 -- | The barycentric coordinate of v3 with respect to v2 should
240 -- be zero.
241 prop_b2_v3_always_zero :: Tetrahedron -> Property
242 prop_b2_v3_always_zero t =
243 (volume t) > 0 ==> (b2 t) (v3 t) ~= 0
244
245 -- | The barycentric coordinate of v3 with respect to itself should
246 -- be one.
247 prop_b3_v3_always_unity :: Tetrahedron -> Property
248 prop_b3_v3_always_unity t =
249 (volume t) > 0 ==> (b3 t) (v3 t) ~= 1.0
250
251 -- | The barycentric coordinate of v0 with respect to v3 should
252 -- be zero.
253 prop_b3_v0_always_zero :: Tetrahedron -> Property
254 prop_b3_v0_always_zero t =
255 (volume t) > 0 ==> (b3 t) (v0 t) ~= 0
256
257 -- | The barycentric coordinate of v1 with respect to v3 should
258 -- be zero.
259 prop_b3_v1_always_zero :: Tetrahedron -> Property
260 prop_b3_v1_always_zero t =
261 (volume t) > 0 ==> (b3 t) (v1 t) ~= 0
262
263 -- | The barycentric coordinate of v2 with respect to v3 should
264 -- be zero.
265 prop_b3_v2_always_zero :: Tetrahedron -> Property
266 prop_b3_v2_always_zero t =
267 (volume t) > 0 ==> (b3 t) (v2 t) ~= 0
268
269
270 -- | Used for convenience in the next few tests; not a test itself.
271 p :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
272 p t i j k l = (polynomial t) (xi t i j k l)
273
274 -- | Given in Sorokina and Zeilfelder, p. 78.
275 prop_c3000_identity :: Tetrahedron -> Property
276 prop_c3000_identity t =
277 (volume t) > 0 ==>
278 c t 3 0 0 0 ~= p t 3 0 0 0
279
280 -- | Given in Sorokina and Zeilfelder, p. 78.
281 prop_c2100_identity :: Tetrahedron -> Property
282 prop_c2100_identity t =
283 (volume t) > 0 ==>
284 c t 2 1 0 0 ~= (term1 - term2 + term3 - term4)
285 where
286 term1 = (1/3)*(p t 0 3 0 0)
287 term2 = (5/6)*(p t 3 0 0 0)
288 term3 = 3*(p t 2 1 0 0)
289 term4 = (3/2)*(p t 1 2 0 0)
290
291 -- | Given in Sorokina and Zeilfelder, p. 78.
292 prop_c1110_identity :: Tetrahedron -> Property
293 prop_c1110_identity t =
294 (volume t) > 0 ==>
295 c t 1 1 1 0 ~= (term1 + term2 - term3 - term4)
296 where
297 term1 = (1/3)*((p t 3 0 0 0) + (p t 0 3 0 0) + (p t 0 0 3 0))
298 term2 = (9/2)*(p t 1 1 1 0)
299 term3 = (3/4)*((p t 2 1 0 0) + (p t 1 2 0 0) + (p t 2 0 1 0))
300 term4 = (3/4)*((p t 1 0 2 0) + (p t 0 2 1 0) + (p t 0 1 2 0))
301
302 prop_x_rotation_doesnt_affect_front :: Tetrahedron -> Bool
303 prop_x_rotation_doesnt_affect_front t =
304 expr1 == expr2
305 where
306 fv0 = Tetrahedron.fv t
307 fv1 = rotate cwx (Tetrahedron.fv t)
308 expr1 = front fv0
309 expr2 = front fv1
310
311 prop_x_rotation_doesnt_affect_back :: Tetrahedron -> Bool
312 prop_x_rotation_doesnt_affect_back t =
313 expr1 == expr2
314 where
315 fv0 = Tetrahedron.fv t
316 fv1 = rotate cwx (Tetrahedron.fv t)
317 expr1 = back fv0
318 expr2 = back fv1
319
320
321 prop_y_rotation_doesnt_affect_left :: Tetrahedron -> Bool
322 prop_y_rotation_doesnt_affect_left t =
323 expr1 == expr2
324 where
325 fv0 = Tetrahedron.fv t
326 fv1 = rotate cwy (Tetrahedron.fv t)
327 expr1 = left fv0
328 expr2 = left fv1
329
330 prop_y_rotation_doesnt_affect_right :: Tetrahedron -> Bool
331 prop_y_rotation_doesnt_affect_right t =
332 expr1 == expr2
333 where
334 fv0 = Tetrahedron.fv t
335 fv1 = rotate cwy (Tetrahedron.fv t)
336 expr1 = right fv0
337 expr2 = right fv1
338
339
340 prop_z_rotation_doesnt_affect_down :: Tetrahedron -> Bool
341 prop_z_rotation_doesnt_affect_down t =
342 expr1 == expr2
343 where
344 fv0 = Tetrahedron.fv t
345 fv1 = rotate cwz (Tetrahedron.fv t)
346 expr1 = down fv0
347 expr2 = down fv1
348
349
350 prop_z_rotation_doesnt_affect_top :: Tetrahedron -> Bool
351 prop_z_rotation_doesnt_affect_top t =
352 expr1 == expr2
353 where
354 fv0 = Tetrahedron.fv t
355 fv1 = rotate cwz (Tetrahedron.fv t)
356 expr1 = top fv0
357 expr2 = top fv1
358
359 prop_swapping_vertices_doesnt_affect_coefficients1 :: Tetrahedron -> Bool
360 prop_swapping_vertices_doesnt_affect_coefficients1 t =
361 c t 0 0 1 2 == c t' 0 0 1 2
362 where
363 t' = t { v0 = (v1 t), v1 = (v0 t) }
364
365 prop_swapping_vertices_doesnt_affect_coefficients2 :: Tetrahedron -> Bool
366 prop_swapping_vertices_doesnt_affect_coefficients2 t =
367 c t 0 1 1 1 == c t' 0 1 1 1
368 where
369 t' = t { v2 = (v3 t), v3 = (v2 t) }
370
371 prop_swapping_vertices_doesnt_affect_coefficients3 :: Tetrahedron -> Bool
372 prop_swapping_vertices_doesnt_affect_coefficients3 t =
373 c t 2 1 0 0 == c t' 2 1 0 0
374 where
375 t' = t { v2 = (v3 t), v3 = (v2 t) }
376
377 prop_swapping_vertices_doesnt_affect_coefficients4 :: Tetrahedron -> Bool
378 prop_swapping_vertices_doesnt_affect_coefficients4 t =
379 c t 2 0 0 1 == c t' 2 0 0 1
380 where
381 t' = t { v0 = (v3 t), v3 = (v0 t) }