]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Tetrahedron.hs
Define a few tests locally.
[spline3.git] / src / Tetrahedron.hs
1 module Tetrahedron (
2 Tetrahedron(..),
3 b0, -- Cube test
4 b1, -- Cube test
5 b2, -- Cube test
6 b3, -- Cube test
7 c,
8 polynomial,
9 tetrahedron_properties,
10 tetrahedron_tests,
11 volume -- Cube test
12 )
13 where
14
15 import qualified Data.Vector as V (
16 singleton,
17 snoc,
18 sum
19 )
20
21 import Test.Framework (Test, testGroup)
22 import Test.Framework.Providers.HUnit (testCase)
23 import Test.Framework.Providers.QuickCheck2 (testProperty)
24 import Test.HUnit (Assertion, assertEqual)
25 import Test.QuickCheck (Arbitrary(..), Gen, Property, (==>))
26
27 import Comparisons ((~=), nearly_ge)
28 import FunctionValues (FunctionValues(..), empty_values)
29 import Misc (factorial)
30 import Point (Point, scale)
31 import RealFunction (RealFunction, cmult, fexp)
32 import ThreeDimensional (ThreeDimensional(..))
33
34 data Tetrahedron =
35 Tetrahedron { function_values :: FunctionValues,
36 v0 :: Point,
37 v1 :: Point,
38 v2 :: Point,
39 v3 :: Point,
40 precomputed_volume :: Double
41 }
42 deriving (Eq)
43
44
45 instance Arbitrary Tetrahedron where
46 arbitrary = do
47 rnd_v0 <- arbitrary :: Gen Point
48 rnd_v1 <- arbitrary :: Gen Point
49 rnd_v2 <- arbitrary :: Gen Point
50 rnd_v3 <- arbitrary :: Gen Point
51 rnd_fv <- arbitrary :: Gen FunctionValues
52
53 -- We can't assign an incorrect precomputed volume,
54 -- so we have to calculate the correct one here.
55 let t' = Tetrahedron rnd_fv rnd_v0 rnd_v1 rnd_v2 rnd_v3 0
56 let vol = volume t'
57 return (Tetrahedron rnd_fv rnd_v0 rnd_v1 rnd_v2 rnd_v3 vol)
58
59
60 instance Show Tetrahedron where
61 show t = "Tetrahedron:\n" ++
62 " function_values: " ++ (show (function_values t)) ++ "\n" ++
63 " v0: " ++ (show (v0 t)) ++ "\n" ++
64 " v1: " ++ (show (v1 t)) ++ "\n" ++
65 " v2: " ++ (show (v2 t)) ++ "\n" ++
66 " v3: " ++ (show (v3 t)) ++ "\n"
67
68
69 instance ThreeDimensional Tetrahedron where
70 center (Tetrahedron _ v0' v1' v2' v3' _) =
71 (v0' + v1' + v2' + v3') `scale` (1/4)
72
73 contains_point t p0 =
74 b0_unscaled `nearly_ge` 0 &&
75 b1_unscaled `nearly_ge` 0 &&
76 b2_unscaled `nearly_ge` 0 &&
77 b3_unscaled `nearly_ge` 0
78 where
79 -- Drop the useless division and volume calculation that we
80 -- would do if we used the regular b0,..b3 functions.
81 b0_unscaled :: Double
82 b0_unscaled = volume inner_tetrahedron
83 where inner_tetrahedron = t { v0 = p0 }
84
85 b1_unscaled :: Double
86 b1_unscaled = volume inner_tetrahedron
87 where inner_tetrahedron = t { v1 = p0 }
88
89 b2_unscaled :: Double
90 b2_unscaled = volume inner_tetrahedron
91 where inner_tetrahedron = t { v2 = p0 }
92
93 b3_unscaled :: Double
94 b3_unscaled = volume inner_tetrahedron
95 where inner_tetrahedron = t { v3 = p0 }
96
97
98 polynomial :: Tetrahedron -> (RealFunction Point)
99 polynomial t =
100 V.sum $ V.singleton ((c t 0 0 0 3) `cmult` (beta t 0 0 0 3)) `V.snoc`
101 ((c t 0 0 1 2) `cmult` (beta t 0 0 1 2)) `V.snoc`
102 ((c t 0 0 2 1) `cmult` (beta t 0 0 2 1)) `V.snoc`
103 ((c t 0 0 3 0) `cmult` (beta t 0 0 3 0)) `V.snoc`
104 ((c t 0 1 0 2) `cmult` (beta t 0 1 0 2)) `V.snoc`
105 ((c t 0 1 1 1) `cmult` (beta t 0 1 1 1)) `V.snoc`
106 ((c t 0 1 2 0) `cmult` (beta t 0 1 2 0)) `V.snoc`
107 ((c t 0 2 0 1) `cmult` (beta t 0 2 0 1)) `V.snoc`
108 ((c t 0 2 1 0) `cmult` (beta t 0 2 1 0)) `V.snoc`
109 ((c t 0 3 0 0) `cmult` (beta t 0 3 0 0)) `V.snoc`
110 ((c t 1 0 0 2) `cmult` (beta t 1 0 0 2)) `V.snoc`
111 ((c t 1 0 1 1) `cmult` (beta t 1 0 1 1)) `V.snoc`
112 ((c t 1 0 2 0) `cmult` (beta t 1 0 2 0)) `V.snoc`
113 ((c t 1 1 0 1) `cmult` (beta t 1 1 0 1)) `V.snoc`
114 ((c t 1 1 1 0) `cmult` (beta t 1 1 1 0)) `V.snoc`
115 ((c t 1 2 0 0) `cmult` (beta t 1 2 0 0)) `V.snoc`
116 ((c t 2 0 0 1) `cmult` (beta t 2 0 0 1)) `V.snoc`
117 ((c t 2 0 1 0) `cmult` (beta t 2 0 1 0)) `V.snoc`
118 ((c t 2 1 0 0) `cmult` (beta t 2 1 0 0)) `V.snoc`
119 ((c t 3 0 0 0) `cmult` (beta t 3 0 0 0))
120
121
122
123 -- | The Bernstein polynomial on t with indices i,j,k,l. Denoted by a
124 -- capital 'B' in the Sorokina/Zeilfelder paper.
125 beta :: Tetrahedron -> Int -> Int -> Int -> Int -> (RealFunction Point)
126 beta t i j k l
127 | (i + j + k + l == 3) =
128 coefficient `cmult` (b0_term * b1_term * b2_term * b3_term)
129 | otherwise = error "basis function index out of bounds"
130 where
131 denominator = (factorial i)*(factorial j)*(factorial k)*(factorial l)
132 coefficient = 6 / (fromIntegral denominator)
133 b0_term = (b0 t) `fexp` i
134 b1_term = (b1 t) `fexp` j
135 b2_term = (b2 t) `fexp` k
136 b3_term = (b3 t) `fexp` l
137
138
139 -- | The coefficient function. c t i j k l returns the coefficient
140 -- c_ijkl with respect to the tetrahedron t. The definition uses
141 -- pattern matching to mimic the definitions given in Sorokina and
142 -- Zeilfelder, pp. 84-86. If incorrect indices are supplied, the
143 -- function will simply error.
144 c :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
145 c t i j k l =
146 coefficient i j k l
147 where
148 fvs = function_values t
149 f = front fvs
150 b = back fvs
151 r = right fvs
152 l' = left fvs
153 t' = top fvs
154 d = down fvs
155 fl = front_left fvs
156 fr = front_right fvs
157 fd = front_down fvs
158 ft = front_top fvs
159 bl = back_left fvs
160 br = back_right fvs
161 bd = back_down fvs
162 bt = back_top fvs
163 ld = left_down fvs
164 lt = left_top fvs
165 rd = right_down fvs
166 rt = right_top fvs
167 fld = front_left_down fvs
168 flt = front_left_top fvs
169 frd = front_right_down fvs
170 frt = front_right_top fvs
171 i' = interior fvs
172
173 coefficient :: Int -> Int -> Int -> Int -> Double
174 coefficient 0 0 3 0 =
175 (1/8) * (i' + f + l' + t' + lt + fl + ft + flt)
176
177 coefficient 0 0 0 3 =
178 (1/8) * (i' + f + r + t' + rt + fr + ft + frt)
179
180 coefficient 0 0 2 1 =
181 (5/24)*(i' + f + t' + ft) + (1/24)*(l' + fl + lt + flt)
182
183 coefficient 0 0 1 2 =
184 (5/24)*(i' + f + t' + ft) + (1/24)*(r + fr + rt + frt)
185
186 coefficient 0 1 2 0 =
187 (5/24)*(i' + f) + (1/8)*(l' + t' + fl + ft)
188 + (1/24)*(lt + flt)
189
190 coefficient 0 1 0 2 =
191 (5/24)*(i' + f) + (1/8)*(r + t' + fr + ft)
192 + (1/24)*(rt + frt)
193
194 coefficient 0 1 1 1 =
195 (13/48)*(i' + f) + (7/48)*(t' + ft)
196 + (1/32)*(l' + r + fl + fr)
197 + (1/96)*(lt + rt + flt + frt)
198
199 coefficient 0 2 1 0 =
200 (13/48)*(i' + f) + (17/192)*(l' + t' + fl + ft)
201 + (1/96)*(lt + flt)
202 + (1/64)*(r + d + fr + fd)
203 + (1/192)*(rt + ld + frt + fld)
204
205 coefficient 0 2 0 1 =
206 (13/48)*(i' + f) + (17/192)*(r + t' + fr + ft)
207 + (1/96)*(rt + frt)
208 + (1/64)*(l' + d + fl + fd)
209 + (1/192)*(rd + lt + flt + frd)
210
211 coefficient 0 3 0 0 =
212 (13/48)*(i' + f) + (5/96)*(l' + r + t' + d + fl + fr + ft + fd)
213 + (1/192)*(rt + rd + lt + ld + frt + frd + flt + fld)
214
215 coefficient 1 0 2 0 =
216 (1/4)*i' + (1/6)*(f + l' + t')
217 + (1/12)*(lt + fl + ft)
218
219 coefficient 1 0 0 2 =
220 (1/4)*i' + (1/6)*(f + r + t')
221 + (1/12)*(rt + fr + ft)
222
223 coefficient 1 0 1 1 =
224 (1/3)*i' + (5/24)*(f + t')
225 + (1/12)*ft
226 + (1/24)*(l' + r)
227 + (1/48)*(lt + rt + fl + fr)
228
229 coefficient 1 1 1 0 =
230 (1/3)*i' + (5/24)*f
231 + (1/8)*(l' + t')
232 + (5/96)*(fl + ft)
233 + (1/48)*(d + r + lt)
234 + (1/96)*(fd + ld + rt + fr)
235
236 coefficient 1 1 0 1 =
237 (1/3)*i' + (5/24)*f
238 + (1/8)*(r + t')
239 + (5/96)*(fr + ft)
240 + (1/48)*(d + l' + rt)
241 + (1/96)*(fd + lt + rd + fl)
242
243 coefficient 1 2 0 0 =
244 (1/3)*i' + (5/24)*f
245 + (7/96)*(l' + r + t' + d)
246 + (1/32)*(fl + fr + ft + fd)
247 + (1/96)*(rt + rd + lt + ld)
248
249 coefficient 2 0 1 0 =
250 (3/8)*i' + (7/48)*(f + t' + l')
251 + (1/48)*(r + d + b + lt + fl + ft)
252 + (1/96)*(rt + bt + fr + fd + ld + bl)
253
254 coefficient 2 0 0 1 =
255 (3/8)*i' + (7/48)*(f + t' + r)
256 + (1/48)*(l' + d + b + rt + fr + ft)
257 + (1/96)*(lt + bt + fl + fd + rd + br)
258
259 coefficient 2 1 0 0 =
260 (3/8)*i' + (1/12)*(t' + r + l' + d)
261 + (1/64)*(ft + fr + fl + fd)
262 + (7/48)*f
263 + (1/48)*b
264 + (1/96)*(rt + ld + lt + rd)
265 + (1/192)*(bt + br + bl + bd)
266
267 coefficient 3 0 0 0 =
268 (3/8)*i' + (1/12)*(t' + f + l' + r + d + b)
269 + (1/96)*(lt + fl + ft + rt + bt + fr)
270 + (1/96)*(fd + ld + bd + br + rd + bl)
271
272 coefficient _ _ _ _ = error "coefficient index out of bounds"
273
274
275
276 -- | Compute the determinant of the 4x4 matrix,
277 --
278 -- [1]
279 -- [x]
280 -- [y]
281 -- [z]
282 --
283 -- where [1] = [1, 1, 1, 1],
284 -- [x] = [x1,x2,x3,x4],
285 --
286 -- et cetera.
287 --
288 -- The termX nonsense is an attempt to prevent Double overflow.
289 -- which has been observed to happen with large coordinates.
290 --
291 det :: Point -> Point -> Point -> Point -> Double
292 det p0 p1 p2 p3 =
293 term5 + term6
294 where
295 (x1, y1, z1) = p0
296 (x2, y2, z2) = p1
297 (x3, y3, z3) = p2
298 (x4, y4, z4) = p3
299 term1 = ((x2 - x4)*y1 - (x1 - x4)*y2 + (x1 - x2)*y4)*z3
300 term2 = ((x2 - x3)*y1 - (x1 - x3)*y2 + (x1 - x2)*y3)*z4
301 term3 = ((x3 - x4)*y2 - (x2 - x4)*y3 + (x2 - x3)*y4)*z1
302 term4 = ((x3 - x4)*y1 - (x1 - x4)*y3 + (x1 - x3)*y4)*z2
303 term5 = term1 - term2
304 term6 = term3 - term4
305
306
307 -- | Computed using the formula from Lai & Schumaker, Definition 15.4,
308 -- page 436.
309 volume :: Tetrahedron -> Double
310 volume t
311 | v0' == v1' = 0
312 | v0' == v2' = 0
313 | v0' == v3' = 0
314 | v1' == v2' = 0
315 | v1' == v3' = 0
316 | v2' == v3' = 0
317 | otherwise = (1/6)*(det v0' v1' v2' v3')
318 where
319 v0' = v0 t
320 v1' = v1 t
321 v2' = v2 t
322 v3' = v3 t
323
324
325 -- | The barycentric coordinates of a point with respect to v0.
326 b0 :: Tetrahedron -> (RealFunction Point)
327 b0 t point = (volume inner_tetrahedron) / (precomputed_volume t)
328 where
329 inner_tetrahedron = t { v0 = point }
330
331
332 -- | The barycentric coordinates of a point with respect to v1.
333 b1 :: Tetrahedron -> (RealFunction Point)
334 b1 t point = (volume inner_tetrahedron) / (precomputed_volume t)
335 where
336 inner_tetrahedron = t { v1 = point }
337
338
339 -- | The barycentric coordinates of a point with respect to v2.
340 b2 :: Tetrahedron -> (RealFunction Point)
341 b2 t point = (volume inner_tetrahedron) / (precomputed_volume t)
342 where
343 inner_tetrahedron = t { v2 = point }
344
345
346 -- | The barycentric coordinates of a point with respect to v3.
347 b3 :: Tetrahedron -> (RealFunction Point)
348 b3 t point = (volume inner_tetrahedron) / (precomputed_volume t)
349 where
350 inner_tetrahedron = t { v3 = point }
351
352
353
354
355 -- Tests
356
357
358 -- | Check the volume of a particular tetrahedron (computed by hand)
359 -- and whether or not it contains a specific point chosen to be
360 -- outside of it. Its vertices are in clockwise order, so the volume
361 -- should be negative.
362 tetrahedron1_geometry_tests :: Test.Framework.Test
363 tetrahedron1_geometry_tests =
364 testGroup "tetrahedron1 geometry"
365 [ testCase "volume1" volume1,
366 testCase "doesn't contain point1" doesnt_contain_point1]
367 where
368 p0 = (0, -0.5, 0)
369 p1 = (0, 0.5, 0)
370 p2 = (2, 0, 0)
371 p3 = (1, 0, 1)
372 t = Tetrahedron { v0 = p0,
373 v1 = p1,
374 v2 = p2,
375 v3 = p3,
376 function_values = empty_values,
377 precomputed_volume = 0 }
378
379 volume1 :: Assertion
380 volume1 =
381 assertEqual "volume is correct" True (vol ~= (-1/3))
382 where
383 vol = volume t
384
385 doesnt_contain_point1 :: Assertion
386 doesnt_contain_point1 =
387 assertEqual "doesn't contain an exterior point" False contained
388 where
389 exterior_point = (5, 2, -9.0212)
390 contained = contains_point t exterior_point
391
392
393 -- | Check the volume of a particular tetrahedron (computed by hand)
394 -- and whether or not it contains a specific point chosen to be
395 -- inside of it. Its vertices are in counter-clockwise order, so the
396 -- volume should be positive.
397 tetrahedron2_geometry_tests :: Test.Framework.Test
398 tetrahedron2_geometry_tests =
399 testGroup "tetrahedron2 geometry"
400 [ testCase "volume1" volume1,
401 testCase "contains point1" contains_point1]
402 where
403 p0 = (0, -0.5, 0)
404 p1 = (2, 0, 0)
405 p2 = (0, 0.5, 0)
406 p3 = (1, 0, 1)
407 t = Tetrahedron { v0 = p0,
408 v1 = p1,
409 v2 = p2,
410 v3 = p3,
411 function_values = empty_values,
412 precomputed_volume = 0 }
413
414 volume1 :: Assertion
415 volume1 = assertEqual "volume1 is correct" True (vol ~= (1/3))
416 where
417 vol = volume t
418
419 contains_point1 :: Assertion
420 contains_point1 = assertEqual "contains an inner point" True contained
421 where
422 inner_point = (1, 0, 0.5)
423 contained = contains_point t inner_point
424
425
426 -- | Ensure that tetrahedra do not contain a particular point chosen to
427 -- be outside of them.
428 containment_tests :: Test.Framework.Test
429 containment_tests =
430 testGroup "containment tests"
431 [ testCase "doesn't contain point2" doesnt_contain_point2,
432 testCase "doesn't contain point3" doesnt_contain_point3,
433 testCase "doesn't contain point4" doesnt_contain_point4,
434 testCase "doesn't contain point5" doesnt_contain_point5]
435 where
436 p2 = (0.5, 0.5, 1)
437 p3 = (0.5, 0.5, 0.5)
438 exterior_point = (0, 0, 0)
439
440 doesnt_contain_point2 :: Assertion
441 doesnt_contain_point2 =
442 assertEqual "doesn't contain an exterior point" False contained
443 where
444 p0 = (0, 1, 1)
445 p1 = (1, 1, 1)
446 t = Tetrahedron { v0 = p0,
447 v1 = p1,
448 v2 = p2,
449 v3 = p3,
450 function_values = empty_values,
451 precomputed_volume = 0 }
452 contained = contains_point t exterior_point
453
454
455 doesnt_contain_point3 :: Assertion
456 doesnt_contain_point3 =
457 assertEqual "doesn't contain an exterior point" False contained
458 where
459 p0 = (1, 1, 1)
460 p1 = (1, 0, 1)
461 t = Tetrahedron { v0 = p0,
462 v1 = p1,
463 v2 = p2,
464 v3 = p3,
465 function_values = empty_values,
466 precomputed_volume = 0 }
467 contained = contains_point t exterior_point
468
469
470 doesnt_contain_point4 :: Assertion
471 doesnt_contain_point4 =
472 assertEqual "doesn't contain an exterior point" False contained
473 where
474 p0 = (1, 0, 1)
475 p1 = (0, 0, 1)
476 t = Tetrahedron { v0 = p0,
477 v1 = p1,
478 v2 = p2,
479 v3 = p3,
480 function_values = empty_values,
481 precomputed_volume = 0 }
482 contained = contains_point t exterior_point
483
484
485 doesnt_contain_point5 :: Assertion
486 doesnt_contain_point5 =
487 assertEqual "doesn't contain an exterior point" False contained
488 where
489 p0 = (0, 0, 1)
490 p1 = (0, 1, 1)
491 t = Tetrahedron { v0 = p0,
492 v1 = p1,
493 v2 = p2,
494 v3 = p3,
495 function_values = empty_values,
496 precomputed_volume = 0 }
497 contained = contains_point t exterior_point
498
499
500 -- | The barycentric coordinate of v0 with respect to itself should
501 -- be one.
502 prop_b0_v0_always_unity :: Tetrahedron -> Property
503 prop_b0_v0_always_unity t =
504 (volume t) > 0 ==> (b0 t) (v0 t) ~= 1.0
505
506 -- | The barycentric coordinate of v1 with respect to v0 should
507 -- be zero.
508 prop_b0_v1_always_zero :: Tetrahedron -> Property
509 prop_b0_v1_always_zero t =
510 (volume t) > 0 ==> (b0 t) (v1 t) ~= 0
511
512 -- | The barycentric coordinate of v2 with respect to v0 should
513 -- be zero.
514 prop_b0_v2_always_zero :: Tetrahedron -> Property
515 prop_b0_v2_always_zero t =
516 (volume t) > 0 ==> (b0 t) (v2 t) ~= 0
517
518 -- | The barycentric coordinate of v3 with respect to v0 should
519 -- be zero.
520 prop_b0_v3_always_zero :: Tetrahedron -> Property
521 prop_b0_v3_always_zero t =
522 (volume t) > 0 ==> (b0 t) (v3 t) ~= 0
523
524 -- | The barycentric coordinate of v1 with respect to itself should
525 -- be one.
526 prop_b1_v1_always_unity :: Tetrahedron -> Property
527 prop_b1_v1_always_unity t =
528 (volume t) > 0 ==> (b1 t) (v1 t) ~= 1.0
529
530 -- | The barycentric coordinate of v0 with respect to v1 should
531 -- be zero.
532 prop_b1_v0_always_zero :: Tetrahedron -> Property
533 prop_b1_v0_always_zero t =
534 (volume t) > 0 ==> (b1 t) (v0 t) ~= 0
535
536 -- | The barycentric coordinate of v2 with respect to v1 should
537 -- be zero.
538 prop_b1_v2_always_zero :: Tetrahedron -> Property
539 prop_b1_v2_always_zero t =
540 (volume t) > 0 ==> (b1 t) (v2 t) ~= 0
541
542 -- | The barycentric coordinate of v3 with respect to v1 should
543 -- be zero.
544 prop_b1_v3_always_zero :: Tetrahedron -> Property
545 prop_b1_v3_always_zero t =
546 (volume t) > 0 ==> (b1 t) (v3 t) ~= 0
547
548 -- | The barycentric coordinate of v2 with respect to itself should
549 -- be one.
550 prop_b2_v2_always_unity :: Tetrahedron -> Property
551 prop_b2_v2_always_unity t =
552 (volume t) > 0 ==> (b2 t) (v2 t) ~= 1.0
553
554 -- | The barycentric coordinate of v0 with respect to v2 should
555 -- be zero.
556 prop_b2_v0_always_zero :: Tetrahedron -> Property
557 prop_b2_v0_always_zero t =
558 (volume t) > 0 ==> (b2 t) (v0 t) ~= 0
559
560 -- | The barycentric coordinate of v1 with respect to v2 should
561 -- be zero.
562 prop_b2_v1_always_zero :: Tetrahedron -> Property
563 prop_b2_v1_always_zero t =
564 (volume t) > 0 ==> (b2 t) (v1 t) ~= 0
565
566 -- | The barycentric coordinate of v3 with respect to v2 should
567 -- be zero.
568 prop_b2_v3_always_zero :: Tetrahedron -> Property
569 prop_b2_v3_always_zero t =
570 (volume t) > 0 ==> (b2 t) (v3 t) ~= 0
571
572 -- | The barycentric coordinate of v3 with respect to itself should
573 -- be one.
574 prop_b3_v3_always_unity :: Tetrahedron -> Property
575 prop_b3_v3_always_unity t =
576 (volume t) > 0 ==> (b3 t) (v3 t) ~= 1.0
577
578 -- | The barycentric coordinate of v0 with respect to v3 should
579 -- be zero.
580 prop_b3_v0_always_zero :: Tetrahedron -> Property
581 prop_b3_v0_always_zero t =
582 (volume t) > 0 ==> (b3 t) (v0 t) ~= 0
583
584 -- | The barycentric coordinate of v1 with respect to v3 should
585 -- be zero.
586 prop_b3_v1_always_zero :: Tetrahedron -> Property
587 prop_b3_v1_always_zero t =
588 (volume t) > 0 ==> (b3 t) (v1 t) ~= 0
589
590 -- | The barycentric coordinate of v2 with respect to v3 should
591 -- be zero.
592 prop_b3_v2_always_zero :: Tetrahedron -> Property
593 prop_b3_v2_always_zero t =
594 (volume t) > 0 ==> (b3 t) (v2 t) ~= 0
595
596
597 prop_swapping_vertices_doesnt_affect_coefficients1 :: Tetrahedron -> Bool
598 prop_swapping_vertices_doesnt_affect_coefficients1 t =
599 c t 0 0 1 2 == c t' 0 0 1 2
600 where
601 t' = t { v0 = (v1 t), v1 = (v0 t) }
602
603 prop_swapping_vertices_doesnt_affect_coefficients2 :: Tetrahedron -> Bool
604 prop_swapping_vertices_doesnt_affect_coefficients2 t =
605 c t 0 1 1 1 == c t' 0 1 1 1
606 where
607 t' = t { v2 = (v3 t), v3 = (v2 t) }
608
609 prop_swapping_vertices_doesnt_affect_coefficients3 :: Tetrahedron -> Bool
610 prop_swapping_vertices_doesnt_affect_coefficients3 t =
611 c t 2 1 0 0 == c t' 2 1 0 0
612 where
613 t' = t { v2 = (v3 t), v3 = (v2 t) }
614
615 prop_swapping_vertices_doesnt_affect_coefficients4 :: Tetrahedron -> Bool
616 prop_swapping_vertices_doesnt_affect_coefficients4 t =
617 c t 2 0 0 1 == c t' 2 0 0 1
618 where
619 t' = t { v0 = (v3 t), v3 = (v0 t) }
620
621
622
623
624 tetrahedron_tests :: Test.Framework.Test
625 tetrahedron_tests =
626 testGroup "Tetrahedron Tests" [
627 tetrahedron1_geometry_tests,
628 tetrahedron2_geometry_tests,
629 containment_tests ]
630
631
632
633 p78_24_properties :: Test.Framework.Test
634 p78_24_properties =
635 testGroup "p. 78, Section (2.4) Properties" [
636 testProperty "c3000 identity" prop_c3000_identity,
637 testProperty "c2100 identity" prop_c2100_identity,
638 testProperty "c1110 identity" prop_c1110_identity]
639 where
640 -- | Returns the domain point of t with indices i,j,k,l.
641 domain_point :: Tetrahedron -> Int -> Int -> Int -> Int -> Point
642 domain_point t i j k l
643 | i + j + k + l == 3 = weighted_sum `scale` (1/3)
644 | otherwise = error "domain point index out of bounds"
645 where
646 v0' = (v0 t) `scale` (fromIntegral i)
647 v1' = (v1 t) `scale` (fromIntegral j)
648 v2' = (v2 t) `scale` (fromIntegral k)
649 v3' = (v3 t) `scale` (fromIntegral l)
650 weighted_sum = v0' + v1' + v2' + v3'
651
652
653 -- | Used for convenience in the next few tests.
654 p :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
655 p t i j k l = (polynomial t) (domain_point t i j k l)
656
657
658 -- | Given in Sorokina and Zeilfelder, p. 78.
659 prop_c3000_identity :: Tetrahedron -> Property
660 prop_c3000_identity t =
661 (volume t) > 0 ==>
662 c t 3 0 0 0 ~= p t 3 0 0 0
663
664 -- | Given in Sorokina and Zeilfelder, p. 78.
665 prop_c2100_identity :: Tetrahedron -> Property
666 prop_c2100_identity t =
667 (volume t) > 0 ==>
668 c t 2 1 0 0 ~= (term1 - term2 + term3 - term4)
669 where
670 term1 = (1/3)*(p t 0 3 0 0)
671 term2 = (5/6)*(p t 3 0 0 0)
672 term3 = 3*(p t 2 1 0 0)
673 term4 = (3/2)*(p t 1 2 0 0)
674
675 -- | Given in Sorokina and Zeilfelder, p. 78.
676 prop_c1110_identity :: Tetrahedron -> Property
677 prop_c1110_identity t =
678 (volume t) > 0 ==>
679 c t 1 1 1 0 ~= (term1 + term2 - term3 - term4)
680 where
681 term1 = (1/3)*((p t 3 0 0 0) + (p t 0 3 0 0) + (p t 0 0 3 0))
682 term2 = (9/2)*(p t 1 1 1 0)
683 term3 = (3/4)*((p t 2 1 0 0) + (p t 1 2 0 0) + (p t 2 0 1 0))
684 term4 = (3/4)*((p t 1 0 2 0) + (p t 0 2 1 0) + (p t 0 1 2 0))
685
686
687
688 tetrahedron_properties :: Test.Framework.Test
689 tetrahedron_properties =
690 testGroup "Tetrahedron Properties" [
691 p78_24_properties,
692 testProperty "b0_v0_always_unity" prop_b0_v0_always_unity,
693 testProperty "b0_v1_always_zero" prop_b0_v1_always_zero,
694 testProperty "b0_v2_always_zero" prop_b0_v2_always_zero,
695 testProperty "b0_v3_always_zero" prop_b0_v3_always_zero,
696 testProperty "b1_v1_always_unity" prop_b1_v1_always_unity,
697 testProperty "b1_v0_always_zero" prop_b1_v0_always_zero,
698 testProperty "b1_v2_always_zero" prop_b1_v2_always_zero,
699 testProperty "b1_v3_always_zero" prop_b1_v3_always_zero,
700 testProperty "b2_v2_always_unity" prop_b2_v2_always_unity,
701 testProperty "b2_v0_always_zero" prop_b2_v0_always_zero,
702 testProperty "b2_v1_always_zero" prop_b2_v1_always_zero,
703 testProperty "b2_v3_always_zero" prop_b2_v3_always_zero,
704 testProperty "b3_v3_always_unity" prop_b3_v3_always_unity,
705 testProperty "b3_v0_always_zero" prop_b3_v0_always_zero,
706 testProperty "b3_v1_always_zero" prop_b3_v1_always_zero,
707 testProperty "b3_v2_always_zero" prop_b3_v2_always_zero,
708 testProperty "swapping_vertices_doesnt_affect_coefficients1" $
709 prop_swapping_vertices_doesnt_affect_coefficients1,
710 testProperty "swapping_vertices_doesnt_affect_coefficients2" $
711 prop_swapping_vertices_doesnt_affect_coefficients2,
712 testProperty "swapping_vertices_doesnt_affect_coefficients3" $
713 prop_swapping_vertices_doesnt_affect_coefficients3,
714 testProperty "swapping_vertices_doesnt_affect_coefficients4" $
715 prop_swapping_vertices_doesnt_affect_coefficients4 ]