absolute_orientation_horn_tester.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 
00004 #include <Eigen/Core>
00005 
00006 #include "../libfovis/absolute_orientation_horn.hpp"
00007 
00008 using namespace std;
00009 
00010 static int
00011 rand_int_range(int min, int max)
00012 {
00013   return rand() % (max - min) + min;
00014 }
00015 
00016 static double 
00017 rand_double_range(double min, double max)
00018 {
00019   double v = rand() / (double)RAND_MAX;
00020   return v * (max - min) + min;
00021 }
00022 
00023 void 
00024 rpy_to_quat (const double rpy[3], double q[4])
00025 {
00026   double roll = rpy[0], pitch = rpy[1], yaw = rpy[2];
00027 
00028   double halfroll = roll / 2;
00029   double halfpitch = pitch / 2;
00030   double halfyaw = yaw / 2;
00031 
00032   double sin_r2 = sin (halfroll);
00033   double sin_p2 = sin (halfpitch);
00034   double sin_y2 = sin (halfyaw);
00035 
00036   double cos_r2 = cos (halfroll);
00037   double cos_p2 = cos (halfpitch);
00038   double cos_y2 = cos (halfyaw);
00039 
00040   q[0] = cos_r2 * cos_p2 * cos_y2 + sin_r2 * sin_p2 * sin_y2;
00041   q[1] = sin_r2 * cos_p2 * cos_y2 - cos_r2 * sin_p2 * sin_y2;
00042   q[2] = cos_r2 * sin_p2 * cos_y2 + sin_r2 * cos_p2 * sin_y2;
00043   q[3] = cos_r2 * cos_p2 * sin_y2 - sin_r2 * sin_p2 * cos_y2;
00044 }
00045 
00046 int main(int argc, char** argv)
00047 {
00048   int num_trials = 1000;
00049 
00050   for(int trial=0; trial<num_trials; trial++) {
00051 
00052     // generate a random set of points
00053     int num_points = rand_int_range(5, 1000);
00054 
00055     Eigen::Matrix3Xd points(3, num_points);
00056 
00057     for(int col=0; col<num_points; col++) {
00058       points(0, col) = rand_double_range(-10, 10);
00059       points(1, col) = rand_double_range(-10, 10);
00060       points(2, col) = rand_double_range(-10, 10);
00061     }
00062 
00063     // generate a random transformation
00064     Eigen::Vector3d translation(rand_double_range(-10, 10),
00065         rand_double_range(-10, 10),
00066         rand_double_range(-10, 10));
00067 
00068     double rpy[3] = {
00069       rand_double_range(-M_PI, M_PI),
00070       rand_double_range(-M_PI, M_PI),
00071       rand_double_range(-M_PI, M_PI) };
00072     double rot_quat[4];
00073     rpy_to_quat(rpy, rot_quat);
00074 
00075     Eigen::Quaterniond rotation(rot_quat[0], rot_quat[1], rot_quat[2], rot_quat[3]);
00076 
00077     Eigen::Isometry3d trans;
00078     trans.setIdentity();
00079     trans.translate(translation);
00080     trans.rotate(rotation);
00081 
00082     // apply transformation to original random point set
00083     Eigen::Matrix3Xd transformed = trans * points;
00084 
00085     Eigen::Isometry3d estimated_transform;
00086     absolute_orientation_horn(points, transformed, &estimated_transform);
00087 
00088     // reproject points using estimated transformation
00089     Eigen::Matrix3Xd reprojected = estimated_transform * points;
00090 
00091     // compute reprojection error
00092     Eigen::Matrix3Xd reproject_err = reprojected - transformed;
00093 
00094     Eigen::Vector3d mean_err = reproject_err.rowwise().sum() / num_points;
00095     printf("%4d (%4d): mean reprojection error: %6.3f, %6.3f, %6.3f\n", trial, num_points, mean_err(0), mean_err(1), mean_err(2));
00096     if(fabs(mean_err(0)) > 1e-9 || fabs(mean_err(1)) > 1e-9 || fabs(mean_err(1)) > 1e-9) {
00097       fprintf(stderr, "FAIL!\n");
00098       exit(1);
00099     }
00100   }
00101 
00102   fprintf(stderr, "OK\n");
00103   return 0;
00104 }


libfovis
Author(s): Albert Huang, Maurice Fallon
autogenerated on Thu Jun 6 2019 20:16:12