File: pose_from2d.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 (303 lines) | stat: -rw-r--r-- 10,163 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
/************************************************************************
 *
 * Copyright (C) 2017-2025 IRCAD France
 * Copyright (C) 2017-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 "pose_from2d.hpp"

#include <core/com/signal.hxx>
#include <core/com/slots.hxx>

#include <geometry/vision/helper.hpp>

#include <io/opencv/camera.hpp>

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

namespace sight::module::geometry::vision
{

static const std::string UPDATE_CAMERA_SLOT = "updateCamera";

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

pose_from2d::pose_from2d() noexcept
{
    new_slot(UPDATE_CAMERA_SLOT, &pose_from2d::initialize, this);
}

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

void pose_from2d::configuring()
{
    service::config_t config = this->get_config();
    m_pattern_width = config.get<double>("pattern_width", m_pattern_width);
    SIGHT_ASSERT("pattern_width setting is set to " << m_pattern_width << " but should be > 0.", m_pattern_width > 0);

    auto inout_cfg = config.equal_range("inout");
    for(auto it_cfg = inout_cfg.first ; it_cfg != inout_cfg.second ; ++it_cfg)
    {
        const auto group = it_cfg->second.get<std::string>("<xmlattr>.group");
        if(group == MATRIX_INOUT)
        {
            auto key_cfg = it_cfg->second.equal_range("key");
            for(auto it_key_cfg = key_cfg.first ; it_key_cfg != key_cfg.second ; ++it_key_cfg)
            {
                const auto key = it_key_cfg->second.get<std::string>("<xmlattr>.id");
                m_matrices_tag.push_back(key);
            }

            break;
        }
    }
}

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

void pose_from2d::starting()
{
    //3D Points
    const float half_width = static_cast<float>(m_pattern_width) * .5F;

    m_3d_model.emplace_back(-half_width, half_width, 0.F);
    m_3d_model.emplace_back(half_width, half_width, 0.F);
    m_3d_model.emplace_back(half_width, -half_width, 0.F);
    m_3d_model.emplace_back(-half_width, -half_width, 0.F);

    auto pl = m_point_list.lock();
    if(pl)
    {
        for(std::size_t i = 0 ; i < m_3d_model.size() ; ++i)
        {
            const cv::Point3f cv_point    = m_3d_model.at(i);
            const data::point::sptr point = std::make_shared<data::point>(cv_point.x, cv_point.y, cv_point.z);
            point->set_label(std::to_string(i));
            pl->push_back(point);
        }

        auto sig = pl->signal<data::object::modified_signal_t>(data::object::MODIFIED_SIG);
        sig->async_emit();
    }

    this->initialize();
}

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

void pose_from2d::stopping()
{
    m_cameras.clear();
    m_3d_model.clear();
    m_last_timestamp = 0;

    auto pl = m_point_list.lock();
    if(pl)
    {
        pl->clear();
        auto sig = pl->signal<data::object::modified_signal_t>(data::object::MODIFIED_SIG);
        sig->async_emit();
    }
}

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

void pose_from2d::updating()
{
    // When working with a frame (newest design), we do not rely on the timetamp
    // So we can just send the current one.
    // When removing timelines from the service then we could get rid of it
    auto timestamp = core::clock::get_time_in_milli_sec();
    this->compute_registration(timestamp);
}

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

void pose_from2d::compute_registration(core::clock::type /*timestamp*/)
{
    SIGHT_WARN_IF("Invoking computeRegistration while service is STOPPED", this->stopped());

    if(this->started())
    {
        // For each marker
        unsigned int marker_index = 0;
        for(const auto& marker_key : m_matrices_tag)
        {
            std::vector<marker> markers;

            // For each camera timeline
            for(const auto& marker_map : m_marker_map)
            {
                const auto marker_ptr = marker_map.second->lock();
                const auto* marker    = marker_ptr->get_marker(marker_key);

                if(marker != nullptr)
                {
                    struct marker current_marker;
                    for(std::size_t i = 0 ; i < 4 ; ++i)
                    {
                        current_marker.corners_2d.emplace_back((*marker)[i][0], (*marker)[i][1]);
                    }

                    markers.push_back(current_marker);
                }
            }

            auto matrix = m_matrix[marker_index].lock();
            SIGHT_ASSERT("Matrix " << marker_index << " not found", matrix);

            if(markers.empty())
            {
                SIGHT_WARN("No Markers!")
            }
            else
            {
                cv::Matx44f rt;
                if(markers.size() == 1)
                {
                    rt = this->camera_pose_from_mono(markers[0]);
                }
                else if(markers.size() == 2)
                {
                    rt = this->camera_pose_from_stereo(markers[0], markers[1]);
                }
                else
                {
                    SIGHT_WARN("More than 2 cameras is not handle for the moment");
                    continue;
                }

                for(std::uint8_t i = 0 ; i < 4 ; ++i)
                {
                    for(std::uint8_t j = 0 ; j < 4 ; ++j)
                    {
                        (*matrix)[4 * i + j] = rt(i, j);
                    }
                }
            }

            // Always send the signal even if we did not find anything.
            // This allows to keep updating the whole processing pipeline.
            auto sig = matrix->signal<data::object::modified_signal_t>(data::object::MODIFIED_SIG);
            sig->async_emit();

            ++marker_index;
        }
    }
}

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

