g2o
Loading...
Searching...
No Matches
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
30#include "isometry3d_mappings.h"
31
32#ifdef G2O_HAVE_OPENGL
35#endif
36
37using namespace std;
38
39namespace g2o {
40
42 setId(-1);
43 setKcam(1, 1, 0.5, 0.5);
44 setOffset();
45}
46
51
52void ParameterCamera::setKcam(double fx, double fy, double cx, double cy) {
53 _Kcam.setZero();
54 _Kcam(0, 0) = fx;
55 _Kcam(1, 1) = fy;
56 _Kcam(0, 2) = cx;
57 _Kcam(1, 2) = cy;
58 _Kcam(2, 2) = 1.0;
59 _invKcam = _Kcam.inverse();
61}
62
63bool ParameterCamera::read(std::istream& is) {
64 Vector7 off;
65 internal::readVector(is, off);
66 // normalize the quaternion to recover numerical precision lost by storing as
67 // human readable text
68 Vector4::MapType(off.data() + 3).normalize();
70 double fx, fy, cx, cy;
71 is >> fx >> fy >> cx >> cy;
72 setKcam(fx, fy, cx, cy);
73 return is.good();
74}
75
76bool ParameterCamera::write(std::ostream& os) const {
78 os << _Kcam(0, 0) << " ";
79 os << _Kcam(1, 1) << " ";
80 os << _Kcam(0, 2) << " ";
81 os << _Kcam(1, 2) << " ";
82 return os.good();
83}
84
86 if (!CacheSE3Offset::resolveDependencies()) return false;
87 params = dynamic_cast<ParameterCamera*>(_parameters[0]);
88 return params != 0;
89}
90
93 _w2i.matrix().topLeftCorner<3, 4>() =
94 params->Kcam() * w2n().matrix().topLeftCorner<3, 4>();
95}
96
97#ifdef G2O_HAVE_OPENGL
98
99CacheCameraDrawAction::CacheCameraDrawAction()
100 : DrawAction(typeid(CacheCamera).name()) {
101 _previousParams = (DrawAction::Parameters*)0x42;
102 refreshPropertyPtrs(0);
103}
104
105bool CacheCameraDrawAction::refreshPropertyPtrs(
106 HyperGraphElementAction::Parameters* params_) {
107 if (!DrawAction::refreshPropertyPtrs(params_)) return false;
108 if (_previousParams) {
109 _cameraZ = _previousParams->makeProperty<FloatProperty>(
110 _typeName + "::CAMERA_Z", .05f);
111 _cameraSide = _previousParams->makeProperty<FloatProperty>(
112 _typeName + "::CAMERA_SIDE", .05f);
113
114 } else {
115 _cameraZ = 0;
116 _cameraSide = 0;
117 }
118 return true;
119}
120
121HyperGraphElementAction* CacheCameraDrawAction::operator()(
122 HyperGraph::HyperGraphElement* element,
123 HyperGraphElementAction::Parameters* params) {
124 if (typeid(*element).name() != _typeName) return nullptr;
125 CacheCamera* that = static_cast<CacheCamera*>(element);
126 refreshPropertyPtrs(params);
127 if (!_previousParams) return this;
128
129 if (_show && !_show->value()) return this;
130
131 glPushAttrib(GL_COLOR);
132 glColor3f(POSE_PARAMETER_COLOR);
133 glPushMatrix();
134 glMultMatrixd(that->camParams()->offset().cast<double>().data());
135 glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
136 opengl::drawPyramid(_cameraSide->value(), _cameraZ->value());
137 glPopMatrix();
138 glPopAttrib();
139 return this;
140}
141#endif
142
143} // namespace g2o
Affine3 _w2i
world to image transform
virtual bool resolveDependencies()
virtual void updateImpl()
redefine this to do the update
ParameterCamera * params
const Isometry3 & w2n() const
virtual void updateImpl()
redefine this to do the update
virtual bool resolveDependencies()
ParameterVector _parameters
Definition cache.h:103
virtual bool refreshPropertyPtrs(HyperGraphElementAction::Parameters *params_)
parameters for a camera
const Matrix3 & Kcam() const
void setKcam(double fx, double fy, double cx, double cy)
virtual bool read(std::istream &is)
read the data from a stream
void setOffset(const Isometry3 &offset_=Isometry3::Identity())
virtual bool write(std::ostream &os) const
write the data to a stream
const Isometry3 & inverseOffset() const
rotation of the inverse offset as 3x3 rotation matrix
void setOffset(const Isometry3 &offset_=Isometry3::Identity())
void setId(int id_)
Definition parameter.cpp:33
Isometry3 fromVectorQT(const Vector7 &v)
bool writeVector(std::ostream &os, const Eigen::DenseBase< Derived > &b)
Definition io_helper.h:36
bool readVector(std::istream &is, Eigen::DenseBase< Derived > &b)
Definition io_helper.h:42
Vector7 toVectorQT(const Isometry3 &t)
void drawPyramid(GLfloat length, GLfloat height)
VectorN< 7 > Vector7
Definition eigen_types.h:54
Eigen::Transform< double, 3, Eigen::Isometry, Eigen::ColMajor > Isometry3
Definition eigen_types.h:77
Property< float > FloatProperty
Definition property.h:150
Definition jet.h:876
#define POSE_PARAMETER_COLOR