g2o
Loading...
Searching...
No Matches
sensor_line3d.cpp
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 G. Grisetti, R. Kuemmerle, 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 "sensor_line3d.h"
28
29#include <cassert>
30
31namespace g2o {
32using namespace std;
33
34// SensorLine3D
35SensorLine3D::SensorLine3D(const std::string& name_)
36 : BinarySensor<Robot3D, EdgeSE3Line3D, WorldObjectLine3D>(name_) {
37 _offsetParam = 0;
38 _information.setIdentity();
39 _information *= 1e9;
40 //_information(2,2)=10;
41 setInformation(_information);
42}
43
44bool SensorLine3D::isVisible(SensorLine3D::WorldObjectType* to) {
45 if (!_robotPoseObject) return false;
46 assert(to && to->vertex());
47 VertexType* v = to->vertex();
48 VertexType::EstimateType pose = v->estimate();
49 VertexType::EstimateType delta = _sensorPose.inverse() * pose;
50 Vector3d translation = delta;
51 double range2 = translation.squaredNorm();
52 if (range2 > _maxRange2) return false;
53 if (range2 < _minRange2) return false;
54 translation.normalize();
55 // the cameras have the z in front
56 double bearing = acos(translation.z());
57 if (fabs(bearing) > _fov) return false;
58 return true;
59}
60
61void SensorLine3D::addParameters() {
62 if (!_offsetParam) _offsetParam = new ParameterSE3Offset();
63 assert(world());
64 world()->addParameter(_offsetParam);
65}
66
67void SensorLine3D::addNoise(EdgeType* e) {
68 EdgeType::ErrorVector n = _sampler.generateSample();
69 e->setMeasurement(e->measurement() + n);
70 e->setInformation(information());
71}
72
73void SensorLine3D::sense() {
74 if (!_offsetParam) {
75 return;
76 }
77 _robotPoseObject = 0;
78 RobotType* r = dynamic_cast<RobotType*>(robot());
79 std::list<PoseObject*>::reverse_iterator it = r->trajectory().rbegin();
80 int count = 0;
81 while (it != r->trajectory().rend() && count < 1) {
82 if (!_robotPoseObject) _robotPoseObject = *it;
83 ++it;
84 count++;
85 }
86 if (!_robotPoseObject) return;
87 _sensorPose = _robotPoseObject->vertex()->estimate() * _offsetParam->offset();
88 for (std::set<BaseWorldObject*>::iterator it = world()->objects().begin();
89 it != world()->objects().end(); ++it) {
90 WorldObjectType* o = dynamic_cast<WorldObjectType*>(*it);
91 if (o && isVisible(o)) {
92 EdgeType* e = mkEdge(o);
93 if (e && graph()) {
94 e->setParameterId(0, _offsetParam->id());
95 graph()->addEdge(e);
96 e->setMeasurementFromState();
97 addNoise(e);
98 }
99 }
100 }
101}
102
103} // namespace g2o
Jet< T, N > acos(const Jet< T, N > &f)
Definition jet.h:458
WorldObject< VertexLine3D > WorldObjectLine3D
Robot< WorldObjectSE3 > Robot3D
Definition jet.h:876