-
Notifications
You must be signed in to change notification settings - Fork 14
/
GaussianProcessPriorPose2.h
149 lines (119 loc) · 4.43 KB
/
GaussianProcessPriorPose2.h
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
/* ----------------------------------------------------------------------------
A new factor of GTSAM 3.2
https://collab.cc.gatech.edu/borg/gtsam/
* -------------------------------------------------------------------------- */
/**
* @file GaussianProcessPriorPose2.H
* @author Xinyan Yan
**/
#pragma once
#include <ostream>
#include <boost/lexical_cast.hpp>
#include <gtsam/geometry/concepts.h>
#include <gtsam/nonlinear/NonlinearFactor.h>
#include <gtsam/base/Testable.h>
#include <gtsam/base/Lie.h>
namespace gtsam {
/**
* 4-way factor for Gaussian Process
* @addtogroup SLAM
*/
template<class POSE, class VELOCITY>
class GaussianProcessPriorPose2: public NoiseModelFactor4<POSE, VELOCITY, POSE, VELOCITY> {
private:
double delta_t_;
Vector measured_;
typedef GaussianProcessPriorPose2<POSE, VELOCITY> This;
typedef NoiseModelFactor4<POSE, VELOCITY, POSE, VELOCITY> Base;
public:
GaussianProcessPriorPose2() {} /* Default constructor */
// delta_t is the time between the two states
GaussianProcessPriorPose2(Key poseKey1, Key velKey1, Key poseKey2, Key velKey2, double delta_t, const SharedNoiseModel& model) :
Base(model, poseKey1, velKey1, poseKey2, velKey2) {
delta_t_ = delta_t;
measured_ = (Vector(6) << 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
}
virtual ~GaussianProcessPriorPose2() {}
/// @return a deep copy of this factor
virtual gtsam::NonlinearFactor::shared_ptr clone() const {
return boost::static_pointer_cast<gtsam::NonlinearFactor>(
gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
/** h(x)-z */
Vector evaluateError(const POSE& pose1, const VELOCITY& vel1, const POSE& pose2, const VELOCITY& vel2,
boost::optional< Matrix & > H1 = boost::none, boost::optional< Matrix & > H2 = boost::none,
boost::optional< Matrix & > H3 = boost::none, boost::optional< Matrix & > H4 = boost::none ) const {
Vector x = (Vector(6) << pose1(0), pose1(1), pose1(2), vel1(0), vel1(1), vel1(2));
Vector y = (Vector(6) << pose2(0), pose2(1), pose2(2), vel2(0), vel2(1), vel2(2));
// Transition function
Matrix phi = (Matrix(6,6) << eye(3), delta_t_ * eye(3),
zeros(3,3), eye(3));
//#if 0
// Compute Jacobian
if (H1) {
*H1 = (Matrix(6,3) << eye(3),
zeros(3,3));
*H2 = (Matrix(6,3) << delta_t_ * eye(3),
eye(3));
*H3 = (Matrix(6,3) << -1.0 * eye(3),
zeros(3,3));
*H4 = (Matrix(6,3) << zeros(3,3),
-1.0 * eye(3));
}
//#endif
#if 0
if (H1) {
*H1 = (Matrix(3,3) << eye(3));
*H2 = (Matrix(3,3) << eye(3));
*H3 = (Matrix(3,3) << -1.0 * eye(3));
*H4 = (Matrix(3,3) << zeros(3, 3));
}
#endif
#if 0
// compute error
std::cout << "********************************************" << std::endl;
std::cout << "In Gaussian Process evaluateError: " << std::endl;
std::cout << "x:\n " << x << std::endl;
std::cout << "y:\n " << y << std::endl;
std::cout << "phi:\n " << phi << std::endl;
std::cout << "error:\n " << phi*x-y << std::endl;
// std::cout << "error:\n " << x.head(3) - y.head(3) << std::endl;
if (H1) {
std::cout << "H1:\n " << *H1 << std::endl;
std::cout << "H2:\n " << *H2 << std::endl;
std::cout << "H3:\n " << *H3 << std::endl;
std::cout << "H4:\n " << *H4 << std::endl;
}
#endif
return phi*x-y;
// return x.head(3) - y.head(3);
}
/** return the measured */
/** return the measured */
const Vector& measured() const {
return measured_;
}
/** number of variables attached to this factor */
std::size_t size() const {
return 4;
}
/** equals specialized to this factor */
virtual bool equals(const NonlinearFactor& expected, double tol=1e-9) const {
const This *e = dynamic_cast<const This*> (&expected);
return e != NULL && Base::equals(*e, tol) && fabs(this->delta_t_ - e->delta_t_) < tol;
}
/** print contents */
void print(const std::string& s="", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
std::cout << s << "4-way Gaussian Process Factor" << std::endl;
Base::print("", keyFormatter);
}
private:
/** Serialization function */
friend class boost::serialization::access;
template<class ARCHIVE>
void serialize(ARCHIVE & ar, const unsigned int version) {
ar & boost::serialization::make_nvp("NoiseModelFactor4",
boost::serialization::base_object<Base>(*this));
ar & BOOST_SERIALIZATION_NVP(measured_);
}
}; // GaussianProcessPriorPose2
} // namespace gtsam