]> gitweb.michael.orlitzky.com - spline3.git/blob - src/FunctionValues.hs
More minor doc fixes.
[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
8 import Cardinal
9
10 -- | The FunctionValues type represents the value of our function f at
11 -- the 27 points surrounding (and including) the center of a
12 -- cube. Each value of f can be accessed by the name of its
13 -- direction.
14 data FunctionValues =
15 FunctionValues { front :: Double,
16 back :: Double,
17 left :: Double,
18 right :: Double,
19 top :: Double,
20 down :: Double,
21 front_left :: Double,
22 front_right :: Double,
23 front_top :: Double,
24 front_down :: Double,
25 back_left :: Double,
26 back_right :: Double,
27 back_top :: Double,
28 back_down :: Double,
29 left_top :: Double,
30 left_down :: Double,
31 right_top :: Double,
32 right_down :: Double,
33 front_left_top :: Double,
34 front_left_down :: Double,
35 front_right_top :: Double,
36 front_right_down :: Double,
37 back_left_top :: Double,
38 back_left_down :: Double,
39 back_right_top :: Double,
40 back_right_down :: Double,
41 interior :: Double }
42 deriving (Eq, Show)
43
44 -- | Return a 'FunctionValues' with a bunch of zeros for data points.
45 empty_values :: FunctionValues
46 empty_values =
47 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
48
49 -- | The eval function is where the magic happens for the
50 -- FunctionValues type. Given a 'Cardinal' direction and a
51 -- 'FunctionValues' object, eval will return the value of the
52 -- function f in that 'Cardinal' direction. Note that 'Cardinal' can
53 -- be a composite type; eval is what performs the \"arithmetic\" on
54 -- 'Cardinal' directions.
55 eval :: FunctionValues -> Cardinal -> Double
56 eval f F = front f
57 eval f B = back f
58 eval f L = left f
59 eval f R = right f
60 eval f T = top f
61 eval f D = down f
62 eval f FL = front_left f
63 eval f FR = front_right f
64 eval f FD = front_down f
65 eval f FT = front_top f
66 eval f BL = back_left f
67 eval f BR = back_right f
68 eval f BD = back_down f
69 eval f BT = back_top f
70 eval f LD = left_down f
71 eval f LT = left_top f
72 eval f RD = right_down f
73 eval f RT = right_top f
74 eval f FLD = front_left_down f
75 eval f FLT = front_left_top f
76 eval f FRD = front_right_down f
77 eval f FRT = front_right_top f
78 eval f BLD = back_left_down f
79 eval f BLT = back_left_top f
80 eval f BRD = back_right_down f
81 eval f BRT = back_right_top f
82 eval f I = interior f
83 eval _ (Scalar x) = x
84 eval f (Sum x y) = (eval f x) + (eval f y)
85 eval f (Difference x y) = (eval f x) - (eval f y)
86 eval f (Product x y) = (eval f x) * (eval f y)
87 eval f (Quotient x y) = (eval f x) / (eval f y)
88
89 -- | Takes a three-dimensional list of 'Double' and a set of 3D
90 -- coordinates (i,j,k), and returns the value at (i,j,k) in the
91 -- supplied list. If there is no such value, zero is returned.
92 value_at :: [[[Double]]] -> Int -> Int -> Int -> Double
93 value_at values i j k
94 | i < 0 = 0
95 | j < 0 = 0
96 | k < 0 = 0
97 | length values <= k = 0
98 | length (values !! k) <= j = 0
99 | length ((values !! k) !! j) <= i = 0
100 | otherwise = ((values !! k) !! j) !! i
101
102
103 -- | Given a three-dimensional list of 'Double' and a set of 3D
104 -- coordinates (i,j,k), constructs and returns the 'FunctionValues'
105 -- object centered at (i,j,k)
106 make_values :: [[[Double]]] -> Int -> Int -> Int -> FunctionValues
107 make_values values i j k =
108 empty_values { front = value_at values (i-1) j k,
109 back = value_at values (i+1) j k,
110 left = value_at values i (j-1) k,
111 right = value_at values i (j+1) k,
112 down = value_at values i j (k-1),
113 top = value_at values i j (k+1),
114 front_left = value_at values (i-1) (j-1) k,
115 front_right = value_at values (i-1) (j+1) k,
116 front_down =value_at values (i-1) j (k-1),
117 front_top = value_at values (i-1) j (k+1),
118 back_left = value_at values (i+1) (j-1) k,
119 back_right = value_at values (i+1) (j+1) k,
120 back_down = value_at values (i+1) j (k-1),
121 back_top = value_at values (i+1) j (k+1),
122 left_down = value_at values i (j-1) (k-1),
123 left_top = value_at values i (j-1) (k+1),
124 right_top = value_at values i (j+1) (k+1),
125 right_down = value_at values i (j+1) (k-1),
126 front_left_down = value_at values (i-1) (j-1) (k-1),
127 front_left_top = value_at values (i-1) (j-1) (k+1),
128 front_right_down = value_at values (i-1) (j+1) (k-1),
129 front_right_top = value_at values (i-1) (j+1) (k+1),
130 back_left_down = value_at values (i-1) (j-1) (k-1),
131 back_left_top = value_at values (i+1) (j-1) (k+1),
132 back_right_down = value_at values (i+1) (j+1) (k-1),
133 back_right_top = value_at values (i+1) (j+1) (k+1),
134 interior = value_at values i j k }
135
136 -- | Takes a 'FunctionValues' and a function that transforms one
137 -- 'Cardinal' to another (called a rotation). Then it applies the
138 -- rotation to each element of the 'FunctionValues' object, and
139 -- returns the result.
140 rotate :: FunctionValues -> (Cardinal -> Cardinal) -> FunctionValues
141 rotate fv rotation =
142 FunctionValues { front = eval fv (rotation F),
143 back = eval fv (rotation B),
144 left = eval fv (rotation L),
145 right = eval fv (rotation R),
146 down = eval fv (rotation D),
147 top = eval fv (rotation T),
148 front_left = eval fv (rotation FL),
149 front_right = eval fv (rotation FR),
150 front_down = eval fv (rotation FD),
151 front_top = eval fv (rotation FT),
152 back_left = eval fv (rotation BL),
153 back_right = eval fv (rotation BR),
154 back_down = eval fv (rotation BD),
155 back_top = eval fv (rotation BT),
156 left_down = eval fv (rotation LD),
157 left_top = eval fv (rotation LT),
158 right_down = eval fv (rotation RD),
159 right_top = eval fv (rotation RT),
160 front_left_down = eval fv (rotation FLD),
161 front_left_top = eval fv (rotation FLT),
162 front_right_down = eval fv (rotation FRD),
163 front_right_top = eval fv (rotation FRT),
164 back_left_down = eval fv (rotation BLD),
165 back_left_top = eval fv (rotation BLT),
166 back_right_down = eval fv (rotation BRD),
167 back_right_top = eval fv (rotation BRT),
168 interior = interior fv }