g2o
parameter_camera.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 "parameter_camera.h"
29 
30 using namespace std;
31 
32 namespace g2o {
33 namespace deprecated {
34 
35 
36  ParameterCamera::ParameterCamera(){
37  setId(-1);
38  setKcam(1,1,0.5,0.5);
39  setOffset();
40  }
41 
42  void ParameterCamera::setOffset(const SE3Quat& offset_){
43  ParameterSE3Offset::setOffset(offset_);
44  _Kcam_inverseOffsetR = _Kcam * inverseOffsetMatrix().rotation();
45  }
46 
47  void ParameterCamera::setKcam(double fx, double fy, double cx, double cy){
48  _Kcam.setZero();
49  _Kcam(0,0) = fx;
50  _Kcam(1,1) = fy;
51  _Kcam(0,2) = cx;
52  _Kcam(1,2) = cy;
53  _Kcam(2,2) = 1.0;
54  _invKcam = _Kcam.inverse();
55  _Kcam_inverseOffsetR = _Kcam * inverseOffsetMatrix().rotation();
56  }
57 
58 
59  bool ParameterCamera::read(std::istream& is) {
60  Vector7d off;
61  for (int i=0; i<7; i++)
62  is >> off[i];
63  setOffset(SE3Quat(off));
64  double fx,fy,cx,cy;
65  is >> fx >> fy >> cx >> cy;
66  setKcam(fx,fy,cx,cy);
67  return is.good();
68  }
69 
70  bool ParameterCamera::write(std::ostream& os) const {
71  Vector7d off = offset().toVector();
72  for (int i=0; i<7; i++)
73  os << off[i] << " ";
74  os << _Kcam(0,0) << " ";
75  os << _Kcam(1,1) << " ";
76  os << _Kcam(0,2) << " ";
77  os << _Kcam(1,2) << " ";
78  return os.good();
79  }
80 
81  bool CacheCamera::resolveDependancies(){
82  if (!CacheSE3Offset::resolveDependancies())
83  return false;
84  params = dynamic_cast<ParameterCamera*>(_parameters[0]);
85  return params != 0;
86  }
87 
88  void CacheCamera::updateImpl(){
89  CacheSE3Offset::updateImpl();
90  _w2i.matrix().topLeftCorner<3,4>() = params->Kcam() * w2nMatrix().matrix().topLeftCorner<3,4>();
91  }
92 
93 #ifdef G2O_HAVE_OPENGL
94  static void drawMyPyramid(float height, float side){
95  Eigen::Vector3f p[6];
96  p[0] << 0, 0., 0.;
97  p[1] << -side, -side, height;
98  p[2] << -side, side, height;
99  p[3] << side, side, height;
100  p[4] << side, -side, height;
101  p[5] << -side, -side, height;
102 
103  glBegin(GL_TRIANGLES);
104  for (int i = 1; i < 5; ++i) {
105  Eigen::Vector3f normal = (p[i] - p[0]).cross(p[i+1] - p[0]);
106  glNormal3f(normal.x(), normal.y(), normal.z());
107  glVertex3f(p[0].x(), p[0].y(), p[0].z());
108  glVertex3f(p[i].x(), p[i].y(), p[i].z());
109  glVertex3f(p[i+1].x(), p[i+1].y(), p[i+1].z());
110  }
111  glEnd();
112  }
113 
114  CacheCameraDrawAction::CacheCameraDrawAction(): DrawAction(typeid(CacheCamera).name()){
115  _previousParams = (DrawAction::Parameters*)0x42;
116  refreshPropertyPtrs(0);
117  }
118 
119 
120  bool CacheCameraDrawAction::refreshPropertyPtrs(HyperGraphElementAction::Parameters* params_){
121  if (! DrawAction::refreshPropertyPtrs(params_))
122  return false;
123  if (_previousParams){
124  _cameraZ = _previousParams->makeProperty<FloatProperty>(_typeName + "::CAMERA_Z", .05f);
125  _cameraSide = _previousParams->makeProperty<FloatProperty>(_typeName + "::CAMERA_SIDE", .05f);
126 
127  } else {
128  _cameraZ = 0;
129  _cameraSide = 0;
130  }
131  return true;
132  }
133 
134  HyperGraphElementAction* CacheCameraDrawAction::operator()(HyperGraph::HyperGraphElement* element,
136  if (typeid(*element).name()!=_typeName)
137  return 0;
138  CacheCamera* that = static_cast<CacheCamera*>(element);
139  refreshPropertyPtrs(params);
140  if (! _previousParams)
141  return this;
142 
143  if (_show && !_show->value())
144  return this;
145 
146  glPushMatrix();
147  glMultMatrixd(that->camParams()->offsetMatrix().data());
148  if (_cameraZ && _cameraSide)
149  drawMyPyramid(_cameraZ->value(), _cameraSide->value());
150  glPopMatrix();
151 
152  return this;
153  }
154 #endif
155 
156 }
157 }
const ParameterCamera * camParams() const
parameters of the camera
Abstract action that operates on a graph entity.
Eigen::Matrix< double, 7, 1 > Vector7d
Definition: sim3.h:35
parameters for a camera
const Eigen::Isometry3d & offsetMatrix() const
rotation of the offset as 3x3 rotation matrix