27 #include <boost/assign/std/vector.hpp> 33 using namespace gtsam;
63 static double missing_uR = std::numeric_limits<double>::quiet_NaN();
68 vector<StereoPoint2> measurements_cam;
73 measurements_cam.push_back(cam1_uv1);
74 measurements_cam.push_back(cam2_uv1);
75 measurements_cam.push_back(cam3_uv1);
77 return measurements_cam;
164 values.
insert(
x2, level_pose_right);
168 factor1.
add(level_uv_right,
x2,
K2);
170 double actualError = factor1.
error(values);
171 double expectedError = 0.0;
174 SmartStereoProjectionPoseFactor::Cameras cameras = factor1.
cameras(values);
205 values.insert(
x2, level_pose_right);
209 factor1.
add(level_uv_right_missing,
x2,
K2);
211 double actualError = factor1.
error(values);
212 double expectedError = 0.0;
216 SmartStereoProjectionPoseFactor::Cameras cameras = factor1.
cameras(values);
230 factor2.
add(level_uv_missing,
x1,
K2);
231 factor2.
add(level_uv_right_missing,
x2,
K2);
258 Pose3 noise_pose = Pose3(Rot3::Ypr(-
M_PI / 10, 0., -
M_PI / 10),
263 factor1->add(level_uv,
x1,
K);
264 factor1->add(level_uv_right,
x2,
K);
266 double actualError1 = factor1->error(values);
269 vector<StereoPoint2> measurements;
270 measurements.push_back(level_uv);
271 measurements.push_back(level_uv_right);
273 vector<boost::shared_ptr<Cal3_S2Stereo> > Ks;
281 factor2->add(measurements, views, Ks);
283 double actualError2 = factor2->error(values);
310 cam2, cam3, landmark1);
312 cam2, cam3, landmark2);
314 cam2, cam3, landmark3);
324 smartFactor1->add(measurements_l1, views,
K2);
327 smartFactor2->add(measurements_l2, views,
K2);
330 smartFactor3->add(measurements_l3, views,
K2);
332 const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10);
342 Pose3 noise_pose = Pose3(Rot3::Ypr(-
M_PI / 100, 0., -
M_PI / 100),
348 values.
insert(
x3, pose3 * noise_pose);
352 Rot3(0, -0.0314107591, 0.99950656, -0.99950656, -0.0313952598,
353 -0.000986635786, 0.0314107591, -0.999013364, -0.0313952598),
354 Point3(0.1, -0.1, 1.9)), values.
at<Pose3>(
x3)));
360 Point3 landmark1_smart = *smartFactor1->point();
361 Point3 landmark2_smart = *smartFactor2->point();
362 Point3 landmark3_smart = *smartFactor3->point();
392 values.
insert(
L(1), landmark1_smart);
393 values.
insert(
L(2), landmark2_smart);
394 values.
insert(
L(3), landmark3_smart);
404 bool verboseCheirality =
true;
406 graph2.
push_back(ProjectionFactor(measurements_l1[0],
model,
x1,
L(1),
K2,
false, verboseCheirality));
407 graph2.
push_back(ProjectionFactor(measurements_l1[1],
model,
x2,
L(1),
K2,
false, verboseCheirality));
408 graph2.
push_back(ProjectionFactor(measurements_l1[2],
model,
x3,
L(1),
K2,
false, verboseCheirality));
410 graph2.
push_back(ProjectionFactor(measurements_l2[0],
model,
x1,
L(2),
K2,
false, verboseCheirality));
411 graph2.
push_back(ProjectionFactor(measurements_l2[1],
model,
x2,
L(2),
K2,
false, verboseCheirality));
412 graph2.
push_back(ProjectionFactor(measurements_l2[2],
model,
x3,
L(2),
K2,
false, verboseCheirality));
414 graph2.
push_back(ProjectionFactor(measurements_l3[0],
model,
x1,
L(3),
K2,
false, verboseCheirality));
415 graph2.
push_back(ProjectionFactor(measurements_l3[1],
model,
x2,
L(3),
K2,
false, verboseCheirality));
416 graph2.
push_back(ProjectionFactor(measurements_l3[2],
model,
x3,
L(3),
K2,
false, verboseCheirality));
422 Values result2 = optimizer2.optimize();
432 Pose3 body_P_sensor =
Pose3(Rot3::Ypr(-0.01, 0., -0.05),
Point3(0.1, 0, 0.1));
466 smartFactor1->add(measurements_l1, views,
K2);
469 smartFactor2->add(measurements_l2, views,
K2);
472 smartFactor3->add(measurements_l3, views,
K2);
474 const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10);
484 Pose3 noise_pose = Pose3(Rot3::Ypr(-
M_PI / 100, 0., -
M_PI / 100),
490 values.
insert(
x3, pose3 * noise_pose);
494 Rot3(0, -0.0314107591, 0.99950656, -0.99950656, -0.0313952598,
495 -0.000986635786, 0.0314107591, -0.999013364, -0.0313952598),
496 Point3(0.1, -0.1, 1.9)), values.
at<Pose3>(
x3)));
524 Pose3 cameraPose3 = cameraPose1 * Pose3(
Rot3(),
Point3(0,-1,0));
543 Point2Vector measurements_cam1, measurements_cam2, measurements_cam3;
557 vector<StereoPoint2> measurements_cam1_stereo, measurements_cam2_stereo, measurements_cam3_stereo;
558 for(
size_t k=0; k<measurements_cam1.size();k++)
559 measurements_cam1_stereo.push_back(
StereoPoint2(measurements_cam1[k].
x() , missing_uR , measurements_cam1[k].
y()));
561 for(
size_t k=0; k<measurements_cam2.size();k++)
562 measurements_cam2_stereo.push_back(
StereoPoint2(measurements_cam2[k].
x() , missing_uR , measurements_cam2[k].
y()));
564 for(
size_t k=0; k<measurements_cam3.size();k++)
565 measurements_cam3_stereo.push_back(
StereoPoint2(measurements_cam3[k].
x() , missing_uR , measurements_cam3[k].
y()));
574 smartFactor1.
add(measurements_cam1_stereo, views, Kmono);
577 smartFactor2.
add(measurements_cam2_stereo, views, Kmono);
580 smartFactor3.
add(measurements_cam3_stereo, views, Kmono);
582 const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10);
597 double actualError = graph.
error(gtValues);
598 double expectedError = 0.0;
606 values.
insert(
x3, bodyPose3*noise_pose);
639 cam2, cam3, landmark1);
641 cam2, cam3, landmark2);
643 cam2, cam3, landmark3);
649 smartFactor1->add(measurements_cam1, views,
K);
652 smartFactor2->add(measurements_cam2, views,
K);
655 smartFactor3->add(measurements_cam3, views,
K);
657 const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10);
667 Pose3 noise_pose = Pose3(Rot3::Ypr(-
M_PI / 100, 0., -
M_PI / 100),
672 values.
insert(
x3, pose3 * noise_pose);
705 cam2, cam3, landmark1);
707 cam2, cam3, landmark2);
709 cam2, cam3, landmark3);
714 sp = measurements_cam2[2];
721 smartFactor1->add(measurements_cam1, views,
K);
724 smartFactor2->add(measurements_cam2, views,
K);
727 smartFactor3->add(measurements_cam3, views,
K);
729 const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10);
739 Pose3 noise_pose = Pose3(Rot3::Ypr(-
M_PI / 100, 0., -
M_PI / 100),
744 values.
insert(
x3, pose3 * noise_pose);
779 cam2, cam3, landmark1);
781 cam2, cam3, landmark2);
783 cam2, cam3, landmark3);
790 smartFactor1->add(measurements_cam1, views,
K);
793 smartFactor2->add(measurements_cam2, views,
K);
796 smartFactor3->add(measurements_cam3, views,
K);
798 const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10);
808 Pose3 noise_pose = Pose3(Rot3::Ypr(-
M_PI / 100, 0., -
M_PI / 100),
813 values.
insert(
x3, pose3 * noise_pose);
848 cam2, cam3, landmark1);
850 cam2, cam3, landmark2);
852 cam2, cam3, landmark3);
854 cam2, cam3, landmark4);
856 measurements_cam4.at(0) = measurements_cam4.at(0) +
StereoPoint2(10, 10, 1);
864 smartFactor1->add(measurements_cam1, views,
K);
867 smartFactor2->add(measurements_cam2, views,
K);
870 smartFactor3->add(measurements_cam3, views,
K);
873 smartFactor4->add(measurements_cam4, views,
K);
877 smartFactor4b->add(measurements_cam4, views,
K);
879 const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10);
889 Pose3 noise_pose = Pose3(Rot3::Ypr(-
M_PI / 100, 0., -
M_PI / 100),
906 EXPECT(smartFactor1->point());
907 EXPECT(smartFactor2->point());
908 EXPECT(smartFactor3->point());
909 EXPECT(smartFactor4->point().outlier());
910 EXPECT(smartFactor4b->point());
1044 views.push_back(
x1);
1045 views.push_back(
x2);
1046 views.push_back(
x3);
1057 Pose3
pose3 = pose2 * Pose3(Rot3::RzRyRx(-0.05, 0.0, -0.05),
Point3(0, 0, 0));
1067 cam2, cam3, landmark1);
1069 cam2, cam3, landmark2);
1071 cam2, cam3, landmark3);
1077 smartFactor1->add(measurements_cam1, views,
K);
1080 smartFactor2->add(measurements_cam2, views,
K);
1083 smartFactor3->add(measurements_cam3, views,
K);
1095 Pose3 noise_pose = Pose3(Rot3::Ypr(-
M_PI / 100, 0., -
M_PI / 100),
1097 values.
insert(
x3, pose3 * noise_pose);
1100 boost::shared_ptr<GaussianFactor> hessianFactor1 = smartFactor1->linearize(
1102 boost::shared_ptr<GaussianFactor> hessianFactor2 = smartFactor2->linearize(
1104 boost::shared_ptr<GaussianFactor> hessianFactor3 = smartFactor3->linearize(
1107 Matrix CumulativeInformation = hessianFactor1->information()
1108 + hessianFactor2->information() + hessianFactor3->information();
1110 boost::shared_ptr<GaussianFactorGraph> GaussianGraph = graph.
linearize(
1112 Matrix GraphInformation = GaussianGraph->hessian().first;
1117 Matrix AugInformationMatrix = hessianFactor1->augmentedInformation()
1118 + hessianFactor2->augmentedInformation()
1119 + hessianFactor3->augmentedInformation();
1122 Vector InfoVector = AugInformationMatrix.block(0, 18, 18, 1);
1313 views.push_back(
x1);
1314 views.push_back(
x2);
1315 views.push_back(
x3);
1332 cam2, cam3, landmark1);
1335 smartFactorInstance->add(measurements_cam1, views,
K);
1342 boost::shared_ptr<GaussianFactor> hessianFactor =
1343 smartFactorInstance->linearize(values);
1346 Pose3 poseDrift = Pose3(Rot3::Ypr(-
M_PI / 2, 0., -
M_PI / 2),
Point3(0, 0, 0));
1353 boost::shared_ptr<GaussianFactor> hessianFactorRot =
1354 smartFactorInstance->linearize(rotValues);
1360 hessianFactorRot->information(), 1
e-7));
1362 Pose3 poseDrift2 = Pose3(Rot3::Ypr(-
M_PI / 2, -
M_PI / 3, -
M_PI / 2),
1370 boost::shared_ptr<GaussianFactor> hessianFactorRotTran =
1371 smartFactorInstance->linearize(tranValues);
1376 hessianFactorRotTran->information(), 1
e-6));
1383 views.push_back(
x1);
1384 views.push_back(
x2);
1385 views.push_back(
x3);
1400 cam2, cam3, landmark1);
1403 smartFactor->add(measurements_cam1, views,
K2);
1410 boost::shared_ptr<GaussianFactor> hessianFactor = smartFactor->linearize(
1414 EXPECT(smartFactor->isValid());
1419 rotValues.
insert(
x1, poseDrift.compose(pose1));
1420 rotValues.
insert(
x2, poseDrift.compose(pose2));
1421 rotValues.
insert(
x3, poseDrift.compose(pose3));
1423 boost::shared_ptr<GaussianFactor> hessianFactorRot = smartFactor->linearize(
1427 EXPECT(smartFactor->isValid());
1432 hessianFactorRot->information(), 1
e-6));
1442 boost::shared_ptr<GaussianFactor> hessianFactorRotTran =
1443 smartFactor->linearize(tranValues);
1448 #ifdef GTSAM_USE_EIGEN_MKL 1449 hessianFactorRotTran->information(), 1
e-5));
1451 hessianFactorRotTran->information(), 1
e-6));
LevenbergMarquardtParams lm_params
static PinholeCamera< Cal3_S2 > cam1(pose3, cal1)
TriangulationResult triangulateSafe(const Cameras &cameras) const
triangulateSafe
virtual const Values & optimize()
static int runAllTests(TestResult &result)
Camera cam3(pose_above, K2)
Camera level_camera_right(pose_right, K2)
void insert(Key j, const Value &val)
bool getThrowCheirality() const
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
bool getVerboseCheirality() const
void setRetriangulationThreshold(double retriangulationTh)
static StereoCamera cam2(pose3, cal4ptr)
#define DOUBLES_EQUAL(expected, actual, threshold)
void setEnableEPI(bool enableEPI)
static StereoPoint2 measurement1(323.0, 300.0, 240.0)
TriangulationParameters triangulation
double dynamicOutlierRejectionThreshold
double totalReprojectionError(const Cameras &cameras, boost::optional< Point3 > externalPoint=boost::none) const
void add(const StereoPoint2 &measured, const Key &poseKey, const boost::shared_ptr< Cal3_S2Stereo > &K)
static Cal3_S2Stereo::shared_ptr K(new Cal3_S2Stereo(fov, w, h, b))
IsDerived< DERIVEDFACTOR > push_back(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Point3 landmark3(3, 0, 3.0)
NonlinearFactorGraph graph
A set of cameras, all with their own calibration.
Point2 landmark1(5.0, 1.5)
Point3 landmark4(10, 0.5, 1.2)
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Smart stereo factor on poses, assuming camera calibration is fixed.
static Point3 landmark(0, 0, 5)
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
void setRankTolerance(double rankTol)
Implements a prior on the translation component of a pose.
Point2 landmark2(7.0, 1.5)
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Class compose(const Class &g) const
double landmarkDistanceThreshold
Pose3 inverse() const
inverse transformation with derivatives
boost::shared_ptr< Cal3_S2Stereo > shared_ptr
double error(const Values &values) const override
const ValueType at(Key j) const
DegeneracyMode getDegeneracyMode() const
bool enableEPI
if set to true, will refine triangulation using LM
double rankTolerance
(the rank is the number of singular values of the triangulation matrix which are larger than rankTole...
std::vector< Point2, Eigen::aligned_allocator< Point2 > > Point2Vector
#define EXPECT(condition)
static Pose3 pose3(rt3, pt3)
void projectToMultipleCameras(const CAMERA &cam1, const CAMERA &cam2, const CAMERA &cam3, Point3 landmark, typename CAMERA::MeasurementVector &measurements_cam)
void setLandmarkDistanceThreshold(double landmarkDistanceThreshold)
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
static SmartStereoProjectionParams params
TriangulationParameters getTriangulationParameters() const
Basic bearing factor from 2D measurement.
Base::Cameras cameras(const Values &values) const override
static const Pose3 pose1(Rot3(), Point3(0, 1, 0.5))
LinearizationMode getLinearizationMode() const
NonlinearFactorGraph graph2()
noiseModel::Diagonal::shared_ptr SharedDiagonal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
TEST(SmartStereoProjectionPoseFactor, params)
static Cal3_S2Stereo::shared_ptr K2(new Cal3_S2Stereo(1500, 1200, 0, 640, 480, b))
void setDegeneracyMode(DegeneracyMode degMode)
LevenbergMarquardtParams lmParams
double getRetriangulationThreshold() const
static SharedNoiseModel model(noiseModel::Isotropic::Sigma(3, 0.1))
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
JacobianFactor factor2(keyX, A21, keyY, A22, b2, noiseModel::Isotropic::Sigma(2, sigma2))
void setLinearizationMode(LinearizationMode linMode)
static Pose3 body_P_sensor1(Rot3::RzRyRx(-M_PI_2, 0.0,-M_PI_2), Point3(0.25,-0.10, 1.0))
void tictoc_finishedIteration_()
StereoPoint2 project(const Point3 &point) const
Project 3D point to StereoPoint2 (uL,uR,v)
double error(const Values &values) const
boost::shared_ptr< GaussianFactorGraph > linearize(const Values &linearizationPoint) const
Linearize a nonlinear factor graph.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
boost::shared_ptr< Cal3_S2 > shared_ptr
Camera level_camera(level_pose, K2)
void setDynamicOutlierRejectionThreshold(double dynOutRejectionThreshold)
vector< StereoPoint2 > stereo_projectToMultipleCameras(const StereoCamera &cam1, const StereoCamera &cam2, const StereoCamera &cam3, Point3 landmark)
std::uint64_t Key
Integer nonlinear key type.
noiseModel::Base::shared_ptr SharedNoiseModel
TEST_UNSAFE(SmartStereoProjectionPoseFactor, noiseless)
JacobianFactor factor1(keyX, A11, b1, noiseModel::Isotropic::Sigma(2, sigma1))