File: NSLGeomTest.cpp

package info (click to toggle)
labplot 2.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,500 kB
  • sloc: cpp: 241,048; ansic: 6,324; python: 915; xml: 400; yacc: 237; sh: 221; awk: 35; makefile: 11
file content (273 lines) | stat: -rw-r--r-- 10,001 bytes parent folder | download | duplicates (2)
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
/*
	File                 : NSLGeomTest.cpp
	Project              : LabPlot
	Description          : NSL Tests for geometric functions
	--------------------------------------------------------------------
	SPDX-FileCopyrightText: 2019 Stefan Gerlach <stefan.gerlach@uni.kn>

	SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "NSLGeomTest.h"

extern "C" {
#include "backend/nsl/nsl_geom.h"
#include "backend/nsl/nsl_geom_linesim.h"
}

void NSLGeomTest::initTestCase() {
	const QString currentDir = QLatin1String(__FILE__);
	m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator();
}

// ##############################################################################
// #################  line sim test
// ##############################################################################

void NSLGeomTest::testDist() {
	double dist = nsl_geom_point_point_dist(0, 0, 1, 1);
	QCOMPARE(dist, M_SQRT2);
	dist = nsl_geom_point_point_dist(1, 2, 2, 1);
	QCOMPARE(dist, M_SQRT2);
	dist = nsl_geom_point_point_dist(-1, -2, 2, 2);
	QCOMPARE(dist, 5.);

	dist = nsl_geom_point_line_dist(0, 0, 1, 0, .5, 1);
	QCOMPARE(dist, 1.);
	dist = nsl_geom_point_line_dist(0, 0, 1, 0, 0, 1);
	QCOMPARE(dist, 1.);
	dist = nsl_geom_point_line_dist(0, 0, 1, 0, 1, 1);
	QCOMPARE(dist, 1.);
	dist = nsl_geom_point_line_dist(0, 0, 1, 1, 0, 1);
	QCOMPARE(dist, M_SQRT1_2);
	dist = nsl_geom_point_line_dist(0, 0, 1, 1, 1, 0);
	QCOMPARE(dist, M_SQRT1_2);

	// TODO: nsl_geom_point_line_dist_y
	// TODO: nsl_geom_three_point_area

	dist = nsl_geom_point_point_dist3(0, 0, 0, 1, 1, 1);
	QCOMPARE(dist, M_SQRT3);
	dist = nsl_geom_point_point_dist3(-1, -1, 1, 1, 1, 1);
	QCOMPARE(dist, 2. * M_SQRT2);
}

void NSLGeomTest::testLineSim() {
	const double xdata[] = {1, 2, 2.5, 3, 4, 7, 9, 11, 13, 14};
	const double ydata[] = {1, 1, 1, 3, 4, 7, 8, 12, 13, 13};
	const size_t n = 10;

	double atol = nsl_geom_linesim_clip_diag_perpoint(xdata, ydata, n);
	printf("automatic tol clip_diag_perpoint = %.15g\n", atol);
	QCOMPARE(atol, 1.76918060129541);
	atol = nsl_geom_linesim_clip_area_perpoint(xdata, ydata, n);
	printf("automatic tol clip_area_perpoint = %.15g\n", atol);
	QCOMPARE(atol, 15.6);
	atol = nsl_geom_linesim_avg_dist_perpoint(xdata, ydata, n);
	printf("automatic tol avg_dist = %.15g\n", atol);
	QCOMPARE(atol, 1.91626789723004);

	size_t index[n], i;
	const double tol = 0.6;
	const size_t result[] = {0, 2, 3, 6, 7, 9};
	printf("* Simplification (Douglas Peucker)\n");
	size_t nout = nsl_geom_linesim_douglas_peucker(xdata, ydata, n, tol, index);
	double perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	double aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("pos. error = %.15g, area error = %.15g\n", perr, aerr);
	QCOMPARE(nout, 6uL);
	QCOMPARE(perr, 0.0378688524590164);
	QCOMPARE(aerr, 0.25);

	for (i = 0; i < nout; ++i)
		QCOMPARE(index[i], result[i]);

	const size_t no = 6;
	printf("* Simplification (Douglas Peucker variant) nout = %zu\n", no);
	double tolout = nsl_geom_linesim_douglas_peucker_variant(xdata, ydata, n, no, index);
	perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("tolout = %.15g, pos. error = %.15g, area error = %.15g)\n", tolout, perr, aerr);
	QCOMPARE(tolout, 0.994505452921406);
	QCOMPARE(perr, 0.0378688524590164);
	QCOMPARE(aerr, 0.25);

	for (i = 0; i < no; ++i)
		QCOMPARE(index[i], result[i]);

	const size_t np = 2;
	const size_t result2[] = {0, 2, 4, 6, 8, 9};
	printf("* N-th point\n");
	nout = nsl_geom_linesim_nthpoint(n, np, index);
	perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("pos. error = %.15g, area error = %.15g\n", perr, aerr);
	QCOMPARE(nout, 6uL);
	QCOMPARE(perr, 0.129756097560976);
	QCOMPARE(aerr, 0.525);

	for (i = 0; i < nout; ++i)
		QCOMPARE(index[i], result2[i]);

	const double tol2 = 1.5;
	const size_t result3[] = {0, 3, 5, 6, 7, 9};
	printf("* Radial distance (tol = %g)\n", tol2);
	nout = nsl_geom_linesim_raddist(xdata, ydata, n, tol2, index);
	perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("pos. error = %.15g, area error = %.15g\n", perr, aerr);
	QCOMPARE(nout, 6uL);
	QCOMPARE(perr, 0.1725);
	QCOMPARE(aerr, 0.2);

	for (i = 0; i < nout; ++i)
		QCOMPARE(index[i], result3[i]);

	const double tol3 = 0.5;
	const size_t repeat = 3;
	const size_t result4[] = {0, 2, 4, 6, 7, 9};
	printf("* Perpendicular distance (repeat = %zu)\n", repeat);
	nout = nsl_geom_linesim_perpdist_repeat(xdata, ydata, n, tol3, repeat, index);
	perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("pos. error = %.15g, area error = %.15g\n", perr, aerr);
	QCOMPARE(nout, 6uL);
	QCOMPARE(perr, 0.0519512195121951);
	QCOMPARE(aerr, 0.275);

	for (i = 0; i < nout; ++i)
		QCOMPARE(index[i], result4[i]);

	const double tol4 = 0.7;
	printf("* Y distance (interpolation)\n");
	nout = nsl_geom_linesim_interp(xdata, ydata, n, tol4, index);
	perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("pos. error = %.15g, area error = %.15g\n", perr, aerr);
	QCOMPARE(nout, 6uL);
	QCOMPARE(perr, 0.0378688524590164);
	QCOMPARE(aerr, 0.25);

	for (i = 0; i < nout; ++i)
		QCOMPARE(index[i], result[i]);

	const double tol5 = 1.6;
	printf("* minimum area (Visvalingam-Whyatt)\n");
	nout = nsl_geom_linesim_visvalingam_whyatt(xdata, ydata, n, tol5, index);
	perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("pos. error = %.15g, area error = %.15g\n", perr, aerr);
	QCOMPARE(nout, 6uL);
	QCOMPARE(perr, 0.1725);
	QCOMPARE(aerr, 0.2);

	// nout can be up to 10 when third arg of nsl_geom_linesim_visvalingam_whyatt() is 10
	for (i = 0; i < std::min(nout, (size_t)6uL); ++i)
		QCOMPARE(index[i], result3[i]);

	const size_t result5[] = {0, 2, 3, 5, 6, 7, 9};
	printf("* Perp. distance (Reumann-Witkam)\n");
	nout = nsl_geom_linesim_reumann_witkam(xdata, ydata, n, tol3, index);
	perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("pos. error = %.15g, area error = %.15g\n", perr, aerr);
	QCOMPARE(nout, 7uL);
	QCOMPARE(perr, 0.01);
	QCOMPARE(aerr, 0.05);

	for (i = 0; i < nout; ++i)
		QCOMPARE(index[i], result5[i]);

	const double mintol = 2.0;
	const double maxtol = 7.0;
	printf("* Perp. distance (Opheim)\n");
	nout = nsl_geom_linesim_opheim(xdata, ydata, n, mintol, maxtol, index);
	perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("pos. error = %.15g, area error = %.15g\n", perr, aerr);
	QCOMPARE(nout, 6uL);
	QCOMPARE(perr, 0.129756097560976);
	QCOMPARE(aerr, 0.525);

	for (i = 0; i < nout; ++i)
		QCOMPARE(index[i], result2[i]);

	const size_t region = 5;
	printf("* Simplification (Lang)\n");
	nout = nsl_geom_linesim_lang(xdata, ydata, n, tol3, region, index);
	perr = nsl_geom_linesim_positional_squared_error(xdata, ydata, n, index);
	aerr = nsl_geom_linesim_area_error(xdata, ydata, n, index);
	printf("pos. error = %.15g, area error = %.15g\n", perr, aerr);
	QCOMPARE(nout, 6uL);
	QCOMPARE(perr, 0.0519512195121951);
	QCOMPARE(aerr, 0.275);

	for (i = 0; i < nout; ++i)
		QCOMPARE(index[i], result4[i]);
}

#ifdef _MSC_VER // crashes on Windows
void NSLGeomTest::testLineSimMorse() {
}
#else
void NSLGeomTest::testLineSimMorse() {
	printf("NSLGeomTest::testLineSimMorse()\n");

	const QString fileName = m_dataDir + QStringLiteral("morse_code.dat");
	FILE* file;
	if ((file = fopen(fileName.toLocal8Bit().constData(), "r")) == nullptr) {
		printf("ERROR reading %s. Giving up.\n", fileName.toLocal8Bit().constData());
		return;
	}

	const int N = 152000;
	const int NOUT = 15200;

	printf("NSLGeomTest::testLineSimMorse(): allocating space for reading data\n");
	QScopedArrayPointer<double> xdata(new double[N]);
	QScopedArrayPointer<double> ydata(new double[N]);

	printf("NSLGeomTest::testLineSimMorse(): reading data from file\n");
	size_t i;
	for (i = 0; i < N; i++) {
		int num = fscanf(file, "%lf %lf", &xdata[i], &ydata[i]);
		if (num != 2) { // failed to read two values
			printf("ERROR reading data\n");
			fclose(file);
			return;
		}
	}
	fclose(file);

	double atol = nsl_geom_linesim_clip_diag_perpoint(xdata.data(), ydata.data(), N);
	printf("automatic tol clip_diag_perpoint = %.15g\n", atol);
	QCOMPARE(atol, 0.999993446759985);
	atol = nsl_geom_linesim_clip_area_perpoint(xdata.data(), ydata.data(), N);
	printf("automatic tol clip_area_perpoint = %.15g\n", atol);
	QCOMPARE(atol, 34.4653732526316);
	atol = nsl_geom_linesim_avg_dist_perpoint(xdata.data(), ydata.data(), N);
	printf("automatic tol avg_dist = %.15g\n", atol);
	QCOMPARE(atol, 4.72091524721907);

	printf("* Simplification (Douglas Peucker variant) nout = %d\n", NOUT);

	double tolout;
	size_t index[N];
	QBENCHMARK {
		tolout = nsl_geom_linesim_douglas_peucker_variant(xdata.data(), ydata.data(), N, NOUT, index);
		QCOMPARE(tolout, 11.5280857733246);
	}

	double perr = nsl_geom_linesim_positional_squared_error(xdata.data(), ydata.data(), N, index);
	double aerr = nsl_geom_linesim_area_error(xdata.data(), ydata.data(), N, index);
	printf("maxtol = %.15g (pos. error = %.15g, area error = %.15g)\n", tolout, perr, aerr);
	QCOMPARE(perr, 11.9586266895937);
	QCOMPARE(aerr, 17.558046450762);
}
#endif

// ##############################################################################
// #################  performance
// ##############################################################################

QTEST_MAIN(NSLGeomTest)