-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmaths_utils.cpp
More file actions
139 lines (122 loc) · 3.35 KB
/
maths_utils.cpp
File metadata and controls
139 lines (122 loc) · 3.35 KB
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
/**
* @author Hauke Strasdat, Steven Lovegrove
*
* Copyright (C) 2010 Hauke Strasdat, Steven Lovegrove
* Imperial College London
*
* maths_utils.cpp is part of RobotVision.
*
* RobotVision 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 any later version.
*
* RobotVision 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 General Public License
* and the GNU Lesser General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <TooN/helpers.h>
#include <TooN/SVD.h>
#include <TooN/LU.h>
#include "maths_utils.h"
#include "sim3.h"
#include <cassert>
#include <limits>
using namespace std;
using namespace TooN;
namespace RobotVision
{
TooN::Vector<3> deltaR(const TooN::Matrix<3> & R)
{
return TooN::makeVector(R(2,1)-R(1,2),R(0,2)-R(2,0),R(1,0)-R(0,1));
}
Vector<3> trans2center(const SE3<double>& pose){
return pose.inverse().get_translation();
}
Vector<3> trans2center(const Sim3<double>& pose){
return pose.inverse().get_translation();
}
TooN::SO3<> Rotation(double yaw, double pitch, double roll )
{
const double cyaw = cos(yaw);
const double syaw = sin(yaw);
const double croll = cos(roll);
const double sroll = sin(roll);
const double cpitch = cos(pitch);
const double spitch = sin(pitch);
const Matrix<3,3> R = Data(
cyaw * croll,
syaw*spitch - cyaw*sroll*cpitch,
cyaw*sroll*spitch + syaw*cpitch,
sroll,
croll*cpitch,
-croll*spitch,
-syaw*croll,
syaw*sroll*cpitch + cyaw*spitch,
-syaw*sroll*spitch + cyaw*cpitch
);
return SO3<>(R);
}
double norm1(const Vector<> & v)
{
double sum = 0;
for (int i=0; i<v.size(); i++)
{
sum += fabs(v[i]);
}
return sum;
}
double norm_max(const Vector<> & v)
{
double max = -1;
for (int i=0; i<v.size(); i++)
{
double abs = fabs(v[i]);
if(abs>max){
max = abs;
}
}
return max;
}
double norm_max(const Vector<> & v, int & idx)
{
double max = -1;
idx = -1;
for (int i=0; i<v.size(); i++)
{
double abs = fabs(v[i]);
if(abs>max){
max = abs;
idx = i;
}
}
return max;
}
double depth(const SE3<double> & pose, const Vector<3> & XYZ)
{
Matrix<3,4> P;
P.slice(0,0,3,3) = pose.get_rotation().get_matrix();
P.T()[3] = pose.get_translation();
Matrix<3> M = pose.get_rotation().get_matrix();
SVD<> SVD_M(M);
Vector<4> p3 = P[2];
Vector<3> m3 = M[2];
double w = p3*unproject(XYZ);
return (sign(SVD_M.determinant())*w) / (norm(m3));
}
SE3<> CameraPose2(const Vector<3>& t_w,
const Vector<3>& lookat_w,
const Vector<3>& up_w )
{
TooN::Matrix<3,3> R_cw;
R_cw.T()[2] = unit((lookat_w - t_w));
R_cw.T()[0] = up_w ^ R_cw.T()[2];
R_cw.T()[1] = R_cw.T()[2] ^ R_cw.T()[0];
return SE3<>( SO3<>(R_cw.T()), -R_cw.T()*t_w);
}
}