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