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 Data.Array (Array, array, (!))
8 import qualified Data.Array.Repa as R
9 import Test.QuickCheck (Arbitrary(..), Gen, Positive(..))
11 import Cube (Cube(Cube), find_containing_tetrahedron)
15 import Tetrahedron (polynomial)
16 import Values (Values3D, dims, empty3d, zoom_shape)
19 type CubeGrid = Array (Int,Int,Int) Cube
22 -- | Our problem is defined on a Grid. The grid size is given by the
23 -- positive number h. The function values are the values of the
24 -- function at the grid points, which are distance h from one
25 -- another in each direction (x,y,z).
26 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
27 function_values :: Values3D }
31 instance Arbitrary Grid where
33 (Positive h') <- arbitrary :: Gen (Positive Double)
34 fvs <- arbitrary :: Gen Values3D
35 return (make_grid h' fvs)
38 -- | The constructor that we want people to use. If we're passed a
39 -- non-positive grid size, we throw an error.
40 make_grid :: Double -> Values3D -> Grid
41 make_grid grid_size values
42 | grid_size <= 0 = error "grid size must be positive"
43 | otherwise = Grid grid_size values
46 -- | Creates an empty grid with grid size 1.
48 empty_grid = Grid 1 empty3d
51 -- | Returns a three-dimensional array of cubes centered on the grid
52 -- points of g with the appropriate 'FunctionValues'.
53 cubes :: Grid -> CubeGrid
55 = array (lbounds, ubounds)
61 let tet_vol = (1/24)*(delta^(3::Int)),
63 Cube delta i j k (make_values fvs i j k) tet_vol]
69 ubounds = (xmax, ymax, zmax)
70 fvs = function_values g
71 (xsize, ysize, zsize) = dims fvs
74 -- | Takes a grid and a position as an argument and returns the cube
75 -- centered on that position. If there is no cube there (i.e. the
76 -- position is outside of the grid), it will throw an error.
77 cube_at :: Grid -> Int -> Int -> Int -> Cube
79 | i < 0 = error "i < 0 in cube_at"
80 | i >= xsize = error "i >= xsize in cube_at"
81 | j < 0 = error "j < 0 in cube_at"
82 | j >= ysize = error "j >= ysize in cube_at"
83 | k < 0 = error "k < 0 in cube_at"
84 | k >= zsize = error "k >= zsize in cube_at"
85 | otherwise = (cubes g) ! (i,j,k)
87 fvs = function_values g
88 (xsize, ysize, zsize) = dims fvs
90 -- The first cube along any axis covers (-h/2, h/2). The second
91 -- covers (h/2, 3h/2). The third, (3h/2, 5h/2), and so on.
93 -- We translate the (x,y,z) coordinates forward by 'h/2' so that the
94 -- first covers (0, h), the second covers (h, 2h), etc. This makes
95 -- it easy to figure out which cube contains the given point.
96 calculate_containing_cube_coordinate :: Grid -> Double -> Int
97 calculate_containing_cube_coordinate g coord
98 -- Don't use a cube on the boundary if we can help it. This
99 -- returns cube #1 if we would have returned cube #0 and cube #1
102 | coord == offset && (xsize > 1 && ysize > 1 && zsize > 1) = 1
103 | otherwise = (ceiling ( (coord + offset) / cube_width )) - 1
105 (xsize, ysize, zsize) = dims (function_values g)
107 offset = cube_width / 2
110 -- | Takes a 'Grid', and returns a 'Cube' containing the given 'Point'.
111 -- Since our grid is rectangular, we can figure this out without having
112 -- to check every cube.
113 find_containing_cube :: Grid -> Point -> Cube
114 find_containing_cube g p =
118 i = calculate_containing_cube_coordinate g x
119 j = calculate_containing_cube_coordinate g y
120 k = calculate_containing_cube_coordinate g z
123 {-# INLINE zoom_lookup #-}
124 zoom_lookup :: Grid -> ScaleFactor -> a -> (R.DIM3 -> Double)
125 zoom_lookup g scale_factor _ = zoom_result g scale_factor
128 {-# INLINE zoom_result #-}
129 zoom_result :: Grid -> ScaleFactor -> R.DIM3 -> Double
130 zoom_result g (sfx, sfy, sfz) (R.Z R.:. i R.:. j R.:. k) =
134 i' = (fromIntegral i) / (fromIntegral sfx) - offset
135 j' = (fromIntegral j) / (fromIntegral sfy) - offset
136 k' = (fromIntegral k) / (fromIntegral sfz) - offset
137 p = (i', j', k') :: Point
138 c = find_containing_cube g p
139 t = find_containing_tetrahedron c p
143 zoom :: Grid -> ScaleFactor -> Values3D
145 | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
147 R.force $ R.traverse arr transExtent (zoom_lookup g scale_factor)
149 arr = function_values g
150 (xsize, ysize, zsize) = dims arr
151 transExtent = zoom_shape scale_factor