g2o
plane3d.h
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 #ifndef G2O_PLANE3D_H_
28 #define G2O_PLANE3D_H_
29 
31 #include "g2o/stuff/misc.h"
32 #include <Eigen/Core>
33 #include <Eigen/Geometry>
34 
35 namespace g2o {
36 
38  public:
40  friend Plane3D operator*(const Eigen::Isometry3d& t, const Plane3D& plane);
41 
43  Eigen::Vector4d v;
44  v << 1., 0., 0., -1.;
45  fromVector(v);
46  }
47 
48  Plane3D(const Eigen::Vector4d& v){
49  fromVector(v);
50  }
51 
52 
53  inline Eigen::Vector4d toVector() const {
54  return _coeffs;
55  }
56 
57  inline const Eigen::Vector4d& coeffs() const {return _coeffs;}
58 
59  inline void fromVector(const Eigen::Vector4d& coeffs_) {
60  _coeffs=coeffs_;
61  normalize(_coeffs);
62  }
63 
64  static double azimuth(const Eigen::Vector3d& v) {
65  return atan2(v(1),v(0));
66  }
67 
68  static double elevation(const Eigen::Vector3d& v) {
69  return atan2(v(2), v.head<2>().norm());
70  }
71 
72  double distance() const {
73  return -_coeffs(3);
74  }
75 
76  Eigen::Vector3d normal() const {
77  return _coeffs.head<3>();
78  }
79 
80 
81  static Eigen::Matrix3d rotation(const Eigen::Vector3d& v) {
82  double _azimuth = azimuth(v);
83  double _elevation = elevation(v);
84  return (Eigen::AngleAxisd(_azimuth, Eigen::Vector3d::UnitZ())*Eigen::AngleAxisd(- _elevation, Eigen::Vector3d::UnitY())).toRotationMatrix();
85  }
86 
87  inline void oplus(const Eigen::Vector3d& v){
88  //construct a normal from azimuth and evelation;
89  double _azimuth=v[0];
90  double _elevation=v[1];
91  double s=sin(_elevation), c=cos(_elevation);
92  Eigen::Vector3d n (c*cos(_azimuth), c*sin(_azimuth), s) ;
93 
94  // rotate the normal
95  Eigen::Matrix3d R=rotation(normal());
96  double d=distance()+v[2];
97  _coeffs.head<3>() = R*n;
98  _coeffs(3) = -d;
99  normalize(_coeffs);
100  }
101 
102  inline Eigen::Vector3d ominus(const Plane3D& plane){
103  //construct the rotation that would bring the plane normal in (1 0 0)
104  Eigen::Matrix3d R=rotation(normal()).transpose();
105  Eigen::Vector3d n=R*plane.normal();
106  double d=distance()-plane.distance();
107  return Eigen::Vector3d(azimuth(n), elevation(n), d);
108  }
109 
110  //protected:
111 
112  static inline void normalize(Eigen::Vector4d& coeffs) {
113  double n=coeffs.head<3>().norm();
114  coeffs = coeffs * (1./n);
115  }
116 
117  Eigen::Vector4d _coeffs;
118  };
119 
120  inline Plane3D operator*(const Eigen::Isometry3d& t, const Plane3D& plane){
121  Eigen::Vector4d v=plane._coeffs;
122  Eigen::Vector4d v2;
123  Eigen::Matrix3d R=t.rotation();
124  v2.head<3>() = R*v.head<3>();
125  v2(3)=v(3) - t.translation().dot(v2.head<3>());
126  return Plane3D(v2);
127  };
128 
129 
130 }
131 
132 #endif
void oplus(const Eigen::Vector3d &v)
Definition: plane3d.h:87
some general case utility functions
static double elevation(const Eigen::Vector3d &v)
Definition: plane3d.h:68
static void normalize(Eigen::Vector4d &coeffs)
Definition: plane3d.h:112
Eigen::Vector3d ominus(const Plane3D &plane)
Definition: plane3d.h:102
const Eigen::Vector4d & coeffs() const
Definition: plane3d.h:57
static double azimuth(const Eigen::Vector3d &v)
Definition: plane3d.h:64
Plane3D(const Eigen::Vector4d &v)
Definition: plane3d.h:48
Line2D operator*(const SE2 &t, const Line2D &l)
Definition: line_2d.h:48
Eigen::Quaterniond & normalize(Eigen::Quaterniond &q)
void fromVector(const Eigen::Vector4d &coeffs_)
Definition: plane3d.h:59
Eigen::Vector3d normal() const
Definition: plane3d.h:76
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: plane3d.h:39
#define G2O_TYPES_SLAM3D_ADDONS_API
static Eigen::Matrix3d rotation(const Eigen::Vector3d &v)
Definition: plane3d.h:81
Eigen::Vector4d toVector() const
Definition: plane3d.h:53
double distance() const
Definition: plane3d.h:72
Eigen::Vector4d _coeffs
Definition: plane3d.h:117