g2o
Loading...
Searching...
No Matches
vertex_line2d.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_line2d.h"
28
29#ifdef G2O_HAVE_OPENGL
31#endif
32
33namespace g2o {
34
35VertexLine2D::VertexLine2D() : BaseVertex<2, Line2D>(), p1Id(-1), p2Id(-1) {
36 _estimate.setZero();
37}
38
39bool VertexLine2D::read(std::istream& is) {
40 is >> _estimate[0] >> _estimate[1] >> p1Id >> p2Id;
41 return true;
42}
43
44bool VertexLine2D::write(std::ostream& os) const {
45 os << estimate()(0) << " " << estimate()(1) << " " << p1Id << " " << p2Id;
46 return os.good();
47}
48
49#ifdef G2O_HAVE_OPENGL
50VertexLine2DDrawAction::VertexLine2DDrawAction()
51 : DrawAction(typeid(VertexLine2D).name()), _pointSize(nullptr) {}
52
53bool VertexLine2DDrawAction::refreshPropertyPtrs(
54 HyperGraphElementAction::Parameters* params_) {
55 if (!DrawAction::refreshPropertyPtrs(params_)) return false;
56 if (_previousParams) {
57 _pointSize = _previousParams->makeProperty<FloatProperty>(
58 _typeName + "::POINT_SIZE", 1.);
59 } else {
60 _pointSize = 0;
61 }
62 return true;
63}
64
65HyperGraphElementAction* VertexLine2DDrawAction::operator()(
66 HyperGraph::HyperGraphElement* element,
67 HyperGraphElementAction::Parameters* params_) {
68 if (typeid(*element).name() != _typeName) return nullptr;
69
70 refreshPropertyPtrs(params_);
71 if (!_previousParams) return this;
72
73 if (_show && !_show->value()) return this;
74
75 VertexLine2D* that = static_cast<VertexLine2D*>(element);
76 glPushAttrib(GL_CURRENT_BIT | GL_BLEND);
77 if (_pointSize) {
78 glPointSize(_pointSize->value());
79 }
80 Vector2 n(std::cos(that->theta()), std::sin(that->theta()));
81 Vector2 pmiddle = n * that->rho();
82 Vector2 t(-n.y(), n.x());
83 double l1{}, l2 = 10;
84 VertexPointXY *vp1 = 0, *vp2 = 0;
85 vp1 = dynamic_cast<VertexPointXY*>(that->graph()->vertex(that->p1Id));
86 vp2 = dynamic_cast<VertexPointXY*>(that->graph()->vertex(that->p2Id));
87
88 glColor4f(0.8f, 0.5f, 0.3f, 0.3f);
89 if (vp1 && vp2) {
90 glColor4f(0.8f, 0.5f, 0.3f, 0.7f);
91 } else if (vp1 || vp2) {
92 glColor4f(0.8f, 0.5f, 0.3f, 0.5f);
93 }
94
95 if (vp1) {
96 glColor4f(0.8f, 0.5f, 0.3f, 0.7f);
97 l1 = t.dot(vp1->estimate() - pmiddle);
98 }
99 if (vp2) {
100 glColor4f(0.8f, 0.5f, 0.3f, 0.7f);
101 l2 = t.dot(vp2->estimate() - pmiddle);
102 }
103 Vector2 p1 = pmiddle + t * l1;
104 Vector2 p2 = pmiddle + t * l2;
105 glBegin(GL_LINES);
106 glVertex3f((float)p1.x(), p1.y(), 0.f);
107 glVertex3f((float)p2.x(), p2.y(), 0.f);
108 glEnd();
109 glPopAttrib();
110 return this;
111}
112#endif
113
114} // namespace g2o
Templatized BaseVertex.
Definition base_vertex.h:51
const EstimateType & estimate() const
return the current estimate of the vertex
virtual bool refreshPropertyPtrs(HyperGraphElementAction::Parameters *params_)
virtual bool write(std::ostream &os) const
write the vertex to a stream
virtual bool read(std::istream &is)
read the vertex from a stream, i.e., the internal state of the vertex
VectorN< 2 > Vector2
Definition eigen_types.h:50
Property< float > FloatProperty
Definition property.h:150