]> gitweb.michael.orlitzky.com - spline3.git/blob - src/FunctionValues.hs
Begin updating everything to use Repa arrays (Values3D).
[spline3.git] / src / FunctionValues.hs
1 -- | The FunctionValues module contains the 'FunctionValues' type and
2 -- the functions used to manipulate it.
3 module FunctionValues
4 where
5
6 import Prelude hiding (LT)
7 import Test.QuickCheck (Arbitrary(..), choose)
8
9 import Cardinal
10 import Values (Values3D, dims, idx)
11
12 -- | The FunctionValues type represents the value of our function f at
13 -- the 27 points surrounding (and including) the center of a
14 -- cube. Each value of f can be accessed by the name of its
15 -- direction.
16 data FunctionValues =
17 FunctionValues { front :: Double,
18 back :: Double,
19 left :: Double,
20 right :: Double,
21 top :: Double,
22 down :: Double,
23 front_left :: Double,
24 front_right :: Double,
25 front_down :: Double,
26 front_top :: Double,
27 back_left :: Double,
28 back_right :: Double,
29 back_down :: Double,
30 back_top :: Double,
31 left_down :: Double,
32 left_top :: Double,
33 right_down :: Double,
34 right_top :: Double,
35 front_left_down :: Double,
36 front_left_top :: Double,
37 front_right_down :: Double,
38 front_right_top :: Double,
39 back_left_down :: Double,
40 back_left_top :: Double,
41 back_right_down :: Double,
42 back_right_top :: Double,
43 interior :: Double }
44 deriving (Eq, Show)
45
46
47 instance Arbitrary FunctionValues where
48 arbitrary = do
49 front' <- choose (min_double, max_double)
50 back' <- choose (min_double, max_double)
51 left' <- choose (min_double, max_double)
52 right' <- choose (min_double, max_double)
53 top' <- choose (min_double, max_double)
54 down' <- choose (min_double, max_double)
55 front_left' <- choose (min_double, max_double)
56 front_right' <- choose (min_double, max_double)
57 front_top' <- choose (min_double, max_double)
58 front_down' <- choose (min_double, max_double)
59 back_left' <- choose (min_double, max_double)
60 back_right' <- choose (min_double, max_double)
61 back_top' <- choose (min_double, max_double)
62 back_down' <- choose (min_double, max_double)
63 left_top' <- choose (min_double, max_double)
64 left_down' <- choose (min_double, max_double)
65 right_top' <- choose (min_double, max_double)
66 right_down' <- choose (min_double, max_double)
67 front_left_top' <- choose (min_double, max_double)
68 front_left_down' <- choose (min_double, max_double)
69 front_right_top' <- choose (min_double, max_double)
70 front_right_down' <- choose (min_double, max_double)
71 back_left_top' <- choose (min_double, max_double)
72 back_left_down' <- choose (min_double, max_double)
73 back_right_top' <- choose (min_double, max_double)
74 back_right_down' <- choose (min_double, max_double)
75 interior' <- choose (min_double, max_double)
76
77 return empty_values { front = front',
78 back = back',
79 left = left',
80 right = right',
81 top = top',
82 down = down',
83 front_left = front_left',
84 front_right = front_right',
85 front_top = front_top',
86 front_down = front_down',
87 back_left = back_left',
88 back_right = back_right',
89 back_top = back_top',
90 back_down = back_down',
91 left_top = left_top',
92 left_down = left_down',
93 right_top = right_top',
94 right_down = right_down',
95 front_left_top = front_left_top',
96 front_left_down = front_left_down',
97 front_right_top = front_right_top',
98 front_right_down = front_right_down',
99 back_left_top = back_left_top',
100 back_left_down = back_left_down',
101 back_right_top = back_right_top',
102 back_right_down = back_right_down',
103 interior = interior' }
104 where
105 -- | We perform addition with the function values contained in a
106 -- FunctionValues object. If we choose random doubles near the machine
107 -- min/max, we risk overflowing or underflowing the 'Double'. This
108 -- places a reasonably safe limit on the maximum size of our generated
109 -- 'Double' members.
110 max_double :: Double
111 max_double = 10000.0
112
113 -- | See 'max_double'.
114 min_double :: Double
115 min_double = (-1) * max_double
116
117
118 -- | Return a 'FunctionValues' with a bunch of zeros for data points.
119 empty_values :: FunctionValues
120 empty_values =
121 FunctionValues 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
122
123 -- | The eval function is where the magic happens for the
124 -- FunctionValues type. Given a 'Cardinal' direction and a
125 -- 'FunctionValues' object, eval will return the value of the
126 -- function f in that 'Cardinal' direction. Note that 'Cardinal' can
127 -- be a composite type; eval is what performs the \"arithmetic\" on
128 -- 'Cardinal' directions.
129 eval :: FunctionValues -> Cardinal -> Double
130 eval f F = front f
131 eval f B = back f
132 eval f L = left f
133 eval f R = right f
134 eval f T = top f
135 eval f D = down f
136 eval f FL = front_left f
137 eval f FR = front_right f
138 eval f FD = front_down f
139 eval f FT = front_top f
140 eval f BL = back_left f
141 eval f BR = back_right f
142 eval f BD = back_down f
143 eval f BT = back_top f
144 eval f LD = left_down f
145 eval f LT = left_top f
146 eval f RD = right_down f
147 eval f RT = right_top f
148 eval f FLD = front_left_down f
149 eval f FLT = front_left_top f
150 eval f FRD = front_right_down f
151 eval f FRT = front_right_top f
152 eval f BLD = back_left_down f
153 eval f BLT = back_left_top f
154 eval f BRD = back_right_down f
155 eval f BRT = back_right_top f
156 eval f I = interior f
157 eval _ (Scalar x) = x
158 eval f (Sum x y) = (eval f x) + (eval f y)
159 eval f (Difference x y) = (eval f x) - (eval f y)
160 eval f (Product x y) = (eval f x) * (eval f y)
161 eval f (Quotient x y) = (eval f x) / (eval f y)
162
163 -- | Takes a three-dimensional list of 'Double' and a set of 3D
164 -- coordinates (i,j,k), and returns the value at (i,j,k) in the
165 -- supplied list. If there is no such value, zero is returned.
166 value_at :: Values3D -> Int -> Int -> Int -> Double
167 value_at values i j k
168 | i < 0 = 0
169 | j < 0 = 0
170 | k < 0 = 0
171 | xsize <= i = 0
172 | ysize <= j = 0
173 | zsize <= k = 0
174 | otherwise = idx values i j k
175 where
176 (xsize, ysize, zsize) = dims values
177
178
179 -- | Given a three-dimensional list of 'Double' and a set of 3D
180 -- coordinates (i,j,k), constructs and returns the 'FunctionValues'
181 -- object centered at (i,j,k)
182 make_values :: Values3D -> Int -> Int -> Int -> FunctionValues
183 make_values values i j k =
184 empty_values { front = value_at values (i-1) j k,
185 back = value_at values (i+1) j k,
186 left = value_at values i (j-1) k,
187 right = value_at values i (j+1) k,
188 down = value_at values i j (k-1),
189 top = value_at values i j (k+1),
190 front_left = value_at values (i-1) (j-1) k,
191 front_right = value_at values (i-1) (j+1) k,
192 front_down =value_at values (i-1) j (k-1),
193 front_top = value_at values (i-1) j (k+1),
194 back_left = value_at values (i+1) (j-1) k,
195 back_right = value_at values (i+1) (j+1) k,
196 back_down = value_at values (i+1) j (k-1),
197 back_top = value_at values (i+1) j (k+1),
198 left_down = value_at values i (j-1) (k-1),
199 left_top = value_at values i (j-1) (k+1),
200 right_down = value_at values i (j+1) (k-1),
201 right_top = value_at values i (j+1) (k+1),
202 front_left_down = value_at values (i-1) (j-1) (k-1),
203 front_left_top = value_at values (i-1) (j-1) (k+1),
204 front_right_down = value_at values (i-1) (j+1) (k-1),
205 front_right_top = value_at values (i-1) (j+1) (k+1),
206 back_left_down = value_at values (i+1) (j-1) (k-1),
207 back_left_top = value_at values (i+1) (j-1) (k+1),
208 back_right_down = value_at values (i+1) (j+1) (k-1),
209 back_right_top = value_at values (i+1) (j+1) (k+1),
210 interior = value_at values i j k }
211
212 -- | Takes a 'FunctionValues' and a function that transforms one
213 -- 'Cardinal' to another (called a rotation). Then it applies the
214 -- rotation to each element of the 'FunctionValues' object, and
215 -- returns the result.
216 rotate :: (Cardinal -> Cardinal) -> FunctionValues -> FunctionValues
217 rotate rotation fv =
218 FunctionValues { front = eval fv (rotation F),
219 back = eval fv (rotation B),
220 left = eval fv (rotation L),
221 right = eval fv (rotation R),
222 down = eval fv (rotation D),
223 top = eval fv (rotation T),
224 front_left = eval fv (rotation FL),
225 front_right = eval fv (rotation FR),
226 front_down = eval fv (rotation FD),
227 front_top = eval fv (rotation FT),
228 back_left = eval fv (rotation BL),
229 back_right = eval fv (rotation BR),
230 back_down = eval fv (rotation BD),
231 back_top = eval fv (rotation BT),
232 left_down = eval fv (rotation LD),
233 left_top = eval fv (rotation LT),
234 right_down = eval fv (rotation RD),
235 right_top = eval fv (rotation RT),
236 front_left_down = eval fv (rotation FLD),
237 front_left_top = eval fv (rotation FLT),
238 front_right_down = eval fv (rotation FRD),
239 front_right_top = eval fv (rotation FRT),
240 back_left_down = eval fv (rotation BLD),
241 back_left_top = eval fv (rotation BLT),
242 back_right_down = eval fv (rotation BRD),
243 back_right_top = eval fv (rotation BRT),
244 interior = interior fv }