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