simulated2D.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file simulated2D.h
  10. * @brief measurement functions and derivatives for simulated 2D robot
  11. * @author Frank Dellaert
  12. */
  13. // \callgraph
  14. #pragma once
  15. #include <gtsam/nonlinear/NonlinearFactor.h>
  16. #include <gtsam/nonlinear/NonlinearFactorGraph.h>
  17. #include <gtsam/geometry/Point2.h>
  18. // \namespace
  19. namespace simulated2D {
  20. using namespace gtsam;
  21. // Simulated2D robots have no orientation, just a position
  22. /**
  23. * Custom Values class that holds poses and points, mainly used as a convenience for MATLAB wrapper
  24. */
  25. class Values: public gtsam::Values {
  26. private:
  27. int nrPoses_, nrPoints_;
  28. public:
  29. typedef gtsam::Values Base; ///< base class
  30. typedef boost::shared_ptr<Point2> sharedPoint; ///< shortcut to shared Point type
  31. /// Constructor
  32. Values() : nrPoses_(0), nrPoints_(0) {
  33. }
  34. /// Copy constructor
  35. Values(const Base& base) :
  36. Base(base), nrPoses_(0), nrPoints_(0) {
  37. }
  38. /// Insert a pose
  39. void insertPose(Key i, const Point2& p) {
  40. insert(i, p);
  41. nrPoses_++;
  42. }
  43. /// Insert a point
  44. void insertPoint(Key j, const Point2& p) {
  45. insert(j, p);
  46. nrPoints_++;
  47. }
  48. /// Number of poses
  49. int nrPoses() const {
  50. return nrPoses_;
  51. }
  52. /// Number of points
  53. int nrPoints() const {
  54. return nrPoints_;
  55. }
  56. /// Return pose i
  57. Point2 pose(Key i) const {
  58. return at<Point2>(i);
  59. }
  60. /// Return point j
  61. Point2 point(Key j) const {
  62. return at<Point2>(j);
  63. }
  64. };
  65. /// Prior on a single pose
  66. inline Point2 prior(const Point2& x) {
  67. return x;
  68. }
  69. /// Prior on a single pose, optionally returns derivative
  70. inline Point2 prior(const Point2& x, boost::optional<Matrix&> H = boost::none) {
  71. if (H) *H = I_2x2;
  72. return x;
  73. }
  74. /// odometry between two poses
  75. inline Point2 odo(const Point2& x1, const Point2& x2) {
  76. return x2 - x1;
  77. }
  78. /// odometry between two poses, optionally returns derivative
  79. inline Point2 odo(const Point2& x1, const Point2& x2, boost::optional<Matrix&> H1 =
  80. boost::none, boost::optional<Matrix&> H2 = boost::none) {
  81. if (H1) *H1 = -I_2x2;
  82. if (H2) *H2 = I_2x2;
  83. return x2 - x1;
  84. }
  85. /// measurement between landmark and pose
  86. inline Point2 mea(const Point2& x, const Point2& l) {
  87. return l - x;
  88. }
  89. /// measurement between landmark and pose, optionally returns derivative
  90. inline Point2 mea(const Point2& x, const Point2& l, boost::optional<Matrix&> H1 =
  91. boost::none, boost::optional<Matrix&> H2 = boost::none) {
  92. if (H1) *H1 = -I_2x2;
  93. if (H2) *H2 = I_2x2;
  94. return l - x;
  95. }
  96. /**
  97. * Unary factor encoding a soft prior on a vector
  98. */
  99. template<class VALUE = Point2>
  100. class GenericPrior: public NoiseModelFactor1<VALUE> {
  101. public:
  102. typedef NoiseModelFactor1<VALUE> Base; ///< base class
  103. typedef GenericPrior<VALUE> This;
  104. typedef boost::shared_ptr<GenericPrior<VALUE> > shared_ptr;
  105. typedef VALUE Pose; ///< shortcut to Pose type
  106. Pose measured_; ///< prior mean
  107. /// Create generic prior
  108. GenericPrior(const Pose& z, const SharedNoiseModel& model, Key key) :
  109. Base(model, key), measured_(z) {
  110. }
  111. /// Return error and optional derivative
  112. Vector evaluateError(const Pose& x, boost::optional<Matrix&> H = boost::none) const override {
  113. return (prior(x, H) - measured_);
  114. }
  115. ~GenericPrior() override {}
  116. /// @return a deep copy of this factor
  117. gtsam::NonlinearFactor::shared_ptr clone() const override {
  118. return boost::static_pointer_cast<gtsam::NonlinearFactor>(
  119. gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
  120. private:
  121. /// Default constructor
  122. GenericPrior() { }
  123. /// Serialization function
  124. friend class boost::serialization::access;
  125. template<class ARCHIVE>
  126. void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
  127. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
  128. ar & BOOST_SERIALIZATION_NVP(measured_);
  129. }
  130. };
  131. /**
  132. * Binary factor simulating "odometry" between two Vectors
  133. */
  134. template<class VALUE = Point2>
  135. class GenericOdometry: public NoiseModelFactor2<VALUE, VALUE> {
  136. public:
  137. typedef NoiseModelFactor2<VALUE, VALUE> Base; ///< base class
  138. typedef GenericOdometry<VALUE> This;
  139. typedef boost::shared_ptr<GenericOdometry<VALUE> > shared_ptr;
  140. typedef VALUE Pose; ///< shortcut to Pose type
  141. Pose measured_; ///< odometry measurement
  142. /// Create odometry
  143. GenericOdometry(const Pose& measured, const SharedNoiseModel& model, Key i1, Key i2) :
  144. Base(model, i1, i2), measured_(measured) {
  145. }
  146. /// Evaluate error and optionally return derivatives
  147. Vector evaluateError(const Pose& x1, const Pose& x2,
  148. boost::optional<Matrix&> H1 = boost::none,
  149. boost::optional<Matrix&> H2 = boost::none) const override {
  150. return (odo(x1, x2, H1, H2) - measured_);
  151. }
  152. ~GenericOdometry() override {}
  153. /// @return a deep copy of this factor
  154. gtsam::NonlinearFactor::shared_ptr clone() const override {
  155. return boost::static_pointer_cast<gtsam::NonlinearFactor>(
  156. gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
  157. private:
  158. /// Default constructor
  159. GenericOdometry() { }
  160. /// Serialization function
  161. friend class boost::serialization::access;
  162. template<class ARCHIVE>
  163. void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
  164. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
  165. ar & BOOST_SERIALIZATION_NVP(measured_);
  166. }
  167. };
  168. /**
  169. * Binary factor simulating "measurement" between two Vectors
  170. */
  171. template<class POSE, class LANDMARK>
  172. class GenericMeasurement: public NoiseModelFactor2<POSE, LANDMARK> {
  173. public:
  174. typedef NoiseModelFactor2<POSE, LANDMARK> Base; ///< base class
  175. typedef GenericMeasurement<POSE, LANDMARK> This;
  176. typedef boost::shared_ptr<GenericMeasurement<POSE, LANDMARK> > shared_ptr;
  177. typedef POSE Pose; ///< shortcut to Pose type
  178. typedef LANDMARK Landmark; ///< shortcut to Landmark type
  179. Landmark measured_; ///< Measurement
  180. /// Create measurement factor
  181. GenericMeasurement(const Landmark& measured, const SharedNoiseModel& model, Key i, Key j) :
  182. Base(model, i, j), measured_(measured) {
  183. }
  184. /// Evaluate error and optionally return derivatives
  185. Vector evaluateError(const Pose& x1, const Landmark& x2,
  186. boost::optional<Matrix&> H1 = boost::none,
  187. boost::optional<Matrix&> H2 = boost::none) const override {
  188. return (mea(x1, x2, H1, H2) - measured_);
  189. }
  190. ~GenericMeasurement() override {}
  191. /// @return a deep copy of this factor
  192. gtsam::NonlinearFactor::shared_ptr clone() const override {
  193. return boost::static_pointer_cast<gtsam::NonlinearFactor>(
  194. gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
  195. private:
  196. /// Default constructor
  197. GenericMeasurement() { }
  198. /// Serialization function
  199. friend class boost::serialization::access;
  200. template<class ARCHIVE>
  201. void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
  202. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
  203. ar & BOOST_SERIALIZATION_NVP(measured_);
  204. }
  205. };
  206. /** Typedefs for regular use */
  207. typedef GenericPrior<Point2> Prior;
  208. typedef GenericOdometry<Point2> Odometry;
  209. typedef GenericMeasurement<Point2, Point2> Measurement;
  210. // Specialization of a graph for this example domain
  211. // TODO: add functions to add factor types
  212. class Graph : public NonlinearFactorGraph {
  213. public:
  214. Graph() {}
  215. };
  216. } // namespace simulated2D
  217. /// traits
  218. namespace gtsam {
  219. template<class POSE, class LANDMARK>
  220. struct traits<simulated2D::GenericMeasurement<POSE, LANDMARK> > : Testable<
  221. simulated2D::GenericMeasurement<POSE, LANDMARK> > {
  222. };
  223. template<>
  224. struct traits<simulated2D::Values> : public Testable<simulated2D::Values> {
  225. };
  226. }