MROB
plane_registration.hpp
1 /* Copyright (c) 2022, Gonzalo Ferrer
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  *
16  * planeRegistration.hpp
17  *
18  * Created on: Jan 28, 2019
19  * Author: Gonzalo Ferrer
20  * g.ferrer@skoltech.ru
21  * Mobile Robotics Lab, Skoltech
22  */
23 
24 #ifndef PLANEREGISTRATION_HPP_
25 #define PLANEREGISTRATION_HPP_
26 
27 #include <vector>
28 #include "mrob/SE3.hpp"
29 #include <Eigen/StdVector>
30 #include "mrob/plane.hpp"
31 #include "mrob/optimizer.hpp"
32 #include "mrob/time_profiling.hpp"
33 
34 #include <unordered_map>
35 #include <memory>
36 
37 
38 namespace mrob{
39 
47 
48  public:
49  // XXX is this mode used anymore? deprecated?
50  enum TrajectoryMode{SEQUENCE=0, INTERPOLATION};
51  // XXX Solve method is almost deprecated
52  //enum SolveModeGrad{GRADIENT_DESCENT_NAIVE=0, GRADIENT_DESCENT_INCR, STEEPEST, HEAVYBALL, MOMENTUM, MOMENTUM_SEQ, BENGIOS_NAG, GRADIENT_DESCENT_BACKTRACKING, BFGS};
53  enum SolveMode{INITIALIZE=0,
54  GRADIENT,
55  GRADIENT_BENGIOS_NAG,
56  GRADIENT_ALL_POSES,
57  GN_HESSIAN,
58  GN_CLAMPED_HESSIAN,
59  LM_SPHER,
60  LM_ELLIP};
61 
62  public:
64  //PlaneRegistration(uint_t numberPlanes , uint_t numberPoses);
66 
67  // Function from the parent class Optimizer
68  virtual matData_t calculate_error() override;
69  virtual void calculate_gradient_hessian() override;
70  virtual void update_state() override;
71  virtual void bookkeep_state() override;
72  virtual void update_state_from_bookkeep() override;
73 
74 
75  // Specific methods
76  void set_number_planes_and_poses(uint_t numPlanes, uint_t numPoses);
77  uint_t get_number_planes() const {return numberPlanes_;};
78  uint_t get_number_poses() const {return numberPoses_;};
79 
84  uint_t solve(SolveMode mode, bool singleIteration = false);
89  uint_t solve_interpolate_gradient(bool singleIteration = false);
90  //Gradient for all poses
91  uint_t solve_gradient_all_poses(bool singleIteration = false);
97  //uint_t solve_interpolate_hessian(bool singleIteration = false);
102  uint_t solve_initialize();
107  uint_t solve_quaternion_plane();
112  void reset_solution();
113  double get_current_error() const;
117  SE3 get_pose(uint_t time);
121  std::vector<SE3>& get_trajectory();
122 
123  SE3 get_last_pose() {return trajectory_->back();}
124 
128  void set_last_pose(SE3 &last);
129 
133  void add_plane(uint_t id, std::shared_ptr<Plane> &plane);
139  void add_new_plane(uint_t id);
145  void plane_push_back_point(uint_t id, uint_t t, Mat31 &point);
146 
147  uint_t calculate_total_number_points();
148  std::shared_ptr<Plane> & get_plane(uint_t id);
149 
150  std::unordered_map<uint_t, std::shared_ptr<Plane>>& get_all_planes() {return planes_;};
151 
152  void set_alpha_parameter(double alpha) {alpha_ = alpha;};
153  void set_beta_parameter(double beta) {beta_ = beta;};
154 
155  double calculate_poses_rmse(std::vector<SE3> & groundTruth) const;
156 
157  void print(bool plotPlanes = true) const;
158 
170  std::vector<double> print_evaluate();
171 
176  void add_point_cloud_planes(uint_t time, std::vector<Mat31>& points, std::vector<uint_t>& point_ids);
181  std::vector<Mat31> get_point_cloud(uint_t time);
182  std::vector<Mat31> get_point_plane_ids(uint_t time);
183 
184  protected:
185  // flag for detecting when is has been solved
186  uint_t numberPlanes_, numberPoses_, numberPoints_;
187  uint_t isSolved_;
188  PlaneRegistration::TrajectoryMode trajMode_ {};
189  uint_t time_{};
190  std::unordered_map<uint_t, std::shared_ptr<Plane>> planes_;
191  std::shared_ptr<std::vector<SE3>> trajectory_;
192  std::vector<SE3> temporary_trajectory_;
193  SE3 bookept_trajectory_;//last pose is stored/bookept
194  double tau_ {};//variable for weighting the number of poses in traj
195  uint_t solveIters_ {};
196 
197  // 1st order parameters methods if used
198  PlaneRegistration::SolveMode solveMode_;
199  std::vector<Mat61> previousState_;
200  double c1_, c2_; //parameters for the Wolfe conditions DEPRECATED?
201  double alpha_, beta_;
202 
203 
204  //2nd order data (if used) TODO remove since they are defined in parent class
205  Mat61 gradient__;
206  Mat6 hessian__;
207 
208  // time profiling
209  TimeProfiling time_profiles_;
210  double initial_error_ {}; // for benchmark purposes
211 
212 
213  // alternative structure to keep planes. This is only for the python bindings
214 
215 };
216 
217 
218 
219 }// namespace
220 #endif /* PLANEREGISTRATION_HPP_ */
void add_point_cloud_planes(uint_t time, std::vector< Mat31 > &points, std::vector< uint_t > &point_ids)
uint_t solve_quaternion_plane()
Definition: plane_registration.cpp:241
Definition: SE3.hpp:50
virtual void update_state() override
Definition: plane_registration.cpp:513
virtual void calculate_gradient_hessian() override
Definition: plane_registration.cpp:490
uint_t solve(SolveMode mode, bool singleIteration=false)
Definition: plane_registration.cpp:80
void reset_solution()
Definition: plane_registration.cpp:70
void add_new_plane(uint_t id)
Definition: plane_registration.cpp:333
Definition: optimizer.hpp:151
SE3 get_pose(uint_t time)
Definition: plane_registration.cpp:382
std::vector< SE3 > & get_trajectory()
Definition: plane_registration.cpp:390
uint_t solve_initialize()
Definition: plane_registration.cpp:251
uint_t solve_interpolate_gradient(bool singleIteration=false)
Definition: plane_registration.cpp:131
void plane_push_back_point(uint_t id, uint_t t, Mat31 &point)
Definition: plane_registration.cpp:340
virtual matData_t calculate_error() override
Definition: plane_registration.cpp:485
virtual void update_state_from_bookkeep() override
Definition: plane_registration.cpp:532
Definition: time_profiling.hpp:40
std::vector< double > print_evaluate()
Definition: plane_registration.cpp:414
Definition: plane_registration.hpp:46
void set_last_pose(SE3 &last)
Definition: plane_registration.cpp:315
Special Euclidean (group) in 3d Is the group representing rotations and translations, that is, rigid body transformations. SE3 = {T = [R t] | R SO3 , t Re^3 } [0 1] Associated to the groups of RBT, there is the Lie algebra se3 representing the same transformation in the tangent space around the identity. Particularly, xi =[w , v] Re^6, where w Re^3 represents the rotation and v the translation. We will preserve this order in this class.
Definition: matrix_base.hpp:36
std::vector< Mat31 > get_point_cloud(uint_t time)
Definition: plane_registration.cpp:370
virtual void bookkeep_state() override
Definition: plane_registration.cpp:527
void add_plane(uint_t id, std::shared_ptr< Plane > &plane)
Definition: plane_registration.cpp:327