test_pca.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id: test_pca.cpp 5026 2012-03-12 02:51:44Z rusu $
00035  */
00036 
00039 #include <gtest/gtest.h>
00040 #include <pcl/common/pca.h>
00041 #include <pcl/point_types.h>
00042 #include <pcl/pcl_tests.h>
00043 
00044 using namespace pcl::test;
00045 
00046 pcl::PointCloud<pcl::PointXYZ> cloud;
00047 pcl::PCA<pcl::PointXYZ> pca;
00048 
00049 TEST(PCA, projection)
00050 {
00051   pcl::PointXYZ projected, reconstructed;
00052   for(size_t i = 0; i < cloud.size(); i++)
00053   {
00054     pca.project (cloud[i], projected);
00055     pca.reconstruct (projected, reconstructed);
00056     EXPECT_NEAR_VECTORS (reconstructed.getVector3fMap (), cloud[i].getVector3fMap (), 2.5e-4);
00057   }
00058 }
00059 
00060 TEST(PCA, copy_constructor)
00061 {
00062   // Test copy constructor
00063   pcl::PCA<pcl::PointXYZ> pca_copy(pca);
00064   try
00065   {
00066     Eigen::Matrix3f eigen_vectors_copy = pca_copy.getEigenVectors ();
00067     Eigen::Matrix3f eigen_vectors = pca.getEigenVectors ();
00068     for(size_t i = 0; i < 3; ++i)
00069       for(size_t j = 0; j < 3; ++j)
00070         EXPECT_EQ (eigen_vectors (i,j), eigen_vectors_copy (i,j));
00071   }
00072   catch (pcl::InitFailedException &/*e*/)
00073   {
00074     std::cerr << "something wrong" << std::endl;
00075   }
00076 }
00077 
00078 TEST(PCA, cloud_projection)
00079 {
00080   pcl::PointCloud<pcl::PointXYZ> cloud_projected, cloud_reconstructed;
00081   try
00082   {
00083     pca.project (cloud, cloud_projected);
00084     EXPECT_EQ (cloud.size (), cloud_projected.size ());
00085     pca.reconstruct (cloud_projected, cloud_reconstructed);
00086     EXPECT_EQ (cloud_reconstructed.size (), cloud_projected.size ());
00087     for(size_t i = 0; i < cloud.size(); i++)
00088       EXPECT_NEAR_VECTORS (cloud[i].getVector3fMap (),
00089                            cloud_reconstructed[i].getVector3fMap (),
00090                            2.5e-4);
00091   }
00092   catch (pcl::InitFailedException &/*e*/)
00093   {
00094     std::cerr << "something wrong" << std::endl;
00095   }
00096 }
00097 
00098 int main (int argc, char** argv)
00099 {
00100   cloud.width = 5;
00101   cloud.height = 4 ;
00102   cloud.is_dense = true;
00103   cloud.resize(20);
00104   cloud[0].x = 100;   cloud[0].y = 8;    cloud[0].z = 5;
00105   cloud[1].x = 228;   cloud[1].y = 21;   cloud[1].z = 2;
00106   cloud[2].x = 341;   cloud[2].y = 31;   cloud[2].z = 10;
00107   cloud[3].x = 472;   cloud[3].y = 40;   cloud[3].z = 15;
00108   cloud[4].x = 578;   cloud[4].y = 48;   cloud[4].z = 3;
00109   cloud[5].x = 699;   cloud[5].y = 60;   cloud[5].z = 12;
00110   cloud[6].x = 807;   cloud[6].y = 71;   cloud[6].z = 14;
00111   cloud[7].x = 929;   cloud[7].y = 79;   cloud[7].z = 16;
00112   cloud[8].x = 1040;  cloud[8].y = 92;   cloud[8].z = 18;
00113   cloud[9].x = 1160;  cloud[9].y = 101;  cloud[9].z = 38;
00114   cloud[10].x = 1262; cloud[10].y = 109; cloud[10].z = 28;
00115   cloud[11].x = 1376; cloud[11].y = 121; cloud[11].z = 32;
00116   cloud[12].x = 1499; cloud[12].y = 128; cloud[12].z = 35;
00117   cloud[13].x = 1620; cloud[13].y = 143; cloud[13].z = 28;
00118   cloud[14].x = 1722; cloud[14].y = 150; cloud[14].z = 30;
00119   cloud[15].x = 1833; cloud[15].y = 159; cloud[15].z = 15;
00120   cloud[16].x = 1948; cloud[16].y = 172; cloud[16].z = 12;
00121   cloud[17].x = 2077; cloud[17].y = 181; cloud[17].z = 33;
00122   cloud[18].x = 2282; cloud[18].y = 190; cloud[18].z = 23;
00123   cloud[19].x = 2999; cloud[19].y = 202; cloud[19].z = 29;  
00124 
00125   pca.setInputCloud (cloud.makeShared ());
00126 
00127   testing::InitGoogleTest (&argc, argv);
00128   return (RUN_ALL_TESTS ());
00129 }


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:18:24