g2o
line3d.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 
27 #include "line3d.h"
28 #include <iostream>
29 
30 namespace g2o {
31  using namespace std;
32  using namespace Eigen;
33 
34  static inline void _skew(Matrix3D& S, const Vector3D& t){
35  S <<
36  0, -t.z(), t.y(),
37  t.z(), 0, -t.x(),
38  -t.y() ,t.x(), 0;
39  }
40 
41  static inline Matrix3D _skew(const Vector3D& t){
42  Matrix3D S;
43  S <<
44  0, -t.z(), t.y(),
45  t.z(), 0, -t.x(),
46  -t.y(), t.x(), 0;
47  return S;
48  }
49 
50 
52  Vector6d cartesian;
53  cartesian.tail<3>() = d()/d().norm();
54  Matrix3D W=-_skew(d());
55  double damping = 1e-9;
56  Matrix3D A = W.transpose()*W+(Matrix3D::Identity()*damping);
57  cartesian.head<3>() = A.ldlt().solve(W.transpose()*w());
58  return cartesian;
59  }
60 
61  void Line3D::jacobian(Matrix7x6d& Jp, Matrix7x6d& Jl, const Isometry3D& x, const Line3D& l){
62  Jp.setZero();
63  Jl.setZero();
64  Matrix6d A=Matrix6d::Zero();
65  A.block<3,3>(0,0)=x.linear();
66  A.block<3,3>(0,3)= _skew(x.translation())*x.linear();
67  A.block<3,3>(3,3)=x.linear();
68  Vector6d v=(Vector6d)l;
69  Line3D lRemapped(v);
70 
71  Matrix6d D = Matrix6d::Zero();
72  D.block<3,3>(0,0) = -_skew(l.d());
73  D.block<3,3>(0,3) = -2*_skew(l.w());
74  D.block<3,3>(3,3) = -2*_skew(l.d());
75  Jp.block<6,6>(0,0) = A*D;
76 
77  Vector3D d = l.d();
78  Vector3D w = l.w();
79  double ln = d.norm();
80  double iln = 1./ln;
81  double iln3 = std::pow(iln,3);
82  Matrix6d Jll;
83  Jll <<
84  iln,0,0,-(w.x()*d.x())*iln3,-(w.x()*d.y())*iln3,-(w.x()*d.z())*iln3,
85  0,iln,0,-(w.y()*d.x())*iln3,-(w.y()*d.y())*iln3,-(w.y()*d.z())*iln3,
86  0,0,iln,-(w.z()*d.x())*iln3,-(w.z()*d.y())*iln3,-(w.z()*d.z())*iln3,
87  0,0,0,-(d.x()*d.x()-ln*ln)*iln3,-(d.x()*d.y())*iln3,-(d.x()*d.z())*iln3,
88  0,0,0,-(d.x()*d.y())*iln3,-(d.y()*d.y()-ln*ln)*iln3,-(d.y()*d.z())*iln3,
89  0,0,0,-(d.x()*d.z())*iln3,-(d.y()*d.z())*iln3,-(d.z()*d.z()-ln*ln)*iln3;
90  Jl.block<6,6>(0,0) = A*Jll;
91  Jl.block<1,6>(6,0) << 2*d.x(),2*d.y(),2*d.z();
92  }
93 
94  Line3D operator*(const Isometry3D& t, const Line3D& line){
95  Matrix6d A=Matrix6d::Zero();
96  A.block<3,3>(0,0)=t.linear();
97  A.block<3,3>(0,3)= _skew(t.translation())*t.linear();
98  A.block<3,3>(3,3)=t.linear();
99  Vector6d v = (Vector6d)line;
100  // cout << "v" << endl << v << endl;
101  // cout << "A" << endl << A << endl;
102  // cout << "Av" << endl << A*v << endl;
103  return Line3D(A*v);
104  }
105 
106  namespace internal
107  {
109  Vector6d l;
110  l.head<3>() = t*line.head<3>();
111  l.tail<3>() = t.linear()*line.tail<3>();
112  return normalizeCartesianLine(l);
113  }
114 
116  Vector3D p0 = line.head<3>();
117  Vector3D d0 = line.tail<3>();
118  d0.normalize();
119  p0-=d0*(d0.dot(p0));
120  Vector6d nl;
121  nl.head<3>()=p0;
122  nl.tail<3>()=d0;
123  return nl;
124  }
125  } // end namespace internal
126 
127 } // end namespace g2o
G2O_TYPES_SLAM3D_ADDONS_API Vector3D w() const
Definition: line3d.h:58
Eigen::Matrix< double, 6, 6, Eigen::ColMajor > Matrix6d
Eigen::Matrix< double, 3, 1, Eigen::ColMajor > Vector3D
Definition: eigen_types.h:46
G2O_TYPES_SLAM3D_ADDONS_API Vector3D d() const
Definition: line3d.h:62
Line2D operator*(const SE2 &t, const Line2D &l)
Definition: line_2d.h:48
static G2O_TYPES_SLAM3D_ADDONS_API void jacobian(Matrix7x6d &Jp, Matrix7x6d &Jl, const Isometry3D &x, const Line3D &l)
Definition: line3d.cpp:61
Vector6d transformCartesianLine(const Isometry3D &t, const Vector6d &line)
Definition: line3d.cpp:108
Eigen::Matrix< double, 7, 6, Eigen::ColMajor > Matrix7x6d
Definition: line3d.h:37
Eigen::Transform< double, 3, Eigen::Isometry, Eigen::ColMajor > Isometry3D
Definition: eigen_types.h:66
G2O_TYPES_SLAM3D_ADDONS_API Vector6d toCartesian() const
Definition: line3d.cpp:51
Eigen::Matrix< double, 3, 3, Eigen::ColMajor > Matrix3D
Definition: eigen_types.h:61
static void _skew(Matrix3D &S, const Vector3D &t)
Definition: line3d.cpp:34
Vector6d normalizeCartesianLine(const Vector6d &line)
Definition: line3d.cpp:115
Matrix< double, 6, 1, Eigen::ColMajor > Vector6d
Definition: types_icp.cpp:75