File: concatenate_matrices_test.cpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (466 lines) | stat: -rw-r--r-- 14,030 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
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
/************************************************************************
 *
 * Copyright (C) 2024 IRCAD France
 *
 * 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 "concatenate_matrices_test.hpp"

#include <core/runtime/runtime.hpp>

#include <data/matrix4.hpp>

#include <service/op.hpp>

#include <boost/property_tree/xml_parser.hpp>

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::module::geometry::ut::concatenate_matrices_test);

namespace sight::module::geometry::ut
{

static const double DELTA = 1e-3;
using matrix_t = std::array<double, 16>;

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

void concatenate_matrices_test::setUp()
{
}

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

void concatenate_matrices_test::tearDown()
{
}

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

void check_matrix(const matrix_t& _expected, const sight::data::matrix4& _actual)
{
    for(size_t i = 0 ; i < _expected.size() ; ++i)
    {
        CPPUNIT_ASSERT_DOUBLES_EQUAL(_expected[i], _actual[i], DELTA);
    }
}

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

void concatenate_matrices_test::identity()
{
    auto srv = sight::service::add("sight::module::geometry::concatenate_matrices");
    CPPUNIT_ASSERT(srv != nullptr);
    CPPUNIT_ASSERT(srv->is_a("sight::module::geometry::concatenate_matrices"));

    // One identity matrix as input
    {
        std::stringstream config_string;
        config_string
        << "<in group=\"matrix\">"
           "     <key uid=\"m1\" />"
           "</in>";
        sight::service::base::config_t config;
        boost::property_tree::read_xml(config_string, config);
        srv->set_config(config);
    }

    auto input  = std::make_shared<sight::data::matrix4>();
    auto output = std::make_shared<sight::data::matrix4>();

    const matrix_t identity = sight::data::matrix4::identity();
    check_matrix(identity, *input);
    check_matrix(identity, *output);

    srv->set_input(input, "matrix", true, false, 0);
    srv->set_inout(output, "output");

    srv->configure();
    srv->start().get();
    srv->update().get();

    check_matrix(identity, *output);

    CPPUNIT_ASSERT_NO_THROW(srv->stop().get());

    // Add a second identity matrix as input
    {
        std::stringstream config_string;
        config_string
        << "<in group=\"matrix\">"
           "     <key uid=\"m1\" />"
           "     <key uid=\"m2\" />"
           "</in>";
        sight::service::base::config_t config;
        boost::property_tree::read_xml(config_string, config);
        srv->set_config(config);
    }
    auto input2 = std::make_shared<sight::data::matrix4>();
    srv->set_input(input2, "matrix", true, false, 1);

    srv->configure();
    srv->start().get();
    srv->update().get();

    check_matrix(identity, *output);

    CPPUNIT_ASSERT_NO_THROW(srv->stop().get());

    sight::service::remove(srv);
}

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

void concatenate_matrices_test::single()
{
    // Silly test to verify the output = the input if there is only one matrix

    auto srv = sight::service::add("sight::module::geometry::concatenate_matrices");
    CPPUNIT_ASSERT(srv != nullptr);
    CPPUNIT_ASSERT(srv->is_a("sight::module::geometry::concatenate_matrices"));

    {
        std::stringstream config_string;
        config_string
        << "<in group=\"matrix\">"
           "     <key uid=\"m1\" />"
           "</in>";
        sight::service::base::config_t config;
        boost::property_tree::read_xml(config_string, config);
        srv->set_config(config);
    }

    auto input  = std::make_shared<sight::data::matrix4>();
    auto output = std::make_shared<sight::data::matrix4>();

    const matrix_t m = {0., 0., 0., 12.,
                        1., -0.866, -0.5, -51.34,
                        0., 0.5, -0.866, 1.314,
                        0., 0., 0., 1.
    };
    *input = m;
    check_matrix(m, *input);

    const matrix_t identity = sight::data::matrix4::identity();
    check_matrix(identity, *output);

    srv->set_input(input, "matrix", true, false, 0);
    srv->set_inout(output, "output");

    srv->configure();
    srv->start().get();
    srv->update().get();

    check_matrix(m, *output);

    CPPUNIT_ASSERT_NO_THROW(srv->stop().get());

    sight::service::remove(srv);
}

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

void concatenate_matrices_test::multiply_2()
{
    auto srv = sight::service::add("sight::module::geometry::concatenate_matrices");
    CPPUNIT_ASSERT(srv != nullptr);
    CPPUNIT_ASSERT(srv->is_a("sight::module::geometry::concatenate_matrices"));

    std::stringstream config_string;
    config_string
    << "<in group=\"matrix\">"
       "     <key uid=\"m1\" />"
       "     <key uid=\"m2\" />"
       "</in>";
    sight::service::base::config_t config;
    boost::property_tree::read_xml(config_string, config);
    srv->set_config(config);

    auto in1    = std::make_shared<sight::data::matrix4>();
    auto in2    = std::make_shared<sight::data::matrix4>();
    auto output = std::make_shared<sight::data::matrix4>();

    *in1 = {
        1., 0., 0., 4.,
        0., 1., 0., -5.,
        0., 0., 1., 2.,
        0., 0., 0., 1.
    };

    *in2 = {1., 0., 0., 0.,
            0., -0.866, -0.5, 0.,
            0., 0.5, -0.866, 0.,
            0., 0., 0., 1.
    };

    srv->set_input(in1, "matrix", true, false, 0);
    srv->set_input(in2, "matrix", true, false, 1);
    srv->set_inout(output, "output");

    srv->configure();
    srv->start().get();

    const matrix_t identity = sight::data::matrix4::identity();
    check_matrix(identity, *output);

    srv->update().get();

    const matrix_t expected = {
        1., 0., 0., 4.,
        0., -0.866, -0.5, -5.,
        0., 0.5, -0.866, 2.,
        0., 0., 0., 1.
    };

    check_matrix(expected, *output);

    CPPUNIT_ASSERT_NO_THROW(srv->stop().get());

    sight::service::remove(srv);
}

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

void concatenate_matrices_test::multiply_4()
{
    auto srv = sight::service::add("sight::module::geometry::concatenate_matrices");
    CPPUNIT_ASSERT(srv != nullptr);
    CPPUNIT_ASSERT(srv->is_a("sight::module::geometry::concatenate_matrices"));

    std::stringstream config_string;
    config_string
    << "<in group=\"matrix\">"
       "     <key uid=\"m1\" />"
       "     <key uid=\"m2\" />"
       "     <key uid=\"m3\" />"
       "     <key uid=\"m4\" />"
       "</in>";
    sight::service::base::config_t config;
    boost::property_tree::read_xml(config_string, config);
    srv->set_config(config);

    auto in1    = std::make_shared<sight::data::matrix4>();
    auto in2    = std::make_shared<sight::data::matrix4>();
    auto in3    = std::make_shared<sight::data::matrix4>();
    auto in4    = std::make_shared<sight::data::matrix4>();
    auto output = std::make_shared<sight::data::matrix4>();

    *in1 = {
        1., 0., 0., 4.,
        0., 1., 0., -5.,
        0., 0., 1., 2.,
        0., 0., 0., 1.
    };
    *in2 = {1., 0., 0., 0.,
            0., -0.866, -0.5, 0.,
            0., 0.5, -0.866, 0.,
            0., 0., 0., 1.
    };
    *in3 = {0.5, 0., 0.866, 0.,
            0., 1., 0., 0.,
            -0.866, 0., 0.5, 0.,
            0., 0., 0., 1.
    };
    *in4 = {-0.866, -0.5, 0., 14.94,
            0.5, -0.866, 0., 0.,
            0., 0., 1., -27.,
            0., 0., 0., 1.
    };

    srv->set_input(in1, "matrix", true, false, 0);
    srv->set_input(in2, "matrix", true, false, 1);
    srv->set_input(in3, "matrix", true, false, 2);
    srv->set_input(in4, "matrix", true, false, 3);
    srv->set_inout(output, "output");

    srv->configure();
    srv->start().get();

    const matrix_t identity = sight::data::matrix4::identity();
    check_matrix(identity, *output);

    srv->update().get();

    const matrix_t expected = {-0.433, -0.25, 0.866, -11.912,
                               -0.807978, 0.533456, -0.25, 8.21902,
                               -0.399462, -0.807978, -0.433, 24.8953,
                               0., 0., 0., 1.,
    };
    check_matrix(expected, *output);

    CPPUNIT_ASSERT_NO_THROW(srv->stop().get());

    sight::service::remove(srv);
}

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

void concatenate_matrices_test::multiply_inverse()
{
    auto srv = sight::service::add("sight::module::geometry::concatenate_matrices");
    CPPUNIT_ASSERT(srv != nullptr);
    CPPUNIT_ASSERT(srv->is_a("sight::module::geometry::concatenate_matrices"));

    std::stringstream config_string;
    config_string
    << "<in group=\"matrix\">"
       "     <key uid=\"m1\" inverse=\"true\"/>"
       "     <key uid=\"m2\" />"
       "     <key uid=\"m3\" inverse=\"true\"/>"
       "     <key uid=\"m4\" inverse=\"true\"/>"
       "</in>";
    sight::service::base::config_t config;
    boost::property_tree::read_xml(config_string, config);
    srv->set_config(config);

    auto in1    = std::make_shared<sight::data::matrix4>();
    auto in2    = std::make_shared<sight::data::matrix4>();
    auto in3    = std::make_shared<sight::data::matrix4>();
    auto in4    = std::make_shared<sight::data::matrix4>();
    auto output = std::make_shared<sight::data::matrix4>();

    *in1 = {
        1., 0., 0., 4.,
        0., 1., 0., -5.,
        0., 0., 1., 2.,
        0., 0., 0., 1.
    };
    *in2 = {1., 0., 0., 0.,
            0., -0.866, -0.5, 0.,
            0., 0.5, -0.866, 0.,
            0., 0., 0., 1.
    };
    *in3 = {0.5, 0., 0.866, 0.,
            0., 1., 0., 0.,
            -0.866, 0., 0.5, 0.,
            0., 0., 0., 1.
    };
    *in4 = {-0.866, -0.5, 0., 14.94,
            0.5, -0.866, 0., 0.,
            0., 0., 1., -27.,
            0., 0., 0., 1.
    };

    srv->set_input(in1, "matrix", true, false, 0);
    srv->set_input(in2, "matrix", true, false, 1);
    srv->set_input(in3, "matrix", true, false, 2);
    srv->set_input(in4, "matrix", true, false, 3);
    srv->set_inout(output, "output");

    srv->configure();
    srv->start().get();

    const matrix_t identity = sight::data::matrix4::identity();
    check_matrix(identity, *output);

    // Stress a bit to ensure we always get the same result
    for(int i = 0 ; i < 500 ; ++i)
    {
        srv->update().get();

        const matrix_t expected = {-0.433038, 0.250022, -0.866038, -20.9134,
                                   0.80803, 0.53347, -0.250011, -13.8223,
                                   0.399508, -0.80803, -0.433019, -19.6602,
                                   0., 0., 0., 1.
        };
        check_matrix(expected, *output);
    }

    CPPUNIT_ASSERT_NO_THROW(srv->stop().get());

    sight::service::remove(srv);
}

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

void concatenate_matrices_test::auto_connect()
{
    auto srv = sight::service::add("sight::module::geometry::concatenate_matrices");
    CPPUNIT_ASSERT(srv != nullptr);
    CPPUNIT_ASSERT(srv->is_a("sight::module::geometry::concatenate_matrices"));

    std::stringstream config_string;
    config_string
    << "<in group=\"matrix\">"
       "     <key uid=\"m1\" auto_connect=\"false\"/>"
       "     <key uid=\"m2\" />"
       "</in>";
    sight::service::base::config_t config;
    boost::property_tree::read_xml(config_string, config);
    srv->set_config(config);

    auto in1    = std::make_shared<sight::data::matrix4>();
    auto in2    = std::make_shared<sight::data::matrix4>();
    auto output = std::make_shared<sight::data::matrix4>();

    const matrix_t identity = sight::data::matrix4::identity();
    check_matrix(identity, *in1);
    check_matrix(identity, *in2);

    srv->set_input(in1, "matrix", false, false, 0);
    srv->set_input(in2, "matrix", true, false, 1);
    srv->set_inout(output, "output");

    srv->configure();
    srv->start().get();

    srv->update().get();
    check_matrix(identity, *output);

    {
        // Matrix 1 is not auto connected
        *in1 = {
            1., 0., 0., 4.,
            0., 1., 0., -5.,
            0., 0., 1., 2.,
            0., 0., 0., 1.
        };
        auto sig = in1->signal<sight::data::matrix4::modified_signal_t>(sight::data::matrix4::MODIFIED_SIG);
        sig->emit();
        check_matrix(identity, *output);

        // Thus we require an update
        srv->update().get();
        check_matrix(in1->get_content(), *output);
    }

    {
        // Matrix 2 is auto connected
        *in2 = {1., 0., 0., 0.,
                0., -0.866, -0.5, 0.,
                0., 0.5, -0.866, 0.,
                0., 0., 0., 1.
        };
        auto sig = in2->signal<sight::data::matrix4::modified_signal_t>(sight::data::matrix4::MODIFIED_SIG);
        sig->emit();

        const matrix_t expected = {
            1., 0., 0., 4.,
            0., -0.866, -0.5, -5.,
            0., 0.5, -0.866, 2.,
            0., 0., 0., 1.
        };
        check_matrix(expected, *output);
    }

    CPPUNIT_ASSERT_NO_THROW(srv->stop().get());

    sight::service::remove(srv);
}

} // namespace sight::module::geometry::ut