2010-10-14 12:54:38 +08:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
|
|
|
|
* Atlanta, Georgia 30332-0415
|
|
|
|
* All Rights Reserved
|
|
|
|
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
|
|
|
|
|
|
|
|
* See LICENSE for the license information
|
|
|
|
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
2011-10-14 11:23:14 +08:00
|
|
|
/**
|
|
|
|
* @file testGraph.cpp
|
|
|
|
* @date Jan 12, 2010
|
|
|
|
* @author nikai
|
|
|
|
* @brief unit test for graph-inl.h
|
2010-01-13 00:12:25 +08:00
|
|
|
*/
|
|
|
|
|
2012-07-24 06:45:07 +08:00
|
|
|
#include <gtsam/slam/BetweenFactor.h>
|
|
|
|
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
|
|
|
|
#include <gtsam/inference/graph.h>
|
|
|
|
#include <gtsam/geometry/Pose2.h>
|
2010-01-13 00:12:25 +08:00
|
|
|
|
2010-10-26 04:10:33 +08:00
|
|
|
#include <CppUnitLite/TestHarness.h>
|
2010-01-13 00:12:25 +08:00
|
|
|
|
2012-06-03 00:18:40 +08:00
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#include <boost/assign/std/list.hpp> // for operator +=
|
|
|
|
using namespace boost::assign;
|
2010-01-18 13:38:53 +08:00
|
|
|
|
2012-06-03 00:18:40 +08:00
|
|
|
#include <iostream>
|
2010-01-13 00:12:25 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace gtsam;
|
|
|
|
|
2010-01-19 03:11:22 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
// x1 -> x2
|
|
|
|
// -> x3 -> x4
|
|
|
|
// -> x5
|
|
|
|
TEST ( Ordering, predecessorMap2Keys ) {
|
2012-02-21 08:53:35 +08:00
|
|
|
PredecessorMap<Key> p_map;
|
2012-06-03 00:18:40 +08:00
|
|
|
p_map.insert(1,1);
|
|
|
|
p_map.insert(2,1);
|
|
|
|
p_map.insert(3,1);
|
|
|
|
p_map.insert(4,3);
|
|
|
|
p_map.insert(5,1);
|
2012-02-07 12:58:11 +08:00
|
|
|
|
2012-02-21 08:53:35 +08:00
|
|
|
list<Key> expected;
|
2012-06-03 00:18:40 +08:00
|
|
|
expected += 4,5,3,2,1;
|
2012-02-07 12:58:11 +08:00
|
|
|
|
2012-02-21 08:53:35 +08:00
|
|
|
list<Key> actual = predecessorMap2Keys<Key>(p_map);
|
2010-01-19 03:11:22 +08:00
|
|
|
LONGS_EQUAL(expected.size(), actual.size());
|
|
|
|
|
2012-02-21 08:53:35 +08:00
|
|
|
list<Key>::const_iterator it1 = expected.begin();
|
|
|
|
list<Key>::const_iterator it2 = actual.begin();
|
2010-01-19 03:11:22 +08:00
|
|
|
for(; it1!=expected.end(); it1++, it2++)
|
|
|
|
CHECK(*it1 == *it2)
|
|
|
|
}
|
|
|
|
|
2010-01-13 00:12:25 +08:00
|
|
|
/* ************************************************************************* */
|
2010-01-14 11:21:07 +08:00
|
|
|
TEST( Graph, predecessorMap2Graph )
|
|
|
|
{
|
|
|
|
typedef SGraph<string>::Vertex SVertex;
|
2012-02-21 08:53:35 +08:00
|
|
|
SGraph<Key> graph;
|
2010-01-14 11:21:07 +08:00
|
|
|
SVertex root;
|
2012-02-21 08:53:35 +08:00
|
|
|
map<Key, SVertex> key2vertex;
|
2010-01-14 11:21:07 +08:00
|
|
|
|
2012-02-21 08:53:35 +08:00
|
|
|
PredecessorMap<Key> p_map;
|
2012-06-03 00:18:40 +08:00
|
|
|
p_map.insert(1, 2);
|
|
|
|
p_map.insert(2, 2);
|
|
|
|
p_map.insert(3, 2);
|
2012-06-05 02:46:05 +08:00
|
|
|
boost::tie(graph, root, key2vertex) = predecessorMap2Graph<SGraph<Key>, SVertex, Key>(p_map);
|
2010-01-14 11:21:07 +08:00
|
|
|
|
|
|
|
LONGS_EQUAL(3, boost::num_vertices(graph));
|
2012-06-03 00:18:40 +08:00
|
|
|
CHECK(root == key2vertex[2]);
|
2010-01-14 11:21:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************************* */
|
2010-01-13 00:12:25 +08:00
|
|
|
TEST( Graph, composePoses )
|
|
|
|
{
|
2012-07-24 06:45:07 +08:00
|
|
|
NonlinearFactorGraph graph;
|
2012-06-23 03:36:49 +08:00
|
|
|
SharedNoiseModel cov = noiseModel::Unit::Create(3);
|
2010-01-27 10:49:58 +08:00
|
|
|
Pose2 p1(1.0, 2.0, 0.3), p2(4.0, 5.0, 0.6), p3(7.0, 8.0, 0.9), p4(2.0, 2.0, 2.9);
|
2010-08-27 03:55:40 +08:00
|
|
|
Pose2 p12=p1.between(p2), p23=p2.between(p3), p43=p4.between(p3);
|
2012-07-24 06:45:07 +08:00
|
|
|
graph.add(BetweenFactor<Pose2>(1,2, p12, cov));
|
|
|
|
graph.add(BetweenFactor<Pose2>(2,3, p23, cov));
|
|
|
|
graph.add(BetweenFactor<Pose2>(4,3, p43, cov));
|
2010-01-13 00:12:25 +08:00
|
|
|
|
2012-02-21 08:53:35 +08:00
|
|
|
PredecessorMap<Key> tree;
|
2012-06-03 00:18:40 +08:00
|
|
|
tree.insert(1,2);
|
|
|
|
tree.insert(2,2);
|
|
|
|
tree.insert(3,2);
|
|
|
|
tree.insert(4,3);
|
2010-01-13 00:12:25 +08:00
|
|
|
|
2010-01-27 10:49:58 +08:00
|
|
|
Pose2 rootPose = p2;
|
2010-01-13 00:12:25 +08:00
|
|
|
|
2012-07-24 06:45:07 +08:00
|
|
|
boost::shared_ptr<Values> actual = composePoses<NonlinearFactorGraph, BetweenFactor<Pose2>, Pose2, Key> (graph, tree, rootPose);
|
Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just says something like
if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
2010-01-14 06:25:03 +08:00
|
|
|
|
2012-02-03 00:16:46 +08:00
|
|
|
Values expected;
|
2012-06-03 00:18:40 +08:00
|
|
|
expected.insert(1, p1);
|
|
|
|
expected.insert(2, p2);
|
|
|
|
expected.insert(3, p3);
|
|
|
|
expected.insert(4, p4);
|
2010-01-13 00:12:25 +08:00
|
|
|
|
2010-01-27 10:49:58 +08:00
|
|
|
LONGS_EQUAL(4, actual->size());
|
2010-01-13 10:09:16 +08:00
|
|
|
CHECK(assert_equal(expected, *actual));
|
2010-01-13 00:12:25 +08:00
|
|
|
}
|
|
|
|
|
2012-09-04 02:00:26 +08:00
|
|
|
// SL-FIX TEST( GaussianFactorGraph, findMinimumSpanningTree )
|
|
|
|
//{
|
|
|
|
// GaussianFactorGraph g;
|
|
|
|
// Matrix I = eye(2);
|
|
|
|
// Vector b = Vector_(0, 0, 0);
|
|
|
|
// g.add(X(1), I, X(2), I, b, model);
|
|
|
|
// g.add(X(1), I, X(3), I, b, model);
|
|
|
|
// g.add(X(1), I, X(4), I, b, model);
|
|
|
|
// g.add(X(2), I, X(3), I, b, model);
|
|
|
|
// g.add(X(2), I, X(4), I, b, model);
|
|
|
|
// g.add(X(3), I, X(4), I, b, model);
|
|
|
|
//
|
|
|
|
// map<string, string> tree = g.findMinimumSpanningTree<string, GaussianFactor>();
|
|
|
|
// EXPECT(tree[X(1)].compare(X(1))==0);
|
|
|
|
// EXPECT(tree[X(2)].compare(X(1))==0);
|
|
|
|
// EXPECT(tree[X(3)].compare(X(1))==0);
|
|
|
|
// EXPECT(tree[X(4)].compare(X(1))==0);
|
|
|
|
//}
|
|
|
|
|
|
|
|
///* ************************************************************************* */
|
|
|
|
// SL-FIX TEST( GaussianFactorGraph, split )
|
|
|
|
//{
|
|
|
|
// GaussianFactorGraph g;
|
|
|
|
// Matrix I = eye(2);
|
|
|
|
// Vector b = Vector_(0, 0, 0);
|
|
|
|
// g.add(X(1), I, X(2), I, b, model);
|
|
|
|
// g.add(X(1), I, X(3), I, b, model);
|
|
|
|
// g.add(X(1), I, X(4), I, b, model);
|
|
|
|
// g.add(X(2), I, X(3), I, b, model);
|
|
|
|
// g.add(X(2), I, X(4), I, b, model);
|
|
|
|
//
|
|
|
|
// PredecessorMap<string> tree;
|
|
|
|
// tree[X(1)] = X(1);
|
|
|
|
// tree[X(2)] = X(1);
|
|
|
|
// tree[X(3)] = X(1);
|
|
|
|
// tree[X(4)] = X(1);
|
|
|
|
//
|
|
|
|
// GaussianFactorGraph Ab1, Ab2;
|
|
|
|
// g.split<string, GaussianFactor>(tree, Ab1, Ab2);
|
|
|
|
// LONGS_EQUAL(3, Ab1.size());
|
|
|
|
// LONGS_EQUAL(2, Ab2.size());
|
|
|
|
//}
|
|
|
|
|
2012-09-04 02:36:05 +08:00
|
|
|
///* ************************************************************************* */
|
|
|
|
// SL-FIX TEST( FactorGraph, splitMinimumSpanningTree )
|
|
|
|
//{
|
|
|
|
// SymbolicFactorGraph G;
|
|
|
|
// G.push_factor("x1", "x2");
|
|
|
|
// G.push_factor("x1", "x3");
|
|
|
|
// G.push_factor("x1", "x4");
|
|
|
|
// G.push_factor("x2", "x3");
|
|
|
|
// G.push_factor("x2", "x4");
|
|
|
|
// G.push_factor("x3", "x4");
|
|
|
|
//
|
|
|
|
// SymbolicFactorGraph T, C;
|
|
|
|
// boost::tie(T, C) = G.splitMinimumSpanningTree();
|
|
|
|
//
|
|
|
|
// SymbolicFactorGraph expectedT, expectedC;
|
|
|
|
// expectedT.push_factor("x1", "x2");
|
|
|
|
// expectedT.push_factor("x1", "x3");
|
|
|
|
// expectedT.push_factor("x1", "x4");
|
|
|
|
// expectedC.push_factor("x2", "x3");
|
|
|
|
// expectedC.push_factor("x2", "x4");
|
|
|
|
// expectedC.push_factor("x3", "x4");
|
|
|
|
// CHECK(assert_equal(expectedT,T));
|
|
|
|
// CHECK(assert_equal(expectedC,C));
|
|
|
|
//}
|
|
|
|
|
|
|
|
///* ************************************************************************* */
|
|
|
|
///**
|
|
|
|
// * x1 - x2 - x3 - x4 - x5
|
|
|
|
// * | | / |
|
|
|
|
// * l1 l2 l3
|
|
|
|
// */
|
|
|
|
// SL-FIX TEST( FactorGraph, removeSingletons )
|
|
|
|
//{
|
|
|
|
// SymbolicFactorGraph G;
|
|
|
|
// G.push_factor("x1", "x2");
|
|
|
|
// G.push_factor("x2", "x3");
|
|
|
|
// G.push_factor("x3", "x4");
|
|
|
|
// G.push_factor("x4", "x5");
|
|
|
|
// G.push_factor("x2", "l1");
|
|
|
|
// G.push_factor("x3", "l2");
|
|
|
|
// G.push_factor("x4", "l2");
|
|
|
|
// G.push_factor("x4", "l3");
|
|
|
|
//
|
|
|
|
// SymbolicFactorGraph singletonGraph;
|
|
|
|
// set<Symbol> singletons;
|
|
|
|
// boost::tie(singletonGraph, singletons) = G.removeSingletons();
|
|
|
|
//
|
|
|
|
// set<Symbol> singletons_excepted; singletons_excepted += "x1", "x2", "x5", "l1", "l3";
|
|
|
|
// CHECK(singletons_excepted == singletons);
|
|
|
|
//
|
|
|
|
// SymbolicFactorGraph singletonGraph_excepted;
|
|
|
|
// singletonGraph_excepted.push_factor("x2", "l1");
|
|
|
|
// singletonGraph_excepted.push_factor("x4", "l3");
|
|
|
|
// singletonGraph_excepted.push_factor("x1", "x2");
|
|
|
|
// singletonGraph_excepted.push_factor("x4", "x5");
|
|
|
|
// singletonGraph_excepted.push_factor("x2", "x3");
|
|
|
|
// CHECK(singletonGraph_excepted.equals(singletonGraph));
|
|
|
|
//}
|
|
|
|
|
2010-01-13 00:12:25 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
int main() {
|
|
|
|
TestResult tr;
|
|
|
|
return TestRegistry::runAllTests(tr);
|
|
|
|
}
|
|
|
|
/* ************************************************************************* */
|