File: matrix.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 (195 lines) | stat: -rw-r--r-- 5,800 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
/************************************************************************
 *
 * Copyright (C) 2018-2023 IRCAD France
 * Copyright (C) 2018 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 "matrix.hpp"

#include <opencv2/calib3d.hpp>

namespace sight::io::opencv
{

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

void matrix::copy_from_cv(const cv::Matx44d& _src, data::matrix4::sptr& _dst)
{
    SIGHT_ASSERT("sight::data::matrix4 is null", _dst);

    for(std::uint8_t i = 0 ; i < 4 ; ++i)
    {
        for(std::uint8_t j = 0 ; j < 4 ; ++j)
        {
            (*_dst)(i, j) = _src(i, j);
        }
    }
}

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

void matrix::copy_to_cv(const data::matrix4::csptr& _src, cv::Matx44d& _dst)
{
    SIGHT_ASSERT("sight::data::matrix4 is null", _src);

    for(std::uint8_t i = 0 ; i < 4 ; ++i)
    {
        for(std::uint8_t j = 0 ; j < 4 ; ++j)
        {
            _dst(i, j) = (*_src)(i, j);
        }
    }
}

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

void matrix::copy_from_cv(const cv::Matx44f& _src, data::matrix4::sptr& _dst)
{
    SIGHT_ASSERT("sight::data::matrix4 is null", _dst);

    for(std::uint8_t i = 0 ; i < 4 ; ++i)
    {
        for(std::uint8_t j = 0 ; j < 4 ; ++j)
        {
            (*_dst)(i, j) = static_cast<double>(_src(i, j));
        }
    }
}

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

void matrix::copy_to_cv(const data::matrix4::csptr& _src, cv::Matx44f& _dst)
{
    SIGHT_ASSERT("sight::data::matrix4 is null", _src);

    for(std::uint8_t i = 0 ; i < 4 ; ++i)
    {
        for(std::uint8_t j = 0 ; j < 4 ; ++j)
        {
            _dst(i, j) = static_cast<float>((*_src)(i, j));
        }
    }
}

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

void matrix::copy_from_cv(const cv::Mat& _rvec, const cv::Mat& _tvec, data::matrix4::sptr& _dst)
{
    // Check validity of _rvec & _tvec.
    SIGHT_ASSERT("Rotation vector 'rvec' should be of size 1x3", _rvec.size().width == 1 && _rvec.size().height == 3);
    SIGHT_ASSERT("Rotation vector 'tvec' should be of size 1x3", _tvec.size().width == 1 && _tvec.size().height == 3);
    SIGHT_ASSERT(
        "Could not convert opencv matrix with multi-channels",
        _rvec.channels() == 1 && _tvec.channels() == 1
    );
    // Check that _dst has correctly been initialized.
    SIGHT_ASSERT("sight::data::matrix4 is null", _dst);

    SIGHT_WARN_IF(
        "OpenCV matrices has non-double type but will be cast in 'double'.",
        _rvec.type() != CV_64F || _tvec.type() != CV_64F
    );

    cv::Mat d_rvec;
    cv::Mat d_tvec;
    _rvec.convertTo(d_rvec, CV_64F);
    _tvec.convertTo(d_tvec, CV_64F);

    cv::Mat r_mat;
    cv::Rodrigues(d_rvec, r_mat);

    for(std::uint8_t i = 0 ; i < 3 ; ++i)
    {
        for(std::uint8_t j = 0 ; j < 3 ; ++j)
        {
            (*_dst)(i, j) = r_mat.at<double>(i, j);
        }

        (*_dst)(i, 3) = d_tvec.at<double>(i);
    }
}

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

void matrix::copy_to_cv(const data::matrix4::csptr& _src, cv::Mat& _rvec, cv::Mat& _tvec)
{
    // Check that _src is not null.
    SIGHT_ASSERT("sight::data::matrix4 is null", _src);

    cv::Mat mat4x4;
    // reinitialize _R & _T.
    _rvec = cv::Mat(1, 3, CV_64F);
    _tvec = cv::Mat(1, 3, CV_64F);

    // First convert Sight to OpenCV 4x4 matrix.
    copy_to_cv(_src, mat4x4);

    // Extract translation (_tvec) from mat4x4.
    _tvec = mat4x4(cv::Rect(3, 0, 1, 3));

    // Convert to rotation vector.
    cv::Rodrigues(mat4x4(cv::Rect(0, 0, 3, 3)), _rvec);
}

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

void matrix::copy_from_cv(const cv::Mat& _src, data::matrix4::sptr& _dst)
{
    SIGHT_ASSERT("Size of OpenCV matrix should be 4x4", _src.cols == 4 && _src.rows == 4);
    SIGHT_ASSERT("Could not convert opencv matrix with multi-channels", _src.channels() == 1);
    SIGHT_ASSERT("sight::data::matrix4 is null", _dst);

    SIGHT_WARN_IF(
        "OpenCV matrix has non-double type but will be cast in 'double'.",
        _src.type() != CV_64F
    );

    cv::Mat dmat;
    _src.convertTo(dmat, CV_64F);

    for(std::uint8_t i = 0 ; i < 4 ; ++i)
    {
        for(std::uint8_t j = 0 ; j < 4 ; ++j)
        {
            (*_dst)(i, j) = dmat.at<double>(i, j);
        }
    }
}

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

void matrix::copy_to_cv(const data::matrix4::csptr& _src, cv::Mat& _dst)
{
    SIGHT_ASSERT("sight::data::matrix4 is null", _src);

    // Reinitialize _dst.
    _dst = cv::Mat(4, 4, CV_64F);

    for(std::uint8_t i = 0 ; i < 4 ; ++i)
    {
        for(std::uint8_t j = 0 ; j < 4 ; ++j)
        {
            _dst.at<double>(i, j) = (*_src)(i, j);
        }
    }
}

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

} //namespace sight::io::opencv