1 -- | The Grid module just contains the Grid type and two constructors
2 -- for it. We hide the main Grid constructor because we don't want
3 -- to allow instantiation of a grid with h <= 0.
7 import Test.QuickCheck (Arbitrary(..), Gen, Positive(..))
9 import Cube (Cube(Cube), find_containing_tetrahedra)
12 import Tetrahedron (polynomial)
13 import Values (Values3D, dims, empty3d, zoom_shape)
15 import qualified Data.Array.Repa as R
17 -- | Our problem is defined on a Grid. The grid size is given by the
18 -- positive number h. The function values are the values of the
19 -- function at the grid points, which are distance h from one
20 -- another in each direction (x,y,z).
21 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
22 function_values :: Values3D }
26 instance Arbitrary Grid where
28 (Positive h') <- arbitrary :: Gen (Positive Double)
29 fvs <- arbitrary :: Gen Values3D
30 return (make_grid h' fvs)
33 -- | The constructor that we want people to use. If we're passed a
34 -- non-positive grid size, we throw an error.
35 make_grid :: Double -> Values3D -> Grid
36 make_grid grid_size values
37 | grid_size <= 0 = error "grid size must be positive"
38 | otherwise = Grid grid_size values
41 -- | Creates an empty grid with grid size 1.
43 empty_grid = Grid 1 empty3d
46 -- | Returns a three-dimensional list of cubes centered on the grid
47 -- points of g with the appropriate 'FunctionValues'.
48 cubes :: Grid -> [[[Cube]]]
50 | xsize == 0 || ysize == 0 || zsize == 0 = [[[]]]
52 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
56 fvs = function_values g
57 (xsize, ysize, zsize) = dims fvs
60 -- | Takes a grid and a position as an argument and returns the cube
61 -- centered on that position. If there is no cube there (i.e. the
62 -- position is outside of the grid), it will return 'Nothing'.
63 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
68 | k >= length (cubes g) = Nothing
69 | j >= length ((cubes g) !! k) = Nothing
70 | i >= length (((cubes g) !! k) !! j) = Nothing
71 | otherwise = Just $ (((cubes g) !! k) !! j) !! i
75 -- The first cube along any axis covers (-h/2, h/2). The second
76 -- covers (h/2, 3h/2). The third, (3h/2, 5h/2), and so on.
78 -- We translate the (x,y,z) coordinates forward by 'h/2' so that the
79 -- first covers (0, h), the second covers (h, 2h), etc. This makes
80 -- it easy to figure out which cube contains the given point.
81 calculate_containing_cube_coordinate :: Grid -> Double -> Int
82 calculate_containing_cube_coordinate g coord
83 -- Don't use a cube on the boundary if we can help it. This
84 -- returns cube #1 if we would have returned cube #0 and cube #1
86 | coord == offset && (xsize > 0 && ysize > 0 && zsize > 0) = 1
87 | otherwise = (ceiling ( (coord + offset) / cube_width )) - 1
89 (xsize, ysize, zsize) = dims (function_values g)
91 offset = cube_width / 2
94 -- | Takes a 'Grid', and returns a 'Cube' containing the given 'Point'.
95 -- Since our grid is rectangular, we can figure this out without having
96 -- to check every cube.
97 find_containing_cube :: Grid -> Point -> Cube
98 find_containing_cube g p =
99 case cube_at g i j k of
101 Nothing -> error "No cube contains the given point."
104 i = calculate_containing_cube_coordinate g x
105 j = calculate_containing_cube_coordinate g y
106 k = calculate_containing_cube_coordinate g z
109 {-# INLINE zoom_lookup #-}
110 zoom_lookup :: Grid -> a -> (R.DIM3 -> Double)
111 zoom_lookup g _ = zoom_result g
114 {-# INLINE zoom_result #-}
115 zoom_result :: Grid -> R.DIM3 -> Double
116 zoom_result g (R.Z R.:. i R.:. j R.:. k) =
122 p = (i', j', k') :: Point
123 c = find_containing_cube g p
124 t = head (find_containing_tetrahedra c p)
128 zoom :: Grid -> Int -> Values3D
130 | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
132 R.force $ R.traverse arr transExtent (zoom_lookup g)
134 arr = function_values g
135 (xsize, ysize, zsize) = dims arr
136 transExtent = zoom_shape scale_factor