-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformation.cpp
186 lines (153 loc) · 5.2 KB
/
transformation.cpp
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
/*
* three-point-calibration
* Copyright (c) 2020 Peter Nebe (mail@peter-nebe.dev)
*
* This file is part of three-point-calibration.
*
* three-point-calibration is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* three-point-calibration 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with three-point-calibration. If not, see <https://www.gnu.org/licenses/>.
*/
#include "transformation.h"
#include "boost/qvm/vec_mat_operations.hpp"
#include "boost/qvm/mat_operations.hpp"
#include "boost/qvm/map_mat_vec.hpp"
#include "boost/qvm/map_mat_mat.hpp"
#include <tuple>
using namespace boost::qvm;
namespace threePointCalibration
{
namespace
{
template<class T>
typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
almost_equal(T x, T y, int ulp = 2)
{
const auto abs = std::fabs(x - y);
// the machine epsilon has to be scaled to the magnitude of the values used
// and multiplied by the desired precision in ULPs (units in the last place)
return abs <= std::numeric_limits<T>::epsilon() * std::fabs(x + y) * ulp
// unless the result is subnormal
|| abs < std::numeric_limits<T>::min();
}
template<int Dim>
bool isMagOne(const Vector_<Dim> &v)
{
return almost_equal(mag(v), 1.0);
}
template<int Dim>
bool isDotZero(const Vector_<Dim> &a, const Vector_<Dim> &b)
{
return std::fabs(dot(a, b)) < 1e-15;
}
template<int Dim>
bool isDetOne(const Matrix_<Dim> &m)
{
return almost_equal(determinant(m), 1.0);
}
Matrix_<2> makeRotation(const ReferencePoints_<2> &rp, const ReferencePoints_<2> &rpMapping)
{
auto normalizedVector = [](const ReferencePoints_<2> &rp)
{
const Vector_<2> v = normalized(rp[1] - rp[0]);
return std::make_tuple(v.x, v.y);
};
const auto [dx, dy] = normalizedVector(rp);
const auto [dxm, dym] = normalizedVector(rpMapping);
const auto xBaseX = dx * dxm + dy * dym;
const auto xBaseY = dy * dxm - dx * dym;
const Vector_<2> xBase{ xBaseX, xBaseY };
const Vector_<2> yBase{ -xBaseY, xBaseX };
assert(isMagOne(xBase));
assert(isMagOne(yBase));
assert(isDotZero(xBase, yBase));
Matrix_<2> rot;
col<0>(rot) = xBase;
col<1>(rot) = yBase;
assert(isDetOne(rot));
return rot;
}
} // namespace
template<>
Transformation_<2>::Transformation_(const RefPoints &rp, const RefPoints &rpMapping)
{
// set rotation matrix
_a = makeRotation(rp, rpMapping);
// rotation matrix: inverse = transpose
_aInv = transposed(_a);
assert(isDetOne(_aInv));
// set translation vector
_b = rp.front() - _a * rpMapping.front();
}
template<>
Transformation_<3>::Transformation_(const RefPoints &triangleInPlane)
{
// calculate orientation of the plane in mapping coordinates
const Vec p = triangleInPlane[0];
const Vec u = triangleInPlane[1] - p;
const Vec v = triangleInPlane[2] - p;
const Vec n0 = normalized(cross(u, v));
assert(isMagOne(n0));
const Vec zBase = -n0;
// dot(yBase, zBase) = 0
// ybx zbx + yby zby + ybz zbz = 0
const Coordinate_t ybx = 0;
const Coordinate_t ybz = 1;
// yby zby + zbz = 0
// yby zby = -zbz
// yby = -zbz / zby
const Coordinate_t yby = -zBase.z / zBase.y;
const Vec yBase = normalized(Vec{ ybx, yby, ybz });
const Vec xBase = cross(yBase, zBase);
assert(isMagOne(xBase));
assert(isMagOne(yBase));
assert(isMagOne(zBase));
assert(isDotZero(xBase, yBase));
assert(isDotZero(yBase, zBase));
assert(isDotZero(zBase, xBase));
Mat rot;
col<0>(rot) = xBase;
col<1>(rot) = yBase;
col<2>(rot) = zBase;
// set inverse rotation matrix
_aInv = rot;
// rotation matrix: inverse = transpose
_a = transposed(_aInv);
assert(isDetOne(_a));
assert(isDetOne(_aInv));
const Coordinate_t distFromOrigin = dot(p, n0);
assert(distFromOrigin > 0);
// set translation vector
_b = Vec{ 0, 0, distFromOrigin };
}
template<>
Transformation_<3>::Transformation_(const RefPoints &rp, const RefPoints &rpMapping)
{
auto makeLinearTransformation = [](const RefPoints &threePoints)
{
const Vec p = threePoints[0];
const Vec u = threePoints[1] - p;
const Vec v = threePoints[2] - p;
Mat m;
col<0>(m) = u;
col<1>(m) = v;
col<2>(m) = cross(u, v);
return m;
};
const Mat m = makeLinearTransformation(rp);
const Mat mm = makeLinearTransformation(rpMapping);
_a = m * inverse(mm);
_aInv = inverse(_a);
// set translation vector
_b = rp.front() - _a * rpMapping.front();
}
} /* namespace threePointCalibration */