NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
timestamped_interpolater.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef LOCALIZATION_COMMON_TIMESTAMPED_INTERPOLATER_H_
20 #define LOCALIZATION_COMMON_TIMESTAMPED_INTERPOLATER_H_
21 
25 #include <gtsam/base/Matrix.h>
26 
27 #include <Eigen/Geometry>
28 
29 #include <boost/optional.hpp>
30 
31 #include <vector>
32 
33 namespace localization_common {
34 // Empty default interpolater params
36 
37 // Provides interpolation functions that extends a timestamp set for interpolatable objects.
38 // Optionally stores params to help with interpolation.
39 template <typename T, class ParamsT = DefaultInterpolationParams>
41  public:
42  explicit TimestampedInterpolater(const boost::optional<int> max_size = boost::none);
43  TimestampedInterpolater(const std::vector<Time>& timestamps, const std::vector<T>& objects,
44  const boost::optional<int> max_size = boost::none);
45 
46  // Returns the interpolated object (or exact object if timestamps match) at the provided timestamp.
47  boost::optional<T> Interpolate(const Time timestamp) const;
48 
49  // Interpolates two objects given an alpha [0,1], where alpha = 0 should return object a,
50  // alpha = 1 should return object b, and otherwise should return the appropriate combination
51  // of object a and b.
52  // This needs to be specialized
53  T Interpolate(const T& a, const T& b, const double alpha) const;
54 
55  // Returns the relative object given timestamps a and b.
56  boost::optional<T> Relative(const Time timestamp_a, const Time timestamp_b) const;
57 
58  // Computes a relative object given objects a and b.
59  // This needs to be specialized.
60  T Relative(const T& a, const T& b) const;
61 
62  // Accessor to params.
63  ParamsT& params();
64 
65  // Const accessor to params.
66  const ParamsT& params() const;
67 
68  mutable boost::optional<gtsam::Matrix> covariance_a_b;
69 
70  private:
71  TimestampedSet<T> timestamped_objects_;
72  ParamsT params_;
73 };
74 
75 // Implementation
76 template <typename T, class ParamsT>
78  : TimestampedSet<T>(max_size) {}
79 
80 template <typename T, class ParamsT>
82  const std::vector<T>& objects,
83  const boost::optional<int> max_size)
84  : TimestampedSet<T>(timestamps, objects, max_size) {}
85 
86 template <typename T, class ParamsT>
87 boost::optional<T> TimestampedInterpolater<T, ParamsT>::Interpolate(const Time timestamp) const {
88  const auto lower_and_upper_bound = TimestampedSet<T>::LowerAndUpperBound(timestamp);
89  // Check if equal timestamp exists
90  if (lower_and_upper_bound.second && lower_and_upper_bound.second->timestamp == timestamp)
91  return lower_and_upper_bound.second->value;
92  if (!lower_and_upper_bound.first || !lower_and_upper_bound.second) {
93  LogDebug("Interpolate: Failed to get lower and upper bound timestamps for query timestamp " << timestamp << ".");
94  return boost::none;
95  }
96 
97  const Time lower_bound_time = lower_and_upper_bound.first->timestamp;
98  const Time upper_bound_time = lower_and_upper_bound.second->timestamp;
99  const double alpha = (timestamp - lower_bound_time) / (upper_bound_time - lower_bound_time);
100  return Interpolate(lower_and_upper_bound.first->value, lower_and_upper_bound.second->value, alpha);
101 }
102 
103 template <typename T, class ParamsT>
104 T TimestampedInterpolater<T, ParamsT>::Interpolate(const T& a, const T& b, const double alpha) const {
105  static_assert(sizeof(T) == std::size_t(-1), "This needs to be specialized by template class.");
106 }
107 
108 template <typename T, class ParamsT>
109 boost::optional<T> TimestampedInterpolater<T, ParamsT>::Relative(const Time timestamp_a, const Time timestamp_b) const {
110  const auto a = Interpolate(timestamp_a);
111  const auto b = Interpolate(timestamp_b);
112  if (!a || !b) {
113  LogDebug("Relative: Failed to get interpolated objects for query timestamps " << timestamp_a << " and "
114  << timestamp_b << ".");
115  return boost::none;
116  }
117 
118  return Relative(*a, *b);
119 }
120 
121 template <typename T, class ParamsT>
122 T TimestampedInterpolater<T, ParamsT>::Relative(const T& a, const T& b) const {
123  static_assert(sizeof(T) == std::size_t(-1), "This needs to be specialized by template class.");
124 }
125 
126 template <typename T, class ParamsT>
128  return params_;
129 }
130 
131 template <typename T, class ParamsT>
132 const ParamsT& TimestampedInterpolater<T, ParamsT>::params() const {
133  return params_;
134 }
135 } // namespace localization_common
136 
137 #endif // LOCALIZATION_COMMON_TIMESTAMPED_INTERPOLATER_H_
localization_common
Definition: averager.h:33
localization_common::TimestampedInterpolater::Interpolate
boost::optional< T > Interpolate(const Time timestamp) const
Definition: timestamped_interpolater.h:87
localization_common::TimestampedInterpolater::Relative
boost::optional< T > Relative(const Time timestamp_a, const Time timestamp_b) const
Definition: timestamped_interpolater.h:109
localization_common::TimestampedInterpolater::covariance_a_b
boost::optional< gtsam::Matrix > covariance_a_b
Definition: timestamped_interpolater.h:68
localization_common::TimestampedInterpolater::TimestampedInterpolater
TimestampedInterpolater(const boost::optional< int > max_size=boost::none)
Definition: timestamped_interpolater.h:77
timestamped_set.h
localization_common::TimestampedSet
Definition: timestamped_set.h:49
localization_common::DefaultInterpolationParams
Definition: timestamped_interpolater.h:35
time.h
localization_common::TimestampedInterpolater
Definition: timestamped_interpolater.h:40
localization_common::Interpolate
Eigen::Isometry3d Interpolate(const Eigen::Isometry3d &lower_bound_pose, const Eigen::Isometry3d &upper_bound_pose, const double alpha)
Definition: utilities.cc:313
eigen_vectors.h
localization_common::TimestampedSet::LowerAndUpperBound
std::pair< boost::optional< TimestampedValue< T > >, boost::optional< TimestampedValue< T > > > LowerAndUpperBound(const Time timestamp) const
Definition: timestamped_set.h:274
LogDebug
#define LogDebug(msg)
Definition: logger.h:69
localization_common::Time
double Time
Definition: time.h:23
localization_common::TimestampedInterpolater::params
ParamsT & params()
Definition: timestamped_interpolater.h:127