]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Grid.hs
Begin updating everything to use Repa arrays (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)
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 return 'Nothing'.
63 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
64 cube_at g i j k
65 | i < 0 = Nothing
66 | j < 0 = Nothing
67 | k < 0 = Nothing
68 | i >= length (cubes g) = Nothing
69 | j >= length ((cubes g) !! i) = Nothing
70 | k >= length (((cubes g) !! i) !! j) = Nothing
71 | otherwise = Just $ (((cubes g) !! i) !! j) !! k
72
73
74 -- | Takes a 'Grid', and returns all 'Cube's belonging to it that
75 -- contain the given 'Point'.
76 find_containing_cubes :: Grid -> Point -> [Cube]
77 find_containing_cubes g p =
78 filter contains_our_point all_cubes
79 where
80 all_cubes = flatten $ cubes g
81 contains_our_point = flip contains_point p
82
83
84
85 zoom :: Grid -> Int -> [[[Double]]]
86 zoom g scale_factor
87 | xsize == 0 || ysize == 0 || zsize == 0 = []
88 | otherwise =
89 [[[f p | i <- [0..scaled_zsize],
90 let i' = scale_dimension i,
91 let j' = scale_dimension j,
92 let k' = scale_dimension k,
93 let p = (i', j', k') :: Point,
94 let c = (find_containing_cubes g p) !! 0,
95 let t = (find_containing_tetrahedra c p) !! 0,
96 let f = polynomial t]
97 | j <- [0..scaled_ysize]]
98 | k <- [0..scaled_xsize]]
99 where
100 scale_dimension :: Int -> Double
101 scale_dimension x = (fromIntegral x) / (fromIntegral scale_factor)
102
103 fvs = function_values g
104 (xsize, ysize, zsize) = dims fvs
105 scaled_xsize = xsize * scale_factor
106 scaled_ysize = ysize * scale_factor
107 scaled_zsize = zsize * scale_factor
108