ISAM2Example_SmartFactor.cpp
Go to the documentation of this file.
1 
12 
13 #include <iostream>
14 #include <vector>
15 
16 using namespace std;
17 using namespace gtsam;
20 
21 // Make the typename short so it looks much cleaner
23 
24 int main(int argc, char* argv[]) {
25  Cal3_S2::shared_ptr K(new Cal3_S2(50.0, 50.0, 0.0, 50.0, 50.0));
26 
27  auto measurementNoise =
28  noiseModel::Isotropic::Sigma(2, 1.0); // one pixel in u and v
29 
30  Vector6 sigmas;
31  sigmas << Vector3::Constant(0.1), Vector3::Constant(0.3);
32  auto noise = noiseModel::Diagonal::Sigmas(sigmas);
33 
35  parameters.relinearizeThreshold = 0.01;
36  parameters.relinearizeSkip = 1;
37  parameters.cacheLinearizedFactors = false;
38  parameters.enableDetailedResults = true;
39  parameters.print();
40  ISAM2 isam(parameters);
41 
42  // Create a factor graph
44  Values initialEstimate;
45 
46  Point3 point(0.0, 0.0, 1.0);
47 
48  // Intentionally initialize the variables off from the ground truth
49  Pose3 delta(Rot3::Rodrigues(0.0, 0.0, 0.0), Point3(0.05, -0.10, 0.20));
50 
51  Pose3 pose1(Rot3(), Point3(0.0, 0.0, 0.0));
52  Pose3 pose2(Rot3(), Point3(0.0, 0.2, 0.0));
53  Pose3 pose3(Rot3(), Point3(0.0, 0.4, 0.0));
54  Pose3 pose4(Rot3(), Point3(0.0, 0.5, 0.0));
55  Pose3 pose5(Rot3(), Point3(0.0, 0.6, 0.0));
56 
57  vector<Pose3> poses = {pose1, pose2, pose3, pose4, pose5};
58 
59  // Add first pose
60  graph.addPrior(X(0), poses[0], noise);
61  initialEstimate.insert(X(0), poses[0].compose(delta));
62 
63  // Create smart factor with measurement from first pose only
64  SmartFactor::shared_ptr smartFactor(new SmartFactor(measurementNoise, K));
65  smartFactor->add(PinholePose<Cal3_S2>(poses[0], K).project(point), X(0));
66  graph.push_back(smartFactor);
67 
68  // loop over remaining poses
69  for (size_t i = 1; i < 5; i++) {
70  cout << "****************************************************" << endl;
71  cout << "i = " << i << endl;
72 
73  // Add prior on new pose
74  graph.addPrior(X(i), poses[i], noise);
75  initialEstimate.insert(X(i), poses[i].compose(delta));
76 
77  // "Simulate" measurement from this pose
78  PinholePose<Cal3_S2> camera(poses[i], K);
79  Point2 measurement = camera.project(point);
80  cout << "Measurement " << i << "" << measurement << endl;
81 
82  // Add measurement to smart factor
83  smartFactor->add(measurement, X(i));
84 
85  // Update iSAM2
86  ISAM2Result result = isam.update(graph, initialEstimate);
87  result.print();
88 
89  cout << "Detailed results:" << endl;
90  for (auto keyedStatus : result.detail->variableStatus) {
91  const auto& status = keyedStatus.second;
92  PrintKey(keyedStatus.first);
93  cout << " {" << endl;
94  cout << "reeliminated: " << status.isReeliminated << endl;
95  cout << "relinearized above thresh: " << status.isAboveRelinThreshold
96  << endl;
97  cout << "relinearized involved: " << status.isRelinearizeInvolved << endl;
98  cout << "relinearized: " << status.isRelinearized << endl;
99  cout << "observed: " << status.isObserved << endl;
100  cout << "new: " << status.isNew << endl;
101  cout << "in the root clique: " << status.inRootClique << endl;
102  cout << "}" << endl;
103  }
104 
105  Values currentEstimate = isam.calculateEstimate();
106  currentEstimate.print("Current estimate: ");
107 
108  boost::optional<Point3> pointEstimate = smartFactor->point(currentEstimate);
109  if (pointEstimate) {
110  cout << *pointEstimate << endl;
111  } else {
112  cout << "Point degenerate." << endl;
113  }
114 
115  // Reset graph and initial estimate for next iteration
116  graph.resize(0);
117  initialEstimate.clear();
118  }
119 
120  return 0;
121 }
void clear()
Definition: Values.h:311
Smart factor on poses, assuming camera calibration is fixed.
static Point2 project(const Pose3 &pose, const Unit3 &pointAtInfinity, const Cal3_S2::shared_ptr &cal)
void insert(Key j, const Value &val)
Definition: Values.cpp:140
Vector2 Point2
Definition: Point2.h:27
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
double measurement(10.0)
Definition: Half.h:150
IsDerived< DERIVEDFACTOR > push_back(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:166
NonlinearFactorGraph graph
T compose(const T &t1, const T &t2)
Definition: lieProxies.h:39
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
Point3 point(10, 0,-5)
Values calculateEstimate() const
Definition: ISAM2.cpp:763
Base class for all pinhole cameras.
Values result
Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization.
SmartProjectionPoseFactor< Cal3_S2 > SmartFactor
static Pose3 pose3(rt3, pt3)
NonlinearISAM isam(relinearizeInterval)
void print(const std::string &str="") const
print iSAM2 parameters
Definition: ISAM2Params.h:252
static ConjugateGradientParameters parameters
static const Pose3 pose1(Rot3(), Point3(0, 1, 0.5))
RelinearizationThreshold relinearizeThreshold
Definition: ISAM2Params.h:168
traits
Definition: chartTesting.h:28
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:77
Point2 project(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose=boost::none, OptionalJacobian< 2, 3 > Dpoint=boost::none, OptionalJacobian< 2, DimK > Dcal=boost::none) const
project a 3D point from world coordinates into the image
Definition: PinholePose.h:118
void print(const std::string str="") const
Print results.
Definition: ISAM2Result.h:168
virtual ISAM2Result update(const NonlinearFactorGraph &newFactors=NonlinearFactorGraph(), const Values &newTheta=Values(), const FactorIndices &removeFactorIndices=FactorIndices(), const boost::optional< FastMap< Key, int > > &constrainedKeys=boost::none, const boost::optional< FastList< Key > > &noRelinKeys=boost::none, const boost::optional< FastList< Key > > &extraReelimKeys=boost::none, bool force_relinearize=false)
Definition: ISAM2.cpp:396
void resize(size_t size)
Definition: FactorGraph.h:358
void PrintKey(Key key, const string &s, const KeyFormatter &keyFormatter)
Utility function to print one key with optional prefix.
Definition: Key.cpp:40
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
static const Pose3 pose2
Vector3 Point3
Definition: Point3.h:35
static const CalibratedCamera camera(kDefaultPose)
#define X
Definition: icosphere.cpp:20
boost::shared_ptr< Cal3_S2 > shared_ptr
Definition: Cal3_S2.h:39
boost::optional< DetailedResults > detail
Definition: ISAM2Result.h:158
The most common 5DOF 3D->2D calibration.
int main(int argc, char *argv[])


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:22