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