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, zoom_shape)
17 import qualified Data.Array.Repa as R
19 -- | Our problem is defined on a Grid. The grid size is given by the
20 -- positive number h. The function values are the values of the
21 -- function at the grid points, which are distance h from one
22 -- another in each direction (x,y,z).
23 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
24 function_values :: Values3D }
28 instance Arbitrary Grid where
30 (Positive h') <- arbitrary :: Gen (Positive Double)
31 fvs <- arbitrary :: Gen Values3D
32 return (make_grid h' fvs)
35 -- | The constructor that we want people to use. If we're passed a
36 -- non-positive grid size, we throw an error.
37 make_grid :: Double -> Values3D -> Grid
38 make_grid grid_size values
39 | grid_size <= 0 = error "grid size must be positive"
40 | otherwise = Grid grid_size values
43 -- | Creates an empty grid with grid size 1.
45 empty_grid = Grid 1 empty3d
48 -- | Returns a three-dimensional list of cubes centered on the grid
49 -- points of g with the appropriate 'FunctionValues'.
50 cubes :: Grid -> [[[Cube]]]
52 | xsize == 0 || ysize == 0 || zsize == 0 = [[[]]]
54 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
58 fvs = function_values g
59 (xsize, ysize, zsize) = dims fvs
62 -- | Takes a grid and a position as an argument and returns the cube
63 -- centered on that position. If there is no cube there (i.e. the
64 -- position is outside of the grid), it will return 'Nothing'.
65 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
70 | i >= length (cubes g) = Nothing
71 | j >= length ((cubes g) !! i) = Nothing
72 | k >= length (((cubes g) !! i) !! j) = Nothing
73 | otherwise = Just $ (((cubes g) !! i) !! j) !! k
76 -- | Takes a 'Grid', and returns all 'Cube's belonging to it that
77 -- contain the given 'Point'.
78 find_containing_cubes :: Grid -> Point -> [Cube]
79 find_containing_cubes g p =
80 filter contains_our_point all_cubes
82 all_cubes = flatten $ cubes g
83 contains_our_point = flip contains_point p
86 zoom :: Grid -> Int -> Values3D
88 | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
90 R.traverse arr transExtent (\_ -> newlookup)
92 fvs = function_values g
93 (xsize, ysize, zsize) = dims fvs
95 transExtent = zoom_shape scale_factor
96 newlookup :: R.DIM3 -> Double
97 newlookup (R.Z R.:. i R.:. j R.:. k) =
103 p = (i', j', k') :: Point
104 c = head (find_containing_cubes g p)
105 t = head (find_containing_tetrahedra c p)