]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Grid.hs
Add the "zoom" function.
[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
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 :: [[[Double]]] }
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 [[[Double]]]
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 -> [[[Double]]] -> 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 [[[]]]
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 | fvs == [[[]]] = [[[]]]
51 | head fvs == [[]] = [[[]]]
52 | otherwise =
53 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
54 | j <- [0..ysize]]
55 | k <- [0..zsize]]
56 where
57 fvs = function_values g
58 zsize = (length fvs) - 1
59 ysize = length (head fvs) - 1
60 xsize = length (head $ head fvs) - 1
61
62
63 -- | Takes a grid and a position as an argument and returns the cube
64 -- centered on that position. If there is no cube there (i.e. the
65 -- position is outside of the grid), it will return 'Nothing'.
66 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
67 cube_at g i j k
68 | i < 0 = Nothing
69 | j < 0 = Nothing
70 | k < 0 = Nothing
71 | i >= length (cubes g) = Nothing
72 | j >= length ((cubes g) !! i) = Nothing
73 | k >= length (((cubes g) !! i) !! j) = Nothing
74 | otherwise = Just $ (((cubes g) !! i) !! j) !! k
75
76
77 -- | Takes a 'Grid', and returns all 'Cube's belonging to it that
78 -- contain the given 'Point'.
79 find_containing_cubes :: Grid -> Point -> [Cube]
80 find_containing_cubes g p =
81 filter contains_our_point all_cubes
82 where
83 all_cubes = flatten $ cubes g
84 contains_our_point = flip contains_point p
85
86
87
88 zoom :: Grid -> Int -> [[[Double]]]
89 zoom g scale_factor
90 | fvs == [[[]]] = []
91 | head fvs == [[]] = []
92 | otherwise =
93 [[[f p | i <- [0..scaled_zsize],
94 let i' = scale_dimension i,
95 let j' = scale_dimension j,
96 let k' = scale_dimension k,
97 let p = (i', j', k') :: Point,
98 let c = (find_containing_cubes g p) !! 0,
99 let t = (find_containing_tetrahedra c p) !! 0,
100 let f = polynomial t]
101 | j <- [0..scaled_ysize]]
102 | k <- [0..scaled_xsize]]
103 where
104 scale_dimension :: Int -> Double
105 scale_dimension x = (fromIntegral x) / (fromIntegral scale_factor)
106
107 fvs = function_values g
108 scaled_zsize = ((length fvs) - 1) * scale_factor
109 scaled_ysize = (length (head fvs) - 1) * scale_factor
110 scaled_xsize = (length (head $ head fvs) - 1) * scale_factor