g2o
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 WINDOWS
30 #include <windows.h>
31 #endif
32 
33 #ifdef G2O_HAVE_OPENGL
35 #endif
36 
37 #include <typeinfo>
38 
39 #include "g2o/stuff/macros.h"
40 
41 namespace g2o {
42 
44  BaseVertex<2, Line2D>()
45  {
46  _estimate.setZero();
47  p1Id=p2Id=-1;
48  }
49 
50  bool VertexLine2D::read(std::istream& is)
51  {
52  is >> _estimate[0] >> _estimate[1] >> p1Id >> p2Id;
53  return true;
54  }
55 
56  bool VertexLine2D::write(std::ostream& os) const
57  {
58  os << estimate()(0) << " " << estimate()(1) << " " << p1Id << " " << p2Id;
59  return os.good();
60  }
61 
62 // VertexLine2DWriteGnuplotAction::VertexLine2DWriteGnuplotAction(): WriteGnuplotAction(typeid(VertexLine2D).name()){}
63 
64 // HyperGraphElementAction* VertexLine2DWriteGnuplotAction::operator()(HyperGraph::HyperGraphElement* element, HyperGraphElementAction::Parameters* params_){
65 // if (typeid(*element).name()!=_typeName)
66 // return 0;
67 
68 // WriteGnuplotAction::Parameters* params=static_cast<WriteGnuplotAction::Parameters*>(params_);
69 // if (!params->os){
70 // std::cerr << __PRETTY_FUNCTION__ << ": warning, on valid os specified" << std::endl;
71 // return false;
72 // }
73 
74 // VertexLine2D* v = static_cast<VertexLine2D*>(element);
75 // *(params->os) << v->estimate().x() << " " << v->estimate().y() << std::endl;
76 // return this;
77 // }
78 
79 #ifdef G2O_HAVE_OPENGL
80  VertexLine2DDrawAction::VertexLine2DDrawAction(): DrawAction(typeid(VertexLine2D).name()){}
81 
82  bool VertexLine2DDrawAction::refreshPropertyPtrs(HyperGraphElementAction::Parameters* params_){
83  if (! DrawAction::refreshPropertyPtrs(params_))
84  return false;
85  if (_previousParams){
86  _pointSize = _previousParams->makeProperty<FloatProperty>(_typeName + "::POINT_SIZE", 1.);
87  } else {
88  _pointSize = 0;
89  }
90  return true;
91  }
92 
93  HyperGraphElementAction* VertexLine2DDrawAction::operator()(HyperGraph::HyperGraphElement* element,
95 
96  if (typeid(*element).name()!=_typeName)
97  return 0;
98 
99  refreshPropertyPtrs(params_);
100  if (! _previousParams)
101  return this;
102 
103  if (_show && !_show->value())
104  return this;
105 
106 
107  VertexLine2D* that = static_cast<VertexLine2D*>(element);
108  glPushAttrib(GL_CURRENT_BIT | GL_BLEND);
109  if (_pointSize) {
110  glPointSize(_pointSize->value());
111  }
112  Vector2D n(cos(that->theta()), sin(that->theta()));
113  Vector2D pmiddle=n*that->rho();
114  Vector2D t(-n.y(), n.x());
115  double l1,l2 = 10;
116  VertexPointXY *vp1=0, *vp2=0;
117  vp1=dynamic_cast<VertexPointXY*> (that->graph()->vertex(that->p1Id));
118  vp2=dynamic_cast<VertexPointXY*> (that->graph()->vertex(that->p2Id));
119 
120  glColor4f(0.8f,0.5f,0.3f,0.3f);
121  if (vp1 && vp2) {
122  glColor4f(0.8f,0.5f,0.3f,0.7f);
123  } else if (vp1 || vp2){
124  glColor4f(0.8f,0.5f,0.3f,0.5f);
125  }
126 
127  if (vp1) {
128  glColor4f(0.8f,0.5f,0.3f,0.7f);
129  l1 = t.dot(vp1->estimate()-pmiddle);
130  }
131  if (vp2) {
132  glColor4f(0.8f,0.5f,0.3f,0.7f);
133  l2 = t.dot(vp2->estimate()-pmiddle);
134  }
135  Vector2D p1=pmiddle+t*l1;
136  Vector2D p2=pmiddle+t*l2;
137  glBegin(GL_LINES);
138  glVertex3f((float)p1.x(),p1.y(),0.f);
139  glVertex3f((float)p2.x(),p2.y(),0.f);
140  glEnd();
141  glPopAttrib();
142  return this;
143  }
144 #endif
145 
146 } // end namespace
Eigen::Matrix< double, 2, 1, Eigen::ColMajor > Vector2D
Definition: eigen_types.h:45
const OptimizableGraph * graph() const
Abstract action that operates on a graph entity.
Templatized BaseVertex.
Definition: base_vertex.h:50
double theta() const
Definition: vertex_line2d.h:48
Vertex * vertex(int id)
returns the vertex number id appropriately casted
virtual bool read(std::istream &is)
read the vertex from a stream, i.e., the internal state of the vertex
virtual bool write(std::ostream &os) const
write the vertex to a stream
const EstimateType & estimate() const
return the current estimate of the vertex
Definition: base_vertex.h:99
virtual bool refreshPropertyPtrs(HyperGraphElementAction::Parameters *params_)
double rho() const
Definition: vertex_line2d.h:51