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