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 qualified Data.Array.Repa as R
8 import Test.QuickCheck (Arbitrary(..), Gen, Positive(..))
10 import Cube (Cube(Cube), find_containing_tetrahedra)
14 import Tetrahedron (polynomial)
15 import Values (Values3D, dims, empty3d, zoom_shape)
18 -- | Our problem is defined on a Grid. The grid size is given by the
19 -- positive number h. The function values are the values of the
20 -- function at the grid points, which are distance h from one
21 -- another in each direction (x,y,z).
22 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
23 function_values :: Values3D }
27 instance Arbitrary Grid where
29 (Positive h') <- arbitrary :: Gen (Positive Double)
30 fvs <- arbitrary :: Gen Values3D
31 return (make_grid h' fvs)
34 -- | The constructor that we want people to use. If we're passed a
35 -- non-positive grid size, we throw an error.
36 make_grid :: Double -> Values3D -> Grid
37 make_grid grid_size values
38 | grid_size <= 0 = error "grid size must be positive"
39 | otherwise = Grid grid_size values
42 -- | Creates an empty grid with grid size 1.
44 empty_grid = Grid 1 empty3d
47 -- | Returns a three-dimensional list of cubes centered on the grid
48 -- points of g with the appropriate 'FunctionValues'.
49 cubes :: Grid -> [[[Cube]]]
51 | xsize == 0 || ysize == 0 || zsize == 0 = [[[]]]
53 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
57 fvs = function_values g
58 (xsize, ysize, zsize) = dims fvs
61 -- | Takes a grid and a position as an argument and returns the cube
62 -- centered on that position. If there is no cube there (i.e. the
63 -- position is outside of the grid), it will throw an error.
64 cube_at :: Grid -> Int -> Int -> Int -> Cube
66 | i < 0 = error "i < 0 in cube_at"
67 | j < 0 = error "j < 0 in cube_at"
68 | k < 0 = error "k < 0 in cube_at"
70 let zsize = length (cubes g) in
72 error "k >= xsize in cube_at"
74 let ysize = length ((cubes g) !! k) in
76 error "j >= ysize in cube_at"
78 let xsize = length (((cubes g) !! k) !! j) in
80 error "i >= xsize in cube_at"
82 (((cubes g) !! k) !! j) !! i
85 -- The first cube along any axis covers (-h/2, h/2). The second
86 -- covers (h/2, 3h/2). The third, (3h/2, 5h/2), and so on.
88 -- We translate the (x,y,z) coordinates forward by 'h/2' so that the
89 -- first covers (0, h), the second covers (h, 2h), etc. This makes
90 -- it easy to figure out which cube contains the given point.
91 calculate_containing_cube_coordinate :: Grid -> Double -> Int
92 calculate_containing_cube_coordinate g coord
93 -- Don't use a cube on the boundary if we can help it. This
94 -- returns cube #1 if we would have returned cube #0 and cube #1
96 | coord == offset && (xsize > 0 && ysize > 0 && zsize > 0) = 1
97 | otherwise = (ceiling ( (coord + offset) / cube_width )) - 1
99 (xsize, ysize, zsize) = dims (function_values g)
101 offset = cube_width / 2
104 -- | Takes a 'Grid', and returns a 'Cube' containing the given 'Point'.
105 -- Since our grid is rectangular, we can figure this out without having
106 -- to check every cube.
107 find_containing_cube :: Grid -> Point -> Cube
108 find_containing_cube g p =
112 i = calculate_containing_cube_coordinate g x
113 j = calculate_containing_cube_coordinate g y
114 k = calculate_containing_cube_coordinate g z
117 {-# INLINE zoom_lookup #-}
118 zoom_lookup :: Grid -> ScaleFactor -> a -> (R.DIM3 -> Double)
119 zoom_lookup g scale_factor _ = zoom_result g scale_factor
122 {-# INLINE zoom_result #-}
123 zoom_result :: Grid -> ScaleFactor -> R.DIM3 -> Double
124 zoom_result g (sfx, sfy, sfz) (R.Z R.:. i R.:. j R.:. k) =
127 i' = (fromIntegral i) / (fromIntegral sfx)
128 j' = (fromIntegral j) / (fromIntegral sfy)
129 k' = (fromIntegral k) / (fromIntegral sfz)
130 p = (i', j', k') :: Point
131 c = find_containing_cube g p
132 t = head (find_containing_tetrahedra c p)
136 zoom :: Grid -> ScaleFactor -> Values3D
138 | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
140 R.force $ R.traverse arr transExtent (zoom_lookup g scale_factor)
142 arr = function_values g
143 (xsize, ysize, zsize) = dims arr
144 transExtent = zoom_shape scale_factor