iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-EigenConversions.C
/*! @file
@author Laurent Itti
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
// that it will be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
#include <nrt/config.h>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE EigenConversions
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(EigenConversionsTest)
{
// Check array/eigenVector conversions each way
{
arr[0] = 4.2;
arr[2] = 5.1;
arr[5] = 6.2;
arr[9] = 1.2345;
Eigen::VectorXf evf = nrt::arrayToEigenVector<float>(arr);
BOOST_CHECK_CLOSE(evf[0],arr[0],boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(evf[2],arr[2],boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(evf[5],arr[5],boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(evf[9],arr[9],boost::numeric::bounds<float>::smallest());
arr = nrt::eigenVectorToArray<float>(evf);
BOOST_CHECK_CLOSE(arr[0],evf[0],boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(arr[2],evf[2],boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(arr[5],evf[5],boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(arr[9],arr[9],boost::numeric::bounds<float>::smallest());
}
// Check image/eigenVector conversions each way
{
Eigen::VectorXd evd(5);
evd[0] = 5.465;
evd[1] = 723.234;
evd[2] = 352561.1235;
evd[3] = 1532;
evd[4] = 0.000023;
nrt::Image<nrt::PixGray<double> > img = nrt::eigenVectorToImage<double>(evd);
BOOST_CHECK_CLOSE(img.at(0,0).val(),evd[0],boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(img.at(1,0).val(),evd[1],boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(img.at(3,0).val(),evd[3],boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(img.at(4,0).val(),evd[4],boost::numeric::bounds<float>::smallest());
evd = nrt::imageToEigenVector<double>(img);
BOOST_CHECK_CLOSE(evd[0],img.at(0,0).val(),boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(evd[1],img.at(1,0).val(),boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(evd[3],img.at(3,0).val(),boost::numeric::bounds<float>::smallest());
BOOST_CHECK_CLOSE(evd[4],img.at(4,0).val(),boost::numeric::bounds<float>::smallest());
}
// Check image/eigenMatrix conversions each way
{
Eigen::MatrixXd emd(7,4);
emd.setZero();
emd(0,0) = 0.001245125;
emd(0,1) = 6721.0012;
emd(1,2) = 33.12;
emd(3,0) = 5.465;
emd(4,0) = 6.1251503;
emd(4,1) = 723.234;
emd(4,2) = 96.8658;
emd(5,0) = 1532;
emd(5,2) = 8832.09;
emd(6,2) = 0.000023;
emd(6,3) = 525.2626;
nrt::Image<nrt::PixGray<double> > img = nrt::eigenMatrixToImage<double>(emd);
for(int y=0;y<emd.cols();y++)
{
for(int x=0;x<emd.rows();x++)
{
BOOST_CHECK_CLOSE(img.at(x,y).val(),emd(x,y),boost::numeric::bounds<float>::smallest());
}
}
// Set emd to invalid values before assignment to prevent commented out code to pass through correctly
emd.setConstant(-1);
emd = nrt::imageToEigenMatrix<double>(img);
for(int y=0;y<img.height();y++)
{
for(int x=0;x<img.width();x++)
{
BOOST_CHECK_CLOSE(emd(x,y),img.at(x,y).val(),boost::numeric::bounds<float>::smallest());
}
}
}
// Check image/eigenMatrix conversions each way
{
Eigen::MatrixXd emd(4,7);
emd.setZero();
emd(0,0) = 0.001245125;
emd(1,0) = 6721.0012;
emd(2,1) = 33.12;
emd(0,3) = 5.465;
emd(0,4) = 6.1251503;
emd(1,4) = 723.234;
emd(2,4) = 96.8658;
emd(0,5) = 1532;
emd(2,5) = 8832.09;
emd(2,6) = 0.000023;
emd(3,6) = 525.2626;
nrt::Image<nrt::PixGray<double> > img = nrt::eigenMatrixToImage<double>(emd);
for(int y=0;y<emd.cols();y++)
{
for(int x=0;x<emd.rows();x++)
{
BOOST_CHECK_CLOSE(img.at(x,y).val(),emd(x,y),boost::numeric::bounds<float>::smallest());
}
}
// Set emd to invalid values before assignment to prevent commented out code to pass through correctly
emd.setConstant(-1);
emd = nrt::imageToEigenMatrix<double>(img);
for(int y=0;y<img.height();y++)
{
for(int x=0;x<img.width();x++)
{
BOOST_CHECK_CLOSE(emd(x,y),img.at(x,y).val(),boost::numeric::bounds<float>::smallest());
}
}
}
// Check image/eigenMatrix conversions each way
{
img(0,0) = 0.001245125;
img(1,0) = 6721.0012;
img(2,1) = 33.12;
img(0,3) = 5.465;
img(0,4) = 6.1251503;
img(1,4) = 723.234;
img(2,4) = 96.8658;
img(0,5) = 1532;
img(2,5) = 8832.09;
img(2,6) = 0.000023;
img(3,6) = 525.2626;
Eigen::MatrixXd emd = nrt::imageToEigenMatrix<double>(img);
for(int y=0;y<img.height();y++)
{
for(int x=0;x<img.width();x++)
{
BOOST_CHECK_CLOSE(img.at(x,y).val(),emd(x,y),boost::numeric::bounds<float>::smallest());
}
}
// Set img to invalid values before assignment to prevent commented out code to pass through correctly
img = nrt::eigenMatrixToImage<double>(emd);
for(int y=0;y<emd.cols();y++)
{
for(int x=0;x<emd.rows();x++)
{
BOOST_CHECK_CLOSE(emd(x,y),img.at(x,y).val(),boost::numeric::bounds<float>::smallest());
}
}
}
}