MarginalizationFactor.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifndef MARGINALIZATIONFACTOR_H_
  2. #define MARGINALIZATIONFACTOR_H_
  3. #include <ceres/ceres.h>
  4. #include <Eigen/Eigen>
  5. #include <unordered_map>
  6. #include "utils/common.h"
  7. #include "utils/math_tools.h"
  8. const int NUM_THREADS = 4;
  9. struct ResidualBlockInfo {
  10. ResidualBlockInfo(ceres::CostFunction *_cost_function,
  11. ceres::LossFunction *_loss_function,
  12. std::vector<double *> _parameter_blocks,
  13. std::vector<int> _drop_set)
  14. : cost_function(_cost_function),
  15. loss_function(_loss_function),
  16. parameter_blocks(_parameter_blocks),
  17. drop_set(_drop_set) {}
  18. void Evaluate();
  19. ceres::CostFunction *cost_function;
  20. ceres::LossFunction *loss_function;
  21. std::vector<double *> parameter_blocks;
  22. std::vector<int> drop_set;
  23. double **raw_jacobians;
  24. std::vector<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> jacobians;
  25. Eigen::VectorXd residuals;
  26. };
  27. class MarginalizationInfo {
  28. public:
  29. ~MarginalizationInfo();
  30. int LocalSize(int size) const;
  31. void AddResidualBlockInfo(ResidualBlockInfo *residual_block_info);
  32. void PreMarginalize();
  33. void Marginalize();
  34. std::vector<double *> GetParameterBlocks(std::unordered_map<long, double *> &addr_shift);
  35. std::vector<ResidualBlockInfo *> factors;
  36. int m, n;
  37. std::unordered_map<long, int> parameter_block_size; //global size
  38. int sum_block_size;
  39. std::unordered_map<long, int> parameter_block_idx; //local size
  40. std::unordered_map<long, double *> parameter_block_data;
  41. std::vector<int> keep_block_size; //global size
  42. std::vector<int> keep_block_idx; //local size
  43. std::vector<double *> keep_block_data;
  44. Eigen::MatrixXd linearized_jacobians;
  45. Eigen::VectorXd linearized_residuals;
  46. const double eps = 1e-8;
  47. };
  48. class MarginalizationFactor : public ceres::CostFunction {
  49. public:
  50. MarginalizationFactor(MarginalizationInfo* _marginalization_info);
  51. virtual bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const;
  52. MarginalizationInfo* marginalization_info;
  53. };
  54. struct ThreadsStruct {
  55. std::vector<ResidualBlockInfo *> sub_factors;
  56. Eigen::MatrixXd A;
  57. Eigen::VectorXd b;
  58. std::unordered_map<long, int> parameter_block_size; //global size
  59. std::unordered_map<long, int> parameter_block_idx; //local size
  60. };
  61. #endif //MARGINALIZATIONFACTOR_H_