File: test_splines.c

package info (click to toggle)
bart 0.9.00-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,040 kB
  • sloc: ansic: 116,116; python: 1,329; sh: 726; makefile: 639; javascript: 589; cpp: 106
file content (310 lines) | stat: -rw-r--r-- 6,304 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Copyright 2016. Martin Uecker.
 * All rights reserved. Use of this source code is governed by
 * a BSD-style license which can be found in the LICENSE file.
 *
 * Authors:
 * 2016 Martin Uecker <martin.uecker@med.uni-goettingen.de>
 */


#include <math.h>
#include <stdio.h>

#include "num/splines.h"

#include "utest.h"

static const double coords[5] = { 0., 1., 0.5, 1., 0.5 };

static bool test_bezier_curve(void)
{
	bool ret = true;

	ret = ret && (coords[0] == bezier_curve(0., 4, coords));
	ret = ret && (coords[4] == bezier_curve(1., 4, coords));

	return ret;
}

UT_REGISTER_TEST(test_bezier_curve);


static bool test_bezier_increase_degree(void)
{
	double coords2[6];
	bezier_increase_degree(4, coords2, coords);

	double err = 0.;

	for (double x = 0.; x < 1.; x += 0.01) {

		double a = bezier_curve(x, 4, coords);
		double b = bezier_curve(x, 5, coords2);

		err += pow(a - b, 2);
	}

	return (err < 1.E-28);
}

UT_REGISTER_TEST(test_bezier_increase_degree);


static bool test_bezier_split(void)
{
	double coords[5] = { 0., 1., 0.5, 1., 0.5 };

	double coordsA[5];
	double coordsB[5];
	bezier_split(0.5, 4, coordsA, coordsB, coords);

	double err = 0.;

	for (double x = 0.; x < 1.; x += 0.01) {

		double a = bezier_curve(x, 4, coords);
		double b = (x <= 0.5) 	? bezier_curve(2. * x, 4, coordsA)
					: bezier_curve(2. * (x - 0.5), 4, coordsB);

		err += pow(a - b, 2);
	}

	return (err < 1.E-28);
}

UT_REGISTER_TEST(test_bezier_split);



static bool test_cspline(void)
{
	const double coeff[4] = { 0., 1., 1., 1. };

	bool ok = true;

	for (double x = 0.; x < 1.; x += 0.1)
		ok &= (fabs(x - cspline(x, coeff)) < 1.E-15);

	return ok;
}


UT_REGISTER_TEST(test_cspline);


static bool test_bspline(void)
{
	const double knots[11] = { 0., 0.0, 0.0, 0., 0.25, 0.5, 0.75, 1., 1., 1., 1. };

	bool ok = true;

	for (int i = 0; i < 7; i++) {

		double coord[7] = { 0., 0., 0., 0., 0., 0., 0. };
		coord[i] = 1.;

		double err = 0.;

		for (double x = 0.; x <= 1.; x += 0.01) {

			double a = bspline(10, i, 3, knots, x);
			double b = bspline_curve(10, 3, knots, coord, x);

			err += pow(a - b, 2);
		}

		ok &= (err < 1.E-28);
	}

	return ok;
}


UT_REGISTER_TEST(test_bspline);


static bool test_bspline_knot_insert(void)
{
	const double knots[11] = { 0., 0.0, 0.0, 0., 0.25, 0.5, 0.75, 1., 1., 1., 1. };
	double coord[7] = { 0., 0., 0.75, 0.5, 0.25, 0., 0 };

	double knots2[12];
	double coord2[8];
	bspline_knot_insert(0.6, 10, 3, knots2, coord2, knots, coord);

	double err = 0.;

	for (double x = 0.; x < 1.; x += 0.01) {

		double a = bspline_curve(10, 3, knots, coord, x);
		double b = bspline_curve(11, 3, knots2, coord2, x);

		err += pow(a - b, 2);
	}

	return (err < 1.E-28);
}

UT_REGISTER_TEST(test_bspline_knot_insert);



static bool test_bspline_derivative(void)
{
	const double knots[11] = { 0., 0.0, 0.0, 0., 0.25, 0.5, 0.75, 1., 1., 1., 1. };

	bool ok = true;

	for (int i = 0; i < 7; i++) {

		double coord[7] = { 0., 0., 0., 0., 0., 0., 0. };
		coord[i] = 1.;

		double err = 0.;

		for (double x = 0.; x <= 1.; x += 0.01) {

			double a = bspline_derivative(10, i, 3, knots, x);
			double b = bspline_curve_derivative(1, 10, 3, knots, coord, x);

			err += pow(a - b, 2);
		}

		ok &= (err < 1.E-1);
	}

	return ok;
}

