Cleaned up/naming conventions/docs
parent
67ffd65838
commit
224299ccb9
|
@ -1,5 +1,8 @@
|
||||||
#include <iostream>
|
/**
|
||||||
#include <vector>
|
* @file ISAM2Example_SmartFactor.cpp
|
||||||
|
* @brief test of iSAM with smart factors, led to bitbucket issue #367
|
||||||
|
* @author Alexander (pumaking on BitBucket)
|
||||||
|
*/
|
||||||
|
|
||||||
#include <gtsam/geometry/SimpleCamera.h>
|
#include <gtsam/geometry/SimpleCamera.h>
|
||||||
#include <gtsam/slam/BetweenFactor.h>
|
#include <gtsam/slam/BetweenFactor.h>
|
||||||
|
@ -7,7 +10,13 @@
|
||||||
|
|
||||||
#include <gtsam/nonlinear/ISAM2.h>
|
#include <gtsam/nonlinear/ISAM2.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
using namespace gtsam;
|
using namespace gtsam;
|
||||||
|
using symbol_shorthand::P;
|
||||||
|
using symbol_shorthand::X;
|
||||||
|
|
||||||
// Make the typename short so it looks much cleaner
|
// Make the typename short so it looks much cleaner
|
||||||
typedef SmartProjectionPoseFactor<Cal3_S2> SmartFactor;
|
typedef SmartProjectionPoseFactor<Cal3_S2> SmartFactor;
|
||||||
|
@ -15,11 +24,12 @@ typedef SmartProjectionPoseFactor<Cal3_S2> SmartFactor;
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
Cal3_S2::shared_ptr K(new Cal3_S2(50.0, 50.0, 0.0, 50.0, 50.0));
|
Cal3_S2::shared_ptr K(new Cal3_S2(50.0, 50.0, 0.0, 50.0, 50.0));
|
||||||
|
|
||||||
noiseModel::Isotropic::shared_ptr measurement_noise = noiseModel::Isotropic::Sigma(2, 1.0); // one pixel in u and v
|
auto measurementNoise =
|
||||||
|
noiseModel::Isotropic::Sigma(2, 1.0); // one pixel in u and v
|
||||||
|
|
||||||
noiseModel::Diagonal::shared_ptr noise = noiseModel::Diagonal::Sigmas(
|
Vector6 sigmas;
|
||||||
(Vector(6) << Vector3::Constant(0.3), Vector3::Constant(0.1)).finished());
|
sigmas << Vector3::Constant(0.3), Vector3::Constant(0.1);
|
||||||
noiseModel::Isotropic::shared_ptr large_noise = noiseModel::Isotropic::Sigma(6, 100);
|
auto noise = noiseModel::Diagonal::Sigmas(sigmas);
|
||||||
|
|
||||||
ISAM2Params parameters;
|
ISAM2Params parameters;
|
||||||
parameters.relinearizeThreshold = 0.01;
|
parameters.relinearizeThreshold = 0.01;
|
||||||
|
@ -31,7 +41,7 @@ int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
// Create a factor graph
|
// Create a factor graph
|
||||||
NonlinearFactorGraph graph;
|
NonlinearFactorGraph graph;
|
||||||
Values initial_estimate;
|
Values initialEstimate;
|
||||||
|
|
||||||
Point3 point(0.0, 0.0, 1.0);
|
Point3 point(0.0, 0.0, 1.0);
|
||||||
|
|
||||||
|
@ -44,62 +54,61 @@ int main(int argc, char* argv[]) {
|
||||||
Pose3 pose4(Rot3(), Point3(0.0, 0.5, 0.0));
|
Pose3 pose4(Rot3(), Point3(0.0, 0.5, 0.0));
|
||||||
Pose3 pose5(Rot3(), Point3(0.0, 0.6, 0.0));
|
Pose3 pose5(Rot3(), Point3(0.0, 0.6, 0.0));
|
||||||
|
|
||||||
std::vector<Pose3> pose_list = { pose1, pose2, pose3, pose4, pose5 };
|
vector<Pose3> poses = {pose1, pose2, pose3, pose4, pose5};
|
||||||
|
|
||||||
SmartFactor::shared_ptr smart_factor(new SmartFactor(measurement_noise, K));
|
SmartFactor::shared_ptr smartFactor(new SmartFactor(measurementNoise, K));
|
||||||
graph.push_back(smart_factor);
|
graph.push_back(smartFactor);
|
||||||
|
|
||||||
graph.emplace_shared<PriorFactor<Pose3>>(0, pose_list[0], noise);
|
graph.emplace_shared<PriorFactor<Pose3>>(X(0), poses[0], noise);
|
||||||
initial_estimate.insert(0, pose_list[0].compose(delta));
|
initialEstimate.insert(X(0), poses[0].compose(delta));
|
||||||
smart_factor->add(PinholePose<Cal3_S2>(pose_list[0], K).project(point), 0);
|
smartFactor->add(PinholePose<Cal3_S2>(poses[0], K).project(point), X(0));
|
||||||
|
|
||||||
for (int i = 1; i < 5; i++) {
|
for (size_t i = 1; i < 5; i++) {
|
||||||
PinholePose<Cal3_S2> camera(pose_list[i], K);
|
cout << "****************************************************" << endl;
|
||||||
|
cout << "i = " << i << endl;
|
||||||
|
|
||||||
|
// Add measurement to smart factor
|
||||||
|
PinholePose<Cal3_S2> camera(poses[i], K);
|
||||||
Point2 measurement = camera.project(point);
|
Point2 measurement = camera.project(point);
|
||||||
|
smartFactor->add(measurement, X(i));
|
||||||
|
cout << "Measurement " << i << "" << measurement << endl;
|
||||||
|
|
||||||
std::cout << "****************************************************" << std::endl;
|
// Add prior on new pose
|
||||||
std::cout << "i = " << i << std::endl;
|
graph.emplace_shared<PriorFactor<Pose3>>(X(i), poses[i], noise);
|
||||||
std::cout << "Measurement " << i << " is " << measurement << std::endl;
|
initialEstimate.insert(X(i), poses[i].compose(delta));
|
||||||
|
|
||||||
graph.emplace_shared<PriorFactor<Pose3>>(i, pose_list[i], noise);
|
|
||||||
//graph.emplace_shared<BetweenFactor<Pose3>>(i - 1, i, Pose3(), large_noise);
|
|
||||||
initial_estimate.insert(i, pose_list[i].compose(delta));
|
|
||||||
smart_factor->add(measurement, i);
|
|
||||||
|
|
||||||
ISAM2Result result = isam.update(graph, initial_estimate);
|
|
||||||
graph.resize(0);
|
|
||||||
initial_estimate.clear();
|
|
||||||
|
|
||||||
|
// Update iSAM2
|
||||||
|
ISAM2Result result = isam.update(graph, initialEstimate);
|
||||||
result.print();
|
result.print();
|
||||||
|
|
||||||
const auto& var_map = (*(result.detail)).variableStatus;
|
cout << "Detailed results:" << endl;
|
||||||
|
for (auto keyedStatus : result.detail->variableStatus) {
|
||||||
std::cout << "Detailed results:" << std::endl;
|
const auto& status = keyedStatus.second;
|
||||||
for (int j = 0; j < 3; j++) {
|
cout << keyedStatus.first << " {" << endl;
|
||||||
if (var_map.exists(j)) {
|
cout << "reeliminated: " << status.isReeliminated << endl;
|
||||||
std::cout << j << " is reeliminated: " << var_map.at(j).isReeliminated << std::endl;
|
cout << "relinearized above thresh: " << status.isAboveRelinThreshold
|
||||||
std::cout << j << " is relinearized above thresh: " << var_map.at(j).isAboveRelinThreshold << std::endl;
|
<< endl;
|
||||||
std::cout << j << " is relinearized involved: " << var_map.at(j).isRelinearizeInvolved << std::endl;
|
cout << "relinearized involved: " << status.isRelinearizeInvolved << endl;
|
||||||
std::cout << j << " is relinearized: " << var_map.at(j).isRelinearized << std::endl;
|
cout << "relinearized: " << status.isRelinearized << endl;
|
||||||
std::cout << j << " is observed: " << var_map.at(j).isObserved << std::endl;
|
cout << "observed: " << status.isObserved << endl;
|
||||||
std::cout << j << " is new: " << var_map.at(j).isNew << std::endl;
|
cout << "new: " << status.isNew << endl;
|
||||||
std::cout << j << " is in the root clique: " << var_map.at(j).inRootClique << std::endl;
|
cout << "in the root clique: " << status.inRootClique << endl;
|
||||||
}
|
cout << "}" << endl;
|
||||||
else {
|
|
||||||
std::cout << j << " does not exist in the detailed results map." << std::endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Values current_estimate = isam.calculateEstimate();
|
Values currentEstimate = isam.calculateEstimate();
|
||||||
current_estimate.print("Current estimate: ");
|
currentEstimate.print("Current estimate: ");
|
||||||
|
|
||||||
boost::optional<Point3> point_res = smart_factor->point(current_estimate);
|
boost::optional<Point3> pointEstimate = smartFactor->point(currentEstimate);
|
||||||
if (point_res) {
|
if (pointEstimate) {
|
||||||
std::cout << *point_res << std::endl;
|
cout << *pointEstimate << endl;
|
||||||
}
|
} else {
|
||||||
else {
|
cout << "Point degenerate." << endl;
|
||||||
std::cout << "Point is degenerate." << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset graph and initial estimate for next iteration
|
||||||
|
graph.resize(0);
|
||||||
|
initialEstimate.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue