1 {-# LANGUAGE BangPatterns #-}
11 tetrahedron_properties,
17 import qualified Data.Vector as V (
23 import Test.Framework (Test, testGroup)
24 import Test.Framework.Providers.HUnit (testCase)
25 import Test.Framework.Providers.QuickCheck2 (testProperty)
26 import Test.HUnit (Assertion, assertEqual)
27 import Test.QuickCheck (Arbitrary(..), Gen, Property, (==>))
29 import Comparisons ((~=))
30 import FunctionValues (FunctionValues(..), empty_values)
31 import Misc (factorial)
32 import Point (Point(..), scale)
33 import RealFunction (RealFunction, cmult, fexp)
36 Tetrahedron { function_values :: FunctionValues,
41 precomputed_volume :: !Double
46 instance Arbitrary Tetrahedron where
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
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
58 return (Tetrahedron rnd_fv rnd_v0 rnd_v1 rnd_v2 rnd_v3 vol)
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"
70 -- | Find the barycenter of the given tetrahedron.
71 -- We just average the four vertices.
72 barycenter :: Tetrahedron -> Point
73 barycenter (Tetrahedron _ v0' v1' v2' v3' _) =
74 (v0' + v1' + v2' + v3') `scale` (1/4)
78 {-# INLINE polynomial #-}
79 polynomial :: Tetrahedron -> (RealFunction Point)
81 V.sum $ V.singleton ((c t 0 0 0 3) `cmult` (beta t 0 0 0 3)) `V.snoc`
82 ((c t 0 0 1 2) `cmult` (beta t 0 0 1 2)) `V.snoc`
83 ((c t 0 0 2 1) `cmult` (beta t 0 0 2 1)) `V.snoc`
84 ((c t 0 0 3 0) `cmult` (beta t 0 0 3 0)) `V.snoc`
85 ((c t 0 1 0 2) `cmult` (beta t 0 1 0 2)) `V.snoc`
86 ((c t 0 1 1 1) `cmult` (beta t 0 1 1 1)) `V.snoc`
87 ((c t 0 1 2 0) `cmult` (beta t 0 1 2 0)) `V.snoc`
88 ((c t 0 2 0 1) `cmult` (beta t 0 2 0 1)) `V.snoc`
89 ((c t 0 2 1 0) `cmult` (beta t 0 2 1 0)) `V.snoc`
90 ((c t 0 3 0 0) `cmult` (beta t 0 3 0 0)) `V.snoc`
91 ((c t 1 0 0 2) `cmult` (beta t 1 0 0 2)) `V.snoc`
92 ((c t 1 0 1 1) `cmult` (beta t 1 0 1 1)) `V.snoc`
93 ((c t 1 0 2 0) `cmult` (beta t 1 0 2 0)) `V.snoc`
94 ((c t 1 1 0 1) `cmult` (beta t 1 1 0 1)) `V.snoc`
95 ((c t 1 1 1 0) `cmult` (beta t 1 1 1 0)) `V.snoc`
96 ((c t 1 2 0 0) `cmult` (beta t 1 2 0 0)) `V.snoc`
97 ((c t 2 0 0 1) `cmult` (beta t 2 0 0 1)) `V.snoc`
98 ((c t 2 0 1 0) `cmult` (beta t 2 0 1 0)) `V.snoc`
99 ((c t 2 1 0 0) `cmult` (beta t 2 1 0 0)) `V.snoc`
100 ((c t 3 0 0 0) `cmult` (beta t 3 0 0 0))
104 -- | The Bernstein polynomial on t with indices i,j,k,l. Denoted by a
105 -- capital 'B' in the Sorokina/Zeilfelder paper.
106 beta :: Tetrahedron -> Int -> Int -> Int -> Int -> (RealFunction Point)
108 coefficient `cmult` (b0_term * b1_term * b2_term * b3_term)
110 denominator = (factorial i)*(factorial j)*(factorial k)*(factorial l)
111 coefficient = 6 / (fromIntegral denominator)
112 b0_term = (b0 t) `fexp` i
113 b1_term = (b1 t) `fexp` j
114 b2_term = (b2 t) `fexp` k
115 b3_term = (b3 t) `fexp` l
118 -- | The coefficient function. c t i j k l returns the coefficient
119 -- c_ijkl with respect to the tetrahedron t. The definition uses
120 -- pattern matching to mimic the definitions given in Sorokina and
121 -- Zeilfelder, pp. 84-86. If incorrect indices are supplied, the world
122 -- will end. This is for performance reasons.
123 c :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
127 fvs = function_values t
146 fld = front_left_down fvs
147 flt = front_left_top fvs
148 frd = front_right_down fvs
149 frt = front_right_top fvs
152 coefficient :: Int -> Int -> Int -> Int -> Double
153 coefficient 0 0 3 0 =
154 (1/8) * (i' + f + l' + t' + lt + fl + ft + flt)
156 coefficient 0 0 0 3 =
157 (1/8) * (i' + f + r + t' + rt + fr + ft + frt)
159 coefficient 0 0 2 1 =
160 (5/24)*(i' + f + t' + ft) + (1/24)*(l' + fl + lt + flt)
162 coefficient 0 0 1 2 =
163 (5/24)*(i' + f + t' + ft) + (1/24)*(r + fr + rt + frt)
165 coefficient 0 1 2 0 =
166 (5/24)*(i' + f) + (1/8)*(l' + t' + fl + ft)
169 coefficient 0 1 0 2 =
170 (5/24)*(i' + f) + (1/8)*(r + t' + fr + ft)
173 coefficient 0 1 1 1 =
174 (13/48)*(i' + f) + (7/48)*(t' + ft)
175 + (1/32)*(l' + r + fl + fr)
176 + (1/96)*(lt + rt + flt + frt)
178 coefficient 0 2 1 0 =
179 (13/48)*(i' + f) + (17/192)*(l' + t' + fl + ft)
181 + (1/64)*(r + d + fr + fd)
182 + (1/192)*(rt + ld + frt + fld)
184 coefficient 0 2 0 1 =
185 (13/48)*(i' + f) + (17/192)*(r + t' + fr + ft)
187 + (1/64)*(l' + d + fl + fd)
188 + (1/192)*(rd + lt + flt + frd)
190 coefficient 0 3 0 0 =
191 (13/48)*(i' + f) + (5/96)*(l' + r + t' + d + fl + fr + ft + fd)
192 + (1/192)*(rt + rd + lt + ld + frt + frd + flt + fld)
194 coefficient 1 0 2 0 =
195 (1/4)*i' + (1/6)*(f + l' + t')
196 + (1/12)*(lt + fl + ft)
198 coefficient 1 0 0 2 =
199 (1/4)*i' + (1/6)*(f + r + t')
200 + (1/12)*(rt + fr + ft)
202 coefficient 1 0 1 1 =
203 (1/3)*i' + (5/24)*(f + t')
206 + (1/48)*(lt + rt + fl + fr)
208 coefficient 1 1 1 0 =
212 + (1/48)*(d + r + lt)
213 + (1/96)*(fd + ld + rt + fr)
215 coefficient 1 1 0 1 =
219 + (1/48)*(d + l' + rt)
220 + (1/96)*(fd + lt + rd + fl)
222 coefficient 1 2 0 0 =
224 + (7/96)*(l' + r + t' + d)
225 + (1/32)*(fl + fr + ft + fd)
226 + (1/96)*(rt + rd + lt + ld)
228 coefficient 2 0 1 0 =
229 (3/8)*i' + (7/48)*(f + t' + l')
230 + (1/48)*(r + d + b + lt + fl + ft)
231 + (1/96)*(rt + bt + fr + fd + ld + bl)
233 coefficient 2 0 0 1 =
234 (3/8)*i' + (7/48)*(f + t' + r)
235 + (1/48)*(l' + d + b + rt + fr + ft)
236 + (1/96)*(lt + bt + fl + fd + rd + br)
238 coefficient 2 1 0 0 =
239 (3/8)*i' + (1/12)*(t' + r + l' + d)
240 + (1/64)*(ft + fr + fl + fd)
243 + (1/96)*(rt + ld + lt + rd)
244 + (1/192)*(bt + br + bl + bd)
246 coefficient 3 0 0 0 =
247 (3/8)*i' + (1/12)*(t' + f + l' + r + d + b)
248 + (1/96)*(lt + fl + ft + rt + bt + fr)
249 + (1/96)*(fd + ld + bd + br + rd + bl)
253 -- | Compute the determinant of the 4x4 matrix,
260 -- where [1] = [1, 1, 1, 1],
261 -- [x] = [x1,x2,x3,x4],
265 -- The termX nonsense is an attempt to prevent Double overflow.
266 -- which has been observed to happen with large coordinates.
268 det :: Point -> Point -> Point -> Point -> Double
276 term1 = ((x2 - x4)*y1 - (x1 - x4)*y2 + (x1 - x2)*y4)*z3
277 term2 = ((x2 - x3)*y1 - (x1 - x3)*y2 + (x1 - x2)*y3)*z4
278 term3 = ((x3 - x4)*y2 - (x2 - x4)*y3 + (x2 - x3)*y4)*z1
279 term4 = ((x3 - x4)*y1 - (x1 - x4)*y3 + (x1 - x3)*y4)*z2
280 term5 = term1 - term2
281 term6 = term3 - term4
284 -- | Computed using the formula from Lai & Schumaker, Definition 15.4,
286 {-# INLINE volume #-}
287 volume :: Tetrahedron -> Double
288 volume (Tetrahedron _ v0' v1' v2' v3' _) =
289 (1/6)*(det v0' v1' v2' v3')
291 -- | The barycentric coordinates of a point with respect to v0.
293 b0 :: Tetrahedron -> (RealFunction Point)
294 b0 t point = (volume inner_tetrahedron) / (precomputed_volume t)
296 inner_tetrahedron = t { v0 = point }
299 -- | The barycentric coordinates of a point with respect to v1.
301 b1 :: Tetrahedron -> (RealFunction Point)
302 b1 t point = (volume inner_tetrahedron) / (precomputed_volume t)
304 inner_tetrahedron = t { v1 = point }
307 -- | The barycentric coordinates of a point with respect to v2.
309 b2 :: Tetrahedron -> (RealFunction Point)
310 b2 t point = (volume inner_tetrahedron) / (precomputed_volume t)
312 inner_tetrahedron = t { v2 = point }
315 -- | The barycentric coordinates of a point with respect to v3.
317 b3 :: Tetrahedron -> (RealFunction Point)
318 b3 t point = (volume inner_tetrahedron) / (precomputed_volume t)
320 inner_tetrahedron = t { v3 = point }
325 -- | Check the volume of a particular tetrahedron (computed by hand)
326 -- Its vertices are in clockwise order, so the volume should be
328 tetrahedron1_geometry_tests :: Test.Framework.Test
329 tetrahedron1_geometry_tests =
330 testGroup "tetrahedron1 geometry"
331 [ testCase "volume1" volume1 ]
333 p0 = Point 0 (-0.5) 0
337 t = Tetrahedron { v0 = p0,
341 function_values = empty_values,
342 precomputed_volume = 0 }
346 assertEqual "volume is correct" True (vol ~= (-1/3))
351 -- | Check the volume of a particular tetrahedron (computed by hand)
352 -- Its vertices are in counter-clockwise order, so the volume should
354 tetrahedron2_geometry_tests :: Test.Framework.Test
355 tetrahedron2_geometry_tests =
356 testGroup "tetrahedron2 geometry"
357 [ testCase "volume1" volume1 ]
359 p0 = Point 0 (-0.5) 0
363 t = Tetrahedron { v0 = p0,
367 function_values = empty_values,
368 precomputed_volume = 0 }
371 volume1 = assertEqual "volume1 is correct" True (vol ~= (1/3))
377 -- | The barycentric coordinate of v0 with respect to itself should
379 prop_b0_v0_always_unity :: Tetrahedron -> Property
380 prop_b0_v0_always_unity t =
381 (volume t) > 0 ==> (b0 t) (v0 t) ~= 1.0
383 -- | The barycentric coordinate of v1 with respect to v0 should
385 prop_b0_v1_always_zero :: Tetrahedron -> Property
386 prop_b0_v1_always_zero t =
387 (volume t) > 0 ==> (b0 t) (v1 t) ~= 0
389 -- | The barycentric coordinate of v2 with respect to v0 should
391 prop_b0_v2_always_zero :: Tetrahedron -> Property
392 prop_b0_v2_always_zero t =
393 (volume t) > 0 ==> (b0 t) (v2 t) ~= 0
395 -- | The barycentric coordinate of v3 with respect to v0 should
397 prop_b0_v3_always_zero :: Tetrahedron -> Property
398 prop_b0_v3_always_zero t =
399 (volume t) > 0 ==> (b0 t) (v3 t) ~= 0
401 -- | The barycentric coordinate of v1 with respect to itself should
403 prop_b1_v1_always_unity :: Tetrahedron -> Property
404 prop_b1_v1_always_unity t =
405 (volume t) > 0 ==> (b1 t) (v1 t) ~= 1.0
407 -- | The barycentric coordinate of v0 with respect to v1 should
409 prop_b1_v0_always_zero :: Tetrahedron -> Property
410 prop_b1_v0_always_zero t =
411 (volume t) > 0 ==> (b1 t) (v0 t) ~= 0
413 -- | The barycentric coordinate of v2 with respect to v1 should
415 prop_b1_v2_always_zero :: Tetrahedron -> Property
416 prop_b1_v2_always_zero t =
417 (volume t) > 0 ==> (b1 t) (v2 t) ~= 0
419 -- | The barycentric coordinate of v3 with respect to v1 should
421 prop_b1_v3_always_zero :: Tetrahedron -> Property
422 prop_b1_v3_always_zero t =
423 (volume t) > 0 ==> (b1 t) (v3 t) ~= 0
425 -- | The barycentric coordinate of v2 with respect to itself should
427 prop_b2_v2_always_unity :: Tetrahedron -> Property
428 prop_b2_v2_always_unity t =
429 (volume t) > 0 ==> (b2 t) (v2 t) ~= 1.0
431 -- | The barycentric coordinate of v0 with respect to v2 should
433 prop_b2_v0_always_zero :: Tetrahedron -> Property
434 prop_b2_v0_always_zero t =
435 (volume t) > 0 ==> (b2 t) (v0 t) ~= 0
437 -- | The barycentric coordinate of v1 with respect to v2 should
439 prop_b2_v1_always_zero :: Tetrahedron -> Property
440 prop_b2_v1_always_zero t =
441 (volume t) > 0 ==> (b2 t) (v1 t) ~= 0
443 -- | The barycentric coordinate of v3 with respect to v2 should
445 prop_b2_v3_always_zero :: Tetrahedron -> Property
446 prop_b2_v3_always_zero t =
447 (volume t) > 0 ==> (b2 t) (v3 t) ~= 0
449 -- | The barycentric coordinate of v3 with respect to itself should
451 prop_b3_v3_always_unity :: Tetrahedron -> Property
452 prop_b3_v3_always_unity t =
453 (volume t) > 0 ==> (b3 t) (v3 t) ~= 1.0
455 -- | The barycentric coordinate of v0 with respect to v3 should
457 prop_b3_v0_always_zero :: Tetrahedron -> Property
458 prop_b3_v0_always_zero t =
459 (volume t) > 0 ==> (b3 t) (v0 t) ~= 0
461 -- | The barycentric coordinate of v1 with respect to v3 should
463 prop_b3_v1_always_zero :: Tetrahedron -> Property
464 prop_b3_v1_always_zero t =
465 (volume t) > 0 ==> (b3 t) (v1 t) ~= 0
467 -- | The barycentric coordinate of v2 with respect to v3 should
469 prop_b3_v2_always_zero :: Tetrahedron -> Property
470 prop_b3_v2_always_zero t =
471 (volume t) > 0 ==> (b3 t) (v2 t) ~= 0
474 prop_swapping_vertices_doesnt_affect_coefficients1 :: Tetrahedron -> Bool
475 prop_swapping_vertices_doesnt_affect_coefficients1 t =
476 c t 0 0 1 2 == c t' 0 0 1 2
478 t' = t { v0 = (v1 t), v1 = (v0 t) }
480 prop_swapping_vertices_doesnt_affect_coefficients2 :: Tetrahedron -> Bool
481 prop_swapping_vertices_doesnt_affect_coefficients2 t =
482 c t 0 1 1 1 == c t' 0 1 1 1
484 t' = t { v2 = (v3 t), v3 = (v2 t) }
486 prop_swapping_vertices_doesnt_affect_coefficients3 :: Tetrahedron -> Bool
487 prop_swapping_vertices_doesnt_affect_coefficients3 t =
488 c t 2 1 0 0 == c t' 2 1 0 0
490 t' = t { v2 = (v3 t), v3 = (v2 t) }
492 prop_swapping_vertices_doesnt_affect_coefficients4 :: Tetrahedron -> Bool
493 prop_swapping_vertices_doesnt_affect_coefficients4 t =
494 c t 2 0 0 1 == c t' 2 0 0 1
496 t' = t { v0 = (v3 t), v3 = (v0 t) }
501 tetrahedron_tests :: Test.Framework.Test
503 testGroup "Tetrahedron Tests" [
504 tetrahedron1_geometry_tests,
505 tetrahedron2_geometry_tests ]
509 p78_24_properties :: Test.Framework.Test
511 testGroup "p. 78, Section (2.4) Properties" [
512 testProperty "c3000 identity" prop_c3000_identity,
513 testProperty "c2100 identity" prop_c2100_identity,
514 testProperty "c1110 identity" prop_c1110_identity]
516 -- | Returns the domain point of t with indices i,j,k,l.
517 domain_point :: Tetrahedron -> Int -> Int -> Int -> Int -> Point
518 domain_point t i j k l =
519 weighted_sum `scale` (1/3)
521 v0' = (v0 t) `scale` (fromIntegral i)
522 v1' = (v1 t) `scale` (fromIntegral j)
523 v2' = (v2 t) `scale` (fromIntegral k)
524 v3' = (v3 t) `scale` (fromIntegral l)
525 weighted_sum = v0' + v1' + v2' + v3'
528 -- | Used for convenience in the next few tests.
529 p :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
530 p t i j k l = (polynomial t) (domain_point t i j k l)
533 -- | Given in Sorokina and Zeilfelder, p. 78.
534 prop_c3000_identity :: Tetrahedron -> Property
535 prop_c3000_identity t =
537 c t 3 0 0 0 ~= p t 3 0 0 0
539 -- | Given in Sorokina and Zeilfelder, p. 78.
540 prop_c2100_identity :: Tetrahedron -> Property
541 prop_c2100_identity t =
543 c t 2 1 0 0 ~= (term1 - term2 + term3 - term4)
545 term1 = (1/3)*(p t 0 3 0 0)
546 term2 = (5/6)*(p t 3 0 0 0)
547 term3 = 3*(p t 2 1 0 0)
548 term4 = (3/2)*(p t 1 2 0 0)
550 -- | Given in Sorokina and Zeilfelder, p. 78.
551 prop_c1110_identity :: Tetrahedron -> Property
552 prop_c1110_identity t =
554 c t 1 1 1 0 ~= (term1 + term2 - term3 - term4)
556 term1 = (1/3)*((p t 3 0 0 0) + (p t 0 3 0 0) + (p t 0 0 3 0))
557 term2 = (9/2)*(p t 1 1 1 0)
558 term3 = (3/4)*((p t 2 1 0 0) + (p t 1 2 0 0) + (p t 2 0 1 0))
559 term4 = (3/4)*((p t 1 0 2 0) + (p t 0 2 1 0) + (p t 0 1 2 0))
563 tetrahedron_properties :: Test.Framework.Test
564 tetrahedron_properties =
565 testGroup "Tetrahedron Properties" [
567 testProperty "b0_v0_always_unity" prop_b0_v0_always_unity,
568 testProperty "b0_v1_always_zero" prop_b0_v1_always_zero,
569 testProperty "b0_v2_always_zero" prop_b0_v2_always_zero,
570 testProperty "b0_v3_always_zero" prop_b0_v3_always_zero,
571 testProperty "b1_v1_always_unity" prop_b1_v1_always_unity,
572 testProperty "b1_v0_always_zero" prop_b1_v0_always_zero,
573 testProperty "b1_v2_always_zero" prop_b1_v2_always_zero,
574 testProperty "b1_v3_always_zero" prop_b1_v3_always_zero,
575 testProperty "b2_v2_always_unity" prop_b2_v2_always_unity,
576 testProperty "b2_v0_always_zero" prop_b2_v0_always_zero,
577 testProperty "b2_v1_always_zero" prop_b2_v1_always_zero,
578 testProperty "b2_v3_always_zero" prop_b2_v3_always_zero,
579 testProperty "b3_v3_always_unity" prop_b3_v3_always_unity,
580 testProperty "b3_v0_always_zero" prop_b3_v0_always_zero,
581 testProperty "b3_v1_always_zero" prop_b3_v1_always_zero,
582 testProperty "b3_v2_always_zero" prop_b3_v2_always_zero,
583 testProperty "swapping_vertices_doesnt_affect_coefficients1"
584 prop_swapping_vertices_doesnt_affect_coefficients1,
585 testProperty "swapping_vertices_doesnt_affect_coefficients2"
586 prop_swapping_vertices_doesnt_affect_coefficients2,
587 testProperty "swapping_vertices_doesnt_affect_coefficients3"
588 prop_swapping_vertices_doesnt_affect_coefficients3,
589 testProperty "swapping_vertices_doesnt_affect_coefficients4"
590 prop_swapping_vertices_doesnt_affect_coefficients4 ]