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