g2o
vertex_ellipse.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 "vertex_ellipse.h"
28 
29 #include "g2o/stuff/macros.h"
30 
31 #ifdef G2O_HAVE_OPENGL
34 #include "EXTERNAL/freeglut/freeglut_minimal.h"
35 #endif
36 
37 #include <iomanip>
38 
39 #include <Eigen/Eigenvalues>
40 
41 using namespace std;
42 
43 namespace g2o {
44 
45  VertexEllipse::VertexEllipse() : RobotData()
46  {
47  _covariance = Matrix3F::Zero();
48  _UMatrix = Matrix2F::Zero();
49  _singularValues = Vector2F::Zero();
50  }
51 
53  {
54  }
55 
57  Eigen::SelfAdjointEigenSolver<Matrix2F> eigenSolver(_covariance.block<2,2>(0,0));
58  _UMatrix = eigenSolver.eigenvectors();
59  _singularValues = eigenSolver.eigenvalues();
60 
61  }
62 
63  bool VertexEllipse::read(std::istream& is)
64  {
65  float cxx, cxy, cxt, cyy, cyt, ctt;
66  is >> cxx >> cxy >> cxt >> cyy >> cyt >> ctt;
67  _covariance(0,0) = cxx;
68  _covariance(0,1) = cxy;
69  _covariance(0,2) = cxt;
70  _covariance(1,0) = cxy;
71  _covariance(1,1) = cyy;
72  _covariance(1,2) = cyt;
73  _covariance(2,0) = cxt;
74  _covariance(2,1) = cyt;
75  _covariance(2,2) = ctt;
76 
77  _updateSVD();
78 
79  int size;
80  is >> size;
81  for (int i =0; i<size; i++){
82  float x, y;
83  is >> x >> y;
84  addMatchingVertex(x,y);
85  }
86 
87  return true;
88  }
89 
90  bool VertexEllipse::write(std::ostream& os) const
91  {
92  os << _covariance(0,0) << " " << _covariance(0,1) << " " << _covariance(0,2) << " "
93  << _covariance(1,1) << " " << _covariance(1,2) << " " << _covariance(2,2) << " ";
94 
95  os << _matchingVertices.size() << " " ;
96  for (size_t i=0 ; i< _matchingVertices.size(); i++){
97  os << _matchingVertices[i].x() << " " << _matchingVertices[i].y() << " ";
98  }
99 
100  return os.good();
101  }
102 
103 
104 
105 #ifdef G2O_HAVE_OPENGL
106  VertexEllipseDrawAction::VertexEllipseDrawAction(): DrawAction(typeid(VertexEllipse).name()){
107  _scaleFactor = 0;
108  }
109 
110  bool VertexEllipseDrawAction::refreshPropertyPtrs(HyperGraphElementAction::Parameters* params_){
111  if (!DrawAction::refreshPropertyPtrs(params_))
112  return false;
113  if (_previousParams){
114  _scaleFactor = _previousParams->makeProperty<DoubleProperty>(_typeName + "::", 1);
115  } else {
116  _scaleFactor = 0;
117  }
118  return true;
119  }
120 
121  HyperGraphElementAction* VertexEllipseDrawAction::operator()(HyperGraph::HyperGraphElement* element,
123  if (typeid(*element).name()!=_typeName)
124  return 0;
125 
126  refreshPropertyPtrs(params_);
127  if (! _previousParams){
128  return this;
129  }
130  if (_show && !_show->value())
131  return this;
132 
133  VertexEllipse* that = dynamic_cast<VertexEllipse*>(element);
134 
135  glPushMatrix();
136 
137  float sigmaTheta = sqrt(that->covariance()(2,2));
138  float x = 0.1f*cosf(sigmaTheta);
139  float y = 0.1f*sinf(sigmaTheta);
140 
141  glColor3f(1.f,0.7f,1.f);
142  glBegin(GL_LINE_STRIP);
143  glVertex3f(x,y,0);
144  glVertex3f(0,0,0);
145  glVertex3f(x,-y,0);
146  glEnd();
147 
148  glColor3f(0.f,1.f,0.f);
149  for (size_t i=0; i< that->matchingVertices().size(); i++){
150  glBegin(GL_LINES);
151  glVertex3f(0,0,0);
152  glVertex3f(that->matchingVertices()[i].x(),that->matchingVertices()[i].y(),0);
153  glEnd();
154  }
155 
156  Matrix2F rot = that->U();
157  float angle = atan2(rot(1,0), rot(0,0));
158  glRotatef(angle*180.0/M_PI, 0., 0., 1.);
159  Vector2F sv = that->singularValues();
160  glScalef(sqrt(sv(0)), sqrt(sv(1)), 1);
161 
162  glColor3f(1.f,0.7f,1.f);
163  glBegin(GL_LINE_LOOP);
164  for(int i=0; i<36; i++){
165  float rad = i*M_PI/18.0;
166  glVertex2f(cos(rad),
167  sin(rad));
168  }
169  glEnd();
170 
171 
172 
173  glPopMatrix();
174  return this;
175  }
176 #endif
177 
178 }
Abstract action that operates on a graph entity.
Vector2F _singularValues
const Vector2F & singularValues()
myVector2fVector _matchingVertices
const Matrix2F & U()
const myVector2fVector & matchingVertices()
const Matrix3F & covariance()
Eigen::Matrix< float, 2, 2, Eigen::ColMajor > Matrix2F
Definition: eigen_types.h:55
virtual bool read(std::istream &is)
read the data from a stream
virtual bool refreshPropertyPtrs(HyperGraphElementAction::Parameters *params_)
void _updateSVD() const
string ellipse to be attached to a vertex
#define M_PI
Definition: misc.h:34
void addMatchingVertex(float x, float y)
Eigen::Matrix< float, 2, 1, Eigen::ColMajor > Vector2F
Definition: eigen_types.h:40
virtual bool write(std::ostream &os) const
write the data to a stream
data recorded by the robot
Definition: robot_data.h:41