]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Grid.hs
Fix (I think) the cube offset issue.
[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 delta = h g,
61 let tet_vol = (1/24)*(delta^(3::Int)),
62 let cube_ijk =
63 Cube delta i j k (make_values fvs i j k) tet_vol]
64 where
65 xmax = xsize - 1
66 ymax = ysize - 1
67 zmax = zsize - 1
68 lbounds = (0, 0, 0)
69 ubounds = (xmax, ymax, zmax)
70 fvs = function_values g
71 (xsize, ysize, zsize) = dims fvs
72
73
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
78 cube_at g i j k
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)
86 where
87 fvs = function_values g
88 (xsize, ysize, zsize) = dims fvs
89
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.
92 --
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
100 -- exists.
101 | coord < offset = 0
102 | coord == offset && (xsize > 1 && ysize > 1 && zsize > 1) = 1
103 | otherwise = (ceiling ( (coord + offset) / cube_width )) - 1
104 where
105 (xsize, ysize, zsize) = dims (function_values g)
106 cube_width = (h g)
107 offset = cube_width / 2
108
109
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 =
115 cube_at g i j k
116 where
117 (x, y, z) = 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
121
122
123 {-# INLINE zoom_lookup #-}
124 zoom_lookup :: Grid -> ScaleFactor -> a -> (R.DIM3 -> Double)
125 zoom_lookup g scale_factor _ = zoom_result g scale_factor
126
127
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) =
131 f p
132 where
133 offset = (h g)/2
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
140 f = polynomial t
141
142
143 zoom :: Grid -> ScaleFactor -> Values3D
144 zoom g scale_factor
145 | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
146 | otherwise =
147 R.force $ R.traverse arr transExtent (zoom_lookup g scale_factor)
148 where
149 arr = function_values g
150 (xsize, ysize, zsize) = dims arr
151 transExtent = zoom_shape scale_factor