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)
13 import Tetrahedron (polynomial)
14 import ThreeDimensional (contains_point)
15 import Values (Values3D, dims, empty3d)
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 | i >= length (cubes g) = Nothing
69 | j >= length ((cubes g) !! i) = Nothing
70 | k >= length (((cubes g) !! i) !! j) = Nothing
71 | otherwise = Just $ (((cubes g) !! i) !! j) !! k
74 -- | Takes a 'Grid', and returns all 'Cube's belonging to it that
75 -- contain the given 'Point'.
76 find_containing_cubes :: Grid -> Point -> [Cube]
77 find_containing_cubes g p =
78 filter contains_our_point all_cubes
80 all_cubes = flatten $ cubes g
81 contains_our_point = flip contains_point p
85 zoom :: Grid -> Int -> [[[Double]]]
87 | xsize == 0 || ysize == 0 || zsize == 0 = []
89 [[[f p | i <- [0..scaled_zsize],
90 let i' = scale_dimension i,
91 let j' = scale_dimension j,
92 let k' = scale_dimension k,
93 let p = (i', j', k') :: Point,
94 let c = (find_containing_cubes g p) !! 0,
95 let t = (find_containing_tetrahedra c p) !! 0,
97 | j <- [0..scaled_ysize]]
98 | k <- [0..scaled_xsize]]
100 scale_dimension :: Int -> Double
101 scale_dimension x = (fromIntegral x) / (fromIntegral scale_factor)
103 fvs = function_values g
104 (xsize, ysize, zsize) = dims fvs
105 scaled_xsize = xsize * scale_factor
106 scaled_ysize = ysize * scale_factor
107 scaled_zsize = zsize * scale_factor