GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
BearingRange.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #pragma once
20 
21 #include <gtsam/base/Manifold.h>
22 #include <gtsam/base/Testable.h>
24 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
25 #include <boost/serialization/nvp.hpp>
26 #endif
27 #include <iostream>
28 
29 namespace gtsam {
30 
31 // Forward declaration of Bearing functor which should be of A1*A2 -> return_type
32 // For example Bearing<Pose3,Point3>(pose,point), defined in Pose3.h will return Unit3
33 // At time of writing only Pose2 and Pose3 specialize this functor.
34 template <typename A1, typename A2>
35 struct Bearing;
36 
37 // Forward declaration of Range functor which should be of A1*A2 -> return_type
38 // For example Range<Pose2,Pose2>(T1,T2), defined in Pose2.h will return double
39 // At time of writing Pose2, Pose3, and several Camera variants specialize this for several types
40 template <typename A1, typename A2>
41 struct Range;
42 
49 template <typename A1, typename A2,
50  typename B = typename Bearing<A1, A2>::result_type,
51  typename R = typename Range<A1, A2>::result_type>
52 struct BearingRange {
53 private:
54  B bearing_;
55  R range_;
56 
57 public:
58  enum { dimB = traits<B>::dimension };
59  enum { dimR = traits<R>::dimension };
60  enum { dimension = dimB + dimR };
61 
64 
65  BearingRange() {}
66  BearingRange(const B& b, const R& r) : bearing_(b), range_(r) {}
67 
71 
73  const B& bearing() const { return bearing_; }
74 
76  const R& range() const { return range_; }
77 
80  const A1& a1, const A2& a2,
81  OptionalJacobian<dimension, traits<A1>::dimension> H1 = {},
83  typename MakeJacobian<B, A1>::type HB1;
84  typename MakeJacobian<B, A2>::type HB2;
85  typename MakeJacobian<R, A1>::type HR1;
86  typename MakeJacobian<R, A2>::type HR2;
87 
88  B b = Bearing<A1, A2>()(a1, a2, H1 ? &HB1 : 0, H2 ? &HB2 : 0);
89  R r = Range<A1, A2>()(a1, a2, H1 ? &HR1 : 0, H2 ? &HR2 : 0);
90 
91  if (H1) *H1 << HB1, HR1;
92  if (H2) *H2 << HB2, HR2;
93  return BearingRange(b, r);
94  }
95 
97  static B MeasureBearing(const A1& a1, const A2& a2) {
98  return Bearing<A1, A2>()(a1, a2);
99  }
100 
102  static R MeasureRange(const A1& a1, const A2& a2) {
103  return Range<A1, A2>()(a1, a2);
104  }
105 
109 
110  void print(const std::string& str = "") const {
111  std::cout << str;
112  traits<B>::Print(bearing_, "bearing ");
113  traits<R>::Print(range_, "range ");
114  }
115  bool equals(const BearingRange<A1, A2>& m2, double tol = 1e-8) const {
116  return traits<B>::Equals(bearing_, m2.bearing_, tol) &&
117  traits<R>::Equals(range_, m2.range_, tol);
118  }
119 
123 
124  inline static size_t Dim() { return dimension; }
125  inline size_t dim() const { return dimension; }
126 
127  typedef Eigen::Matrix<double, dimension, 1> TangentVector;
129 
131  BearingRange retract(const TangentVector& xi) const {
132  B m1 = traits<B>::Retract(bearing_, xi.template head<dimB>());
133  R m2 = traits<R>::Retract(range_, xi.template tail<dimR>());
134  return BearingRange(m1, m2);
135  }
136 
138  TangentVector localCoordinates(const BearingRange& other) const {
139  typename traits<B>::TangentVector v1 = traits<B>::Local(bearing_, other.bearing_);
140  typename traits<R>::TangentVector v2 = traits<R>::Local(range_, other.range_);
141  TangentVector v;
142  v << v1, v2;
143  return v;
144  }
145 
149 
150 private:
151 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
152  template <class ARCHIVE>
154  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
155  ar& boost::serialization::make_nvp("bearing", bearing_);
156  ar& boost::serialization::make_nvp("range", range_);
157  }
158 
159  friend class boost::serialization::access;
160 #endif
161 
163 
164  // Alignment, see https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
165  enum {
166  NeedsToAlign = (sizeof(B) % 16) == 0 || (sizeof(R) % 16) == 0
167  };
168 public:
170 };
171 
172 // Declare this to be both Testable and a Manifold
173 template <typename A1, typename A2>
174 struct traits<BearingRange<A1, A2> >
175  : Testable<BearingRange<A1, A2> >,
176  internal::ManifoldTraits<BearingRange<A1, A2> > {};
177 
178 // Helper class for to implement Range traits for classes with a bearing method
179 // For example, to specialize Bearing to Pose3 and Point3, using Pose3::bearing, it suffices to say
180 // template <> struct Bearing<Pose3, Point3> : HasBearing<Pose3, Point3, Unit3> {};
181 // where the third argument is used to indicate the return type
182 template <class A1, typename A2, class RT>
183 struct HasBearing {
184  typedef RT result_type;
185  RT operator()(
186  const A1& a1, const A2& a2,
189  return a1.bearing(a2, H1, H2);
190  }
191 };
192 
193 // Similar helper class for to implement Range traits for classes with a range method
194 // For classes with overloaded range methods, such as PinholeCamera, this can even be templated:
195 // template <typename T> struct Range<PinholeCamera, T> : HasRange<PinholeCamera, T, double> {};
196 template <class A1, typename A2, class RT>
197 struct HasRange {
198  typedef RT result_type;
199  RT operator()(
200  const A1& a1, const A2& a2,
203  return a1.range(a2, H1, H2);
204  }
205 };
206 
207 } // namespace gtsam
Definition: BearingRange.h:52
static B MeasureBearing(const A1 &a1, const A2 &a2)
Predict bearing.
Definition: BearingRange.h:97
Concept check for values that can be used in unit tests.
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
Definition: BearingRange.h:197
Definition: Testable.h:152
Definition: Group.h:43
static BearingRange Measure(const A1 &a1, const A2 &a2, OptionalJacobian< dimension, traits< A1 >::dimension > H1={}, OptionalJacobian< dimension, traits< A2 >::dimension > H2={})
Prediction function that stacks measurements.
Definition: BearingRange.h:79
Definition: BearingRange.h:183
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
Definition: types.h:293
TangentVector localCoordinates(const BearingRange &other) const
Compute the coordinates in the tangent space.
Definition: BearingRange.h:138
Definition: Testable.h:112
Base class and basic functions for Manifold types.
Definition: BearingRange.h:35
BearingRange retract(const TangentVector &xi) const
Retract delta to manifold.
Definition: BearingRange.h:131
Definition: BearingRange.h:41
Definition: chartTesting.h:28
Definition: OptionalJacobian.h:38
Definition: Manifold.h:92
const R & range() const
Return range measurement.
Definition: BearingRange.h:76
Special class for optional Jacobian arguments.
static R MeasureRange(const A1 &a1, const A2 &a2)
Predict range.
Definition: BearingRange.h:102
const B & bearing() const
Return bearing measurement.
Definition: BearingRange.h:73