]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Grid.hs
Convert the zoom function to use Values3D.
[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 ThreeDimensional (contains_point)
15 import Values (Values3D, dims, empty3d, zoom_shape)
16
17 import qualified Data.Array.Repa as R
18
19 -- | Our problem is defined on a Grid. The grid size is given by the
20 -- positive number h. The function values are the values of the
21 -- function at the grid points, which are distance h from one
22 -- another in each direction (x,y,z).
23 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
24 function_values :: Values3D }
25 deriving (Eq, Show)
26
27
28 instance Arbitrary Grid where
29 arbitrary = do
30 (Positive h') <- arbitrary :: Gen (Positive Double)
31 fvs <- arbitrary :: Gen Values3D
32 return (make_grid h' fvs)
33
34
35 -- | The constructor that we want people to use. If we're passed a
36 -- non-positive grid size, we throw an error.
37 make_grid :: Double -> Values3D -> Grid
38 make_grid grid_size values
39 | grid_size <= 0 = error "grid size must be positive"
40 | otherwise = Grid grid_size values
41
42
43 -- | Creates an empty grid with grid size 1.
44 empty_grid :: Grid
45 empty_grid = Grid 1 empty3d
46
47
48 -- | Returns a three-dimensional list of cubes centered on the grid
49 -- points of g with the appropriate 'FunctionValues'.
50 cubes :: Grid -> [[[Cube]]]
51 cubes g
52 | xsize == 0 || ysize == 0 || zsize == 0 = [[[]]]
53 | otherwise =
54 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
55 | j <- [0..ysize]]
56 | k <- [0..zsize]]
57 where
58 fvs = function_values g
59 (xsize, ysize, zsize) = dims fvs
60
61
62 -- | Takes a grid and a position as an argument and returns the cube
63 -- centered on that position. If there is no cube there (i.e. the
64 -- position is outside of the grid), it will return 'Nothing'.
65 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
66 cube_at g i j k
67 | i < 0 = Nothing
68 | j < 0 = Nothing
69 | k < 0 = Nothing
70 | i >= length (cubes g) = Nothing
71 | j >= length ((cubes g) !! i) = Nothing
72 | k >= length (((cubes g) !! i) !! j) = Nothing
73 | otherwise = Just $ (((cubes g) !! i) !! j) !! k
74
75
76 -- | Takes a 'Grid', and returns all 'Cube's belonging to it that
77 -- contain the given 'Point'.
78 find_containing_cubes :: Grid -> Point -> [Cube]
79 find_containing_cubes g p =
80 filter contains_our_point all_cubes
81 where
82 all_cubes = flatten $ cubes g
83 contains_our_point = flip contains_point p
84
85
86 zoom :: Grid -> Int -> Values3D
87 zoom g scale_factor
88 | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
89 | otherwise =
90 R.traverse arr transExtent (\_ -> newlookup)
91 where
92 fvs = function_values g
93 (xsize, ysize, zsize) = dims fvs
94 arr = fvs
95 transExtent = zoom_shape scale_factor
96 newlookup :: R.DIM3 -> Double
97 newlookup (R.Z R.:. i R.:. j R.:. k) =
98 f p
99 where
100 i' = fromIntegral i
101 j' = fromIntegral j
102 k' = fromIntegral k
103 p = (i', j', k') :: Point
104 c = head (find_containing_cubes g p)
105 t = head (find_containing_tetrahedra c p)
106 f = polynomial t