]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Grid.hs
Begin overhauling the program to handle other tetrahedra. Main is
[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
33 -- This is how we do a 'for' loop in Haskell.
34 -- No, seriously.
35 cubes :: Grid -> [[[Cube]]]
36 cubes g
37 | fvs == [[[]]] = [[[]]]
38 | head fvs == [[]] = [[[]]]
39 | otherwise =
40 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
41 | j <- [0..ysize]]
42 | k <- [0..zsize]]
43 where
44 fvs = function_values g
45 zsize = (length fvs) - 1
46 ysize = (length $ head fvs) - 1
47 xsize = (length $ head $ head fvs) - 1