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