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