NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
point_to_handrail_endpoint_factor.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 GRAPH_FACTORS_POINT_TO_HANDRAIL_ENDPOINT_FACTOR_H_
20 #define GRAPH_FACTORS_POINT_TO_HANDRAIL_ENDPOINT_FACTOR_H_
21 
22 #include <gtsam/geometry/Pose3.h>
23 #include <gtsam/geometry/Point3.h>
24 #include <gtsam/nonlinear/NonlinearFactor.h>
25 
26 #include <string>
27 
28 namespace gtsam {
29 class PointToHandrailEndpointFactor : public NoiseModelFactor1<Pose3> {
30  typedef NoiseModelFactor1<Pose3> Base;
32 
33  public:
35 
37  const Point3& world_t_endpoint_b, const Pose3& body_T_sensor,
38  const SharedNoiseModel& model, Key pose_key)
39  : Base(model, pose_key),
40  sensor_t_point_(sensor_t_point),
41  world_t_endpoint_a_(world_t_endpoint_a),
42  world_t_endpoint_b_(world_t_endpoint_b),
43  body_T_sensor_(body_T_sensor) {}
44 
45  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
46  std::cout << s << "PointToHandrailEndpointFactor, z = ";
47  traits<Point3>::Print(sensor_t_point_);
48  traits<Point3>::Print(world_t_endpoint_a_);
49  traits<Point3>::Print(world_t_endpoint_b_);
50  traits<Pose3>::Print(body_T_sensor_);
51  Base::print("", keyFormatter);
52  }
53 
54  bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
55  const This* e = dynamic_cast<const This*>(&p);
56  return e && Base::equals(p, tol) && traits<Point3>::Equals(this->sensor_t_point(), e->sensor_t_point(), tol) &&
57  traits<Point3>::Equals(this->world_t_endpoint_a(), e->world_t_endpoint_a(), tol) &&
58  traits<Point3>::Equals(this->world_t_endpoint_b(), e->world_t_endpoint_b(), tol) &&
59  traits<Pose3>::Equals(this->body_T_sensor(), e->body_T_sensor(), tol);
60  }
61 
62  Vector evaluateError(const Pose3& world_T_body, boost::optional<Matrix&> H = boost::none) const override {
63  if (H) {
64  Matrix66 d_world_T_sensor_d_world_T_body;
65  Matrix36 d_world_t_point_d_world_T_sensor;
66  Matrix33 d_local_d_world_t_point;
67  const auto error = getError(world_T_body, d_world_T_sensor_d_world_T_body, d_world_t_point_d_world_T_sensor,
68  d_local_d_world_t_point);
69  *H = d_local_d_world_t_point * d_world_t_point_d_world_T_sensor * d_world_T_sensor_d_world_T_body;
70  return error;
71  }
72  return getError(world_T_body);
73  }
74 
75  Vector getError(const Pose3& world_T_body, OptionalJacobian<6, 6> d_world_T_sensor_d_world_T_body = boost::none,
76  OptionalJacobian<3, 6> d_world_t_point_d_world_T_sensor = boost::none,
77  OptionalJacobian<3, 3> d_local_d_world_t_point = boost::none) const {
78  const Pose3 world_T_sensor = world_T_body.transformPoseFrom(body_T_sensor_, d_world_T_sensor_d_world_T_body);
79  const Point3 world_t_point = world_T_sensor.transformFrom(sensor_t_point_, d_world_t_point_d_world_T_sensor);
80  const double distance_to_endpoint_a = (world_t_point - world_t_endpoint_a_).norm();
81  const double distance_to_endpoint_b = (world_t_point - world_t_endpoint_b_).norm();
82  // TODO(rsoussan): Find closest endpoint before creating factor and stick to it? Add option to redo correspondence?
83  const Point3& world_t_closest_endpoint =
84  distance_to_endpoint_a < distance_to_endpoint_b ? world_t_endpoint_a_ : world_t_endpoint_b_;
85  const Point3 world_F_point_t_closest_endpoint =
86  local(world_t_point, world_t_closest_endpoint, d_local_d_world_t_point);
87  return world_F_point_t_closest_endpoint;
88  }
89 
90  // In gtsam terminology, local is the difference in the tangent space between two Lie group elements.
91  // In this case this is simply the difference between the two Point3 elements.
92  static Point3 local(const Point3& point1, const Point3& point2,
93  OptionalJacobian<3, 3> d_local_d_point1 = boost::none) {
94  if (d_local_d_point1) {
95  *d_local_d_point1 = I_3x3;
96  }
97  return point1 - point2;
98  }
99 
100  const Point3& sensor_t_point() const { return sensor_t_point_; }
101  const Point3& world_t_endpoint_a() const { return world_t_endpoint_a_; }
102  const Point3& world_t_endpoint_b() const { return world_t_endpoint_b_; }
103  const Pose3& body_T_sensor() const { return body_T_sensor_; }
104 
105  private:
107  template <class ARCHIVE>
108  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
109  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
110  ar& BOOST_SERIALIZATION_NVP(sensor_t_point_);
111  ar& BOOST_SERIALIZATION_NVP(world_t_endpoint_a_);
112  ar& BOOST_SERIALIZATION_NVP(world_t_endpoint_b_);
113  ar& BOOST_SERIALIZATION_NVP(body_T_sensor_);
114  }
115 
116  Point3 sensor_t_point_;
117  Point3 world_t_endpoint_a_;
118  Point3 world_t_endpoint_b_;
119  Pose3 body_T_sensor_;
120 
121  public:
122  GTSAM_MAKE_ALIGNED_OPERATOR_NEW
123 };
124 } // namespace gtsam
125 
126 #endif // GRAPH_FACTORS_POINT_TO_HANDRAIL_ENDPOINT_FACTOR_H_
gtsam::PointToHandrailEndpointFactor
Definition: point_to_handrail_endpoint_factor.h:29
gtsam::PointToHandrailEndpointFactor::PointToHandrailEndpointFactor
PointToHandrailEndpointFactor()
Definition: point_to_handrail_endpoint_factor.h:34
gtsam::PointToHandrailEndpointFactor::world_t_endpoint_a
const Point3 & world_t_endpoint_a() const
Definition: point_to_handrail_endpoint_factor.h:101
gtsam::PointToHandrailEndpointFactor::world_t_endpoint_b
const Point3 & world_t_endpoint_b() const
Definition: point_to_handrail_endpoint_factor.h:102
gtsam::PointToHandrailEndpointFactor::PointToHandrailEndpointFactor
PointToHandrailEndpointFactor(const Point3 &sensor_t_point, const Point3 &world_t_endpoint_a, const Point3 &world_t_endpoint_b, const Pose3 &body_T_sensor, const SharedNoiseModel &model, Key pose_key)
Definition: point_to_handrail_endpoint_factor.h:36
gtsam::PointToHandrailEndpointFactor::evaluateError
Vector evaluateError(const Pose3 &world_T_body, boost::optional< Matrix & > H=boost::none) const override
Definition: point_to_handrail_endpoint_factor.h:62
gtsam::PointToHandrailEndpointFactor::local
static Point3 local(const Point3 &point1, const Point3 &point2, OptionalJacobian< 3, 3 > d_local_d_point1=boost::none)
Definition: point_to_handrail_endpoint_factor.h:92
gtsam::PointToHandrailEndpointFactor::getError
Vector getError(const Pose3 &world_T_body, OptionalJacobian< 6, 6 > d_world_T_sensor_d_world_T_body=boost::none, OptionalJacobian< 3, 6 > d_world_t_point_d_world_T_sensor=boost::none, OptionalJacobian< 3, 3 > d_local_d_world_t_point=boost::none) const
Definition: point_to_handrail_endpoint_factor.h:75
gtsam::PointToHandrailEndpointFactor::sensor_t_point
const Point3 & sensor_t_point() const
Definition: point_to_handrail_endpoint_factor.h:100
Print
int Print(std::string const &msg)
Definition: eps_driver_tool.cc:86
gtsam
Definition: cumulative_factor.h:26
gtsam::PointToHandrailEndpointFactor::body_T_sensor
const Pose3 & body_T_sensor() const
Definition: point_to_handrail_endpoint_factor.h:103
gtsam::PointToHandrailEndpointFactor::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: point_to_handrail_endpoint_factor.h:45
error
uint8_t error
Definition: signal_lights.h:89
gtsam::PointToHandrailEndpointFactor::equals
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
Definition: point_to_handrail_endpoint_factor.h:54
gtsam::PointToHandrailEndpointFactor::access
friend class boost::serialization::access
Definition: point_to_handrail_endpoint_factor.h:106