]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Grid.hs
b8f1d759e1f268a1e71c33cfa5b7dd3dcb58919a
[spline3.git] / src / Grid.hs
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.
4 module Grid
5 where
6
7 import Test.QuickCheck (Arbitrary(..), Gen, Positive(..))
8
9 import Cube (Cube(Cube), find_containing_tetrahedra)
10 import FunctionValues
11 import Misc (flatten)
12 import Point (Point)
13 import Tetrahedron (polynomial)
14 import Values (Values3D, dims, empty3d, zoom_shape)
15
16 import qualified Data.Array.Repa as R
17
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 }
24 deriving (Eq, Show)
25
26
27 instance Arbitrary Grid where
28 arbitrary = do
29 (Positive h') <- arbitrary :: Gen (Positive Double)
30 fvs <- arbitrary :: Gen Values3D
31 return (make_grid h' fvs)
32
33
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
40
41
42 -- | Creates an empty grid with grid size 1.
43 empty_grid :: Grid
44 empty_grid = Grid 1 empty3d
45
46
47 -- | Returns a three-dimensional list of cubes centered on the grid
48 -- points of g with the appropriate 'FunctionValues'.
49 cubes :: Grid -> [[[Cube]]]
50 cubes g
51 | xsize == 0 || ysize == 0 || zsize == 0 = [[[]]]
52 | otherwise =
53 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
54 | j <- [0..ysize]]
55 | k <- [0..zsize]]
56 where
57 fvs = function_values g
58 (xsize, ysize, zsize) = dims fvs
59
60
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 return 'Nothing'.
64 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
65 cube_at g i j k
66 | i < 0 = Nothing
67 | j < 0 = Nothing
68 | k < 0 = Nothing
69 | i >= length (cubes g) = Nothing
70 | j >= length ((cubes g) !! i) = Nothing
71 | k >= length (((cubes g) !! i) !! j) = Nothing
72 | otherwise = Just $ (((cubes g) !! i) !! j) !! k
73
74
75
76 -- The first cube along any axis covers (-h/2, h/2). The second
77 -- covers (h/2, 3h/2). The third, (3h/2, 5h/2), and so on.
78 --
79 -- We translate the (x,y,z) coordinates forward by 'h' so that the
80 -- first covers (0, h), the second covers (h, 2h), etc. This makes
81 -- it easy to figure out which cube contains the given point.
82 calculate_containing_cube_coordinate :: Grid -> Double -> Int
83 calculate_containing_cube_coordinate g coord
84 -- Don't use a cube on the boundary if we can help it.
85 | coord == delta && (xsize > 0 && ysize > 0 && zsize > 0) = 1
86 | otherwise = (ceiling ( (coord + delta) / cube_width )) - 1
87 where
88 (xsize, ysize, zsize) = dims (function_values g)
89 delta = (h g)
90 cube_width = 2 * delta
91
92
93 -- | Takes a 'Grid', and returns a 'Cube' containing the given 'Point'.
94 -- Since our grid is rectangular, we can figure this out without having
95 -- to check every cube.
96 find_containing_cube :: Grid -> Point -> Cube
97 find_containing_cube g p =
98 case cube_at g i j k of
99 Just c -> c
100 Nothing -> error "No cube contains the given point."
101 where
102 (x, y, z) = p
103 i = calculate_containing_cube_coordinate g x
104 j = calculate_containing_cube_coordinate g y
105 k = calculate_containing_cube_coordinate g z
106
107
108 {-# INLINE zoom_lookup #-}
109 zoom_lookup :: Grid -> a -> (R.DIM3 -> Double)
110 zoom_lookup g _ = zoom_result g
111
112
113 {-# INLINE zoom_result #-}
114 zoom_result :: Grid -> R.DIM3 -> Double
115 zoom_result g (R.Z R.:. i R.:. j R.:. k) =
116 f p
117 where
118 i' = fromIntegral i
119 j' = fromIntegral j
120 k' = fromIntegral k
121 p = (i', j', k') :: Point
122 c = find_containing_cube g p
123 t = head (find_containing_tetrahedra c p)
124 f = polynomial t
125
126
127 zoom :: Grid -> Int -> Values3D
128 zoom g scale_factor
129 | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
130 | otherwise =
131 R.force $ R.traverse arr transExtent (zoom_lookup g)
132 where
133 arr = function_values g
134 (xsize, ysize, zsize) = dims arr
135 transExtent = zoom_shape scale_factor