NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
point_to_line_segment_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_LINE_SEGMENT_FACTOR_H_
20 #define GRAPH_FACTORS_POINT_TO_LINE_SEGMENT_FACTOR_H_
21 
23 #include <graph_factors/silu.h>
24 
25 #include <gtsam/geometry/Pose3.h>
26 #include <gtsam/geometry/Point3.h>
27 
28 #include <string>
29 
30 namespace gtsam {
32  typedef PointToLineFactorBase Base;
34 
35  public:
37 
38  PointToLineSegmentFactor(const Point3& sensor_t_point, const Pose3& world_T_line, const Pose3& body_T_sensor,
39  const double line_length, const SharedNoiseModel& model, Key pose_key,
40  const bool use_silu = false)
41  : Base(sensor_t_point, world_T_line, body_T_sensor, model, pose_key),
42  line_length_(line_length),
43  use_silu_(use_silu) {}
44 
45  gtsam::NonlinearFactor::shared_ptr clone() const override {
46  return boost::static_pointer_cast<gtsam::NonlinearFactor>(gtsam::NonlinearFactor::shared_ptr(new This(*this)));
47  }
48 
49  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
50  std::cout << s << "PointToLineSegmentFactor, z = ";
51  std::cout << "line length: " << line_length_;
52  Base::print("", keyFormatter);
53  }
54 
55  bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
56  const This* e = dynamic_cast<const This*>(&p);
57  return Base::equals(p, tol) && traits<double>::Equals(this->line_length(), e->line_length(), tol);
58  }
59 
60  Vector evaluateError(const Pose3& world_T_body, boost::optional<Matrix&> H = boost::none) const override {
61  const auto line_t_point = Base::error(world_T_body, H);
62  const auto line_z_point = line_t_point.z();
63  Point3 error = line_t_point;
64  // Check if z value is in between segment endpoints, zero z error component and Jacobian row if so.
65  // Otherwise add or subtract half line length to the z error to make it reflect the closest distance to
66  // one of the segment endpoints.
67  if (!use_silu_) {
68  if (line_z_point >= -1.0 * line_length_ / 2.0 && line_z_point <= line_length_ / 2.0) {
69  error.z() = 0;
70  if (H) {
71  H->block<1, 6>(2, 0) = Eigen::Matrix<double, 1, 6>::Zero();
72  }
73  } else if (line_z_point < -1.0 * line_length_ / 2.0) {
74  error.z() = error.z() + line_length_ / 2.0;
75  } else {
76  error.z() = error.z() - line_length_ / 2.0;
77  }
78  } else {
79  if (H) {
80  Matrix11 d_silu_d_z;
81  error.z() = graph_factors::SiluWithOffsetTwoWay(error.z(), line_length_ / 2.0, d_silu_d_z);
82  H->block<1, 6>(2, 0) = d_silu_d_z * H->block<1, 6>(2, 0);
83  }
84  error.z() = graph_factors::SiluWithOffsetTwoWay(error.z(), line_length_ / 2.0);
85  }
86  return error;
87  }
88 
89  double line_length() const { return line_length_; }
90  bool use_silu() const { return use_silu_; }
91 
92  private:
94  template <class ARCHIVE>
95  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
96  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
97  ar& BOOST_SERIALIZATION_NVP(line_length_);
98  ar& BOOST_SERIALIZATION_NVP(use_silu_);
99  }
100 
101  double line_length_;
102  bool use_silu_;
103 };
104 } // namespace gtsam
105 
106 #endif // GRAPH_FACTORS_POINT_TO_LINE_SEGMENT_FACTOR_H_
gtsam::PointToLineSegmentFactor::PointToLineSegmentFactor
PointToLineSegmentFactor(const Point3 &sensor_t_point, const Pose3 &world_T_line, const Pose3 &body_T_sensor, const double line_length, const SharedNoiseModel &model, Key pose_key, const bool use_silu=false)
Definition: point_to_line_segment_factor.h:38
gtsam::PointToLineFactorBase::error
Vector error(const Pose3 &world_T_body, boost::optional< Matrix & > H=boost::none) const
Definition: point_to_line_factor_base.h:62
gtsam::PointToLineSegmentFactor
Definition: point_to_line_segment_factor.h:31
graph_factors::SiluWithOffsetTwoWay
double SiluWithOffsetTwoWay(const double x, const double offset, gtsam::OptionalJacobian< 1, 1 > d_silu_d_x=boost::none)
Definition: silu.cc:37
gtsam::PointToLineFactorBase::sensor_t_point
const Point3 & sensor_t_point() const
Definition: point_to_line_factor_base.h:87
silu.h
gtsam::PointToLineFactorBase
Definition: point_to_line_factor_base.h:29
gtsam::PointToLineSegmentFactor::use_silu
bool use_silu() const
Definition: point_to_line_segment_factor.h:90
gtsam::PointToLineFactorBase::equals
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
Definition: point_to_line_factor_base.h:55
gtsam::PointToLineFactorBase::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: point_to_line_factor_base.h:47
gtsam::PointToLineFactorBase::world_T_line
const Pose3 & world_T_line() const
Definition: point_to_line_factor_base.h:88
gtsam::PointToLineSegmentFactor::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: point_to_line_segment_factor.h:49
gtsam::PointToLineSegmentFactor::equals
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
Definition: point_to_line_segment_factor.h:55
gtsam
Definition: cumulative_factor.h:26
point_to_line_factor_base.h
gtsam::PointToLineSegmentFactor::evaluateError
Vector evaluateError(const Pose3 &world_T_body, boost::optional< Matrix & > H=boost::none) const override
Definition: point_to_line_segment_factor.h:60
gtsam::PointToLineSegmentFactor::clone
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: point_to_line_segment_factor.h:45
gtsam::PointToLineSegmentFactor::PointToLineSegmentFactor
PointToLineSegmentFactor()
Definition: point_to_line_segment_factor.h:36
gtsam::PointToLineSegmentFactor::line_length
double line_length() const
Definition: point_to_line_segment_factor.h:89
gtsam::PointToLineFactorBase::body_T_sensor
const Pose3 & body_T_sensor() const
Definition: point_to_line_factor_base.h:89
gtsam::PointToLineSegmentFactor::access
friend class boost::serialization::access
Definition: point_to_line_segment_factor.h:93