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 Cube (Cube(Cube))
10 -- | Our problem is defined on a Grid. The grid size is given by the
11 -- positive number h. The function values are the values of the
12 -- function at the grid points, which are distance h from one
13 -- another in each direction (x,y,z).
14 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
15 function_values :: [[[Double]]] }
19 -- | The constructor that we want people to use. If we're passed a
20 -- non-positive grid size, we throw an error.
21 make_grid :: Double -> [[[Double]]] -> Grid
22 make_grid grid_size values
23 | grid_size <= 0 = error "grid size must be positive"
24 | otherwise = Grid grid_size values
27 -- | Creates an empty grid with grid size 1.
29 empty_grid = Grid 1 [[[]]]
32 -- | Returns a three-dimensional list of cubes centered on the grid
33 -- points of g with the appropriate 'FunctionValues'.
34 cubes :: Grid -> [[[Cube]]]
36 | fvs == [[[]]] = [[[]]]
37 | head fvs == [[]] = [[[]]]
39 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
43 fvs = function_values g
44 zsize = (length fvs) - 1
45 ysize = length (head fvs) - 1
46 xsize = length (head $ head fvs) - 1
49 -- | Takes a grid and a position as an argument and returns the cube
50 -- centered on that position. If there is no cube there (i.e. the
51 -- position is outside of the grid), it will return 'Nothing'.
52 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
57 | i >= length (cubes g) = Nothing
58 | j >= length ((cubes g) !! i) = Nothing
59 | k >= length (((cubes g) !! i) !! j) = Nothing
60 | otherwise = Just $ (((cubes g) !! i) !! j) !! k