NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
plane.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_MEASUREMENTS_PLANE_H_
20 #define LOCALIZATION_MEASUREMENTS_PLANE_H_
21 
22 #include <gtsam/base/Testable.h>
23 #include <gtsam/base/Vector.h>
24 #include <gtsam/geometry/Point3.h>
25 
26 #include <iostream>
27 #include <string>
28 
29 namespace localization_measurements {
31  public:
32  PointNormalPlane(const gtsam::Point3& point, const gtsam::Vector3& normal) : point_(point), normal_(normal) {}
33  const gtsam::Point3& point() const { return point_; }
34  const gtsam::Vector3& normal() const { return normal_; }
35 
36  private:
37  gtsam::Point3 point_;
38  gtsam::Vector3 normal_;
39 };
40 
41 // Plane parameterized by ax+by+cz = d
42 class GeneralPlane {
43  public:
44  explicit GeneralPlane(const PointNormalPlane& point_normal_plane) {
45  normal_ = point_normal_plane.normal();
46  // Since the point (p) of the point normal parameterization is on the plane, the vector between another point on the
47  // plane u = (x,y,z) and p is perpendicular to the normal. Thus (u-p) dot normal = 0 and a(x - p_x) + b(y - p_y) +
48  // c(z - p_z) = 0. Thus ax + by + cz = ap_x + bp_y + cp_z = -d. So d = -(ap_x + bp_y + cp_z) = -(normal dot p)
49  d_ = -1.0 * normal_.dot(point_normal_plane.point());
50  }
51 
52  const gtsam::Vector3& normal() const { return normal_; }
53  const double& d() const { return d_; }
54 
55  private:
56  // Contains plane parameters a,b,c
57  gtsam::Vector3 normal_;
58  double d_;
59 };
60 
61 // Normalized version of General Plane.
62 // Useful for calculating distances from points to plane
64  public:
65  HessianNormalPlane() = default;
66  explicit HessianNormalPlane(const GeneralPlane& general_plane) {
67  const double norm = general_plane.normal().norm();
68  unit_normal_ = general_plane.normal() / norm;
69  constant_ = general_plane.d() / norm;
70  }
71  const gtsam::Vector3& unit_normal() const { return unit_normal_; }
72  const double& constant() const { return constant_; }
73 
74  private:
75  // Serialization function
77  template <class ARCHIVE>
78  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
79  ar& BOOST_SERIALIZATION_NVP(unit_normal_);
80  ar& BOOST_SERIALIZATION_NVP(constant_);
81  }
82 
83  gtsam::Vector3 unit_normal_;
84  double constant_;
85 };
86 
87 class Plane : public HessianNormalPlane {
88  typedef HessianNormalPlane Base;
89 
90  public:
91  Plane() = default;
92  explicit Plane(const PointNormalPlane& point_normal_plane) : Base(GeneralPlane(point_normal_plane)) {}
93  Plane(const gtsam::Point3& point, const gtsam::Vector3& normal) : Plane(PointNormalPlane(point, normal)) {}
94  double Distance(const gtsam::Point3& point, gtsam::OptionalJacobian<1, 3> d_distance_d_point = boost::none) const {
95  const double distance = unit_normal().dot(point) + constant();
96  if (d_distance_d_point) {
97  *d_distance_d_point = unit_normal();
98  }
99  return distance;
100  }
101  void print(const std::string& s = "") const {
102  std::cout << (s.empty() ? s : s + " ") << unit_normal() << ", " << constant() << std::endl;
103  }
104  bool equals(const Plane& plane, double tol = 1e-9) const {
105  return gtsam::traits<gtsam::Vector3>::Equals(unit_normal(), plane.unit_normal(), tol) &&
106  std::abs(constant() - plane.constant()) < tol;
107  }
108 
109  private:
110  // Serialization function
112  template <class ARCHIVE>
113  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
114  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
115  }
116 };
117 
118 } // namespace localization_measurements
119 
120 namespace gtsam {
121 template <>
122 struct traits<localization_measurements::Plane> : public Testable<localization_measurements::Plane> {};
123 } // namespace gtsam
124 
125 #endif // LOCALIZATION_MEASUREMENTS_PLANE_H_
localization_measurements::HessianNormalPlane::constant
const double & constant() const
Definition: plane.h:72
localization_measurements::Plane::access
friend class boost::serialization::access
Definition: plane.h:111
localization_measurements::GeneralPlane::d
const double & d() const
Definition: plane.h:53
localization_measurements::HessianNormalPlane::unit_normal
const gtsam::Vector3 & unit_normal() const
Definition: plane.h:71
localization_measurements::GeneralPlane::GeneralPlane
GeneralPlane(const PointNormalPlane &point_normal_plane)
Definition: plane.h:44
localization_measurements::Plane::Plane
Plane(const PointNormalPlane &point_normal_plane)
Definition: plane.h:92
localization_measurements::GeneralPlane::normal
const gtsam::Vector3 & normal() const
Definition: plane.h:52
localization_measurements::Plane::equals
bool equals(const Plane &plane, double tol=1e-9) const
Definition: plane.h:104
localization_measurements::PointNormalPlane::point
const gtsam::Point3 & point() const
Definition: plane.h:33
localization_measurements
Definition: depth_correspondences.h:25
localization_measurements::HessianNormalPlane
Definition: plane.h:63
localization_measurements::Plane::Plane
Plane()=default
gtsam
Definition: cumulative_factor.h:26
localization_measurements::Plane::Plane
Plane(const gtsam::Point3 &point, const gtsam::Vector3 &normal)
Definition: plane.h:93
localization_measurements::Plane::Distance
double Distance(const gtsam::Point3 &point, gtsam::OptionalJacobian< 1, 3 > d_distance_d_point=boost::none) const
Definition: plane.h:94
localization_measurements::PointNormalPlane
Definition: plane.h:30
localization_measurements::HessianNormalPlane::HessianNormalPlane
HessianNormalPlane()=default
localization_measurements::HessianNormalPlane::HessianNormalPlane
HessianNormalPlane(const GeneralPlane &general_plane)
Definition: plane.h:66
localization_measurements::Plane::print
void print(const std::string &s="") const
Definition: plane.h:101
localization_measurements::GeneralPlane
Definition: plane.h:42
localization_measurements::HessianNormalPlane::access
friend class boost::serialization::access
Definition: plane.h:76
localization_measurements::Plane
Definition: plane.h:87
localization_measurements::PointNormalPlane::PointNormalPlane
PointNormalPlane(const gtsam::Point3 &point, const gtsam::Vector3 &normal)
Definition: plane.h:32
localization_measurements::PointNormalPlane::normal
const gtsam::Vector3 & normal() const
Definition: plane.h:34