void pose_from2d::initialize()
{
    m_cameras.clear();
    for(std::size_t idx = 0 ; idx < m_camera.size() ; ++idx)
    {
        auto camera = m_camera[idx].lock();
        SIGHT_FATAL_IF("Camera[" << idx << "] not found", !camera);

        struct camera cam;
        std::tie(
            cam.intrinsic_mat,
            cam.image_size,
            cam.dist_coef
        ) = io::opencv::camera::copy_to_cv(camera.get_shared());

        // set extrinsic matrix only if stereo.
        if(idx == 1)
        {
            auto extrinsic_matrix = m_extrinsic.lock();
            SIGHT_FATAL_IF("Extrinsic matrix with key '" << EXTRINSIC_INPUT << "' not found", !extrinsic_matrix);

            m_extrinsic_mat.matrix4x4   = cv::Mat::eye(4, 4, CV_64F);
            m_extrinsic_mat.rotation    = cv::Mat::eye(3, 3, CV_64F);
            m_extrinsic_mat.translation = cv::Mat::eye(3, 1, CV_64F);

            for(std::uint8_t i = 0 ; i < 3 ; ++i)
            {
                for(std::uint8_t j = 0 ; j < 3 ; ++j)
                {
                    m_extrinsic_mat.rotation.at<double>(i, j)  = (*extrinsic_matrix)(i, j);
                    m_extrinsic_mat.matrix4x4.at<double>(i, j) = (*extrinsic_matrix)(i, j);
                }
            }

            m_extrinsic_mat.translation.at<double>(0, 0) = (*extrinsic_matrix)(0, 3);
            m_extrinsic_mat.translation.at<double>(1, 0) = (*extrinsic_matrix)(1, 3);
            m_extrinsic_mat.translation.at<double>(2, 0) = (*extrinsic_matrix)(2, 3);

            m_extrinsic_mat.matrix4x4.at<double>(0, 3) = (*extrinsic_matrix)(0, 3);
            m_extrinsic_mat.matrix4x4.at<double>(1, 3) = (*extrinsic_matrix)(1, 3);
            m_extrinsic_mat.matrix4x4.at<double>(2, 3) = (*extrinsic_matrix)(2, 3);
        }

        m_cameras.push_back(cam);
    }
}

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

cv::Matx44f pose_from2d::camera_pose_from_stereo(
    const pose_from2d::marker& _marker_cam1,
    const pose_from2d::marker& _marker_cam2
) const
{
    cv::Matx44f pose = sight::geometry::vision::helper::camera_pose_stereo(
        m_3d_model,
        m_cameras[0].intrinsic_mat,
        m_cameras[0].dist_coef,
        m_cameras[1].intrinsic_mat,
        m_cameras[1].dist_coef,
        _marker_cam1.corners_2d,
        _marker_cam2.corners_2d,
        m_extrinsic_mat.rotation,
        m_extrinsic_mat.translation
    );

    return pose;
}

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

cv::Matx44f pose_from2d::camera_pose_from_mono(const pose_from2d::marker& _marker_cam1) const
{
    cv::Matx44f pose =
        sight::geometry::vision::helper::camera_pose_monocular(
            m_3d_model,
            _marker_cam1.corners_2d,
            m_cameras[0].intrinsic_mat,
            m_cameras[0].dist_coef
        );
    return pose;
}

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

service::connections_t pose_from2d::auto_connections() const
{
    return {
        {marker_map_INPUT, data::object::MODIFIED_SIG, service::slots::UPDATE},
        {CAMERA_INPUT, data::object::MODIFIED_SIG, UPDATE_CAMERA_SLOT},
        {CAMERA_INPUT, data::camera::INTRINSIC_CALIBRATED_SIG, UPDATE_CAMERA_SLOT}
    };
}

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

} // namespace sight::module::geometry::vision