UT_REGISTER_TEST(test_bspline_derivative);


static bool test_bspline_zero(void)
{
	const double knots[11] = { 0., 0.0, 0.0, 0., 0.25, 0.5, 0.75, 1., 1., 1., 1. };
	const double z0[7] = { 0., 0., 0.75, 0.5, 0.25, 0., 0 };

	bool ok = true;

	for (int i = 2; i < 5; i++) { // FIXME

		double coord[7] = { 0., 0., 0., 0., 0., 0., 0. };
		coord[i] = 1.;

		double k2[10] = { 0., 0., 0., 0., 0., 0., 0., 0., 0., 0. }; // FIXME: this and next line's inits may hide bug (crash with clang)
		double c2[7] = { 0., 0., 0., 0., 0., 0., 0. };			// (Also length must be longer than in prototype)

		bspline_coeff_derivative_n(1, 10, 3, k2, c2, knots, coord);
		double z = bspline_curve_zero(8, 2, k2, c2);

		ok &= (fabs(z - z0[i]) < 1.E-5);

	}

	return ok;
}

UT_REGISTER_TEST(test_bspline_zero);




static bool test_nurbs(void)
{
	const double knots[11] = { 0., 0.0, 0.0, 0., 0.25, 0.5, 0.75, 1., 1., 1., 1. };

	bool ok = true;

	for (int i = 0; i < 7; i++) {

		double coord[7] = { 0., 0., 0., 0., 0., 0., 0. };
		double weights[7] = { 1., 1., 1., 1., 1., 1., 1. };
		coord[i] = 1.;

		double err = 0.;

		for (double x = 0.; x <= 1.; x += 0.01) {

			double a = nurbs(10, 3, knots, coord, weights, x);
			double b = bspline_curve(10, 3, knots, coord, x);
			double c = bspline_curve(10, 3, knots, weights, x);

			err += pow(a - b / c, 2);
		}

		ok &= (err < 1.E-28);
	}

	return ok;
}


UT_REGISTER_TEST(test_nurbs);


static bool test_nurbs_arc(void)
{
	const double knots[6] = { 0., 0., 0., 1., 1., 1. };

	double coordx[3] = { 0., 1., 1. };
	double coordy[3] = { 1., 1., 0. };
	double weights[3] = { sqrt(2.), 1., sqrt(2.) };

	bool ok = true;

	for (double t = 0.; t <= 1.; t += 0.01) {

		double x = nurbs(5, 2, knots, coordx, weights, t);
		double y = nurbs(5, 2, knots, coordy, weights, t);

		ok &= fabs(pow(x, 2.) + pow(y, 2.) - 1.) < 1.E-15;
	}

	return ok;
}


UT_REGISTER_TEST(test_nurbs_arc);



static bool test_nurbs_circle(void)
{
	const double knots[10] = { 0., 0., 0., 1., 1., 2., 2., 3., 3., 3. };

	double coordx[7] = {
		cos(0. * 2. * M_PI / 3.) + cos(1. * 2. * M_PI / 3.),
		cos(1. * 2. * M_PI / 3.) * 2.,
		cos(1. * 2. * M_PI / 3.) + cos(2. * 2. * M_PI / 3.),
		cos(2. * 2. * M_PI / 3.) * 2.,
		cos(2. * 2. * M_PI / 3.) + cos(3. * 2. * M_PI / 3.),
		cos(3. * 2. * M_PI / 3.) * 2.,
		cos(3. * 2. * M_PI / 3.) + cos(1. * 2. * M_PI / 3.),
	};
	double coordy[7] = {
		sin(0. * 2. * M_PI / 3.) + sin(1. * 2. * M_PI / 3.),
		sin(1. * 2. * M_PI / 3.) * 2.,
		sin(1. * 2. * M_PI / 3.) + sin(2. * 2. * M_PI / 3.),
		sin(2. * 2. * M_PI / 3.) * 2.,
		sin(2. * 2. * M_PI / 3.) + sin(3. * 2. * M_PI / 3.),
		sin(3. * 2. * M_PI / 3.) * 2.,
		sin(3. * 2. * M_PI / 3.) + sin(1. * 2. * M_PI / 3.),
	};
	double weights[7] = { 1., 0.5, 1., 0.5, 1., 0.5, 1. };

	bool ok = true;

	for (double t = 0.; t <= 3.; t += 0.01) {

		double x = nurbs(9, 2, knots, coordx, weights, t);
		double y = nurbs(9, 2, knots, coordy, weights, t);

		ok &= fabs(pow(x, 2.) + pow(y, 2.) - 1.) < 1.E-15;
	}

	return ok;
}


UT_REGISTER_TEST(test_nurbs_circle);