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