SFMExampleExpressions.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file SFMExampleExpressions.cpp
  10. * @brief A structure-from-motion example done with Expressions
  11. * @author Frank Dellaert
  12. * @author Duy-Nguyen Ta
  13. * @date October 1, 2014
  14. */
  15. /**
  16. * This is the Expression version of SFMExample
  17. * See detailed description of headers there, this focuses on explaining the AD part
  18. */
  19. // The two new headers that allow using our Automatic Differentiation Expression framework
  20. #include <gtsam/slam/expressions.h>
  21. #include <gtsam/nonlinear/ExpressionFactorGraph.h>
  22. // Header order is close to far
  23. #include "SFMdata.h"
  24. #include <gtsam/geometry/Point2.h>
  25. #include <gtsam/nonlinear/DoglegOptimizer.h>
  26. #include <gtsam/nonlinear/Values.h>
  27. #include <gtsam/inference/Symbol.h>
  28. #include <vector>
  29. using namespace std;
  30. using namespace gtsam;
  31. using namespace noiseModel;
  32. /* ************************************************************************* */
  33. int main(int argc, char* argv[]) {
  34. Cal3_S2 K(50.0, 50.0, 0.0, 50.0, 50.0);
  35. Isotropic::shared_ptr measurementNoise = Isotropic::Sigma(2, 1.0); // one pixel in u and v
  36. // Create the set of ground-truth landmarks and poses
  37. vector<Point3> points = createPoints();
  38. vector<Pose3> poses = createPoses();
  39. // Create a factor graph
  40. ExpressionFactorGraph graph;
  41. // Specify uncertainty on first pose prior
  42. Vector6 sigmas; sigmas << Vector3(0.3,0.3,0.3), Vector3(0.1,0.1,0.1);
  43. Diagonal::shared_ptr poseNoise = Diagonal::Sigmas(sigmas);
  44. // Here we don't use a PriorFactor but directly the ExpressionFactor class
  45. // x0 is an Expression, and we create a factor wanting it to be equal to poses[0]
  46. Pose3_ x0('x',0);
  47. graph.addExpressionFactor(x0, poses[0], poseNoise);
  48. // We create a constant Expression for the calibration here
  49. Cal3_S2_ cK(K);
  50. // Simulated measurements from each camera pose, adding them to the factor graph
  51. for (size_t i = 0; i < poses.size(); ++i) {
  52. Pose3_ x('x', i);
  53. PinholeCamera<Cal3_S2> camera(poses[i], K);
  54. for (size_t j = 0; j < points.size(); ++j) {
  55. Point2 measurement = camera.project(points[j]);
  56. // Below an expression for the prediction of the measurement:
  57. Point3_ p('l', j);
  58. Point2_ prediction = uncalibrate(cK, project(transformTo(x, p)));
  59. // Again, here we use an ExpressionFactor
  60. graph.addExpressionFactor(prediction, measurement, measurementNoise);
  61. }
  62. }
  63. // Add prior on first point to constrain scale, again with ExpressionFactor
  64. Isotropic::shared_ptr pointNoise = Isotropic::Sigma(3, 0.1);
  65. graph.addExpressionFactor(Point3_('l', 0), points[0], pointNoise);
  66. // Create perturbed initial
  67. Values initial;
  68. Pose3 delta(Rot3::Rodrigues(-0.1, 0.2, 0.25), Point3(0.05, -0.10, 0.20));
  69. for (size_t i = 0; i < poses.size(); ++i)
  70. initial.insert(Symbol('x', i), poses[i].compose(delta));
  71. for (size_t j = 0; j < points.size(); ++j)
  72. initial.insert<Point3>(Symbol('l', j), points[j] + Point3(-0.25, 0.20, 0.15));
  73. cout << "initial error = " << graph.error(initial) << endl;
  74. /* Optimize the graph and print results */
  75. Values result = DoglegOptimizer(graph, initial).optimize();
  76. cout << "final error = " << graph.error(result) << endl;
  77. return 0;
  78. }
  79. /* ************************************************************************* */