]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Tests/Tetrahedron.hs
3b511d2104770e0ce3b5d6a3a6bfe764b49440af
[spline3.git] / src / Tests / Tetrahedron.hs
1 module Tests.Tetrahedron
2 where
3
4 import Test.Framework (Test, testGroup)
5 import Test.Framework.Providers.HUnit (testCase)
6 import Test.HUnit
7 import Test.QuickCheck (Property, (==>))
8
9 import Cardinal
10 import Comparisons
11 import FunctionValues
12 import Tests.FunctionValues()
13 import Tetrahedron
14 import ThreeDimensional
15
16 -- HUnit Tests
17
18
19 -- | Check the volume of a particular tetrahedron (computed by hand)
20 -- and whether or not it contains a specific point chosen to be
21 -- outside of it. Its vertices are in clockwise order, so the volume
22 -- should be negative.
23 tetrahedron1_geometry_tests :: Test.Framework.Test
24 tetrahedron1_geometry_tests =
25 testGroup "tetrahedron1 geometry"
26 [ testCase "volume1" volume1,
27 testCase "doesn't contain point1" doesnt_contain_point1]
28 where
29 p0 = (0, -0.5, 0)
30 p1 = (0, 0.5, 0)
31 p2 = (2, 0, 0)
32 p3 = (1, 0, 1)
33 t = Tetrahedron { v0 = p0,
34 v1 = p1,
35 v2 = p2,
36 v3 = p3,
37 fv = empty_values,
38 precomputed_volume = 0 }
39
40 volume1 :: Assertion
41 volume1 =
42 assertEqual "volume is correct" True (vol ~= (-1/3))
43 where
44 vol = volume t
45
46 doesnt_contain_point1 :: Assertion
47 doesnt_contain_point1 =
48 assertEqual "doesn't contain an exterior point" False contained
49 where
50 exterior_point = (5, 2, -9.0212)
51 contained = contains_point t exterior_point
52
53
54 -- | Check the volume of a particular tetrahedron (computed by hand)
55 -- and whether or not it contains a specific point chosen to be
56 -- inside of it. Its vertices are in counter-clockwise order, so the
57 -- volume should be positive.
58 tetrahedron2_geometry_tests :: Test.Framework.Test
59 tetrahedron2_geometry_tests =
60 testGroup "tetrahedron2 geometry"
61 [ testCase "volume1" volume1,
62 testCase "contains point1" contains_point1]
63 where
64 p0 = (0, -0.5, 0)
65 p1 = (2, 0, 0)
66 p2 = (0, 0.5, 0)
67 p3 = (1, 0, 1)
68 t = Tetrahedron { v0 = p0,
69 v1 = p1,
70 v2 = p2,
71 v3 = p3,
72 fv = empty_values,
73 precomputed_volume = 0 }
74
75 volume1 :: Assertion
76 volume1 = assertEqual "volume1 is correct" True (vol ~= (1/3))
77 where
78 vol = volume t
79
80 contains_point1 :: Assertion
81 contains_point1 = assertEqual "contains an inner point" True contained
82 where
83 inner_point = (1, 0, 0.5)
84 contained = contains_point t inner_point
85
86
87 -- | Ensure that tetrahedra do not contain a particular point chosen to
88 -- be outside of them.
89 containment_tests :: Test.Framework.Test
90 containment_tests =
91 testGroup "containment tests"
92 [ testCase "doesn't contain point2" doesnt_contain_point2,
93 testCase "doesn't contain point3" doesnt_contain_point3,
94 testCase "doesn't contain point4" doesnt_contain_point4,
95 testCase "doesn't contain point5" doesnt_contain_point5]
96 where
97 p2 = (0.5, 0.5, 1)
98 p3 = (0.5, 0.5, 0.5)
99 exterior_point = (0, 0, 0)
100
101 doesnt_contain_point2 :: Assertion
102 doesnt_contain_point2 =
103 assertEqual "doesn't contain an exterior point" False contained
104 where
105 p0 = (0, 1, 1)
106 p1 = (1, 1, 1)
107 t = Tetrahedron { v0 = p0,
108 v1 = p1,
109 v2 = p2,
110 v3 = p3,
111 fv = empty_values,
112 precomputed_volume = 0 }
113 contained = contains_point t exterior_point
114
115
116 doesnt_contain_point3 :: Assertion
117 doesnt_contain_point3 =
118 assertEqual "doesn't contain an exterior point" False contained
119 where
120 p0 = (1, 1, 1)
121 p1 = (1, 0, 1)
122 t = Tetrahedron { v0 = p0,
123 v1 = p1,
124 v2 = p2,
125 v3 = p3,
126 fv = empty_values,
127 precomputed_volume = 0 }
128 contained = contains_point t exterior_point
129
130
131 doesnt_contain_point4 :: Assertion
132 doesnt_contain_point4 =
133 assertEqual "doesn't contain an exterior point" False contained
134 where
135 p0 = (1, 0, 1)
136 p1 = (0, 0, 1)
137 t = Tetrahedron { v0 = p0,
138 v1 = p1,
139 v2 = p2,
140 v3 = p3,
141 fv = empty_values,
142 precomputed_volume = 0 }
143 contained = contains_point t exterior_point
144
145
146 doesnt_contain_point5 :: Assertion
147 doesnt_contain_point5 =
148 assertEqual "doesn't contain an exterior point" False contained
149 where
150 p0 = (0, 0, 1)
151 p1 = (0, 1, 1)
152 t = Tetrahedron { v0 = p0,
153 v1 = p1,
154 v2 = p2,
155 v3 = p3,
156 fv = empty_values,
157 precomputed_volume = 0 }
158 contained = contains_point t exterior_point
159
160
161 -- | The barycentric coordinate of v0 with respect to itself should
162 -- be one.
163 prop_b0_v0_always_unity :: Tetrahedron -> Property
164 prop_b0_v0_always_unity t =
165 (volume t) > 0 ==> (b0 t) (v0 t) ~= 1.0
166
167 -- | The barycentric coordinate of v1 with respect to v0 should
168 -- be zero.
169 prop_b0_v1_always_zero :: Tetrahedron -> Property
170 prop_b0_v1_always_zero t =
171 (volume t) > 0 ==> (b0 t) (v1 t) ~= 0
172
173 -- | The barycentric coordinate of v2 with respect to v0 should
174 -- be zero.
175 prop_b0_v2_always_zero :: Tetrahedron -> Property
176 prop_b0_v2_always_zero t =
177 (volume t) > 0 ==> (b0 t) (v2 t) ~= 0
178
179 -- | The barycentric coordinate of v3 with respect to v0 should
180 -- be zero.
181 prop_b0_v3_always_zero :: Tetrahedron -> Property
182 prop_b0_v3_always_zero t =
183 (volume t) > 0 ==> (b0 t) (v3 t) ~= 0
184
185 -- | The barycentric coordinate of v1 with respect to itself should
186 -- be one.
187 prop_b1_v1_always_unity :: Tetrahedron -> Property
188 prop_b1_v1_always_unity t =
189 (volume t) > 0 ==> (b1 t) (v1 t) ~= 1.0
190
191 -- | The barycentric coordinate of v0 with respect to v1 should
192 -- be zero.
193 prop_b1_v0_always_zero :: Tetrahedron -> Property
194 prop_b1_v0_always_zero t =
195 (volume t) > 0 ==> (b1 t) (v0 t) ~= 0
196
197 -- | The barycentric coordinate of v2 with respect to v1 should
198 -- be zero.
199 prop_b1_v2_always_zero :: Tetrahedron -> Property
200 prop_b1_v2_always_zero t =
201 (volume t) > 0 ==> (b1 t) (v2 t) ~= 0
202
203 -- | The barycentric coordinate of v3 with respect to v1 should
204 -- be zero.
205 prop_b1_v3_always_zero :: Tetrahedron -> Property
206 prop_b1_v3_always_zero t =
207 (volume t) > 0 ==> (b1 t) (v3 t) ~= 0
208
209 -- | The barycentric coordinate of v2 with respect to itself should
210 -- be one.
211 prop_b2_v2_always_unity :: Tetrahedron -> Property
212 prop_b2_v2_always_unity t =
213 (volume t) > 0 ==> (b2 t) (v2 t) ~= 1.0
214
215 -- | The barycentric coordinate of v0 with respect to v2 should
216 -- be zero.
217 prop_b2_v0_always_zero :: Tetrahedron -> Property
218 prop_b2_v0_always_zero t =
219 (volume t) > 0 ==> (b2 t) (v0 t) ~= 0
220
221 -- | The barycentric coordinate of v1 with respect to v2 should
222 -- be zero.
223 prop_b2_v1_always_zero :: Tetrahedron -> Property
224 prop_b2_v1_always_zero t =
225 (volume t) > 0 ==> (b2 t) (v1 t) ~= 0
226
227 -- | The barycentric coordinate of v3 with respect to v2 should
228 -- be zero.
229 prop_b2_v3_always_zero :: Tetrahedron -> Property
230 prop_b2_v3_always_zero t =
231 (volume t) > 0 ==> (b2 t) (v3 t) ~= 0
232
233 -- | The barycentric coordinate of v3 with respect to itself should
234 -- be one.
235 prop_b3_v3_always_unity :: Tetrahedron -> Property
236 prop_b3_v3_always_unity t =
237 (volume t) > 0 ==> (b3 t) (v3 t) ~= 1.0
238
239 -- | The barycentric coordinate of v0 with respect to v3 should
240 -- be zero.
241 prop_b3_v0_always_zero :: Tetrahedron -> Property
242 prop_b3_v0_always_zero t =
243 (volume t) > 0 ==> (b3 t) (v0 t) ~= 0
244
245 -- | The barycentric coordinate of v1 with respect to v3 should
246 -- be zero.
247 prop_b3_v1_always_zero :: Tetrahedron -> Property
248 prop_b3_v1_always_zero t =
249 (volume t) > 0 ==> (b3 t) (v1 t) ~= 0
250
251 -- | The barycentric coordinate of v2 with respect to v3 should
252 -- be zero.
253 prop_b3_v2_always_zero :: Tetrahedron -> Property
254 prop_b3_v2_always_zero t =
255 (volume t) > 0 ==> (b3 t) (v2 t) ~= 0
256
257
258 -- | Used for convenience in the next few tests; not a test itself.
259 p :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
260 p t i j k l = (polynomial t) (xi t i j k l)
261
262 -- | Given in Sorokina and Zeilfelder, p. 78.
263 prop_c3000_identity :: Tetrahedron -> Property
264 prop_c3000_identity t =
265 (volume t) > 0 ==>
266 c t 3 0 0 0 ~= p t 3 0 0 0
267
268 -- | Given in Sorokina and Zeilfelder, p. 78.
269 prop_c2100_identity :: Tetrahedron -> Property
270 prop_c2100_identity t =
271 (volume t) > 0 ==>
272 c t 2 1 0 0 ~= (term1 - term2 + term3 - term4)
273 where
274 term1 = (1/3)*(p t 0 3 0 0)
275 term2 = (5/6)*(p t 3 0 0 0)
276 term3 = 3*(p t 2 1 0 0)
277 term4 = (3/2)*(p t 1 2 0 0)
278
279 -- | Given in Sorokina and Zeilfelder, p. 78.
280 prop_c1110_identity :: Tetrahedron -> Property
281 prop_c1110_identity t =
282 (volume t) > 0 ==>
283 c t 1 1 1 0 ~= (term1 + term2 - term3 - term4)
284 where
285 term1 = (1/3)*((p t 3 0 0 0) + (p t 0 3 0 0) + (p t 0 0 3 0))
286 term2 = (9/2)*(p t 1 1 1 0)
287 term3 = (3/4)*((p t 2 1 0 0) + (p t 1 2 0 0) + (p t 2 0 1 0))
288 term4 = (3/4)*((p t 1 0 2 0) + (p t 0 2 1 0) + (p t 0 1 2 0))
289
290 prop_x_rotation_doesnt_affect_front :: Tetrahedron -> Bool
291 prop_x_rotation_doesnt_affect_front t =
292 expr1 == expr2
293 where
294 fv0 = Tetrahedron.fv t
295 fv1 = rotate cwx (Tetrahedron.fv t)
296 expr1 = front fv0
297 expr2 = front fv1
298
299 prop_x_rotation_doesnt_affect_back :: Tetrahedron -> Bool
300 prop_x_rotation_doesnt_affect_back t =
301 expr1 == expr2
302 where
303 fv0 = Tetrahedron.fv t
304 fv1 = rotate cwx (Tetrahedron.fv t)
305 expr1 = back fv0
306 expr2 = back fv1
307
308
309 prop_y_rotation_doesnt_affect_left :: Tetrahedron -> Bool
310 prop_y_rotation_doesnt_affect_left t =
311 expr1 == expr2
312 where
313 fv0 = Tetrahedron.fv t
314 fv1 = rotate cwy (Tetrahedron.fv t)
315 expr1 = left fv0
316 expr2 = left fv1
317
318 prop_y_rotation_doesnt_affect_right :: Tetrahedron -> Bool
319 prop_y_rotation_doesnt_affect_right t =
320 expr1 == expr2
321 where
322 fv0 = Tetrahedron.fv t
323 fv1 = rotate cwy (Tetrahedron.fv t)
324 expr1 = right fv0
325 expr2 = right fv1
326
327
328 prop_z_rotation_doesnt_affect_down :: Tetrahedron -> Bool
329 prop_z_rotation_doesnt_affect_down t =
330 expr1 == expr2
331 where
332 fv0 = Tetrahedron.fv t
333 fv1 = rotate cwz (Tetrahedron.fv t)
334 expr1 = down fv0
335 expr2 = down fv1
336
337
338 prop_z_rotation_doesnt_affect_top :: Tetrahedron -> Bool
339 prop_z_rotation_doesnt_affect_top t =
340 expr1 == expr2
341 where
342 fv0 = Tetrahedron.fv t
343 fv1 = rotate cwz (Tetrahedron.fv t)
344 expr1 = top fv0
345 expr2 = top fv1
346
347 prop_swapping_vertices_doesnt_affect_coefficients1 :: Tetrahedron -> Bool
348 prop_swapping_vertices_doesnt_affect_coefficients1 t =
349 c t 0 0 1 2 == c t' 0 0 1 2
350 where
351 t' = t { v0 = (v1 t), v1 = (v0 t) }
352
353 prop_swapping_vertices_doesnt_affect_coefficients2 :: Tetrahedron -> Bool
354 prop_swapping_vertices_doesnt_affect_coefficients2 t =
355 c t 0 1 1 1 == c t' 0 1 1 1
356 where
357 t' = t { v2 = (v3 t), v3 = (v2 t) }
358
359 prop_swapping_vertices_doesnt_affect_coefficients3 :: Tetrahedron -> Bool
360 prop_swapping_vertices_doesnt_affect_coefficients3 t =
361 c t 2 1 0 0 == c t' 2 1 0 0
362 where
363 t' = t { v2 = (v3 t), v3 = (v2 t) }
364
365 prop_swapping_vertices_doesnt_affect_coefficients4 :: Tetrahedron -> Bool
366 prop_swapping_vertices_doesnt_affect_coefficients4 t =
367 c t 2 0 0 1 == c t' 2 0 0 1
368 where
369 t' = t { v0 = (v3 t), v3 = (v0 t) }