]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Grid.hs
Add a find_containing_cubes function to the Grid module.
[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 import Misc (flatten)
10 import Point (Point)
11 import ThreeDimensional (contains_point)
12
13
14 -- | Our problem is defined on a Grid. The grid size is given by the
15 -- positive number h. The function values are the values of the
16 -- function at the grid points, which are distance h from one
17 -- another in each direction (x,y,z).
18 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
19 function_values :: [[[Double]]] }
20 deriving (Eq, Show)
21
22
23 -- | The constructor that we want people to use. If we're passed a
24 -- non-positive grid size, we throw an error.
25 make_grid :: Double -> [[[Double]]] -> Grid
26 make_grid grid_size values
27 | grid_size <= 0 = error "grid size must be positive"
28 | otherwise = Grid grid_size values
29
30
31 -- | Creates an empty grid with grid size 1.
32 empty_grid :: Grid
33 empty_grid = Grid 1 [[[]]]
34
35
36 -- | Returns a three-dimensional list of cubes centered on the grid
37 -- points of g with the appropriate 'FunctionValues'.
38 cubes :: Grid -> [[[Cube]]]
39 cubes g
40 | fvs == [[[]]] = [[[]]]
41 | head fvs == [[]] = [[[]]]
42 | otherwise =
43 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
44 | j <- [0..ysize]]
45 | k <- [0..zsize]]
46 where
47 fvs = function_values g
48 zsize = (length fvs) - 1
49 ysize = length (head fvs) - 1
50 xsize = length (head $ head fvs) - 1
51
52
53 -- | Takes a grid and a position as an argument and returns the cube
54 -- centered on that position. If there is no cube there (i.e. the
55 -- position is outside of the grid), it will return 'Nothing'.
56 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
57 cube_at g i j k
58 | i < 0 = Nothing
59 | j < 0 = Nothing
60 | k < 0 = Nothing
61 | i >= length (cubes g) = Nothing
62 | j >= length ((cubes g) !! i) = Nothing
63 | k >= length (((cubes g) !! i) !! j) = Nothing
64 | otherwise = Just $ (((cubes g) !! i) !! j) !! k
65
66
67 -- | Takes a 'Grid', and returns all 'Cube's belonging to it that
68 -- contain the given 'Point'.
69 find_containing_cubes :: Grid -> Point -> [Cube]
70 find_containing_cubes g p =
71 filter contains_our_point all_cubes
72 where
73 all_cubes = flatten $ cubes g
74 contains_our_point = flip contains_point p