]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Grid.hs
Remove the Point.distance function and associated assertion. We only need the dot...
[spline3.git] / src / Grid.hs
1 {-# LANGUAGE BangPatterns #-}
2 -- | The Grid module just contains the Grid type and two constructors
3 -- for it. We hide the main Grid constructor because we don't want
4 -- to allow instantiation of a grid with h <= 0.
5 module Grid (
6 cube_at,
7 grid_tests,
8 make_grid,
9 slow_tests,
10 zoom
11 )
12 where
13
14 import qualified Data.Array.Repa as R
15 import Test.HUnit (Assertion, assertEqual)
16 import Test.Framework (Test, testGroup)
17 import Test.Framework.Providers.HUnit (testCase)
18 import Test.Framework.Providers.QuickCheck2 (testProperty)
19 import Test.QuickCheck ((==>),
20 Arbitrary(..),
21 Gen,
22 Positive(..),
23 Property,
24 choose)
25 import Assertions (assertAlmostEqual, assertTrue)
26 import Comparisons ((~=))
27 import Cube (Cube(Cube),
28 find_containing_tetrahedron,
29 tetrahedra,
30 tetrahedron)
31 import Examples (trilinear, trilinear9x9x9, zeros, naturals_1d)
32 import FunctionValues (make_values, value_at)
33 import Point (Point)
34 import ScaleFactor (ScaleFactor)
35 import Tetrahedron (Tetrahedron, c, polynomial, v0, v1, v2, v3)
36 import ThreeDimensional (ThreeDimensional(..))
37 import Values (Values3D, dims, empty3d, zoom_shape)
38
39
40 -- | Our problem is defined on a Grid. The grid size is given by the
41 -- positive number h. The function values are the values of the
42 -- function at the grid points, which are distance h from one
43 -- another in each direction (x,y,z).
44 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
45 function_values :: Values3D }
46 deriving (Eq, Show)
47
48
49 instance Arbitrary Grid where
50 arbitrary = do
51 (Positive h') <- arbitrary :: Gen (Positive Double)
52 fvs <- arbitrary :: Gen Values3D
53 return (make_grid h' fvs)
54
55
56 -- | The constructor that we want people to use. If we're passed a
57 -- non-positive grid size, we throw an error.
58 make_grid :: Double -> Values3D -> Grid
59 make_grid grid_size values
60 | grid_size <= 0 = error "grid size must be positive"
61 | otherwise = Grid grid_size values
62
63
64
65 -- | Takes a grid and a position as an argument and returns the cube
66 -- centered on that position. If there is no cube there (i.e. the
67 -- position is outside of the grid), it will throw an error.
68 cube_at :: Grid -> Int -> Int -> Int -> Cube
69 cube_at !g !i !j !k
70 | i < 0 = error "i < 0 in cube_at"
71 | i >= xsize = error "i >= xsize in cube_at"
72 | j < 0 = error "j < 0 in cube_at"
73 | j >= ysize = error "j >= ysize in cube_at"
74 | k < 0 = error "k < 0 in cube_at"
75 | k >= zsize = error "k >= zsize in cube_at"
76 | otherwise = Cube delta i j k fvs' tet_vol
77 where
78 fvs = function_values g
79 (xsize, ysize, zsize) = dims fvs
80 fvs' = make_values fvs i j k
81 delta = h g
82 tet_vol = (1/24)*(delta^(3::Int))
83
84 -- The first cube along any axis covers (-h/2, h/2). The second
85 -- covers (h/2, 3h/2). The third, (3h/2, 5h/2), and so on.
86 --
87 -- We translate the (x,y,z) coordinates forward by 'h/2' so that the
88 -- first covers (0, h), the second covers (h, 2h), etc. This makes
89 -- it easy to figure out which cube contains the given point.
90 calculate_containing_cube_coordinate :: Grid -> Double -> Int
91 calculate_containing_cube_coordinate g coord
92 -- Don't use a cube on the boundary if we can help it. This
93 -- returns cube #1 if we would have returned cube #0 and cube #1
94 -- exists.
95 | coord < offset = 0
96 | coord == offset && (xsize > 1 && ysize > 1 && zsize > 1) = 1
97 | otherwise = (ceiling ( (coord + offset) / cube_width )) - 1
98 where
99 (xsize, ysize, zsize) = dims (function_values g)
100 cube_width = (h g)
101 offset = cube_width / 2
102
103
104 -- | Takes a 'Grid', and returns a 'Cube' containing the given 'Point'.
105 -- Since our grid is rectangular, we can figure this out without having
106 -- to check every cube.
107 find_containing_cube :: Grid -> Point -> Cube
108 find_containing_cube g p =
109 cube_at g i j k
110 where
111 (x, y, z) = p
112 i = calculate_containing_cube_coordinate g x
113 j = calculate_containing_cube_coordinate g y
114 k = calculate_containing_cube_coordinate g z
115
116
117 zoom_lookup :: Values3D -> ScaleFactor -> a -> (R.DIM3 -> Double)
118 zoom_lookup v3d scale_factor _ =
119 zoom_result v3d scale_factor
120
121
122 zoom_result :: Values3D -> ScaleFactor -> R.DIM3 -> Double
123 zoom_result v3d (sfx, sfy, sfz) (R.Z R.:. m R.:. n R.:. o) =
124 f p
125 where
126 g = make_grid 1 v3d
127 offset = (h g)/2
128 m' = (fromIntegral m) / (fromIntegral sfx) - offset
129 n' = (fromIntegral n) / (fromIntegral sfy) - offset
130 o' = (fromIntegral o) / (fromIntegral sfz) - offset
131 p = (m', n', o') :: Point
132 cube = find_containing_cube g p
133 t = find_containing_tetrahedron cube p
134 f = polynomial t
135
136
137 zoom :: Values3D -> ScaleFactor -> Values3D
138 zoom v3d scale_factor
139 | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
140 | otherwise =
141 R.force $ R.unsafeTraverse v3d transExtent f
142 where
143 (xsize, ysize, zsize) = dims v3d
144 transExtent = zoom_shape scale_factor
145 f = zoom_lookup v3d scale_factor
146
147
148 -- | Check all coefficients of tetrahedron0 belonging to the cube
149 -- centered on (1,1,1) with a grid constructed from the trilinear
150 -- values. See example one in the paper.
151 --
152 -- We also verify that the four vertices on face0 of the cube are
153 -- in the correct location.
154 --
155 trilinear_c0_t0_tests :: Test.Framework.Test
156 trilinear_c0_t0_tests =
157 testGroup "trilinear c0 t0"
158 [testGroup "coefficients"
159 [testCase "c0030 is correct" test_trilinear_c0030,
160 testCase "c0003 is correct" test_trilinear_c0003,
161 testCase "c0021 is correct" test_trilinear_c0021,
162 testCase "c0012 is correct" test_trilinear_c0012,
163 testCase "c0120 is correct" test_trilinear_c0120,
164 testCase "c0102 is correct" test_trilinear_c0102,
165 testCase "c0111 is correct" test_trilinear_c0111,
166 testCase "c0210 is correct" test_trilinear_c0210,
167 testCase "c0201 is correct" test_trilinear_c0201,
168 testCase "c0300 is correct" test_trilinear_c0300,
169 testCase "c1020 is correct" test_trilinear_c1020,
170 testCase "c1002 is correct" test_trilinear_c1002,
171 testCase "c1011 is correct" test_trilinear_c1011,
172 testCase "c1110 is correct" test_trilinear_c1110,
173 testCase "c1101 is correct" test_trilinear_c1101,
174 testCase "c1200 is correct" test_trilinear_c1200,
175 testCase "c2010 is correct" test_trilinear_c2010,
176 testCase "c2001 is correct" test_trilinear_c2001,
177 testCase "c2100 is correct" test_trilinear_c2100,
178 testCase "c3000 is correct" test_trilinear_c3000],
179
180 testGroup "face0 vertices"
181 [testCase "v0 is correct" test_trilinear_f0_t0_v0,
182 testCase "v1 is correct" test_trilinear_f0_t0_v1,
183 testCase "v2 is correct" test_trilinear_f0_t0_v2,
184 testCase "v3 is correct" test_trilinear_f0_t0_v3]
185 ]
186 where
187 g = make_grid 1 trilinear
188 cube = cube_at g 1 1 1
189 t = tetrahedron cube 0
190
191 test_trilinear_c0030 :: Assertion
192 test_trilinear_c0030 =
193 assertAlmostEqual "c0030 is correct" (c t 0 0 3 0) (17/8)
194
195 test_trilinear_c0003 :: Assertion
196 test_trilinear_c0003 =
197 assertAlmostEqual "c0003 is correct" (c t 0 0 0 3) (27/8)
198
199 test_trilinear_c0021 :: Assertion
200 test_trilinear_c0021 =
201 assertAlmostEqual "c0021 is correct" (c t 0 0 2 1) (61/24)
202
203 test_trilinear_c0012 :: Assertion
204 test_trilinear_c0012 =
205 assertAlmostEqual "c0012 is correct" (c t 0 0 1 2) (71/24)
206
207 test_trilinear_c0120 :: Assertion
208 test_trilinear_c0120 =
209 assertAlmostEqual "c0120 is correct" (c t 0 1 2 0) (55/24)
210
211 test_trilinear_c0102 :: Assertion
212 test_trilinear_c0102 =
213 assertAlmostEqual "c0102 is correct" (c t 0 1 0 2) (73/24)
214
215 test_trilinear_c0111 :: Assertion
216 test_trilinear_c0111 =
217 assertAlmostEqual "c0111 is correct" (c t 0 1 1 1) (8/3)
218
219 test_trilinear_c0210 :: Assertion
220 test_trilinear_c0210 =
221 assertAlmostEqual "c0210 is correct" (c t 0 2 1 0) (29/12)
222
223 test_trilinear_c0201 :: Assertion
224 test_trilinear_c0201 =
225 assertAlmostEqual "c0201 is correct" (c t 0 2 0 1) (11/4)
226
227 test_trilinear_c0300 :: Assertion
228 test_trilinear_c0300 =
229 assertAlmostEqual "c0300 is correct" (c t 0 3 0 0) (5/2)
230
231 test_trilinear_c1020 :: Assertion
232 test_trilinear_c1020 =
233 assertAlmostEqual "c1020 is correct" (c t 1 0 2 0) (8/3)
234
235 test_trilinear_c1002 :: Assertion
236 test_trilinear_c1002 =
237 assertAlmostEqual "c1002 is correct" (c t 1 0 0 2) (23/6)
238
239 test_trilinear_c1011 :: Assertion
240 test_trilinear_c1011 =
241 assertAlmostEqual "c1011 is correct" (c t 1 0 1 1) (13/4)
242
243 test_trilinear_c1110 :: Assertion
244 test_trilinear_c1110 =
245 assertAlmostEqual "c1110 is correct" (c t 1 1 1 0) (23/8)
246
247 test_trilinear_c1101 :: Assertion
248 test_trilinear_c1101 =
249 assertAlmostEqual "c1101 is correct" (c t 1 1 0 1) (27/8)
250
251 test_trilinear_c1200 :: Assertion
252 test_trilinear_c1200 =
253 assertAlmostEqual "c1200 is correct" (c t 1 2 0 0) 3
254
255 test_trilinear_c2010 :: Assertion
256 test_trilinear_c2010 =
257 assertAlmostEqual "c2010 is correct" (c t 2 0 1 0) (10/3)
258
259 test_trilinear_c2001 :: Assertion
260 test_trilinear_c2001 =
261 assertAlmostEqual "c2001 is correct" (c t 2 0 0 1) 4
262
263 test_trilinear_c2100 :: Assertion
264 test_trilinear_c2100 =
265 assertAlmostEqual "c2100 is correct" (c t 2 1 0 0) (7/2)
266
267 test_trilinear_c3000 :: Assertion
268 test_trilinear_c3000 =
269 assertAlmostEqual "c3000 is correct" (c t 3 0 0 0) 4
270
271 test_trilinear_f0_t0_v0 :: Assertion
272 test_trilinear_f0_t0_v0 =
273 assertEqual "v0 is correct" (v0 t) (1, 1, 1)
274
275 test_trilinear_f0_t0_v1 :: Assertion
276 test_trilinear_f0_t0_v1 =
277 assertEqual "v1 is correct" (v1 t) (0.5, 1, 1)
278
279 test_trilinear_f0_t0_v2 :: Assertion
280 test_trilinear_f0_t0_v2 =
281 assertEqual "v2 is correct" (v2 t) (0.5, 0.5, 1.5)
282
283 test_trilinear_f0_t0_v3 :: Assertion
284 test_trilinear_f0_t0_v3 =
285 assertEqual "v3 is correct" (v3 t) (0.5, 1.5, 1.5)
286
287
288 test_trilinear_reproduced :: Assertion
289 test_trilinear_reproduced =
290 assertTrue "trilinears are reproduced correctly" $
291 and [p (i', j', k') ~= value_at trilinear i j k
292 | i <- [0..2],
293 j <- [0..2],
294 k <- [0..2],
295 c0 <- cs,
296 t <- tetrahedra c0,
297 let p = polynomial t,
298 let i' = fromIntegral i,
299 let j' = fromIntegral j,
300 let k' = fromIntegral k]
301 where
302 g = make_grid 1 trilinear
303 cs = [ cube_at g ci cj ck | ci <- [0..2], cj <- [0..2], ck <- [0..2] ]
304
305
306 test_zeros_reproduced :: Assertion
307 test_zeros_reproduced =
308 assertTrue "the zero function is reproduced correctly" $
309 and [p (i', j', k') ~= value_at zeros i j k
310 | i <- [0..2],
311 j <- [0..2],
312 k <- [0..2],
313 let i' = fromIntegral i,
314 let j' = fromIntegral j,
315 let k' = fromIntegral k,
316 c0 <- cs,
317 t0 <- tetrahedra c0,
318 let p = polynomial t0 ]
319 where
320 g = make_grid 1 zeros
321 cs = [ cube_at g ci cj ck | ci <- [0..2], cj <- [0..2], ck <- [0..2] ]
322
323
324 -- | Make sure we can reproduce a 9x9x9 trilinear from the 3x3x3 one.
325 test_trilinear9x9x9_reproduced :: Assertion
326 test_trilinear9x9x9_reproduced =
327 assertTrue "trilinear 9x9x9 is reproduced correctly" $
328 and [p (i', j', k') ~= value_at trilinear9x9x9 i j k
329 | i <- [0..8],
330 j <- [0..8],
331 k <- [0..8],
332 t <- tetrahedra c0,
333 let p = polynomial t,
334 let i' = (fromIntegral i) * 0.5,
335 let j' = (fromIntegral j) * 0.5,
336 let k' = (fromIntegral k) * 0.5]
337 where
338 g = make_grid 1 trilinear
339 c0 = cube_at g 1 1 1
340
341
342 -- | The point 'p' in this test lies on the boundary of tetrahedra 12 and 15.
343 -- However, the 'contains_point' test fails due to some numerical innacuracy.
344 -- This bug should have been fixed by setting a positive tolerance level.
345 --
346 -- Example from before the fix:
347 --
348 -- b1 (tetrahedron c 20) (0, 17.5, 0.5)
349 -- -0.0
350 --
351 test_tetrahedra_collision_sensitivity :: Assertion
352 test_tetrahedra_collision_sensitivity =
353 assertTrue "tetrahedron collision tests isn't too sensitive" $
354 contains_point t20 p
355 where
356 g = make_grid 1 naturals_1d
357 cube = cube_at g 0 18 0
358 p = (0, 17.5, 0.5) :: Point
359 t20 = tetrahedron cube 20
360
361
362 prop_cube_indices_never_go_out_of_bounds :: Grid -> Gen Bool
363 prop_cube_indices_never_go_out_of_bounds g =
364 do
365 let delta = Grid.h g
366 let coordmin = negate (delta/2)
367
368 let (xsize, ysize, zsize) = dims $ function_values g
369 let xmax = delta*(fromIntegral xsize) - (delta/2)
370 let ymax = delta*(fromIntegral ysize) - (delta/2)
371 let zmax = delta*(fromIntegral zsize) - (delta/2)
372
373 x <- choose (coordmin, xmax)
374 y <- choose (coordmin, ymax)
375 z <- choose (coordmin, zmax)
376
377 let idx_x = calculate_containing_cube_coordinate g x
378 let idx_y = calculate_containing_cube_coordinate g y
379 let idx_z = calculate_containing_cube_coordinate g z
380
381 return $
382 idx_x >= 0 &&
383 idx_x <= xsize - 1 &&
384 idx_y >= 0 &&
385 idx_y <= ysize - 1 &&
386 idx_z >= 0 &&
387 idx_z <= zsize - 1
388
389
390 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). Note that the
391 -- third and fourth indices of c-t10 have been switched. This is
392 -- because we store the triangles oriented such that their volume is
393 -- positive. If T and T-tilde share \<v1,v2,v3\> and v0,v0-tilde point
394 -- in opposite directions, one of them has to have negative volume!
395 prop_c0120_identity :: Grid -> Property
396 prop_c0120_identity g =
397 and [xsize >= 3, ysize >= 3, zsize >= 3] ==>
398 c t0 0 1 2 0 ~= (c t0 1 0 2 0 + c t10 1 0 0 2) / 2
399 where
400 fvs = function_values g
401 (xsize, ysize, zsize) = dims fvs
402 cube0 = cube_at g 1 1 1
403 cube1 = cube_at g 0 1 1
404 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
405 t10 = tetrahedron cube1 10
406
407
408 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
409 -- 'prop_c0120_identity'.
410 prop_c0111_identity :: Grid -> Property
411 prop_c0111_identity g =
412 and [xsize >= 3, ysize >= 3, zsize >= 3] ==>
413 c t0 0 1 1 1 ~= (c t0 1 0 1 1 + c t10 1 0 1 1) / 2
414 where
415 fvs = function_values g
416 (xsize, ysize, zsize) = dims fvs
417 cube0 = cube_at g 1 1 1
418 cube1 = cube_at g 0 1 1
419 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
420 t10 = tetrahedron cube1 10
421
422
423 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
424 -- 'prop_c0120_identity'.
425 prop_c0201_identity :: Grid -> Property
426 prop_c0201_identity g =
427 and [xsize >= 3, ysize >= 3, zsize >= 3] ==>
428 c t0 0 2 0 1 ~= (c t0 1 1 0 1 + c t10 1 1 1 0) / 2
429 where
430 fvs = function_values g
431 (xsize, ysize, zsize) = dims fvs
432 cube0 = cube_at g 1 1 1
433 cube1 = cube_at g 0 1 1
434 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
435 t10 = tetrahedron cube1 10
436
437
438 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
439 -- 'prop_c0120_identity'.
440 prop_c0102_identity :: Grid -> Property
441 prop_c0102_identity g =
442 and [xsize >= 3, ysize >= 3, zsize >= 3] ==>
443 c t0 0 1 0 2 ~= (c t0 1 0 0 2 + c t10 1 0 2 0) / 2
444 where
445 fvs = function_values g
446 (xsize, ysize, zsize) = dims fvs
447 cube0 = cube_at g 1 1 1
448 cube1 = cube_at g 0 1 1
449 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
450 t10 = tetrahedron cube1 10
451
452
453 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
454 -- 'prop_c0120_identity'.
455 prop_c0210_identity :: Grid -> Property
456 prop_c0210_identity g =
457 and [xsize >= 3, ysize >= 3, zsize >= 3] ==>
458 c t0 0 2 1 0 ~= (c t0 1 1 1 0 + c t10 1 1 0 1) / 2
459 where
460 fvs = function_values g
461 (xsize, ysize, zsize) = dims fvs
462 cube0 = cube_at g 1 1 1
463 cube1 = cube_at g 0 1 1
464 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
465 t10 = tetrahedron cube1 10
466
467
468 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
469 -- 'prop_c0120_identity'.
470 prop_c0300_identity :: Grid -> Property
471 prop_c0300_identity g =
472 and [xsize >= 3, ysize >= 3, zsize >= 3] ==>
473 c t0 0 3 0 0 ~= (c t0 1 2 0 0 + c t10 1 2 0 0) / 2
474 where
475 fvs = function_values g
476 (xsize, ysize, zsize) = dims fvs
477 cube0 = cube_at g 1 1 1
478 cube1 = cube_at g 0 1 1
479 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
480 t10 = tetrahedron cube1 10
481
482
483 -- | All of the properties from Section (2.9), p. 80. These require a
484 -- grid since they refer to two adjacent cubes.
485 p80_29_properties :: Test.Framework.Test
486 p80_29_properties =
487 testGroup "p. 80, Section (2.9) Properties" [
488 testProperty "c0120 identity" prop_c0120_identity,
489 testProperty "c0111 identity" prop_c0111_identity,
490 testProperty "c0201 identity" prop_c0201_identity,
491 testProperty "c0102 identity" prop_c0102_identity,
492 testProperty "c0210 identity" prop_c0210_identity,
493 testProperty "c0300 identity" prop_c0300_identity ]
494
495
496 grid_tests :: Test.Framework.Test
497 grid_tests =
498 testGroup "Grid Tests" [
499 trilinear_c0_t0_tests,
500 p80_29_properties,
501 testCase "tetrahedra collision test isn't too sensitive"
502 test_tetrahedra_collision_sensitivity,
503 testProperty "cube indices within bounds"
504 prop_cube_indices_never_go_out_of_bounds ]
505
506
507 -- Do the slow tests last so we can stop paying attention.
508 slow_tests :: Test.Framework.Test
509 slow_tests =
510 testGroup "Slow Tests" [
511 testCase "trilinear reproduced" test_trilinear_reproduced,
512 testCase "trilinear9x9x9 reproduced" test_trilinear9x9x9_reproduced,
513 testCase "zeros reproduced" test_zeros_reproduced ]