iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-EigenConversions.C
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 
37 
38 #include <nrt/config.h>
39 #include <nrt/Eigen/Eigen.H>
42 #include <nrt/Core/Image/Image.H>
43 
44 #define BOOST_TEST_DYN_LINK
45 #define BOOST_TEST_MODULE EigenConversions
46 
47 #include <boost/test/unit_test.hpp>
48 
49 BOOST_AUTO_TEST_CASE(EigenConversionsTest)
50 {
51  // Check array/eigenVector conversions each way
52  {
53  nrt::Array<float> arr(10);
54  arr[0] = 4.2;
55  arr[2] = 5.1;
56  arr[5] = 6.2;
57  arr[9] = 1.2345;
58  Eigen::VectorXf evf = nrt::arrayToEigenVector<float>(arr);
59  BOOST_CHECK_CLOSE(evf[0],arr[0],boost::numeric::bounds<float>::smallest());
60  BOOST_CHECK_CLOSE(evf[2],arr[2],boost::numeric::bounds<float>::smallest());
61  BOOST_CHECK_CLOSE(evf[5],arr[5],boost::numeric::bounds<float>::smallest());
62  BOOST_CHECK_CLOSE(evf[9],arr[9],boost::numeric::bounds<float>::smallest());
63 
64  arr = nrt::eigenVectorToArray<float>(evf);
65  BOOST_CHECK_CLOSE(arr[0],evf[0],boost::numeric::bounds<float>::smallest());
66  BOOST_CHECK_CLOSE(arr[2],evf[2],boost::numeric::bounds<float>::smallest());
67  BOOST_CHECK_CLOSE(arr[5],evf[5],boost::numeric::bounds<float>::smallest());
68  BOOST_CHECK_CLOSE(arr[9],arr[9],boost::numeric::bounds<float>::smallest());
69  }
70 
71  // Check image/eigenVector conversions each way
72  {
73  Eigen::VectorXd evd(5);
74  evd[0] = 5.465;
75  evd[1] = 723.234;
76  evd[2] = 352561.1235;
77  evd[3] = 1532;
78  evd[4] = 0.000023;
79  nrt::Image<nrt::PixGray<double> > img = nrt::eigenVectorToImage<double>(evd);
80  BOOST_CHECK_CLOSE(img.at(0,0).val(),evd[0],boost::numeric::bounds<float>::smallest());
81  BOOST_CHECK_CLOSE(img.at(1,0).val(),evd[1],boost::numeric::bounds<float>::smallest());
82  BOOST_CHECK_CLOSE(img.at(3,0).val(),evd[3],boost::numeric::bounds<float>::smallest());
83  BOOST_CHECK_CLOSE(img.at(4,0).val(),evd[4],boost::numeric::bounds<float>::smallest());
84 
85  evd = nrt::imageToEigenVector<double>(img);
86  BOOST_CHECK_CLOSE(evd[0],img.at(0,0).val(),boost::numeric::bounds<float>::smallest());
87  BOOST_CHECK_CLOSE(evd[1],img.at(1,0).val(),boost::numeric::bounds<float>::smallest());
88  BOOST_CHECK_CLOSE(evd[3],img.at(3,0).val(),boost::numeric::bounds<float>::smallest());
89  BOOST_CHECK_CLOSE(evd[4],img.at(4,0).val(),boost::numeric::bounds<float>::smallest());
90  }
91 
92  // Check image/eigenMatrix conversions each way
93  {
94  Eigen::MatrixXd emd(7,4);
95  emd.setZero();
96  emd(0,0) = 0.001245125;
97  emd(0,1) = 6721.0012;
98  emd(1,2) = 33.12;
99  emd(3,0) = 5.465;
100  emd(4,0) = 6.1251503;
101  emd(4,1) = 723.234;
102  emd(4,2) = 96.8658;
103  emd(5,0) = 1532;
104  emd(5,2) = 8832.09;
105  emd(6,2) = 0.000023;
106  emd(6,3) = 525.2626;
107  nrt::Image<nrt::PixGray<double> > img = nrt::eigenMatrixToImage<double>(emd);
108  for(int y=0;y<emd.cols();y++)
109  {
110  for(int x=0;x<emd.rows();x++)
111  {
112  BOOST_CHECK_CLOSE(img.at(x,y).val(),emd(x,y),boost::numeric::bounds<float>::smallest());
113  }
114  }
115  // Set emd to invalid values before assignment to prevent commented out code to pass through correctly
116  emd.setConstant(-1);
117  emd = nrt::imageToEigenMatrix<double>(img);
118  for(int y=0;y<img.height();y++)
119  {
120  for(int x=0;x<img.width();x++)
121  {
122  BOOST_CHECK_CLOSE(emd(x,y),img.at(x,y).val(),boost::numeric::bounds<float>::smallest());
123  }
124  }
125 
126  }
127 
128 
129  // Check image/eigenMatrix conversions each way
130  {
131  Eigen::MatrixXd emd(4,7);
132  emd.setZero();
133  emd(0,0) = 0.001245125;
134  emd(1,0) = 6721.0012;
135  emd(2,1) = 33.12;
136  emd(0,3) = 5.465;
137  emd(0,4) = 6.1251503;
138  emd(1,4) = 723.234;
139  emd(2,4) = 96.8658;
140  emd(0,5) = 1532;
141  emd(2,5) = 8832.09;
142  emd(2,6) = 0.000023;
143  emd(3,6) = 525.2626;
144  nrt::Image<nrt::PixGray<double> > img = nrt::eigenMatrixToImage<double>(emd);
145  for(int y=0;y<emd.cols();y++)
146  {
147  for(int x=0;x<emd.rows();x++)
148  {
149  BOOST_CHECK_CLOSE(img.at(x,y).val(),emd(x,y),boost::numeric::bounds<float>::smallest());
150  }
151  }
152  // Set emd to invalid values before assignment to prevent commented out code to pass through correctly
153  emd.setConstant(-1);
154  emd = nrt::imageToEigenMatrix<double>(img);
155  for(int y=0;y<img.height();y++)
156  {
157  for(int x=0;x<img.width();x++)
158  {
159  BOOST_CHECK_CLOSE(emd(x,y),img.at(x,y).val(),boost::numeric::bounds<float>::smallest());
160  }
161  }
162 
163  }
164 
165 
166  // Check image/eigenMatrix conversions each way
167  {
169  img(0,0) = 0.001245125;
170  img(1,0) = 6721.0012;
171  img(2,1) = 33.12;
172  img(0,3) = 5.465;
173  img(0,4) = 6.1251503;
174  img(1,4) = 723.234;
175  img(2,4) = 96.8658;
176  img(0,5) = 1532;
177  img(2,5) = 8832.09;
178  img(2,6) = 0.000023;
179  img(3,6) = 525.2626;
180  Eigen::MatrixXd emd = nrt::imageToEigenMatrix<double>(img);
181  for(int y=0;y<img.height();y++)
182  {
183  for(int x=0;x<img.width();x++)
184  {
185  BOOST_CHECK_CLOSE(img.at(x,y).val(),emd(x,y),boost::numeric::bounds<float>::smallest());
186  }
187  }
188  // Set img to invalid values before assignment to prevent commented out code to pass through correctly
189  img.clear(nrt::PixGray<float>(-1));
190  img = nrt::eigenMatrixToImage<double>(emd);
191  for(int y=0;y<emd.cols();y++)
192  {
193  for(int x=0;x<emd.rows();x++)
194  {
195  BOOST_CHECK_CLOSE(emd(x,y),img.at(x,y).val(),boost::numeric::bounds<float>::smallest());
196  }
197  }
198 
199  }
200 
201 
202 }