File: point_list_test.cpp

package info (click to toggle)
sight 25.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,108 kB
  • sloc: cpp: 306,170; xml: 18,037; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (528 lines) | stat: -rw-r--r-- 20,888 bytes parent folder | download
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
/************************************************************************
 *
 * Copyright (C) 2017-2023 IRCAD France
 * Copyright (C) 2017-2021 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "point_list_test.hpp"

#include <core/exception.hpp>

#include <data/point.hpp>

#include <geometry/data/point_list.hpp>

#include <glm/geometric.hpp>
#include <glm/vec3.hpp>

#include <random>

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::geometry::data::ut::point_list_test);

namespace sight::geometry::data::ut
{

//------------------------------------------------------------------------------

void point_list_test::setUp()
{
    // Set up context before running a test.
}

//------------------------------------------------------------------------------

void point_list_test::tearDown()
{
    // Clean up after the test run.
}

//------------------------------------------------------------------------------

void point_list_test::compute_distance()
{
    const std::size_t nb_points = 42;

    sight::data::point_list::sptr pl1;
    sight::data::point_list::sptr pl2;

    sight::data::point::sptr p;

    // Simple test with empty point lists
    {
        pl1 = std::make_shared<sight::data::point_list>();
        pl2 = std::make_shared<sight::data::point_list>();

        // Compare the point lists
        auto output_array = geometry::data::point_list::compute_distance(pl1, pl2);

        CPPUNIT_ASSERT(output_array->num_elements() == 0);
    }

    // Simple test with parallel point lists
    {
        pl1 = std::make_shared<sight::data::point_list>();
        pl2 = std::make_shared<sight::data::point_list>();

        // Build 2 pointlists:
        // The first one with increasing x values
        // And the second one with inscreasing x values but shifted in y
        for(std::size_t i = 0 ; i < nb_points ; i++)
        {
            p = std::make_shared<sight::data::point>(static_cast<float>(i), 0.0F, 0.0F);
            pl1->push_back(p);

            p = std::make_shared<sight::data::point>(static_cast<float>(i), 1.0F, 0.0F);
            pl2->push_back(p);
        }

        // Compare the point lists
        auto output_array = geometry::data::point_list::compute_distance(pl1, pl2);

        const auto dump_lock    = output_array->dump_lock();
        auto distance_array_itr = output_array->begin<double>();

        for(std::size_t i = 0 ; i < nb_points ; i++, ++distance_array_itr)
        {
            CPPUNIT_ASSERT_DOUBLES_EQUAL(*distance_array_itr, 1.0, 1e-8);
        }
    }

    // Simple test with diverging point lists
    {
        pl1 = std::make_shared<sight::data::point_list>();
        pl2 = std::make_shared<sight::data::point_list>();

        // Build 2 point lists:
        // The first one with increasing x values
        // And the second one with increasing x values but shifted in y
        for(std::size_t i = 0 ; i < nb_points ; i++)
        {
            p = std::make_shared<sight::data::point>(static_cast<float>(i), 0.0F, 0.0F);
            pl1->push_back(p);

            p = std::make_shared<sight::data::point>(static_cast<float>(i), static_cast<float>(i), 0.0F);
            pl2->push_back(p);
        }

        // Compare the point lists
        auto output_array    = geometry::data::point_list::compute_distance(pl1, pl2);
        const auto dump_lock = output_array->dump_lock();

        auto distance_array_itr = output_array->begin<double>();

        for(std::size_t i = 0 ; i < nb_points ; i++, ++distance_array_itr)
        {
            CPPUNIT_ASSERT_DOUBLES_EQUAL(*distance_array_itr, static_cast<double>(i), 1e-8);
        }
    }
}

//------------------------------------------------------------------------------

void point_list_test::transform()
{
    // Simple test with identity
    {
        // Test sample
        sight::data::point_list::sptr pl1 = std::make_shared<sight::data::point_list>();
        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 0.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 1.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 1.0F, 0.0F));

        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 0.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 1.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 1.0F, 1.0F));

        // Reference list
        const sight::data::point_list::container_t points1 = pl1->get_points();
        const std::size_t size                             = points1.size();

        sight::data::point_list::sptr pl2 = std::make_shared<sight::data::point_list>();
        for(std::size_t i = 0 ; i < size ; i++)
        {
            const sight::data::point::point_coord_array_t tmp = points1[i]->get_coord();
            pl2->push_back(std::make_shared<sight::data::point>(tmp[0], tmp[1], tmp[2]));
        }

        const auto tf1 = std::make_shared<sight::data::matrix4>();
        geometry::data::point_list::transform(pl1, tf1);

        const sight::data::point_list::container_t points2 = pl2->get_points();
        for(std::size_t i = 0 ; i < size ; i++)
        {
            const sight::data::point::point_coord_array_t tmp1 = points1[i]->get_coord();
            const sight::data::point::point_coord_array_t tmp2 = points2[i]->get_coord();

            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[0], tmp2[0], 1e-8);
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[1], tmp2[1], 1e-8);
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[2], tmp2[2], 1e-8);
        }
    }

    // Simple test with translation
    {
        std::vector<float> translation(3, 0.0F);
        translation[0] = 8.0;
        translation[1] = 16.0;
        translation[2] = 32.0;

        // Test sample
        sight::data::point_list::sptr pl1 = std::make_shared<sight::data::point_list>();
        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 0.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 1.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 1.0F, 0.0F));

        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 0.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 1.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 1.0F, 1.0F));

        // Reference list
        const sight::data::point_list::container_t points1 = pl1->get_points();
        const std::size_t size                             = points1.size();

        sight::data::point_list::sptr pl2 = std::make_shared<sight::data::point_list>();
        for(std::size_t i = 0 ; i < size ; i++)
        {
            const sight::data::point::point_coord_array_t tmp = points1[i]->get_coord();
            pl2->push_back(
                std::make_shared<sight::data::point>(
                    tmp[0] + translation[0],
                    tmp[1] + translation[1],
                    tmp[2] + translation[2]
                )
            );
        }

        const auto tf1 = std::make_shared<sight::data::matrix4>();
        (*tf1)(0, 3) = translation[0];
        (*tf1)(1, 3) = translation[1];
        (*tf1)(2, 3) = translation[2];
        geometry::data::point_list::transform(pl1, tf1);

        const sight::data::point_list::container_t points2 = pl2->get_points();
        for(std::size_t i = 0 ; i < size ; i++)
        {
            const sight::data::point::point_coord_array_t tmp1 = points1[i]->get_coord();
            const sight::data::point::point_coord_array_t tmp2 = points2[i]->get_coord();

            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[0], tmp2[0], 1e-8);
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[1], tmp2[1], 1e-8);
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[2], tmp2[2], 1e-8);
        }
    }

    // Simple test with rotation
    {
        sight::data::point_list::sptr pl1 = std::make_shared<sight::data::point_list>();
        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 0.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 1.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 1.0F, 0.0F));

        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 0.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 1.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(0.0F, 1.0F, 1.0F));

        sight::data::point_list::sptr pl2 = std::make_shared<sight::data::point_list>();
        pl2->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 0.0F));
        pl2->push_back(std::make_shared<sight::data::point>(-1.0F, 0.0F, 0.0F));
        pl2->push_back(std::make_shared<sight::data::point>(-1.0F, -1.0F, 0.0F));
        pl2->push_back(std::make_shared<sight::data::point>(0.0F, -1.0F, 0.0F));

        pl2->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 1.0F));
        pl2->push_back(std::make_shared<sight::data::point>(-1.0F, 0.0F, 1.0F));
        pl2->push_back(std::make_shared<sight::data::point>(-1.0F, -1.0F, 1.0F));
        pl2->push_back(std::make_shared<sight::data::point>(0.0F, -1.0F, 1.0F));

        // Perform a 180 degrees rotation around Z
        const auto tf1 = std::make_shared<sight::data::matrix4>();
        (*tf1)(0, 0) = -1.0F;
        (*tf1)(0, 1) = 0.0F;
        (*tf1)(1, 0) = 0.0F;
        (*tf1)(1, 1) = -1.0F;

        geometry::data::point_list::transform(pl1, tf1);

        const sight::data::point_list::container_t points1 = pl1->get_points();
        std::size_t size                                   = points1.size();
        const sight::data::point_list::container_t points2 = pl2->get_points();

        for(std::size_t i = 0 ; i < size ; i++)
        {
            const sight::data::point::point_coord_array_t tmp1 = points1[i]->get_coord();
            const sight::data::point::point_coord_array_t tmp2 = points2[i]->get_coord();

            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[0], tmp2[0], 1e-8);
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[1], tmp2[1], 1e-8);
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[2], tmp2[2], 1e-8);
        }
    }
}

//------------------------------------------------------------------------------

void point_list_test::associate()
{
    const std::size_t nb_points = 42;

    sight::data::point_list::sptr pl1;
    sight::data::point_list::sptr pl2;

    sight::data::point::sptr p;

    // Simple test with empty point lists
    {
        pl1 = std::make_shared<sight::data::point_list>();
        pl2 = std::make_shared<sight::data::point_list>();

        // Associate empty point lists
        geometry::data::point_list::associate(pl1, pl2);

        // No results expected
    }

    // Test with simple matrices
    // Create two lists with the same sets of points and shift them with transformation matrices
    // Associating them should make the x components match
    {
        pl1 = std::make_shared<sight::data::point_list>();
        pl2 = std::make_shared<sight::data::point_list>();

        // Build 2 point lists with the same points, the point are in the inverse order in the second list
        for(std::size_t i = 0 ; i <= nb_points ; i++)
        {
            p = std::make_shared<sight::data::point>(static_cast<float>(i), 0.0F, 0.0F);
            pl1->push_back(p);

            p = std::make_shared<sight::data::point>(static_cast<float>(nb_points - i), 0.0F, 0.0F);
            pl2->push_back(p);
        }

        // Transform the point lists, shift the points in y
        auto tf1 = std::make_shared<sight::data::matrix4>();
        (*tf1)(1, 3) = 42.0;
        auto tf2 = std::make_shared<sight::data::matrix4>();
        (*tf2)(1, 3) = -42.0;

        geometry::data::point_list::transform(pl1, tf1);
        geometry::data::point_list::transform(pl2, tf2);

        // Associate the point lists
        geometry::data::point_list::associate(pl1, pl2);

        // Check that the two list are equal (re-ordered)
        // Only the last component should differ, as there is a (0,0,42) translation
        // on the first one and a (0,0,-42) translation on the second one
        const sight::data::point_list::container_t points1 = pl1->get_points();
        const sight::data::point_list::container_t points2 = pl2->get_points();

        const std::size_t size = points1.size();

        for(std::size_t i = 0 ; i < size ; i++)
        {
            const sight::data::point::point_coord_array_t tmp1 = points1[i]->get_coord();
            const sight::data::point::point_coord_array_t tmp2 = points2[i]->get_coord();

            // Check that the last component is equal to i
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[0], tmp2[0], 1e-8);
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[0], static_cast<float>(i), 1e-8);

            // Check that the second component is equal to 42
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[1], -tmp2[1], 1e-8);
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[1], static_cast<float>(42), 1e-8);

            // Check that the last component is equal to 0
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[2], tmp2[2], 1e-8);
            CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[2], 0.0, 1e-8);
        }
    }

    // Test with random point lists
    // Fill two point lists with random points
    // Check that the matched points are really the closest
    {
        // Create an initial octahedron with points
        pl1 = std::make_shared<sight::data::point_list>();
        pl1->push_back(std::make_shared<sight::data::point>(1.0F, 0.0F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(2.0F, 0.0F, 1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(2.0F, 0.8F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(2.0F, 0.0F, -1.0F));
        pl1->push_back(std::make_shared<sight::data::point>(2.0F, -0.8F, 0.0F));
        pl1->push_back(std::make_shared<sight::data::point>(3.0F, 0.0F, 0.0F));

        // Reference final octahedron
        pl2 = std::make_shared<sight::data::point_list>();
        pl2->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 5.2F));
        pl2->push_back(std::make_shared<sight::data::point>(1.0F, 0.0F, 6.2F));
        pl2->push_back(std::make_shared<sight::data::point>(0.0F, 0.8F, 6.2F));
        pl2->push_back(std::make_shared<sight::data::point>(-1.0F, 0.0F, 6.2F));
        pl2->push_back(std::make_shared<sight::data::point>(0.0F, -0.8F, 6.2F));
        pl2->push_back(std::make_shared<sight::data::point>(0.0F, 0.0F, 7.2F));

        // Transform the point list
        auto tf1 = std::make_shared<sight::data::matrix4>();
        // Shift the points in Z
        (*tf1)(2, 3) = 4.2;

        // Add a 90 degrees rotation around Y
        (*tf1)(0, 0) = 0.0F;
        (*tf1)(0, 2) = 1.0F;
        (*tf1)(2, 0) = 1.0F;
        (*tf1)(2, 2) = 0.0F;

        geometry::data::point_list::transform(pl1, tf1);

        // Associate the point lists
        geometry::data::point_list::associate(pl1, pl2);

        // Check that the two lists are equal
        // The second one corresponds to a Z translation + a 90 degrees Y translation of the first one
        const sight::data::point_list::container_t points1 = pl1->get_points();
        const sight::data::point_list::container_t points2 = pl2->get_points();

        const std::size_t size  = points1.size();
        const int nb_components = 3;

        for(std::size_t i = 0 ; i < size ; i++)
        {
            const sight::data::point::point_coord_array_t tmp1 = points1[i]->get_coord();
            const sight::data::point::point_coord_array_t tmp2 = points2[i]->get_coord();

            // Compare the components
            for(std::size_t j = 0 ; j < nb_components ; j++)
            {
                CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp1[j], tmp2[j], 1e-5);
            }
        }
    }
}

//------------------------------------------------------------------------------

void point_list_test::remove_closest_point_nominal()
{
    const std::size_t nb_points      = 42;
    sight::data::point_list::sptr pl = std::make_shared<sight::data::point_list>();

    // Remove points in an empty list
    for(std::size_t i = 0 ; i < nb_points ; ++i)
    {
        const auto p =
            std::make_shared<sight::data::point>(static_cast<float>(i), static_cast<float>(i), static_cast<float>(i));
        CPPUNIT_ASSERT(
            geometry::data::point_list::remove_closest_point(
                pl,
                p,
                std::numeric_limits<float>::min()
            )
            == nullptr
        );
    }

    // Build a list
    for(std::size_t i = 0 ; i < nb_points ; ++i)
    {
        const auto p =
            std::make_shared<sight::data::point>(static_cast<float>(i), static_cast<float>(i), static_cast<float>(i));
        pl->push_back(p);
    }

    // Remove points with an unmatched delta
    for(std::size_t i = 0 ; i < nb_points ; ++i)
    {
        const auto p = std::make_shared<sight::data::point>(
            static_cast<float>(nb_points + 1),
            static_cast<float>(nb_points + 1),
            static_cast<float>(nb_points + 1)
        );
        CPPUNIT_ASSERT(
            geometry::data::point_list::remove_closest_point(
                pl,
                p,
                std::numeric_limits<float>::min()
            )
            == nullptr
        );
    }

    // Remove points
    for(std::size_t i = 0 ; i < nb_points ; ++i)
    {
        const auto p =
            std::make_shared<sight::data::point>(static_cast<float>(i), static_cast<float>(i), static_cast<float>(i));
        const auto p_res =
            geometry::data::point_list::remove_closest_point(pl, p, std::numeric_limits<float>::max());
        CPPUNIT_ASSERT(p_res != nullptr);
        CPPUNIT_ASSERT_EQUAL(p_res->get_coord()[0], p->get_coord()[0]);
        CPPUNIT_ASSERT_EQUAL(p_res->get_coord()[1], p->get_coord()[1]);
        CPPUNIT_ASSERT_EQUAL(p_res->get_coord()[2], p->get_coord()[2]);
    }
}

//------------------------------------------------------------------------------

void point_list_test::remove_closest_point_extreme()
{
    const std::size_t nb_points      = 42;
    sight::data::point_list::sptr pl = std::make_shared<sight::data::point_list>();

    // Build a list
    for(std::size_t i = 0 ; i < nb_points ; ++i)
    {
        const auto p =
            std::make_shared<sight::data::point>(static_cast<float>(i), static_cast<float>(i), static_cast<float>(i));
        pl->push_back(p);
    }

    // Remove points with negative delta
    for(std::size_t i = 0 ; i < nb_points ; ++i)
    {
        const auto p =
            std::make_shared<sight::data::point>(static_cast<float>(i), static_cast<float>(i), static_cast<float>(i));
        CPPUNIT_ASSERT(
            geometry::data::point_list::remove_closest_point(
                pl,
                p,
                -std::numeric_limits<float>::max()
            )
            == nullptr
        );
    }

    // Remove points with biggest delta
    for(std::size_t i = 0 ; i < nb_points ; ++i)
    {
        const auto p =
            std::make_shared<sight::data::point>(static_cast<float>(i), static_cast<float>(i), static_cast<float>(i));
        const auto p_res =
            geometry::data::point_list::remove_closest_point(pl, p, std::numeric_limits<float>::max());
        CPPUNIT_ASSERT(p_res != nullptr);
        CPPUNIT_ASSERT_EQUAL(p_res->get_coord()[0], p->get_coord()[0]);
        CPPUNIT_ASSERT_EQUAL(p_res->get_coord()[1], p->get_coord()[1]);
        CPPUNIT_ASSERT_EQUAL(p_res->get_coord()[2], p->get_coord()[2]);
    }
}

} // namespace sight::geometry::data::ut