-
Notifications
You must be signed in to change notification settings - Fork 260
/
tools.cpp
82 lines (63 loc) · 1.94 KB
/
tools.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
#include <iostream>
#include "tools.h"
using Eigen::VectorXd;
using Eigen::MatrixXd;
using std::vector;
Tools::Tools() {}
Tools::~Tools() {}
VectorXd Tools::CalculateRMSE(const vector<VectorXd> &estimations,
const vector<VectorXd> &ground_truth) {
/**
* Calculate the RMSE here.
*/
VectorXd rmse(4);
rmse << 0,0,0,0;
// check the validity of the following inputs:
// * the estimation vector size should not be zero
// * the estimation vector size should equal ground truth vector size
if(estimations.size() != ground_truth.size() || estimations.size() == 0){
std::cout << "Invalid estimation or ground_truth data" << std::endl;
return rmse;
}
//accumulate squared residuals
for(unsigned int i=0; i < estimations.size(); ++i){
VectorXd residual = estimations[i] - ground_truth[i];
//coefficient-wise multiplication
residual = residual.array()*residual.array();
rmse += residual;
}
//calculate the mean
rmse = rmse/estimations.size();
//calculate the squared root
rmse = rmse.array().sqrt();
//return the result
return rmse;
}
MatrixXd Tools::CalculateJacobian(const VectorXd& x_state) {
/**
* Calculate a Jacobian here.
*/
MatrixXd Hj(3,4);
Hj << 0,0,0,0,
0,0,0,0,
0,0,0,0;
//recover state parameters
float px = x_state(0);
float py = x_state(1);
float vx = x_state(2);
float vy = x_state(3);
//pre-compute a set of terms to avoid repeated calculation
float c1 = px*px+py*py;
float c2 = sqrt(c1);
float c3 = (c1*c2);
//check division by zero
if(fabs(c1) < 0.0001){
std::cout << "Function CalculateJacobian() has Error: Division by Zero" << std::endl;
return Hj;
}
//compute the Jacobian matrix
Hj << (px/c2), (py/c2), 0, 0,
-(py/c1), (px/c1), 0, 0,
py*(vx*py - vy*px)/c3, px*(px*vy - py*vx)/c3, px/c2, py/c2;
return Hj;
}