]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Tests/Tetrahedron.hs
bafb7fe2e5a9afdb793df41930d84cd4cd21f566
[spline3.git] / src / Tests / Tetrahedron.hs
1 module Tests.Tetrahedron
2 where
3
4 import Test.HUnit
5 import Test.QuickCheck
6
7 import Comparisons
8 import Point
9 import FunctionValues
10 import Tests.FunctionValues()
11 import Tetrahedron
12 import ThreeDimensional
13
14 instance Arbitrary Tetrahedron where
15 arbitrary = do
16 rnd_v0 <- arbitrary :: Gen Point
17 rnd_v1 <- arbitrary :: Gen Point
18 rnd_v2 <- arbitrary :: Gen Point
19 rnd_v3 <- arbitrary :: Gen Point
20 rnd_fv <- arbitrary :: Gen FunctionValues
21 return (Tetrahedron rnd_fv rnd_v0 rnd_v1 rnd_v2 rnd_v3)
22
23 -- HUnit Tests
24
25
26 -- | Check the volume of a particular tetrahedron against the value
27 -- computed by hand. Its vertices are in clockwise order, so the
28 -- volume should be negative.
29 test_volume1 :: Test
30 test_volume1 =
31 TestCase $ assertEqual "volume is correct" True (vol ~= (-1/3))
32 where
33 p0 = (0, -0.5, 0)
34 p1 = (0, 0.5, 0)
35 p2 = (2, 0, 0)
36 p3 = (1, 0, 1)
37 t = Tetrahedron { v0 = p0,
38 v1 = p1,
39 v2 = p2,
40 v3 = p3,
41 fv = empty_values }
42 vol = volume t
43
44
45 -- | Check the volume of a particular tetrahedron against the value
46 -- computed by hand. Its vertices are in counter-clockwise order, so
47 -- the volume should be positive.
48 test_volume2 :: Test
49 test_volume2 =
50 TestCase $ assertEqual "volume is correct" True (vol ~= (1/3))
51 where
52 p0 = (0, -0.5, 0)
53 p1 = (2, 0, 0)
54 p2 = (0, 0.5, 0)
55 p3 = (1, 0, 1)
56 t = Tetrahedron { v0 = p0,
57 v1 = p1,
58 v2 = p2,
59 v3 = p3,
60 fv = empty_values }
61 vol = volume t
62
63
64 -- | Ensure that a tetrahedron contains a particular point chosen to
65 -- be inside of it.
66 test_contains_point1 :: Test
67 test_contains_point1 =
68 TestCase $ assertEqual "contains an inner point" True (contains_point t inner_point)
69 where
70 p0 = (0, -0.5, 0)
71 p1 = (0, 0.5, 0)
72 p2 = (2, 0, 0)
73 p3 = (1, 0, 1)
74 inner_point = (1, 0, 0.5)
75 t = Tetrahedron { v0 = p0,
76 v1 = p1,
77 v2 = p2,
78 v3 = p3,
79 fv = empty_values }
80
81
82 -- | Ensure that a tetrahedron does not contain a particular point chosen to
83 -- be outside of it (first test).
84 test_doesnt_contain_point1 :: Test
85 test_doesnt_contain_point1 =
86 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
87 where
88 p0 = (0, -0.5, 0)
89 p1 = (0, 0.5, 0)
90 p2 = (2, 0, 0)
91 p3 = (1, 0, 1)
92 exterior_point = (5, 2, -9.0212)
93 t = Tetrahedron { v0 = p0,
94 v1 = p1,
95 v2 = p2,
96 v3 = p3,
97 fv = empty_values }
98
99
100 -- | Ensure that a tetrahedron does not contain a particular point chosen to
101 -- be outside of it (second test).
102 test_doesnt_contain_point2 :: Test
103 test_doesnt_contain_point2 =
104 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
105 where
106 p0 = (0, 1, 1)
107 p1 = (1, 1, 1)
108 p2 = (0.5, 0.5, 1)
109 p3 = (0.5, 0.5, 0.5)
110 exterior_point = (0, 0, 0)
111 t = Tetrahedron { v0 = p0,
112 v1 = p1,
113 v2 = p2,
114 v3 = p3,
115 fv = empty_values }
116
117
118 -- | Ensure that a tetrahedron does not contain a particular point chosen to
119 -- be outside of it (third test).
120 test_doesnt_contain_point3 :: Test
121 test_doesnt_contain_point3 =
122 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
123 where
124 p0 = (1, 1, 1)
125 p1 = (1, 0, 1)
126 p2 = (0.5, 0.5, 1)
127 p3 = (0.5, 0.5, 0.5)
128 exterior_point = (0, 0, 0)
129 t = Tetrahedron { v0 = p0,
130 v1 = p1,
131 v2 = p2,
132 v3 = p3,
133 fv = empty_values }
134
135
136 -- | Ensure that a tetrahedron does not contain a particular point chosen to
137 -- be outside of it (fourth test).
138 test_doesnt_contain_point4 :: Test
139 test_doesnt_contain_point4 =
140 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
141 where
142 p0 = (1, 0, 1)
143 p1 = (0, 0, 1)
144 p2 = (0.5, 0.5, 1)
145 p3 = (0.5, 0.5, 0.5)
146 exterior_point = (0, 0, 0)
147 t = Tetrahedron { v0 = p0,
148 v1 = p1,
149 v2 = p2,
150 v3 = p3,
151 fv = empty_values }
152
153
154 -- | Ensure that a tetrahedron does not contain a particular point chosen to
155 -- be outside of it (fifth test).
156 test_doesnt_contain_point5 :: Test
157 test_doesnt_contain_point5 =
158 TestCase $ assertEqual "doesn't contain an exterior point" False (contains_point t exterior_point)
159 where
160 p0 = (0, 0, 1)
161 p1 = (0, 1, 1)
162 p2 = (0.5, 0.5, 1)
163 p3 = (0.5, 0.5, 0.5)
164 exterior_point = (0, 0, 0)
165 t = Tetrahedron { v0 = p0,
166 v1 = p1,
167 v2 = p2,
168 v3 = p3,
169 fv = empty_values }
170
171 -- | A list of all HUnit tests defined in this module.
172 tetrahedron_tests :: [Test]
173 tetrahedron_tests = [test_volume1,
174 test_volume2,
175 test_contains_point1,
176 test_doesnt_contain_point1,
177 test_doesnt_contain_point2,
178 test_doesnt_contain_point3,
179 test_doesnt_contain_point4,
180 test_doesnt_contain_point5 ]
181
182
183 -- | The barycentric coordinate of v0 with respect to itself should
184 -- be one.
185 prop_b0_v0_always_unity :: Tetrahedron -> Property
186 prop_b0_v0_always_unity t =
187 (volume t) > 0 ==> (b0 t) (v0 t) ~= 1.0
188
189 -- | The barycentric coordinate of v1 with respect to v0 should
190 -- be zero.
191 prop_b0_v1_always_zero :: Tetrahedron -> Property
192 prop_b0_v1_always_zero t =
193 (volume t) > 0 ==> (b0 t) (v1 t) ~= 0
194
195 -- | The barycentric coordinate of v2 with respect to v0 should
196 -- be zero.
197 prop_b0_v2_always_zero :: Tetrahedron -> Property
198 prop_b0_v2_always_zero t =
199 (volume t) > 0 ==> (b0 t) (v2 t) ~= 0
200
201 -- | The barycentric coordinate of v3 with respect to v0 should
202 -- be zero.
203 prop_b0_v3_always_zero :: Tetrahedron -> Property
204 prop_b0_v3_always_zero t =
205 (volume t) > 0 ==> (b0 t) (v3 t) ~= 0
206
207 -- | The barycentric coordinate of v1 with respect to itself should
208 -- be one.
209 prop_b1_v1_always_unity :: Tetrahedron -> Property
210 prop_b1_v1_always_unity t =
211 (volume t) > 0 ==> (b1 t) (v1 t) ~= 1.0
212
213 -- | The barycentric coordinate of v0 with respect to v1 should
214 -- be zero.
215 prop_b1_v0_always_zero :: Tetrahedron -> Property
216 prop_b1_v0_always_zero t =
217 (volume t) > 0 ==> (b1 t) (v0 t) ~= 0
218
219 -- | The barycentric coordinate of v2 with respect to v1 should
220 -- be zero.
221 prop_b1_v2_always_zero :: Tetrahedron -> Property
222 prop_b1_v2_always_zero t =
223 (volume t) > 0 ==> (b1 t) (v2 t) ~= 0
224
225 -- | The barycentric coordinate of v3 with respect to v1 should
226 -- be zero.
227 prop_b1_v3_always_zero :: Tetrahedron -> Property
228 prop_b1_v3_always_zero t =
229 (volume t) > 0 ==> (b1 t) (v3 t) ~= 0
230
231 -- | The barycentric coordinate of v2 with respect to itself should
232 -- be one.
233 prop_b2_v2_always_unity :: Tetrahedron -> Property
234 prop_b2_v2_always_unity t =
235 (volume t) > 0 ==> (b2 t) (v2 t) ~= 1.0
236
237 -- | The barycentric coordinate of v0 with respect to v2 should
238 -- be zero.
239 prop_b2_v0_always_zero :: Tetrahedron -> Property
240 prop_b2_v0_always_zero t =
241 (volume t) > 0 ==> (b2 t) (v0 t) ~= 0
242
243 -- | The barycentric coordinate of v1 with respect to v2 should
244 -- be zero.
245 prop_b2_v1_always_zero :: Tetrahedron -> Property
246 prop_b2_v1_always_zero t =
247 (volume t) > 0 ==> (b2 t) (v1 t) ~= 0
248
249 -- | The barycentric coordinate of v3 with respect to v2 should
250 -- be zero.
251 prop_b2_v3_always_zero :: Tetrahedron -> Property
252 prop_b2_v3_always_zero t =
253 (volume t) > 0 ==> (b2 t) (v3 t) ~= 0
254
255 -- | The barycentric coordinate of v3 with respect to itself should
256 -- be one.
257 prop_b3_v3_always_unity :: Tetrahedron -> Property
258 prop_b3_v3_always_unity t =
259 (volume t) > 0 ==> (b3 t) (v3 t) ~= 1.0
260
261 -- | The barycentric coordinate of v0 with respect to v3 should
262 -- be zero.
263 prop_b3_v0_always_zero :: Tetrahedron -> Property
264 prop_b3_v0_always_zero t =
265 (volume t) > 0 ==> (b3 t) (v0 t) ~= 0
266
267 -- | The barycentric coordinate of v1 with respect to v3 should
268 -- be zero.
269 prop_b3_v1_always_zero :: Tetrahedron -> Property
270 prop_b3_v1_always_zero t =
271 (volume t) > 0 ==> (b3 t) (v1 t) ~= 0
272
273 -- | The barycentric coordinate of v2 with respect to v3 should
274 -- be zero.
275 prop_b3_v2_always_zero :: Tetrahedron -> Property
276 prop_b3_v2_always_zero t =
277 (volume t) > 0 ==> (b3 t) (v2 t) ~= 0
278
279
280 -- | Used for convenience in the next few tests; not a test itself.
281 p :: Tetrahedron -> Int -> Int -> Int -> Int -> Double
282 p t i j k l = (polynomial t) (xi t i j k l)
283
284 -- | Given in Sorokina and Zeilfelder, p. 78.
285 prop_c3000_identity :: Tetrahedron -> Property
286 prop_c3000_identity t =
287 (volume t) > 0 ==>
288 c t 3 0 0 0 ~= p t 3 0 0 0
289
290 -- | Given in Sorokina and Zeilfelder, p. 78.
291 prop_c2100_identity :: Tetrahedron -> Property
292 prop_c2100_identity t =
293 (volume t) > 0 ==>
294 c t 2 1 0 0 ~= (term1 - term2 + term3 - term4)
295 where
296 term1 = (1/3)*(p t 0 3 0 0)
297 term2 = (5/6)*(p t 3 0 0 0)
298 term3 = 3*(p t 2 1 0 0)
299 term4 = (3/2)*(p t 1 2 0 0)
300
301 -- | Given in Sorokina and Zeilfelder, p. 78.
302 prop_c1110_identity :: Tetrahedron -> Property
303 prop_c1110_identity t =
304 (volume t) > 0 ==>
305 c t 1 1 1 0 ~= (term1 + term2 - term3 - term4)
306 where
307 term1 = (1/3)*((p t 3 0 0 0) + (p t 0 3 0 0) + (p t 0 0 3 0))
308 term2 = (9/2)*(p t 1 1 1 0)
309 term3 = (3/4)*((p t 2 1 0 0) + (p t 1 2 0 0) + (p t 2 0 1 0))
310 term4 = (3/4)*((p t 1 0 2 0) + (p t 0 2 1 0) + (p t 0 1 2 0))