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