Skip to content
Navigation Menu
{{ message }}
forked from SpaceGroupUCL/depthmapX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestpointmap.cpp
More file actions
549 lines (448 loc) · 21.6 KB
/
Copy pathtestpointmap.cpp
File metadata and controls
549 lines (448 loc) · 21.6 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// Copyright (C) 2017 Petros Koutsolampros
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "catch.hpp"
#include "salalib/mgraph.h"
TEST_CASE("Test MetaGraph construction", "")
{
const float EPSILON = 0.001;
double spacing = 0.5;
Point2f offset(0,0); // seems that this is always set to 0,0
// create a new MetaGraph
// The PointMap needs the m_region variable from this
// object as a definition of the area the grid needs to cover
std::unique_ptr<MetaGraph> metaGraph(new MetaGraph("Test MetaGraph"));
SECTION( "Construct a plain MetaGraph without underlying geometry" )
{
Point2f bottomLeft(0,0);
Point2f topRight(2,4);
// set m_region to the bounds
metaGraph->setRegion(bottomLeft, topRight);
// check if the bounds are set correctly
REQUIRE(metaGraph->getRegion().bottom_left.x == Approx(bottomLeft.x).epsilon(EPSILON));
REQUIRE(metaGraph->getRegion().bottom_left.y == Approx(bottomLeft.y).epsilon(EPSILON));
REQUIRE(metaGraph->getRegion().top_right.x == Approx(topRight.x).epsilon(EPSILON));
REQUIRE(metaGraph->getRegion().top_right.y == Approx(topRight.y).epsilon(EPSILON));
}
SECTION( "Construct a MetaGraph using underlying geometry" )
{
Point2f lineStart(0,0);
Point2f lineEnd(2,4);
Point2f bottomLeft(std::min(lineStart.x,lineEnd.x),std::min(lineStart.y,lineEnd.y));
Point2f topRight(std::max(lineStart.x,lineEnd.x),std::max(lineStart.y,lineEnd.y));
// push a SpacePixelFile in the MetaGraph
metaGraph->m_drawingFiles.emplace_back("Test MetaGraph");
// push a ShapeMap in the SpacePixelFile
metaGraph->m_drawingFiles.back().m_spacePixels.emplace_back("Test ShapeMap");
// add a line to the ShapeMap
metaGraph->m_drawingFiles.back().m_spacePixels.back().makeLineShape(Line(lineStart, lineEnd));
// check if the ShapeMap bounds are set correctly
REQUIRE(metaGraph->m_drawingFiles.back().m_spacePixels.back().getRegion().bottom_left.x == Approx(bottomLeft.x).epsilon(EPSILON));
REQUIRE(metaGraph->m_drawingFiles.back().m_spacePixels.back().getRegion().bottom_left.y == Approx(bottomLeft.y).epsilon(EPSILON));
REQUIRE(metaGraph->m_drawingFiles.back().m_spacePixels.back().getRegion().top_right.x == Approx(topRight.x).epsilon(EPSILON));
REQUIRE(metaGraph->m_drawingFiles.back().m_spacePixels.back().getRegion().top_right.y == Approx(topRight.y).epsilon(EPSILON));
// MetaGraph and SpacePixelFile do not automatically grow
// their region when new shapemaps/files are added to them
// therefore we have to do this externally
metaGraph->m_drawingFiles.back().m_region = metaGraph->m_drawingFiles.back().m_spacePixels.back().getRegion();
// check if the SpacePixelFile bounds are set correctly
REQUIRE(metaGraph->m_drawingFiles.back().m_region.bottom_left.x == Approx(bottomLeft.x).epsilon(EPSILON));
REQUIRE(metaGraph->m_drawingFiles.back().m_region.bottom_left.y == Approx(bottomLeft.y).epsilon(EPSILON));
REQUIRE(metaGraph->m_drawingFiles.back().m_region.top_right.x == Approx(topRight.x).epsilon(EPSILON));
REQUIRE(metaGraph->m_drawingFiles.back().m_region.top_right.y == Approx(topRight.y).epsilon(EPSILON));
metaGraph->setRegion(metaGraph->m_drawingFiles.back().m_region.bottom_left,
metaGraph->m_drawingFiles.back().m_region.top_right);
// check if the MetaGraph bounds are set correctly
REQUIRE(metaGraph->getRegion().bottom_left.x == Approx(bottomLeft.x).epsilon(EPSILON));
REQUIRE(metaGraph->getRegion().bottom_left.y == Approx(bottomLeft.y).epsilon(EPSILON));
REQUIRE(metaGraph->getRegion().top_right.x == Approx(topRight.x).epsilon(EPSILON));
REQUIRE(metaGraph->getRegion().top_right.y == Approx(topRight.y).epsilon(EPSILON));
}
// construct a sample pointMap
PointMap pointMap(metaGraph->getRegion(), metaGraph->m_drawingFiles, "Test PointMap");
}
TEST_CASE("Test grid filling", "")
{
const float EPSILON = 0.001;
double spacing = 0.5;
Point2f offset(0,0); // seems that this is always set to 0,0
// create a new MetaGraph
// The PointMap needs the m_region variable from this
// object as a definition of the area the grid needs to cover
std::unique_ptr<MetaGraph> metaGraph(new MetaGraph("Test MetaGraph"));
// Construct a plain MetaGraph without underlying geometry
{
Point2f bottomLeft(0,0);
Point2f topRight(2,4);
// set m_region to the bounds
metaGraph->setRegion(bottomLeft, topRight);
// check if the bounds are set correctly
REQUIRE(metaGraph->getRegion().bottom_left.x == Approx(bottomLeft.x).epsilon(EPSILON));
REQUIRE(metaGraph->getRegion().bottom_left.y == Approx(bottomLeft.y).epsilon(EPSILON));
REQUIRE(metaGraph->getRegion().top_right.x == Approx(topRight.x).epsilon(EPSILON));
REQUIRE(metaGraph->getRegion().top_right.y == Approx(topRight.y).epsilon(EPSILON));
}
// construct a sample pointMap
PointMap pointMap(metaGraph->getRegion(), metaGraph->m_drawingFiles, "Test PointMap");
// set the grid
// create the grid with bounds as set above
bool gridIsSet = pointMap.setGrid(spacing, offset);
// check if the grid was set
REQUIRE(gridIsSet);
// check if the spacing is correct
REQUIRE(spacing == pointMap.getSpacing());
// fill the grid
// seems like fill_type is actually connected to the
// QDepthmapView class which is a GUI class (depthmapview.h)
// TODO Disentangle GUI enum from pointMap.makePoints
int fill_type = 0; // = QDepthmapView::FULLFILL
Point2f gridBottomLeft = pointMap.getRegion().bottom_left;
SECTION( "Check if the points are made when fill selection in a cell" )
{
// Check if the points are made (grid filled) when
// the selected position is certainly in a cell
// This calculation should make the point directly
// at the centre of a central cell
Point2f midPoint(gridBottomLeft.x + spacing * (floor(pointMap.getCols() * 0.5) + 0.5),
gridBottomLeft.y + spacing * (floor(pointMap.getRows() * 0.5) + 0.5));
bool pointsMade = pointMap.makePoints(midPoint, fill_type);
REQUIRE(pointsMade);
}
SECTION("Check if the points are made when fill selection between cells")
{
// Check if the points are made (grid filled) when
// the selected position is certainly between cells
// This calculation should make the point directly
// at the edge of a central cell
Point2f midPoint(gridBottomLeft.x + spacing * (floor(pointMap.getCols() * 0.5)),
gridBottomLeft.y + spacing * (floor(pointMap.getRows() * 0.5)));
bool pointsMade = pointMap.makePoints(midPoint, fill_type);
REQUIRE(pointsMade);
}
}
// PointMap::setGrid is quite convoluted with various parameters
// affecting the result, such as the limits of the region to be
// covered (bottomLeft, topRight), the spacing and the location
// of the plan in space. For example every grid created will be
// in relation to the origin (0,0), no matter where the region
// is and the current pixel can always be calculated as if the
// origin always falls in the centre of a cell.
TEST_CASE("Quirks in grid creation - Origin always at 0", "")
{
double spacing = 0.5;
const float EPSILON = 0.001;
Point2f offset(0,0); // seems that this is always set to 0,0
Point2f bottomLeft(0,0);
Point2f topRight(0,0);
SECTION ("Region from origin to positive x, positive y quadrant")
{
spacing = 0.5;
bottomLeft.x = 0;
bottomLeft.y = 0;
topRight.x = 1;
topRight.y = 1;
}
SECTION ("Region away from origin to positive x, positive y quadrant")
{
spacing = 0.5;
bottomLeft.x = 1;
bottomLeft.y = 1;
topRight.x = 2;
topRight.y = 2;
}
SECTION ("Region from origin to negative x, negative y quadrant")
{
spacing = 0.5;
bottomLeft.x = -1;
bottomLeft.y = -1;
topRight.x = 0;
topRight.y = 0;
}
SECTION ("Region in all quadrants")
{
spacing = 0.5;
bottomLeft.x = -1;
bottomLeft.y = -1;
topRight.x = 1;
topRight.y = 1;
}
SECTION ("Region in positive x, positive y quadrant, non-rectangular")
{
spacing = 0.5;
bottomLeft.x = 1;
bottomLeft.y = 2;
topRight.x = 3;
topRight.y = 4;
}
SECTION ("Region in positive x, positive y quadrant, floating-point limits")
{
spacing = 0.5;
bottomLeft.x = 1.1;
bottomLeft.y = 2.2;
topRight.x = 3.3;
topRight.y = 4.4;
}
SECTION ("Region in positive x, positive y quadrant, floating-point limits")
{
spacing = 0.5;
bottomLeft.x = 0.1;
bottomLeft.y = 0.2;
topRight.x = 0.3;
topRight.y = 0.4;
}
SECTION ("Region in negative x, negative y quadrant, floating-point limits")
{
spacing = 0.5;
bottomLeft.x = -0.4;
bottomLeft.y = -0.3;
topRight.x = -0.2;
topRight.y = -0.1;
}
SECTION ("Region in all quadrants, floating-point limits")
{
spacing = 0.5;
bottomLeft.x = -1.1;
bottomLeft.y = -2.2;
topRight.x = 3.3;
topRight.y = 4.4;
}
SECTION ("Region in all quadrants, floating-point limits, smaller spacing")
{
spacing = 0.25;
bottomLeft.x = 1.1;
bottomLeft.y = 2.2;
topRight.x = 3.3;
topRight.y = 4.4;
}
std::unique_ptr<MetaGraph> metaGraph(new MetaGraph("Test MetaGraph"));
metaGraph->setRegion(bottomLeft, topRight);
PointMap pointMap(metaGraph->getRegion(), metaGraph->m_drawingFiles, "Test PointMap");
bool gridIsSet = pointMap.setGrid(spacing, offset);
int bottomLeftPixelIndexX = int(floor(bottomLeft.x / spacing - 0.5)) + 1;
int bottomLeftPixelIndexY = int(floor(bottomLeft.y / spacing - 0.5)) + 1;
int topRightPixelIndexX = int(floor(topRight.x / spacing - 0.5)) + 1;
int topRightPixelIndexY = int(floor(topRight.y / spacing - 0.5)) + 1;
int numCellsX = topRightPixelIndexX - bottomLeftPixelIndexX + 1;
int numCellsY = topRightPixelIndexY - bottomLeftPixelIndexY + 1;
// check if the size of the grid is as expected
REQUIRE(pointMap.getCols() == numCellsX);
REQUIRE(pointMap.getRows() == numCellsY);
Point2f gridBottomLeft(bottomLeftPixelIndexX * spacing - 0.5 * spacing,
bottomLeftPixelIndexY * spacing - 0.5 * spacing);
// check if the bottom-left corner of the bottom-left pixel is as expected
REQUIRE(pointMap.getRegion().bottom_left.x == Approx(gridBottomLeft.x).epsilon(EPSILON));
REQUIRE(pointMap.getRegion().bottom_left.y == Approx(gridBottomLeft.y).epsilon(EPSILON));
Point2f midPoint(gridBottomLeft.x + spacing * (floor(numCellsX * 0.5) + 0.5),
gridBottomLeft.y + spacing * (floor(numCellsY * 0.5) + 0.5));
int fill_type = 0; // = QDepthmapView::FULLFILL
bool pointsMade = pointMap.makePoints(midPoint, fill_type);
// check if the grid is filled
REQUIRE(pointsMade);
}
TEST_CASE("Test PointMap connections output", "")
{
const float EPSILON = 0.001;
double spacing = 0.5;
Point2f offset(0,0); // seems that this is always set to 0,0
std::unique_ptr<MetaGraph> metaGraph(new MetaGraph("Test MetaGraph"));
double rectSize = 1.5;
Point2f line0Start(0,0);
Point2f line0End(0,rectSize);
Point2f line1Start(0,rectSize);
Point2f line1End(rectSize,rectSize);
Point2f line2Start(rectSize,rectSize);
Point2f line2End(rectSize,0);
Point2f line3Start(rectSize,0);
Point2f line3End(0,0);
metaGraph->m_drawingFiles.emplace_back("Test SpacePixelGroup");
metaGraph->m_drawingFiles.back().m_spacePixels.emplace_back("Test ShapeMap");
metaGraph->m_drawingFiles.back().m_spacePixels.back().makeLineShape(Line(line0Start, line0End));
metaGraph->m_drawingFiles.back().m_spacePixels.back().makeLineShape(Line(line1Start, line1End));
metaGraph->m_drawingFiles.back().m_spacePixels.back().makeLineShape(Line(line2Start, line2End));
metaGraph->m_drawingFiles.back().m_spacePixels.back().makeLineShape(Line(line3Start, line3End));
metaGraph->m_drawingFiles.back().m_region = metaGraph->m_drawingFiles.back().m_spacePixels.back().getRegion();
metaGraph->setRegion(metaGraph->m_drawingFiles.back().m_region.bottom_left,
metaGraph->m_drawingFiles.back().m_region.top_right);
PointMap pointMap(metaGraph->getRegion(), metaGraph->m_drawingFiles, "Test PointMap");
Point2f gridBottomLeft = pointMap.getRegion().bottom_left;
Point2f midPoint(gridBottomLeft.x + spacing * (floor(pointMap.getCols() * 0.5) + 0.5),
gridBottomLeft.y + spacing * (floor(pointMap.getRows() * 0.5) + 0.5));
int fill_type = 0; // = QDepthmapView::FULLFILL
bool gridIsSet = pointMap.setGrid(spacing, offset);
bool pointsMade = pointMap.makePoints(midPoint, fill_type);
bool boundaryGraph = false;
double maxDist = -1;
// a communicator is required in order to create the connections between the pixels
std::unique_ptr<Communicator> comm(new ICommunicator());
bool graphMade = pointMap.sparkGraph2(comm.get(), boundaryGraph, maxDist);
REQUIRE(graphMade);
SECTION("PointMap::outputLinksAsCSV") {
std::stringstream stream;
pointMap.mergePixels(65537, 131074);
pointMap.mergePixels(131073, 65538);
pointMap.outputLinksAsCSV(stream);
REQUIRE(stream.good());
char line[1000];
std::vector<std::string> lines;
while( !stream.eof())
{
stream.getline(line, 1000);
lines.push_back(line);
}
std::vector<std::string> expected{ "RefFrom,RefTo",
"65537,131074",
"65538,131073"};
REQUIRE(lines == expected);
}
SECTION("PointMap::outputConnectionsAsCSV") {
std::stringstream stream;
pointMap.outputConnectionsAsCSV(stream);
REQUIRE(stream.good());
char line[1000];
std::vector<std::string> lines;
while( !stream.eof())
{
stream.getline(line, 1000);
lines.push_back(line);
}
std::vector<std::string> expected{ "RefFrom,RefTo",
"65537,131073", "65537,131074", "65537,65538",
"65538,131074", "65538,131073", "131073,131074"};
REQUIRE(lines == expected);
}
SECTION("PointMap::outputConnections") {
std::stringstream stream;
pointMap.outputConnections(stream);
REQUIRE(stream.good());
char line[1000];
std::vector<std::string> lines;
while( !stream.eof())
{
stream.getline(line, 1000);
lines.push_back(line);
}
std::vector<std::string> expected{ "#graph v1.0",
"node {",
" ref 65537",
" origin 0.5 0.5 0",
" connections [",
" 131073,",
" 131074,",
" 65538,",
" ]",
"}",
"node {",
" ref 65538",
" origin 0.5 1 0",
" connections [",
" 131074,",
" 65537,",
" 131073,",
" ]",
"}",
"node {",
" ref 131073",
" origin 1 0.5 0",
" connections [",
" 131074,",
" 65538,",
" 65537,",
" ]",
"}",
"node {",
" ref 131074",
" origin 1 1 0",
" connections [",
" 65538,",
" 65537,",
" 131073,",
" ]",
"}",
"" };
REQUIRE(lines == expected);
}
}
TEST_CASE("Direct pointmap linking - fully filled grid (no geometry)", "")
{
double spacing = 0.5;
Point2f offset(0,0); // seems that this is always set to 0,0
Point2f bottomLeft(0,0);
Point2f topRight(2,4);
int fill_type = 0; // = QDepthmapView::FULLFILL
std::unique_ptr<MetaGraph> metaGraph(new MetaGraph("Test MetaGraph"));
metaGraph->setRegion(bottomLeft, topRight);
PointMap pointMap(metaGraph->getRegion(), metaGraph->m_drawingFiles, "Test PointMap");
pointMap.setGrid(spacing, offset);
Point2f gridBottomLeft = pointMap.getRegion().bottom_left;
Point2f midPoint(gridBottomLeft.x + spacing * (floor(pointMap.getCols() * 0.5) + 0.5),
gridBottomLeft.y + spacing * (floor(pointMap.getRows() * 0.5) + 0.5));
pointMap.makePoints(midPoint, fill_type);
std::vector<Line> mergeLines;
PixelRef bottomLeftPixel = pointMap.pixelate(bottomLeft);
PixelRef topRightPixel = pointMap.pixelate(topRight);
// make sure pixels are not already merged
REQUIRE(!pointMap.isPixelMerged(bottomLeftPixel));
REQUIRE(!pointMap.isPixelMerged(topRightPixel));
// merge
pointMap.mergePixels(bottomLeftPixel, topRightPixel);
// make sure pixels are merged
REQUIRE(pointMap.isPixelMerged(bottomLeftPixel));
REQUIRE(pointMap.isPixelMerged(topRightPixel));
SECTION ("Make sure we get the correct number of merged pixel pairs")
{
const std::vector<std::pair<PixelRef, PixelRef>> &pixelPairs = pointMap.getMergedPixelPairs();
REQUIRE(pixelPairs.size() == 1);
REQUIRE(pixelPairs[0].first == bottomLeftPixel);
REQUIRE(pixelPairs[0].second == topRightPixel);
}
SECTION ("Overwrite the pixelpair by re-merging the first pixel of the pair")
{
PixelRef aboveBottomLeftPixel = pointMap.pixelate(Point2f(bottomLeft.x, bottomLeft.y + 1));
// merge
pointMap.mergePixels(aboveBottomLeftPixel, topRightPixel);
// make sure pixels are merged
REQUIRE(pointMap.isPixelMerged(aboveBottomLeftPixel));
REQUIRE(pointMap.isPixelMerged(topRightPixel));
// and previous pixel is not merged any more
REQUIRE(!pointMap.isPixelMerged(bottomLeftPixel));
// make sure we get the correct number of merged pixel pairs
const std::vector<std::pair<PixelRef, PixelRef>> &pixelPairs = pointMap.getMergedPixelPairs();
REQUIRE(pixelPairs.size() == 1);
REQUIRE(pixelPairs[0].first == aboveBottomLeftPixel);
REQUIRE(pixelPairs[0].second == topRightPixel);
}
SECTION ("Overwrite the pixelpair by re-merging the second pixel of the pair")
{
PixelRef belowTopRightPixel = pointMap.pixelate(Point2f(topRight.x, topRight.y - 1));
// merge
pointMap.mergePixels(bottomLeftPixel, belowTopRightPixel);
// make sure pixels are merged
REQUIRE(pointMap.isPixelMerged(bottomLeftPixel));
REQUIRE(pointMap.isPixelMerged(belowTopRightPixel));
// and previous pixel is not merged any more
REQUIRE(!pointMap.isPixelMerged(topRightPixel));
// make sure we get the correct number of merged pixel pairs
const std::vector<std::pair<PixelRef, PixelRef>> &pixelPairs2 = pointMap.getMergedPixelPairs();
REQUIRE(pixelPairs2.size() == 1);
REQUIRE(pixelPairs2[0].first == bottomLeftPixel);
REQUIRE(pixelPairs2[0].second == belowTopRightPixel);
}
SECTION ("Merge the same pixel twice to erase the pair")
{
pointMap.mergePixels(bottomLeftPixel, bottomLeftPixel);
// make sure no pixel is merged
REQUIRE(!pointMap.isPixelMerged(bottomLeftPixel));
REQUIRE(!pointMap.isPixelMerged(topRightPixel));
// make sure we get the correct number of merged pixel pairs
const std::vector<std::pair<PixelRef, PixelRef>> &pixelPairs3 = pointMap.getMergedPixelPairs();
REQUIRE(pixelPairs3.size() == 0);
}
}
You can’t perform that action at this time.
