| 
									
										
										
										
											2011-06-03 23:07:11 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2011-06-03 23:07:11 +08:00
										 |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file    simulated2DConstraints.h | 
					
						
							|  |  |  |  * @brief   measurement functions and constraint definitions for simulated 2D robot | 
					
						
							|  |  |  |  * @author  Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/base/numericalDerivative.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-10 10:05:03 +08:00
										 |  |  | #include <gtsam/nonlinear/NonlinearEquality.h>
 | 
					
						
							| 
									
										
										
										
											2011-11-10 06:15:40 +08:00
										 |  |  | #include <gtsam/slam/BetweenFactor.h>
 | 
					
						
							| 
									
										
										
										
											2011-06-03 23:07:11 +08:00
										 |  |  | #include <gtsam/slam/BoundingConstraint.h>
 | 
					
						
							| 
									
										
										
										
											2012-06-10 04:15:44 +08:00
										 |  |  | #include <tests/simulated2D.h>
 | 
					
						
							| 
									
										
										
										
											2011-06-03 23:07:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // \namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | namespace simulated2D { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   namespace equality_constraints { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** Typedefs for regular use */ | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |     typedef NonlinearEquality1<Point2> UnaryEqualityConstraint; | 
					
						
							|  |  |  |     typedef NonlinearEquality1<Point2> UnaryEqualityPointConstraint; | 
					
						
							|  |  |  |     typedef BetweenConstraint<Point2> OdoEqualityConstraint; | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** Equality between variables */ | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |     typedef NonlinearEquality2<Point2> PoseEqualityConstraint; | 
					
						
							|  |  |  |     typedef NonlinearEquality2<Point2> PointEqualityConstraint; | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   } // \namespace equality_constraints
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   namespace inequality_constraints { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Unary inequality constraint forcing a coordinate to be greater/less than a fixed value (c) | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |      * @tparam VALUE is the value type for the variable constrained, e.g. Pose2, Point3, etc. | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  |      * @tparam IDX is an index in tangent space to constrain, must be less than KEY::VALUE::Dim() | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |     template<class VALUE, unsigned int IDX> | 
					
						
							|  |  |  |     struct ScalarCoordConstraint1: public BoundingConstraint1<VALUE> { | 
					
						
							|  |  |  |       typedef BoundingConstraint1<VALUE> Base;  ///< Base class convenience typedef
 | 
					
						
							| 
									
										
										
										
											2012-05-22 04:54:40 +08:00
										 |  |  |       typedef ScalarCoordConstraint1<VALUE, IDX> This; ///< This class convenience typedef
 | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |       typedef boost::shared_ptr<ScalarCoordConstraint1<VALUE, IDX> > shared_ptr; ///< boost::shared_ptr convenience typedef
 | 
					
						
							|  |  |  |       typedef VALUE Point; ///< Constrained variable type
 | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       virtual ~ScalarCoordConstraint1() {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-10 05:06:06 +08:00
										 |  |  |       /// @return a deep copy of this factor
 | 
					
						
							|  |  |  |       virtual gtsam::NonlinearFactor::shared_ptr clone() const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |         return boost::static_pointer_cast<gtsam::NonlinearFactor>( | 
					
						
							|  |  |  |             gtsam::NonlinearFactor::shared_ptr(new This(*this))); } | 
					
						
							| 
									
										
										
										
											2012-05-22 04:54:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  |       /**
 | 
					
						
							|  |  |  |        * Constructor for constraint | 
					
						
							|  |  |  |        * @param key is the label for the constrained variable | 
					
						
							|  |  |  |        * @param c is the measured value for the fixed coordinate | 
					
						
							|  |  |  |        * @param isGreaterThan is a flag to set inequality as greater than or less than | 
					
						
							|  |  |  |        * @param mu is the penalty function gain | 
					
						
							|  |  |  |        */ | 
					
						
							| 
									
										
										
										
											2012-02-19 09:02:07 +08:00
										 |  |  |       ScalarCoordConstraint1(Key key, double c, | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |           bool isGreaterThan, double mu = 1000.0) : | 
					
						
							|  |  |  |             Base(key, c, isGreaterThan, mu) { | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /**
 | 
					
						
							|  |  |  |        * Access function for the constrained index | 
					
						
							|  |  |  |        * @return the index for the constrained coordinate | 
					
						
							|  |  |  |        */ | 
					
						
							|  |  |  |       inline unsigned int index() const { return IDX; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /**
 | 
					
						
							|  |  |  |        * extracts a single value from the point to compute error | 
					
						
							|  |  |  |        * @param x is the estimate of the constrained variable being evaluated | 
					
						
							|  |  |  |        * @param H is an optional Jacobian, linearized at x | 
					
						
							|  |  |  |        */ | 
					
						
							|  |  |  |       virtual double value(const Point& x, boost::optional<Matrix&> H = | 
					
						
							|  |  |  |           boost::none) const { | 
					
						
							|  |  |  |         if (H) { | 
					
						
							| 
									
										
										
										
											2016-04-12 03:11:29 +08:00
										 |  |  |           Matrix D = Matrix::Zero(1, traits<Point>::GetDimension(x)); | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  |           D(0, IDX) = 1.0; | 
					
						
							|  |  |  |           *H = D; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-12-26 23:47:51 +08:00
										 |  |  |         return traits<Point>::Logmap(x)(IDX); | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** typedefs for use with simulated2D systems */ | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |     typedef ScalarCoordConstraint1<Point2, 0> PoseXInequality; ///< Simulated2D domain example factor constraining X
 | 
					
						
							|  |  |  |     typedef ScalarCoordConstraint1<Point2, 1> PoseYInequality; ///< Simulated2D domain example factor constraining Y
 | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Trait for distance constraints to provide distance | 
					
						
							|  |  |  |      * @tparam T1 is a Lie value for which distance functions exist | 
					
						
							|  |  |  |      * @tparam T2 is a Lie value for which distance functions exist | 
					
						
							|  |  |  |      * @param a is the first Lie element | 
					
						
							|  |  |  |      * @param b is the second Lie element | 
					
						
							|  |  |  |      * @return a scalar distance between values | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     template<class T1, class T2> | 
					
						
							| 
									
										
										
										
											2016-06-07 12:57:52 +08:00
										 |  |  |     double range_trait(const T1& a, const T2& b) { return distance2(a, b); } | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Binary inequality constraint forcing the range between points | 
					
						
							|  |  |  |      * to be less than or equal to a bound | 
					
						
							|  |  |  |      * @tparam VALUES is the variable set for the graph | 
					
						
							|  |  |  |      * @tparam KEY is the type of the keys for the variables constrained | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |     template<class VALUE> | 
					
						
							|  |  |  |     struct MaxDistanceConstraint : public BoundingConstraint2<VALUE, VALUE> { | 
					
						
							|  |  |  |       typedef BoundingConstraint2<VALUE, VALUE> Base;  ///< Base class for factor
 | 
					
						
							| 
									
										
										
										
											2012-05-22 04:54:40 +08:00
										 |  |  |       typedef MaxDistanceConstraint<VALUE> This;  ///< This class for factor
 | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |       typedef VALUE Point; ///< Type of variable constrained
 | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       virtual ~MaxDistanceConstraint() {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-10 05:06:06 +08:00
										 |  |  |       /// @return a deep copy of this factor
 | 
					
						
							|  |  |  |       virtual gtsam::NonlinearFactor::shared_ptr clone() const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |         return boost::static_pointer_cast<gtsam::NonlinearFactor>( | 
					
						
							|  |  |  |             gtsam::NonlinearFactor::shared_ptr(new This(*this))); } | 
					
						
							| 
									
										
										
										
											2012-05-22 04:54:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  |       /**
 | 
					
						
							|  |  |  |        * Primary constructor for factor | 
					
						
							|  |  |  |        * @param key1 is the first variable key | 
					
						
							|  |  |  |        * @param key2 is the second variable key | 
					
						
							|  |  |  |        * @param range_bound is the maximum range allowed between the variables | 
					
						
							|  |  |  |        * @param mu is the gain for the penalty function | 
					
						
							|  |  |  |        */ | 
					
						
							| 
									
										
										
										
											2012-02-19 09:02:07 +08:00
										 |  |  |       MaxDistanceConstraint(Key key1, Key key2, double range_bound, double mu = 1000.0) : | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |         Base(key1, key2, range_bound, false, mu) {} | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       /**
 | 
					
						
							|  |  |  |        * computes the range with derivatives | 
					
						
							|  |  |  |        * @param x1 is the first variable value | 
					
						
							|  |  |  |        * @param x2 is the second variable value | 
					
						
							|  |  |  |        * @param H1 is an optional Jacobian in x1 | 
					
						
							|  |  |  |        * @param H2 is an optional Jacobian in x2 | 
					
						
							|  |  |  |        * @return the distance between the variables | 
					
						
							|  |  |  |        */ | 
					
						
							|  |  |  |       virtual double value(const Point& x1, const Point& x2, | 
					
						
							|  |  |  |           boost::optional<Matrix&> H1 = boost::none, | 
					
						
							|  |  |  |           boost::optional<Matrix&> H2 = boost::none) const { | 
					
						
							|  |  |  |         if (H1) *H1 = numericalDerivative21(range_trait<Point,Point>, x1, x2, 1e-5); | 
					
						
							|  |  |  |         if (H1) *H2 = numericalDerivative22(range_trait<Point,Point>, x1, x2, 1e-5); | 
					
						
							|  |  |  |         return range_trait(x1, x2); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-07 12:02:20 +08:00
										 |  |  |     typedef MaxDistanceConstraint<Point2> PoseMaxDistConstraint; ///< Simulated2D domain example factor
 | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Binary inequality constraint forcing a minimum range | 
					
						
							|  |  |  |      * NOTE: this is not a convex function!  Be careful with initialization. | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |      * @tparam POSE is the type of the pose value constrained | 
					
						
							|  |  |  |      * @tparam POINT is the type of the point value constrained | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |     template<class POSE, class POINT> | 
					
						
							|  |  |  |     struct MinDistanceConstraint : public BoundingConstraint2<POSE, POINT> { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       typedef BoundingConstraint2<POSE, POINT> Base; ///< Base class for factor
 | 
					
						
							|  |  |  |       typedef MinDistanceConstraint<POSE, POINT> This; ///< This class for factor
 | 
					
						
							|  |  |  |       typedef POSE Pose; ///< Type of pose variable constrained
 | 
					
						
							|  |  |  |       typedef POINT Point; ///< Type of point variable constrained
 | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       virtual ~MinDistanceConstraint() {} | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       /// @return a deep copy of this factor
 | 
					
						
							|  |  |  |       virtual gtsam::NonlinearFactor::shared_ptr clone() const { | 
					
						
							|  |  |  |         return boost::static_pointer_cast<gtsam::NonlinearFactor>( | 
					
						
							|  |  |  |             gtsam::NonlinearFactor::shared_ptr(new This(*this))); } | 
					
						
							| 
									
										
										
										
											2012-05-22 04:54:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       /**
 | 
					
						
							|  |  |  |        * Primary constructor for factor | 
					
						
							|  |  |  |        * @param key1 is the first variable key | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  |        * @param key2 is the second variable key | 
					
						
							|  |  |  |        * @param range_bound is the minimum range allowed between the variables | 
					
						
							|  |  |  |        * @param mu is the gain for the penalty function | 
					
						
							|  |  |  |        */ | 
					
						
							| 
									
										
										
										
											2012-02-19 09:02:07 +08:00
										 |  |  |       MinDistanceConstraint(Key key1, Key key2, | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  |           double range_bound, double mu = 1000.0) | 
					
						
							| 
									
										
										
										
											2012-02-04 01:18:32 +08:00
										 |  |  |       : Base(key1, key2, range_bound, true, mu) {} | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       /**
 | 
					
						
							|  |  |  |        * computes the range with derivatives | 
					
						
							|  |  |  |        * @param x1 is the first variable value | 
					
						
							|  |  |  |        * @param x2 is the second variable value | 
					
						
							|  |  |  |        * @param H1 is an optional Jacobian in x1 | 
					
						
							|  |  |  |        * @param H2 is an optional Jacobian in x2 | 
					
						
							|  |  |  |        * @return the distance between the variables | 
					
						
							|  |  |  |        */ | 
					
						
							|  |  |  |       virtual double value(const Pose& x1, const Point& x2, | 
					
						
							|  |  |  |           boost::optional<Matrix&> H1 = boost::none, | 
					
						
							|  |  |  |           boost::optional<Matrix&> H2 = boost::none) const { | 
					
						
							|  |  |  |         if (H1) *H1 = numericalDerivative21(range_trait<Pose,Point>, x1, x2, 1e-5); | 
					
						
							|  |  |  |         if (H1) *H2 = numericalDerivative22(range_trait<Pose,Point>, x1, x2, 1e-5); | 
					
						
							|  |  |  |         return range_trait(x1, x2); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-06 08:44:25 +08:00
										 |  |  |     typedef MinDistanceConstraint<Point2, Point2> LandmarkAvoid; ///< Simulated2D domain example factor
 | 
					
						
							| 
									
										
										
										
											2012-01-31 06:00:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   } // \namespace inequality_constraints
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // \namespace simulated2D
 | 
					
						
							|  |  |  | 
 |