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"
28 #include "isometry3d_gradients.h"
29 #include "isometry3d_mappings.h"
30 
31 #ifdef G2O_HAVE_OPENGL
34 #endif
35 
36 using namespace std;
37 
38 namespace g2o {
39 
40  ParameterCamera::ParameterCamera(){
41  setId(-1);
42  setKcam(1,1,0.5,0.5);
43  setOffset();
44  }
45 
46  void ParameterCamera::setOffset(const Isometry3D& offset_){
47  ParameterSE3Offset::setOffset(offset_);
48  _Kcam_inverseOffsetR = _Kcam * inverseOffset().rotation();
49  }
50 
51  void ParameterCamera::setKcam(double fx, double fy, double cx, double cy){
52  _Kcam.setZero();
53  _Kcam(0,0) = fx;
54  _Kcam(1,1) = fy;
55  _Kcam(0,2) = cx;
56  _Kcam(1,2) = cy;
57  _Kcam(2,2) = 1.0;
58  _invKcam = _Kcam.inverse();
59  _Kcam_inverseOffsetR = _Kcam * inverseOffset().rotation();
60  }
61 
62 
63  bool ParameterCamera::read(std::istream& is) {
64  Vector7d off;
65  for (int i=0; i<7; i++)
66  is >> off[i];
67  // normalize the quaternion to recover numerical precision lost by storing as human readable text
68  Vector4D::MapType(off.data()+3).normalize();
69  setOffset(internal::fromVectorQT(off));
70  double fx,fy,cx,cy;
71  is >> fx >> fy >> cx >> cy;
72  setKcam(fx,fy,cx,cy);
73  return is.good();
74  }
75 
76  bool ParameterCamera::write(std::ostream& os) const {
77  Vector7d off = internal::toVectorQT(_offset);
78  for (int i=0; i<7; i++)
79  os << off[i] << " ";
80  os << _Kcam(0,0) << " ";
81  os << _Kcam(1,1) << " ";
82  os << _Kcam(0,2) << " ";
83  os << _Kcam(1,2) << " ";
84  return os.good();
85  }
86 
87  bool CacheCamera::resolveDependancies(){
88  if (!CacheSE3Offset::resolveDependancies())
89  return false;
90  params = dynamic_cast<ParameterCamera*>(_parameters[0]);
91  return params != 0;
92  }
93 
94  void CacheCamera::updateImpl(){
95  CacheSE3Offset::updateImpl();
96  _w2i.matrix().topLeftCorner<3,4>() = params->Kcam() * w2n().matrix().topLeftCorner<3,4>();
97  }
98 
99 #ifdef G2O_HAVE_OPENGL
100 
101  CacheCameraDrawAction::CacheCameraDrawAction(): DrawAction(typeid(CacheCamera).name()){
102  _previousParams = (DrawAction::Parameters*)0x42;
103  refreshPropertyPtrs(0);
104  }
105 
106 
107  bool CacheCameraDrawAction::refreshPropertyPtrs(HyperGraphElementAction::Parameters* params_){
108  if (! DrawAction::refreshPropertyPtrs(params_))
109  return false;
110  if (_previousParams){
111  _cameraZ = _previousParams->makeProperty<FloatProperty>(_typeName + "::CAMERA_Z", .05f);
112  _cameraSide = _previousParams->makeProperty<FloatProperty>(_typeName + "::CAMERA_SIDE", .05f);
113 
114  } else {
115  _cameraZ = 0;
116  _cameraSide = 0;
117  }
118  return true;
119  }
120 
121  HyperGraphElementAction* CacheCameraDrawAction::operator()(HyperGraph::HyperGraphElement* element,
123  if (typeid(*element).name()!=_typeName)
124  return 0;
125  CacheCamera* that = static_cast<CacheCamera*>(element);
126  refreshPropertyPtrs(params);
127  if (! _previousParams)
128  return this;
129 
130  if (_show && !_show->value())
131  return this;
132 
133  glPushAttrib(GL_COLOR);
134  glColor3f(POSE_PARAMETER_COLOR);
135  glPushMatrix();
136  glMultMatrixd(that->camParams()->offset().data());
137  glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
138  opengl::drawPyramid(_cameraSide->value(), _cameraZ->value());
139  glPopMatrix();
140  glPopAttrib();
141  return this;
142  }
143 #endif
144 
145 }
Isometry3D fromVectorQT(const Vector7d &v)
Abstract action that operates on a graph entity.
const ParameterCamera * camParams() const
parameters of the camera
parameters for a camera
const Isometry3D & offset() const
rotation of the offset as 3x3 rotation matrix
Eigen::Matrix< double, 7, 1 > Vector7d
Definition: sim3.h:35
Eigen::Transform< double, 3, Eigen::Isometry, Eigen::ColMajor > Isometry3D
Definition: eigen_types.h:66
Eigen::Quaterniond & normalize(Eigen::Quaterniond &q)
#define POSE_PARAMETER_COLOR
Vector7d toVectorQT(const Isometry3D &t)
void drawPyramid(GLfloat length, GLfloat height)