6 import ThreeDimensional
17 data Cube = Cube { grid :: Grid,
25 instance Show Cube where
27 "Cube_" ++ (show (i c)) ++ "," ++ (show (j c)) ++ "," ++ (show (k c)) ++
28 " (Grid: " ++ (show (grid c)) ++ ")" ++
29 " (Center: " ++ (show (center c)) ++ ")" ++
30 " (xmin: " ++ (show (xmin c)) ++ ")" ++
31 " (xmax: " ++ (show (xmax c)) ++ ")" ++
32 " (ymin: " ++ (show (ymin c)) ++ ")" ++
33 " (ymax: " ++ (show (ymax c)) ++ ")" ++
34 " (zmin: " ++ (show (zmin c)) ++ ")" ++
35 " (zmax: " ++ (show (zmax c)) ++ ")" ++
36 " (datum: " ++ (show (datum c)) ++ ")\n\n"
39 empty_cube = Cube empty_grid 0 0 0 0
42 instance Gridded Cube where
43 back c = cube_at (grid c) ((i c) + 1) (j c) (k c)
44 down c = cube_at (grid c) (i c) (j c) ((k c) - 1)
45 front c = cube_at (grid c) ((i c) - 1) (j c) (k c)
46 left c = cube_at (grid c) (i c) ((j c) - 1) (k c)
47 right c = cube_at (grid c) (i c) ((j c) + 1) (k c)
48 top c = cube_at (grid c) (i c) (j c) ((k c) + 1)
50 -- | The left-side boundary of the cube. See Sorokina and Zeilfelder,
52 xmin :: Cube -> Double
53 xmin c = (2*i' - 1)*delta / 2
55 i' = fromIntegral (i c) :: Double
58 -- | The right-side boundary of the cube. See Sorokina and Zeilfelder,
60 xmax :: Cube -> Double
61 xmax c = (2*i' + 1)*delta / 2
63 i' = fromIntegral (i c) :: Double
66 -- | The front boundary of the cube. See Sorokina and Zeilfelder,
68 ymin :: Cube -> Double
69 ymin c = (2*j' - 1)*delta / 2
71 j' = fromIntegral (j c) :: Double
74 -- | The back boundary of the cube. See Sorokina and Zeilfelder,
76 ymax :: Cube -> Double
77 ymax c = (2*j' + 1)*delta / 2
79 j' = fromIntegral (j c) :: Double
82 -- | The bottom boundary of the cube. See Sorokina and Zeilfelder,
84 zmin :: Cube -> Double
85 zmin c = (2*k' - 1)*delta / 2
87 k' = fromIntegral (k c) :: Double
90 -- | The top boundary of the cube. See Sorokina and Zeilfelder,
92 zmax :: Cube -> Double
93 zmax c = (2*k' + 1)*delta / 2
95 k' = fromIntegral (k c) :: Double
98 instance ThreeDimensional Cube where
99 -- | The center of Cube_ijk coincides with v_ijk at
100 -- (ih, jh, kh). See Sorokina and Zeilfelder, p. 76.
104 i' = fromIntegral (i c) :: Double
105 j' = fromIntegral (j c) :: Double
106 k' = fromIntegral (k c) :: Double
112 | (x_coord p) < (xmin c) = False
113 | (x_coord p) > (xmax c) = False
114 | (y_coord p) < (ymin c) = False
115 | (y_coord p) > (ymax c) = False
116 | (z_coord p) < (zmin c) = False
117 | (z_coord p) > (zmax c) = False
121 instance Num Cube where
122 (Cube g1 i1 j1 k1 d1) + (Cube _ i2 j2 k2 d2) =
123 Cube g1 (i1 + i2) (j1 + j2) (k1 + k2) (d1 + d2)
125 (Cube g1 i1 j1 k1 d1) - (Cube _ i2 j2 k2 d2) =
126 Cube g1 (i1 - i2) (j1 - j2) (k1 - k2) (d1 - d2)
128 (Cube g1 i1 j1 k1 d1) * (Cube _ i2 j2 k2 d2) =
129 Cube g1 (i1 * i2) (j1 * j2) (k1 * k2) (d1 * d2)
131 abs (Cube g1 i1 j1 k1 d1) =
132 Cube g1 (abs i1) (abs j1) (abs k1) (abs d1)
134 signum (Cube g1 i1 j1 k1 d1) =
135 Cube g1 (signum i1) (signum j1) (signum k1) (signum d1)
137 fromInteger x = empty_cube { datum = (fromIntegral x) }
139 instance Fractional Cube where
140 (Cube g1 i1 j1 k1 d1) / (Cube _ _ _ _ d2) =
141 Cube g1 i1 j1 k1 (d1 / d2)
143 recip (Cube g1 i1 j1 k1 d1) =
144 Cube g1 i1 j1 k1 (recip d1)
146 fromRational q = empty_cube { datum = fromRational q }
148 -- | Constructs a cube, switching the x and z axes.
149 reverse_cube :: Grid -> Int -> Int -> Int -> Double -> Cube
150 reverse_cube g k' j' i' = Cube g i' j' k'
153 -- | Return the cube corresponding to the grid point i,j,k. The list
154 -- of cubes is stored as [z][y][x] but we'll be requesting it by
155 -- [x][y][z] so we flip the indices in the last line.
156 cube_at :: Grid -> Int -> Int -> Int -> Cube
158 | i' >= length (function_values g) = Cube g i' j' k' 0
159 | i' < 0 = Cube g i' j' k' 0
160 | j' >= length ((function_values g) !! i') = Cube g i' j' k' 0
161 | j' < 0 = Cube g i' j' k' 0
162 | k' >= length (((function_values g) !! i') !! j') = Cube g i' j' k' 0
163 | k' < 0 = Cube g i' j' k' 0
165 (((cubes g) !! k') !! j') !! i'
168 -- These next three functions basically form a 'for' loop, looping
169 -- through the xs, ys, and zs in that order.
171 -- | The cubes_from_values function will return a function that takes
172 -- a list of values and returns a list of cubes. It could just as
173 -- well be written to take the values as a parameter; the omission
174 -- of the last parameter is known as an eta reduce.
175 cubes_from_values :: Grid -> Int -> Int -> ([Double] -> [Cube])
176 cubes_from_values g i' j' =
177 zipWith (reverse_cube g i' j') [0..]
179 -- | The cubes_from_planes function will return a function that takes
180 -- a list of planes and returns a list of cubes. It could just as
181 -- well be written to take the planes as a parameter; the omission
182 -- of the last parameter is known as an eta reduce.
183 cubes_from_planes :: Grid -> Int -> ([[Double]] -> [[Cube]])
184 cubes_from_planes g i' =
185 zipWith (cubes_from_values g i') [0..]
187 -- | Takes a grid as an argument, and returns a three-dimensional list
188 -- of cubes centered on its grid points.
189 cubes :: Grid -> [[[Cube]]]
190 cubes g = zipWith (cubes_from_planes g) [0..] (function_values g)