g2o
se3quat_gradients.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
28 
29 namespace g2o {
30 namespace deprecated {
31 
33 Eigen::Quaterniond euler_to_quat(double yaw, double pitch, double roll)
34 {
35  double sy = sin(yaw*0.5);
36  double cy = cos(yaw*0.5);
37  double sp = sin(pitch*0.5);
38  double cp = cos(pitch*0.5);
39  double sr = sin(roll*0.5);
40  double cr = cos(roll*0.5);
41  double w = cr*cp*cy + sr*sp*sy;
42  double x = sr*cp*cy - cr*sp*sy;
43  double y = cr*sp*cy + sr*cp*sy;
44  double z = cr*cp*sy - sr*sp*cy;
45  return Eigen::Quaterniond(w,x,y,z);
46 }
47 
48 void quat_to_euler(const Eigen::Quaterniond& q, double& yaw, double& pitch, double& roll)
49 {
50  const double& q0 = q.w();
51  const double& q1 = q.x();
52  const double& q2 = q.y();
53  const double& q3 = q.z();
54  roll = atan2(2*(q0*q1+q2*q3), 1-2*(q1*q1+q2*q2));
55  pitch = asin(2*(q0*q2-q3*q1));
56  yaw = atan2(2*(q0*q3+q1*q2), 1-2*(q2*q2+q3*q3));
57 }
58 
59 void jac_quat3_euler3(Eigen::Matrix<double, 6, 6>& J, const SE3Quat& t)
60 {
61  const Eigen::Vector3d& tr0 = t.translation();
62  const Eigen::Quaterniond& q0 = t.rotation();
63 
64  double delta=1e-6;
65  double idelta= 1. / (2. * delta);
66 
67  for (int i=0; i<6; i++){
68  SE3Quat ta, tb;
69  if (i<3){
70  Eigen::Vector3d tra=tr0;
71  Eigen::Vector3d trb=tr0;
72  tra[i] -= delta;
73  trb[i] += delta;
74  ta = SE3Quat(q0, tra);
75  tb = SE3Quat(q0, trb);
76  } else {
77  Eigen::Quaterniond qa=q0;
78  Eigen::Quaterniond qb=q0;
79  if (i == 3) {
80  qa.x() -= delta;
81  qb.x() += delta;
82  }
83  else if (i == 4) {
84  qa.y() -= delta;
85  qb.y() += delta;
86  }
87  else if (i == 5) {
88  qa.z() -= delta;
89  qb.z() += delta;
90  }
91  qa.normalize();
92  qb.normalize();
93  ta = SE3Quat(qa, tr0);
94  tb = SE3Quat(qb, tr0);
95  }
96 
97  Eigen::Vector3d dtr = (tb.translation() - ta.translation())*idelta;
98  Eigen::Vector3d taAngles, tbAngles;
99  quat_to_euler(ta.rotation(), taAngles(2), taAngles(1), taAngles(0));
100  quat_to_euler(tb.rotation(), tbAngles(2), tbAngles(1), tbAngles(0));
101  Eigen::Vector3d da = (tbAngles - taAngles) * idelta; //TODO wraparounds not handled
102 
103  for (int j=0; j<6; j++){
104  if (j<3){
105  J(j, i) = dtr(j);
106  } else {
107  J(j, i) = da(j-3);
108  }
109  }
110  }
111 }
112 
113 } // end namespace
114 } // end namespace
void quat_to_euler(const Eigen::Quaterniond &q, double &yaw, double &pitch, double &roll)
const Eigen::Quaterniond & rotation() const
Definition: se3quat.h:97
void jac_quat3_euler3(Eigen::Matrix< double, 6, 6 > &J, const SE3Quat &t)
Eigen::Quaterniond euler_to_quat(double yaw, double pitch, double roll)
const Vector3D & translation() const
Definition: se3quat.h:93