]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Grid.hs
Add a bunch of documentation.
[spline3.git] / src / Grid.hs
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.
4 module Grid
5 where
6
7 import Cube (Cube(Cube))
8 import FunctionValues
9
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]]] }
16 deriving (Eq, Show)
17
18
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
25
26
27 -- | Creates an empty grid with grid size 1.
28 empty_grid :: Grid
29 empty_grid = Grid 1 [[[]]]
30
31
32 -- | Returns a three-dimensional list of cubes centered on the grid
33 -- points of g with the appropriate 'FunctionValues'.
34 cubes :: Grid -> [[[Cube]]]
35 cubes g
36 | fvs == [[[]]] = [[[]]]
37 | head fvs == [[]] = [[[]]]
38 | otherwise =
39 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
40 | j <- [0..ysize]]
41 | k <- [0..zsize]]
42 where
43 fvs = function_values g
44 zsize = (length fvs) - 1
45 ysize = (length $ head fvs) - 1
46 xsize = (length $ head $ head fvs) - 1
47
48
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
53 cube_at g i j k
54 | i < 0 = Nothing
55 | j < 0 = Nothing
56 | k < 0 = Nothing
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