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