iSAM
covariances.cpp
Go to the documentation of this file.
00001 
00028 #include <iostream>
00029 #include <stdio.h>
00030 
00031 #include <Eigen/LU> 
00032 
00033 #include <isam/isam.h>
00034 
00035 using namespace std;
00036 using namespace isam;
00037 using namespace Eigen;
00038 
00039 Slam slam;
00040 
00041 int main(int argc, const char* argv[]) {
00042   Pose2d origin;
00043   Noise noise = SqrtInformation(10. * eye(3));
00044   Pose2d_Node* pose_node_1 = new Pose2d_Node(); // create node
00045   slam.add_node(pose_node_1); // add it to the Slam graph
00046   Pose2d_Factor* prior = new Pose2d_Factor(pose_node_1, origin, noise); // create prior measurement, an factor
00047   slam.add_factor(prior); // add it to the Slam graph
00048 
00049   Pose2d_Node* pose_node_2 = new Pose2d_Node(); // create node
00050   slam.add_node(pose_node_2); // add it to the Slam graph
00051 
00052   Pose2d delta(1., 0., 0.);
00053   Pose2d_Pose2d_Factor* odo = new Pose2d_Pose2d_Factor(pose_node_1, pose_node_2, delta, noise);
00054   slam.add_factor(odo);
00055 
00056   slam.batch_optimization();
00057 
00058 #if 0
00059   const Covariances& covariances = slam.covariances();
00060 #else
00061   // operate on a copy (just an example, cloning is useful for running
00062   // covariance recovery in a separate thread)
00063   const Covariances& covariances = slam.covariances().clone();
00064 #endif
00065 
00066   // recovering the full covariance matrix
00067   cout << "Full covariance matrix:" << endl;
00068   MatrixXd cov_full = covariances.marginal(slam.get_nodes());
00069   cout << cov_full << endl << endl;
00070 
00071   // sanity checking by inverting the information matrix, not using R
00072   SparseSystem Js = slam.jacobian();
00073   MatrixXd J(Js.num_cols(), Js.num_cols());
00074   for (int r=0; r<Js.num_cols(); r++) {
00075     for (int c=0; c<Js.num_cols(); c++) {
00076       J(r,c) = Js(r,c);
00077     }
00078   }
00079   MatrixXd H = J.transpose() * J;
00080   MatrixXd cov_full2 = H.inverse();
00081   cout << cov_full2 << endl;
00082 
00083   // recovering the block-diagonals only of the full covariance matrix
00084   cout << "Block-diagonals only:" << endl;
00085   Covariances::node_lists_t node_lists;
00086   list<Node*> nodes;
00087   nodes.push_back(pose_node_1);
00088   node_lists.push_back(nodes);
00089   nodes.clear();
00090   nodes.push_back(pose_node_2);
00091   node_lists.push_back(nodes);
00092   list<MatrixXd> cov_blocks = covariances.marginal(node_lists);
00093   int i = 1;
00094   for (list<MatrixXd>::iterator it = cov_blocks.begin(); it!=cov_blocks.end(); it++, i++) {
00095     cout << "block " << i << endl;
00096     cout << *it << endl;
00097   }
00098 
00099   // recovering individual entries, here the right block column
00100   cout << "Right block column:" << endl;
00101   Covariances::node_pair_list_t node_pair_list;
00102   node_pair_list.push_back(make_pair(pose_node_1, pose_node_2));
00103   node_pair_list.push_back(make_pair(pose_node_2, pose_node_2));
00104   list<MatrixXd> cov_entries = covariances.access(node_pair_list);
00105   for (list<MatrixXd>::iterator it = cov_entries.begin(); it!=cov_entries.end(); it++) {
00106     cout << *it << endl;
00107   }
00108 }
 All Classes Files Functions Variables