]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Tetrahedron.hs
Add two more function comments.
[spline3.git] / src / Tetrahedron.hs
1 module Tetrahedron
2 where
3
4 import Numeric.LinearAlgebra hiding (i, scale)
5 import Prelude hiding (LT)
6
7 import Cardinal
8 import FunctionValues
9 import Misc (factorial)
10 import Point
11 import RealFunction
12 import ThreeDimensional
13
14 data Tetrahedron = Tetrahedron { fv :: FunctionValues,
15 v0 :: Point,
16 v1 :: Point,
17 v2 :: Point,
18 v3 :: Point }
19 deriving (Eq)
20
21 instance Show Tetrahedron where
22 show t = "Tetrahedron:\n" ++
23 " fv: " ++ (show (fv t)) ++ "\n" ++
24 " v0: " ++ (show (v0 t)) ++ "\n" ++
25 " v1: " ++ (show (v1 t)) ++ "\n" ++
26 " v2: " ++ (show (v2 t)) ++ "\n" ++
27 " v3: " ++ (show (v3 t)) ++ "\n"
28
29
30 instance ThreeDimensional Tetrahedron where
31 center t = ((v0 t) + (v1 t) + (v2 t) + (v3 t)) `scale` (1/4)
32 contains_point t p =
33 (b0 t p) >= 0 && (b1 t p) >= 0 && (b2 t p) >= 0 && (b3 t p) >= 0
34
35
36 polynomial :: Tetrahedron -> (RealFunction Point)
37 polynomial t =
38 sum [ (c t i j k l) `cmult` (beta t i j k l) | i <- [0..3],
39 j <- [0..3],
40 k <- [0..3],
41 l <- [0..3],
42 i + j + k + l == 3]
43
44
45 -- | Returns the domain point of t with indices i,j,k,l.
46 -- Simply an alias for the domain_point function.
47 xi :: Tetrahedron -> Int -> Int -> Int -> Int -> Point
48 xi = domain_point
49
50 -- | Returns the domain point of t with indices i,j,k,l.
51 domain_point :: Tetrahedron -> Int -> Int -> Int -> Int -> Point
52 domain_point t i j k l
53 | i + j + k + l == 3 = weighted_sum `scale` (1/3)
54 | otherwise = error "domain point index out of bounds"
55 where
56 v0' = (v0 t) `scale` (fromIntegral i)
57 v1' = (v1 t) `scale` (fromIntegral j)
58 v2' = (v2 t) `scale` (fromIntegral k)
59 v3' = (v3 t) `scale` (fromIntegral l)
60 weighted_sum = v0' + v1' + v2' + v3'
61
62
63 -- | The Bernstein polynomial on t with indices i,j,k,l. Denoted by a
64 -- capital 'B' in the Sorokina/Zeilfelder paper.
65 beta :: Tetrahedron -> Int -> Int -> Int -> Int -> (RealFunction Point)
66 beta t i j k l
67 | (i + j + k + l == 3) =
68 coefficient `cmult` (b0_term * b1_term * b2_term * b3_term)
69 | otherwise = error "basis function index out of bounds"
70 where
71 denominator = (factorial i)*(factorial j)*(factorial k)*(factorial l)
72 coefficient = 6 / (fromIntegral denominator)
73 b0_term = (b0 t) `fexp` i
74 b1_term = (b1 t) `fexp` j
75 b2_term = (b2 t) `fexp` k
76 b3_term = (b3 t) `fexp` l
77
78
79 -- | The coefficient function. c t i j k l returns the coefficient
80 -- c_ijkl with respect to the tetrahedron t. The definition uses
81 -- pattern matching to mimic the definitions given in Sorokina and
82 -- Zeilfelder, pp. 84-86. If incorrect indices are supplied, the
83 -- function will simply error.
84 c :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
85 c t 0 0 3 0 = eval (fv t) $
86 (1/8) * (I + F + L + T + LT + FL + FT + FLT)
87
88 c t 0 0 0 3 = eval (fv t) $
89 (1/8) * (I + F + R + T + RT + FR + FT + FRT)
90
91 c t 0 0 2 1 = eval (fv t) $
92 (5/24)*(I + F + T + FT) +
93 (1/24)*(L + FL + LT + FLT)
94
95 c t 0 0 1 2 = eval (fv t) $
96 (5/24)*(I + F + T + FT) +
97 (1/24)*(R + FR + RT + FRT)
98
99 c t 0 1 2 0 = eval (fv t) $
100 (5/24)*(I + F) +
101 (1/8)*(L + T + FL + FT) +
102 (1/24)*(LT + FLT)
103
104 c t 0 1 0 2 = eval (fv t) $
105 (5/24)*(I + F) +
106 (1/8)*(R + T + FR + FT) +
107 (1/24)*(RT + FRT)
108
109 c t 0 1 1 1 = eval (fv t) $
110 (13/48)*(I + F) +
111 (7/48)*(T + FT) +
112 (1/32)*(L + R + FL + FR) +
113 (1/96)*(LT + RT + FLT + FRT)
114
115 c t 0 2 1 0 = eval (fv t) $
116 (13/48)*(I + F) +
117 (17/192)*(L + T + FL + FT) +
118 (1/96)*(LT + FLT) +
119 (1/64)*(R + D + FR + FD) +
120 (1/192)*(RT + LD + FRT + FLD)
121
122 c t 0 2 0 1 = eval (fv t) $
123 (13/48)*(I + F) +
124 (17/192)*(R + T + FR + FT) +
125 (1/96)*(RT + FRT) +
126 (1/64)*(L + D + FL + FD) +
127 (1/192)*(RD + LT + FLT + FRD)
128
129 c t 0 3 0 0 = eval (fv t) $
130 (13/48)*(I + F) +
131 (5/96)*(L + R + T + D + FL + FR + FT + FD) +
132 (1/192)*(RT + RD + LT + LD + FRT + FRD + FLT + FLD)
133
134 c t 1 0 2 0 = eval (fv t) $
135 (1/4)*I +
136 (1/6)*(F + L + T) +
137 (1/12)*(LT + FL + FT)
138
139 c t 1 0 0 2 = eval (fv t) $
140 (1/4)*I +
141 (1/6)*(F + R + T) +
142 (1/12)*(RT + FR + FT)
143
144 c t 1 0 1 1 = eval (fv t) $
145 (1/3)*I +
146 (5/24)*(F + T) +
147 (1/12)*FT +
148 (1/24)*(L + R) +
149 (1/48)*(LT + RT + FL + FR)
150
151 c t 1 1 1 0 = eval (fv t) $
152 (1/3)*I +
153 (5/24)*F +
154 (1/8)*(L + T) +
155 (5/96)*(FL + FT) +
156 (1/48)*(D + R + LT) +
157 (1/96)*(FD + LD + RT + FR)
158
159 c t 1 1 0 1 = eval (fv t) $
160 (1/3)*I +
161 (5/24)*F +
162 (1/8)*(R + T) +
163 (5/96)*(FR + FT) +
164 (1/48)*(D + L + RT) +
165 (1/96)*(FD + LT + RD + FL)
166
167 c t 1 2 0 0 = eval (fv t) $
168 (1/3)*I +
169 (5/24)*F +
170 (7/96)*(L + R + T + D) +
171 (1/32)*(FL + FR + FT + FD) +
172 (1/96)*(RT + RD + LT + LD)
173
174 c t 2 0 1 0 = eval (fv t) $
175 (3/8)*I +
176 (7/48)*(F + T + L) +
177 (1/48)*(R + D + B + LT + FL + FT) +
178 (1/96)*(RT + BT + FR + FD + LD + BL)
179
180 c t 2 0 0 1 = eval (fv t) $
181 (3/8)*I +
182 (7/48)*(F + T + R) +
183 (1/48)*(L + D + B + RT + FR + FT) +
184 (1/96)*(LT + BT + FL + FD + RD + BR)
185
186 c t 2 1 0 0 = eval (fv t) $
187 (3/8)*I +
188 (1/12)*(T + R + L + D) +
189 (1/64)*(FT + FR + FL + FD) +
190 (7/48)*F +
191 (1/48)*B +
192 (1/96)*(RT + LD + LT + RD) +
193 (1/192)*(BT + BR + BL + BD)
194
195 c t 3 0 0 0 = eval (fv t) $
196 (3/8)*I +
197 (1/12)*(T + F + L + R + D + B) +
198 (1/96)*(LT + FL + FT + RT + BT + FR) +
199 (1/96)*(FD + LD + BD + BR + RD + BL)
200
201 c _ _ _ _ _ = error "coefficient index out of bounds"
202
203
204
205 -- | The matrix used in the tetrahedron volume calculation as given in
206 -- Lai & Schumaker, Definition 15.4, page 436.
207 vol_matrix :: Tetrahedron -> Matrix Double
208 vol_matrix t = (4><4)
209 [1, 1, 1, 1,
210 x1, x2, x3, x4,
211 y1, y2, y3, y4,
212 z1, z2, z3, z4 ]
213 where
214 x1 = x_coord (v0 t)
215 x2 = x_coord (v1 t)
216 x3 = x_coord (v2 t)
217 x4 = x_coord (v3 t)
218 y1 = y_coord (v0 t)
219 y2 = y_coord (v1 t)
220 y3 = y_coord (v2 t)
221 y4 = y_coord (v3 t)
222 z1 = z_coord (v0 t)
223 z2 = z_coord (v1 t)
224 z3 = z_coord (v2 t)
225 z4 = z_coord (v3 t)
226
227 -- | Computed using the formula from Lai & Schumaker, Definition 15.4,
228 -- page 436.
229 volume :: Tetrahedron -> Double
230 volume t
231 | (v0 t) == (v1 t) = 0
232 | (v0 t) == (v2 t) = 0
233 | (v0 t) == (v3 t) = 0
234 | (v1 t) == (v2 t) = 0
235 | (v1 t) == (v3 t) = 0
236 | (v2 t) == (v3 t) = 0
237 | otherwise = (1/6)*(det (vol_matrix t))
238
239
240 -- | The barycentric coordinates of a point with respect to v0.
241 b0 :: Tetrahedron -> (RealFunction Point)
242 b0 t point = (volume inner_tetrahedron) / (volume t)
243 where
244 inner_tetrahedron = t { v0 = point }
245
246
247 -- | The barycentric coordinates of a point with respect to v1.
248 b1 :: Tetrahedron -> (RealFunction Point)
249 b1 t point = (volume inner_tetrahedron) / (volume t)
250 where
251 inner_tetrahedron = t { v1 = point }
252
253
254 -- | The barycentric coordinates of a point with respect to v2.
255 b2 :: Tetrahedron -> (RealFunction Point)
256 b2 t point = (volume inner_tetrahedron) / (volume t)
257 where
258 inner_tetrahedron = t { v2 = point }
259
260
261 -- | The barycentric coordinates of a point with respect to v3.
262 b3 :: Tetrahedron -> (RealFunction Point)
263 b3 t point = (volume inner_tetrahedron) / (volume t)
264 where
265 inner_tetrahedron = t { v3 = point }