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)
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 :: [[[Double]]] }
26 instance Arbitrary Grid where
28 (Positive h') <- arbitrary :: Gen (Positive Double)
29 fvs <- arbitrary :: Gen [[[Double]]]
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 -> [[[Double]]] -> 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 [[[]]]
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 | fvs == [[[]]] = [[[]]]
51 | head fvs == [[]] = [[[]]]
53 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
57 fvs = function_values g
58 zsize = (length fvs) - 1
59 ysize = length (head fvs) - 1
60 xsize = length (head $ head fvs) - 1
63 -- | Takes a grid and a position as an argument and returns the cube
64 -- centered on that position. If there is no cube there (i.e. the
65 -- position is outside of the grid), it will return 'Nothing'.
66 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
71 | i >= length (cubes g) = Nothing
72 | j >= length ((cubes g) !! i) = Nothing
73 | k >= length (((cubes g) !! i) !! j) = Nothing
74 | otherwise = Just $ (((cubes g) !! i) !! j) !! k
77 -- | Takes a 'Grid', and returns all 'Cube's belonging to it that
78 -- contain the given 'Point'.
79 find_containing_cubes :: Grid -> Point -> [Cube]
80 find_containing_cubes g p =
81 filter contains_our_point all_cubes
83 all_cubes = flatten $ cubes g
84 contains_our_point = flip contains_point p
88 zoom :: Grid -> Int -> [[[Double]]]
91 | head fvs == [[]] = []
93 [[[f p | i <- [0..scaled_zsize],
94 let i' = scale_dimension i,
95 let j' = scale_dimension j,
96 let k' = scale_dimension k,
97 let p = (i', j', k') :: Point,
98 let c = (find_containing_cubes g p) !! 0,
99 let t = (find_containing_tetrahedra c p) !! 0,
100 let f = polynomial t]
101 | j <- [0..scaled_ysize]]
102 | k <- [0..scaled_xsize]]
104 scale_dimension :: Int -> Double
105 scale_dimension x = (fromIntegral x) / (fromIntegral scale_factor)
107 fvs = function_values g
108 scaled_zsize = ((length fvs) - 1) * scale_factor
109 scaled_ysize = (length (head fvs) - 1) * scale_factor
110 scaled_xsize = (length (head $ head fvs) - 1) * scale_factor