File: camera_set_test.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (175 lines) | stat: -rw-r--r-- 6,083 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
/************************************************************************
 *
 * Copyright (C) 2014-2024 IRCAD France
 * Copyright (C) 2014-2020 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 "camera_set_test.hpp"

#include <core/spy_log.hpp>

#include <data/camera.hpp>
#include <data/camera_set.hpp>

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

namespace sight::data::ut
{

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

void camera_set_test::setUp()
{
}

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

void camera_set_test::tearDown()
{
}

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

camera_set::sptr init_camera_set()
{
    auto camera_set = std::make_shared<data::camera_set>();

    // --------------- Camera 1 ----------------------
    auto camera1 = std::make_shared<camera>();
    camera1->set_cx(3.5);
    camera1->set_cy(9.3);
    camera1->set_fx(48.2);
    camera1->set_fy(7.3);
    camera1->set_skew(1.9);
    camera1->set_distortion_coefficient(1.1, 2.2, 3.3, 4.4, 5.5);
    camera_set->add_camera(camera1);

    // --------------- Camera 2 ----------------------
    auto camera2 = std::make_shared<camera>();
    camera2->set_cx(86.5);
    camera2->set_cy(543.);
    camera2->set_fx(4.4);
    camera2->set_fy(5.14);
    camera2->set_skew(0.19);
    camera2->set_distortion_coefficient(2.4, 5.1, 66., 4.1, 6.4);
    camera_set->add_camera(camera2);

    // --------------- Extrinsic matrix ----------------------
    auto mat = std::make_shared<matrix4>();
    for(std::size_t i = 0 ; i < 4 ; ++i)
    {
        for(std::size_t j = 0 ; j < 4 ; ++j)
        {
            const auto value = static_cast<data::matrix4::value_type>(2 * i + j);
            (*mat)(i, j) = value;
        }
    }

    camera_set->set_extrinsic_matrix(1, mat);
    camera_set->set_calibration_error(0.123);

    return camera_set;
}

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

void camera_set_test::camera_test()
{
    auto camera_set = std::make_shared<data::camera_set>();
    CPPUNIT_ASSERT(camera_set);

    auto identity = std::make_shared<matrix4>();
    auto mat      = std::make_shared<matrix4>();
    for(std::size_t i = 0 ; i < 4 ; ++i)
    {
        for(std::size_t j = 0 ; j < 4 ; ++j)
        {
            const auto value = static_cast<data::matrix4::value_type>(2 * i + j);
            (*mat)(i, j) = value;
        }
    }

    auto camera1 = std::make_shared<camera>();
    auto camera2 = std::make_shared<camera>();
    auto camera3 = std::make_shared<camera>();

    CPPUNIT_ASSERT_NO_THROW(camera_set->add_camera(camera1));
    CPPUNIT_ASSERT_NO_THROW(camera_set->add_camera(camera2));
    CPPUNIT_ASSERT_THROW(camera_set->add_camera(camera2), core::exception);

    CPPUNIT_ASSERT(camera_set->get_extrinsic_matrix(0));
    CPPUNIT_ASSERT(*identity == *camera_set->get_extrinsic_matrix(0));
    CPPUNIT_ASSERT(!camera_set->get_extrinsic_matrix(1));
    CPPUNIT_ASSERT_NO_THROW(camera_set->set_extrinsic_matrix(1, mat));
    CPPUNIT_ASSERT_THROW(camera_set->set_extrinsic_matrix(2, mat), std::out_of_range);
    CPPUNIT_ASSERT_THROW(camera_set->get_extrinsic_matrix(2), std::out_of_range);
    CPPUNIT_ASSERT(camera_set->get_extrinsic_matrix(1) == mat);

    CPPUNIT_ASSERT_NO_THROW(camera_set->set_calibration_error(0.4587));
    CPPUNIT_ASSERT_EQUAL(0.4587, camera_set->calibration_error());

    CPPUNIT_ASSERT_EQUAL(std::size_t(2), camera_set->size());

    CPPUNIT_ASSERT(camera_set->get_camera(0) == camera1);
    CPPUNIT_ASSERT(camera_set->get_camera(1) == camera2);
    CPPUNIT_ASSERT_THROW(camera_set->get_camera(2), std::out_of_range);

    CPPUNIT_ASSERT_NO_THROW(camera_set->add_camera(camera3));
    CPPUNIT_ASSERT(camera_set->get_camera(2) == camera3);

    CPPUNIT_ASSERT_NO_THROW(camera_set->remove_camera(camera1));
    CPPUNIT_ASSERT_EQUAL(std::size_t(2), camera_set->size());
    CPPUNIT_ASSERT(camera_set->get_camera(0) == camera2);
    CPPUNIT_ASSERT_THROW(camera_set->remove_camera(camera1), core::exception);

    CPPUNIT_ASSERT_NO_THROW(camera_set->remove_camera(camera2));
    CPPUNIT_ASSERT_EQUAL(std::size_t(1), camera_set->size());
    CPPUNIT_ASSERT(camera_set->get_camera(0) == camera3);
    CPPUNIT_ASSERT_NO_THROW(camera_set->remove_camera(camera3));

    CPPUNIT_ASSERT_EQUAL(std::size_t(0), camera_set->size());
}

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

void camera_set_test::shallow_copy_test()
{
    auto camera_set  = init_camera_set();
    auto camera_set2 = std::make_shared<data::camera_set>();

    camera_set2->shallow_copy(camera_set);

    CPPUNIT_ASSERT_EQUAL(camera_set->size(), camera_set2->size());
    CPPUNIT_ASSERT_EQUAL(camera_set->get_camera(0), camera_set2->get_camera(0));
    CPPUNIT_ASSERT_EQUAL(camera_set->get_camera(1), camera_set2->get_camera(1));
}

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

void camera_set_test::deep_copy_test()
{
    camera_set::sptr camera_set = init_camera_set();
    camera_set::sptr camera_set2;
    camera_set2 = data::object::copy<data::camera_set>(camera_set);

    CPPUNIT_ASSERT(*camera_set == *camera_set2);
}

} // namespace sight::data::ut