]> gitweb.michael.orlitzky.com - spline3.git/blob - Grid.hs
a8bc68543a03b4423884b8abcf9deb95295a6f28
[spline3.git] / Grid.hs
1 module Tests.Grid
2 where
3
4 import Test.Framework (Test, testGroup)
5 import Test.Framework.Providers.HUnit (testCase)
6 import Test.HUnit
7 import Test.QuickCheck
8
9
10 import Assertions
11 import Comparisons
12 import Cube hiding (i, j, k)
13 import Examples
14 import FunctionValues (value_at)
15 import Grid
16 import Point (Point)
17 import Tetrahedron
18 import ThreeDimensional
19 import Values (dims)
20
21
22 -- | Check all coefficients of tetrahedron0 belonging to the cube
23 -- centered on (1,1,1) with a grid constructed from the trilinear
24 -- values. See example one in the paper.
25 --
26 -- We also verify that the four vertices on face0 of the cube are
27 -- in the correct location.
28 --
29 trilinear_c0_t0_tests :: Test.Framework.Test
30 trilinear_c0_t0_tests =
31 testGroup "trilinear c0 t0"
32 [testGroup "coefficients"
33 [testCase "c0030 is correct" test_trilinear_c0030,
34 testCase "c0003 is correct" test_trilinear_c0003,
35 testCase "c0021 is correct" test_trilinear_c0021,
36 testCase "c0012 is correct" test_trilinear_c0012,
37 testCase "c0120 is correct" test_trilinear_c0120,
38 testCase "c0102 is correct" test_trilinear_c0102,
39 testCase "c0111 is correct" test_trilinear_c0111,
40 testCase "c0210 is correct" test_trilinear_c0210,
41 testCase "c0201 is correct" test_trilinear_c0201,
42 testCase "c0300 is correct" test_trilinear_c0300,
43 testCase "c1020 is correct" test_trilinear_c1020,
44 testCase "c1002 is correct" test_trilinear_c1002,
45 testCase "c1011 is correct" test_trilinear_c1011,
46 testCase "c1110 is correct" test_trilinear_c1110,
47 testCase "c1101 is correct" test_trilinear_c1101,
48 testCase "c1200 is correct" test_trilinear_c1200,
49 testCase "c2010 is correct" test_trilinear_c2010,
50 testCase "c2001 is correct" test_trilinear_c2001,
51 testCase "c2100 is correct" test_trilinear_c2100,
52 testCase "c3000 is correct" test_trilinear_c3000],
53
54 testGroup "face0 vertices"
55 [testCase "v0 is correct" test_trilinear_f0_t0_v0,
56 testCase "v1 is correct" test_trilinear_f0_t0_v1,
57 testCase "v2 is correct" test_trilinear_f0_t0_v2,
58 testCase "v3 is correct" test_trilinear_f0_t0_v3]
59 ]
60 where
61 g = make_grid 1 trilinear
62 cube = cube_at g 1 1 1
63 t = tetrahedron0 cube
64
65 test_trilinear_c0030 :: Assertion
66 test_trilinear_c0030 =
67 assertAlmostEqual "c0030 is correct" (c t 0 0 3 0) (17/8)
68
69 test_trilinear_c0003 :: Assertion
70 test_trilinear_c0003 =
71 assertAlmostEqual "c0003 is correct" (c t 0 0 0 3) (27/8)
72
73 test_trilinear_c0021 :: Assertion
74 test_trilinear_c0021 =
75 assertAlmostEqual "c0021 is correct" (c t 0 0 2 1) (61/24)
76
77 test_trilinear_c0012 :: Assertion
78 test_trilinear_c0012 =
79 assertAlmostEqual "c0012 is correct" (c t 0 0 1 2) (71/24)
80
81 test_trilinear_c0120 :: Assertion
82 test_trilinear_c0120 =
83 assertAlmostEqual "c0120 is correct" (c t 0 1 2 0) (55/24)
84
85 test_trilinear_c0102 :: Assertion
86 test_trilinear_c0102 =
87 assertAlmostEqual "c0102 is correct" (c t 0 1 0 2) (73/24)
88
89 test_trilinear_c0111 :: Assertion
90 test_trilinear_c0111 =
91 assertAlmostEqual "c0111 is correct" (c t 0 1 1 1) (8/3)
92
93 test_trilinear_c0210 :: Assertion
94 test_trilinear_c0210 =
95 assertAlmostEqual "c0210 is correct" (c t 0 2 1 0) (29/12)
96
97 test_trilinear_c0201 :: Assertion
98 test_trilinear_c0201 =
99 assertAlmostEqual "c0201 is correct" (c t 0 2 0 1) (11/4)
100
101 test_trilinear_c0300 :: Assertion
102 test_trilinear_c0300 =
103 assertAlmostEqual "c0300 is correct" (c t 0 3 0 0) (5/2)
104
105 test_trilinear_c1020 :: Assertion
106 test_trilinear_c1020 =
107 assertAlmostEqual "c1020 is correct" (c t 1 0 2 0) (8/3)
108
109 test_trilinear_c1002 :: Assertion
110 test_trilinear_c1002 =
111 assertAlmostEqual "c1002 is correct" (c t 1 0 0 2) (23/6)
112
113 test_trilinear_c1011 :: Assertion
114 test_trilinear_c1011 =
115 assertAlmostEqual "c1011 is correct" (c t 1 0 1 1) (13/4)
116
117 test_trilinear_c1110 :: Assertion
118 test_trilinear_c1110 =
119 assertAlmostEqual "c1110 is correct" (c t 1 1 1 0) (23/8)
120
121 test_trilinear_c1101 :: Assertion
122 test_trilinear_c1101 =
123 assertAlmostEqual "c1101 is correct" (c t 1 1 0 1) (27/8)
124
125 test_trilinear_c1200 :: Assertion
126 test_trilinear_c1200 =
127 assertAlmostEqual "c1200 is correct" (c t 1 2 0 0) 3
128
129 test_trilinear_c2010 :: Assertion
130 test_trilinear_c2010 =
131 assertAlmostEqual "c2010 is correct" (c t 2 0 1 0) (10/3)
132
133 test_trilinear_c2001 :: Assertion
134 test_trilinear_c2001 =
135 assertAlmostEqual "c2001 is correct" (c t 2 0 0 1) 4
136
137 test_trilinear_c2100 :: Assertion
138 test_trilinear_c2100 =
139 assertAlmostEqual "c2100 is correct" (c t 2 1 0 0) (7/2)
140
141 test_trilinear_c3000 :: Assertion
142 test_trilinear_c3000 =
143 assertAlmostEqual "c3000 is correct" (c t 3 0 0 0) 4
144
145 test_trilinear_f0_t0_v0 :: Assertion
146 test_trilinear_f0_t0_v0 =
147 assertEqual "v0 is correct" (v0 t) (1, 1, 1)
148
149 test_trilinear_f0_t0_v1 :: Assertion
150 test_trilinear_f0_t0_v1 =
151 assertEqual "v1 is correct" (v1 t) (0.5, 1, 1)
152
153 test_trilinear_f0_t0_v2 :: Assertion
154 test_trilinear_f0_t0_v2 =
155 assertEqual "v2 is correct" (v2 t) (0.5, 0.5, 1.5)
156
157 test_trilinear_f0_t0_v3 :: Assertion
158 test_trilinear_f0_t0_v3 =
159 assertClose "v3 is correct" (v3 t) (0.5, 1.5, 1.5)
160
161
162 test_trilinear_reproduced :: Assertion
163 test_trilinear_reproduced =
164 assertTrue "trilinears are reproduced correctly" $
165 and [p (i', j', k') ~= value_at trilinear i j k
166 | i <- [0..2],
167 j <- [0..2],
168 k <- [0..2],
169 t <- tetrahedra c0,
170 let p = polynomial t,
171 let i' = fromIntegral i,
172 let j' = fromIntegral j,
173 let k' = fromIntegral k]
174 where
175 g = make_grid 1 trilinear
176 c0 = cube_at g 1 1 1
177
178
179 test_zeros_reproduced :: Assertion
180 test_zeros_reproduced =
181 assertTrue "the zero function is reproduced correctly" $
182 and [p (i', j', k') ~= value_at zeros i j k
183 | i <- [0..2],
184 j <- [0..2],
185 k <- [0..2],
186 let i' = fromIntegral i,
187 let j' = fromIntegral j,
188 let k' = fromIntegral k]
189 where
190 g = make_grid 1 zeros
191 c0 = cube_at g 1 1 1
192 t0 = tetrahedron0 c0
193 p = polynomial t0
194
195
196 -- | Make sure we can reproduce a 9x9x9 trilinear from the 3x3x3 one.
197 test_trilinear9x9x9_reproduced :: Assertion
198 test_trilinear9x9x9_reproduced =
199 assertTrue "trilinear 9x9x9 is reproduced correctly" $
200 and [p (i', j', k') ~= value_at trilinear9x9x9 i j k
201 | i <- [0..8],
202 j <- [0..8],
203 k <- [0..8],
204 t <- tetrahedra c0,
205 let p = polynomial t,
206 let i' = (fromIntegral i) * 0.5,
207 let j' = (fromIntegral j) * 0.5,
208 let k' = (fromIntegral k) * 0.5]
209 where
210 g = make_grid 1 trilinear
211 c0 = cube_at g 1 1 1
212
213
214 -- | The point 'p' in this test lies on the boundary of tetrahedra 12 and 15.
215 -- However, the 'contains_point' test fails due to some numerical innacuracy.
216 -- This bug should have been fixed by setting a positive tolerance level.
217 --
218 -- Example from before the fix:
219 --
220 -- > b0 (tetrahedron15 c) p
221 -- -3.4694469519536365e-18
222 --
223 test_tetrahedra_collision_sensitivity :: Assertion
224 test_tetrahedra_collision_sensitivity =
225 assertTrue "tetrahedron collision tests isn't too sensitive" $
226 contains_point t15 p
227 where
228 g = make_grid 1 naturals_1d
229 c = cube_at g 0 17 1
230 p = (0, 16.75, 0.5) :: Point
231 t15 = tetrahedron15 c
232
233
234 prop_cube_indices_never_go_out_of_bounds :: Grid -> Gen Bool
235 prop_cube_indices_never_go_out_of_bounds g =
236 do
237 let delta = Grid.h g
238 let coordmin = negate (delta/2)
239
240 let (xsize, ysize, zsize) = dims $ function_values g
241 let xmax = delta*(fromIntegral xsize) - (delta/2)
242 let ymax = delta*(fromIntegral ysize) - (delta/2)
243 let zmax = delta*(fromIntegral zsize) - (delta/2)
244
245 x <- choose (coordmin, xmax)
246 y <- choose (coordmin, ymax)
247 z <- choose (coordmin, zmax)
248
249 let p = (x,y,z) :: Point
250 let idx_x = calculate_containing_cube_coordinate g x
251 let idx_y = calculate_containing_cube_coordinate g y
252 let idx_z = calculate_containing_cube_coordinate g z
253
254 return $
255 idx_x >= 0 &&
256 idx_x <= xsize - 1 &&
257 idx_y >= 0 &&
258 idx_y <= ysize - 1 &&
259 idx_z >= 0 &&
260 idx_z <= zsize - 1