Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / examples / slam / pose_graph_2d / types.h
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2016 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 //   this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 //   this list of conditions and the following disclaimer in the documentation
12 //   and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 //   used to endorse or promote products derived from this software without
15 //   specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: vitus@google.com (Michael Vitus)
30 //
31 // Defines the types used in the 2D pose graph SLAM formulation. Each vertex of
32 // the graph has a unique integer ID with a position and orientation. There are
33 // delta transformation constraints between two vertices.
34
35 #ifndef CERES_EXAMPLES_POSE_GRAPH_2D_TYPES_H_
36 #define CERES_EXAMPLES_POSE_GRAPH_2D_TYPES_H_
37
38 #include <fstream>
39
40 #include "Eigen/Core"
41 #include "normalize_angle.h"
42
43 namespace ceres {
44 namespace examples {
45
46 // The state for each vertex in the pose graph.
47 struct Pose2d {
48   double x;
49   double y;
50   double yaw_radians;
51
52   // The name of the data type in the g2o file format.
53   static std::string name() {
54     return "VERTEX_SE2";
55   }
56 };
57
58 std::istream& operator>>(std::istream& input, Pose2d& pose) {
59   input >> pose.x >> pose.y >> pose.yaw_radians;
60   // Normalize the angle between -pi to pi.
61   pose.yaw_radians = NormalizeAngle(pose.yaw_radians);
62   return input;
63 }
64
65 // The constraint between two vertices in the pose graph. The constraint is the
66 // transformation from vertex id_begin to vertex id_end.
67 struct Constraint2d {
68   int id_begin;
69   int id_end;
70
71   double x;
72   double y;
73   double yaw_radians;
74
75   // The inverse of the covariance matrix for the measurement. The order of the
76   // entries are x, y, and yaw.
77   Eigen::Matrix3d information;
78
79   // The name of the data type in the g2o file format.
80   static std::string name() {
81     return "EDGE_SE2";
82   }
83 };
84
85 std::istream& operator>>(std::istream& input, Constraint2d& constraint) {
86   input >> constraint.id_begin >> constraint.id_end >> constraint.x >>
87       constraint.y >> constraint.yaw_radians >>
88       constraint.information(0, 0) >> constraint.information(0, 1) >>
89       constraint.information(0, 2) >> constraint.information(1, 1) >>
90       constraint.information(1, 2) >> constraint.information(2, 2);
91
92   // Set the lower triangular part of the information matrix.
93   constraint.information(1, 0) = constraint.information(0, 1);
94   constraint.information(2, 0) = constraint.information(0, 2);
95   constraint.information(2, 1) = constraint.information(1, 2);
96
97   // Normalize the angle between -pi to pi.
98   constraint.yaw_radians = NormalizeAngle(constraint.yaw_radians);
99   return input;
100 }
101
102 }  // namespace examples
103 }  // namespace ceres
104
105 #endif  // CERES_EXAMPLES_POSE_GRAPH_2D_TYPES_H_