invariant_surface_feature_unit_tests.hpp
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2011
00004  *
00005  * Fraunhofer Institute for Manufacturing Engineering
00006  * and Automation (IPA)
00007  *
00008  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00009  *
00010  * Project name: care-o-bot
00011  * ROS stack name: cob_environment_perception_intern
00012  * ROS package name: cob_3d_features
00013  * Description:
00014  *
00015  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00016  *
00017  * Author: Joshua Hampp, email:joshua.hampp@ipa.fraunhofer.de
00018  *
00019  * Date of creation: 03/2014
00020  * ToDo:
00021  *
00022  *
00023  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00024  *
00025  * Redistribution and use in source and binary forms, with or without
00026  * modification, are permitted provided that the following conditions are met:
00027  *
00028  *     * Redistributions of source code must retain the above copyright
00029  *       notice, this list of conditions and the following disclaimer.
00030  *     * Redistributions in binary form must reproduce the above copyright
00031  *       notice, this list of conditions and the following disclaimer in the
00032  *       documentation and/or other materials provided with the distribution.
00033  *     * Neither the name of the Fraunhofer Institute for Manufacturing
00034  *       Engineering and Automation (IPA) nor the names of its
00035  *       contributors may be used to endorse or promote products derived from
00036  *       this software without specific prior written permission.
00037  *
00038  * This program is free software: you can redistribute it and/or modify
00039  * it under the terms of the GNU Lesser General Public License LGPL as
00040  * published by the Free Software Foundation, either version 3 of the
00041  * License, or (at your option) any later version.
00042  *
00043  * This program is distributed in the hope that it will be useful,
00044  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00045  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00046  * GNU Lesser General Public License LGPL for more details.
00047  *
00048  * You should have received a copy of the GNU Lesser General Public
00049  * License LGPL along with this program.
00050  * If not, see <http://www.gnu.org/licenses/>.
00051  *
00052  ****************************************************************/
00053 
00054 #pragma once
00055 
00056 #include "invariant_surface_feature.hpp"
00057 #include "gtest/gtest.h"
00058 
00059 
00060 template<const int num_radius_, const int num_angle_, typename TSurface, typename Scalar, typename TAffine>
00061 void cob_3d_features::InvariantSurfaceFeature<num_radius_,num_angle_,TSurface,Scalar,TAffine>::test_addOffset(const Scalar off_x, const Scalar off_y, const Scalar off_z) {
00062         for(size_t i=0; i<input_->size(); i++) {
00063                 (*input_)[i].model_.p(0) += off_z;
00064                 for(size_t j=0; j<(*input_)[i].segments_.size(); j++)
00065                         for(size_t k=0; k<(*input_)[i].segments_[j].size(); k++) {
00066                                 (*input_)[i].segments_[j][k](0) += off_x;
00067                                 (*input_)[i].segments_[j][k](1) += off_y;
00068                         }
00069         }
00070 
00071         for(size_t i=0; i<triangulated_input_.size(); i++) {
00072                 for(int j=0; j<3; j++) {
00073                         triangulated_input_[i].p_[j](0) += off_x;
00074                         triangulated_input_[i].p_[j](1) += off_y;
00075                 }
00076                 triangulated_input_[i].compute(radii_);
00077         }
00078 }
00079 
00080 template<const int num_radius_, const int num_angle_, typename TSurface, typename Scalar, typename TAffine>
00081 void cob_3d_features::InvariantSurfaceFeature<num_radius_,num_angle_,TSurface,Scalar,TAffine>::test_rotate(const Scalar angle) {
00082         for(size_t i=0; i<triangulated_input_.size(); i++) {
00083                 std::cout<<"AAAAA "<<i<<std::endl;
00084                 triangulated_input_[i].print();
00085         }
00086 
00087         const Scalar s = std::sin(angle);
00088         const Scalar c = std::cos(angle);
00089 
00090         Eigen::Matrix<Scalar, 2,2> R3;
00091         Eigen::Matrix<float, 4,4> R, Ri;
00092         Eigen::Matrix3f R2;
00093         R.fill(0);
00094         R(0,0)=1;
00095         R(1,1)=c; R(1,2)=-s;
00096         R(2,1)=s; R(2,2)=c;
00097         Ri = R;
00098         Ri(2,1)*=-1;
00099         Ri(1,2)*=-1;
00100 
00101         R2.fill(0);
00102         R2(2,2)=1;
00103         R2(0,0)=c; R2(0,1)=-s;
00104         R2(1,0)=s; R2(1,1)=c;
00105 
00106         R3.fill(0);
00107         R3(0,0)=c; R3(0,1)=-s;
00108         R3(1,0)=s; R3(1,1)=c;
00109 
00110         for(size_t i=0; i<input_->size(); i++) {
00111                 (*input_)[i].model_.p = Ri*(*input_)[i].model_.p;
00112                 for(size_t j=0; j<(*input_)[i].segments_.size(); j++)
00113                         for(size_t k=0; k<(*input_)[i].segments_[j].size(); k++)
00114                                 (*input_)[i].segments_[j][k] = R2*(*input_)[i].segments_[j][k];
00115         }
00116 
00117         for(size_t i=0; i<triangulated_input_.size(); i++) {
00118                 for(int j=0; j<3; j++)
00119                         triangulated_input_[i].p_[j] = R3*triangulated_input_[i].p_[j];
00120                 triangulated_input_[i].compute(radii_);
00121                 std::cout<<"BBBBB "<<i<<std::endl;
00122                 triangulated_input_[i].print();
00123         }
00124 }
00125 
00126 template<const int num_radius_, const int num_angle_, typename TSurface, typename Scalar, typename TAffine>
00127 pcl::PolygonMesh::Ptr cob_3d_features::InvariantSurfaceFeature<num_radius_,num_angle_,TSurface,Scalar,TAffine>::test_subsampling_of_Map(const int num, const Scalar r2) {
00128         //generate random map
00129         input_.reset(new TSurfaceList);
00130         input_->resize(num);
00131         triangulated_input_.clear();
00132         for(int i=0; i<num; i++) {
00133                 //some model
00134                 typename TSurface::Model *model = &(*input_)[i].model_;
00135                 model->p = model->p.Random();
00136 
00137                 //generate random triangle
00138                 Triangle t;
00139                 (*input_)[i].segments_.resize(1);
00140                 for(int j=0; j<3; j++) {
00141                         t.p_[j] = t.p_[j].Random();
00142                         Eigen::Vector3f v = Eigen::Vector3f::Zero();
00143                         v.head<2>() = t.p_[j].template cast<float>();
00144                         (*input_)[i].segments_[0].push_back(v);
00145                 }
00146                 t.model_ = model;
00147                 t.compute(radii_);
00148                 triangulated_input_.push_back(t);
00149         }
00150         
00151         //select some point and generate mesh
00152         std::vector<Triangle> res;
00153 //    subsample(triangulated_input_[rand()%triangulated_input_.size()].p3_[rand()%3], r2, res);
00154     subsample(Eigen::Matrix<Scalar, 3, 1>::UnitX(), r2, res);
00155 
00156         return dbg_triangles2mesh(res);
00157 }
00158 
00159 template<const int num_radius_, const int num_angle_, typename TSurface, typename Scalar, typename TAffine>
00160 bool cob_3d_features::InvariantSurfaceFeature<num_radius_,num_angle_,TSurface,Scalar,TAffine>::test_singleTriangle(const int num) const {
00161 
00162         bool zero_model = false, random_triangles=true;
00163         int test = 0, tests_succeeded=0, tests_num=0;
00164 
00165         for(int i=0; i<num; i++) {
00166                 //some model
00167                 typename TSurface::Model model;
00168                 if(zero_model)
00169                         model.p.fill(0);
00170                 else
00171                         model.p = model.p.Random();
00172                 model.p(0) += 2;
00173 //model.p(3)=model.p(4)=model.p(5)=0;
00174 
00175                 //generate random triangle
00176                 Triangle t;
00177                 if(random_triangles) {
00178                         for(int j=0; j<3; j++) t.p_[j] = t.p_[j].Random();
00179                 } else {
00180                         t.p_[0](0)=0;
00181                         t.p_[0](1)=0;
00182                         t.p_[1](0)=0;
00183                         t.p_[1](1)=1;
00184                         t.p_[2](0)=1;
00185                         t.p_[2](1)=1;
00186                 }
00187                 t.model_ = &model;
00188                 t.compute(radii_);
00189 
00190                 //subdivide triangle to 3 and re run
00191                 //sum should be equal to first triangle
00192                 Triangle t1 = t, t2=t, t3=t;
00193                 t1.p_[0] = (t.p_[0]+t.p_[1]+t.p_[2])/3;
00194                 t2.p_[1] = (t.p_[0]+t.p_[1]+t.p_[2])/3;
00195                 t3.p_[2] = (t.p_[0]+t.p_[1]+t.p_[2])/3;
00196 
00197                 t1.compute(radii_);
00198                 t2.compute(radii_);
00199                 t3.compute(radii_);
00200 
00201                 if(test==0) {
00202                         Scalar m=0.9,n=0.4,p=0;
00203 
00204                         for(m=0; m<=3; m++)
00205                         for(n=0; n<=3; n++)
00206                         for(p=0; p<=3; p++)
00207                         {
00208                                 std::complex<Scalar> c, c2, c3;
00209 
00210                                 std::cout<<"mnp "<<m<<" "<<n<<" "<<p <<"    ------------------------"<<std::endl;
00211                                 c = t1.kernel(m,n,p);
00212                                 std::cout<< c<<" "<<std::abs(c) <<std::endl<<std::endl;
00213                                 c = t2.kernel(m,n,p);
00214                                 std::cout<< c<<" "<<std::abs(c) <<std::endl<<std::endl;
00215                                 c = t3.kernel(m,n,p);
00216                                 std::cout<< c<<" "<<std::abs(c) <<std::endl<<std::endl;
00217 
00218                                 std::cout<<"mnp "<<m<<" "<<n<<" "<<p <<"    -------------------11111"<<std::endl;
00219                                 c = (t1.kernel(m,n,p)+t2.kernel(m,n,p)+t3.kernel(m,n,p));
00220                                 std::cout<< c<<" "<<std::abs(c) <<std::endl<<std::endl;
00221                                 std::cout<<"mnp "<<m<<" "<<n<<" "<<p <<"    -------------------22222"<<std::endl;
00222                                 c2 = t.kernel(m,n,p);
00223                                 std::cout<< c2<<" "<<std::abs(c2) <<std::endl<<std::endl;
00224                                 std::cout<<"----------------------------------------"<<std::endl;
00225 
00226                                 t1.print();
00227                                 t2.print();
00228                                 t3.print();
00229                                 t.print();
00230 
00231                                 std::cout<<"model: "<<model.p.transpose()<<std::endl;
00232 
00233                                 /*if(std::abs(c-c2) > 0.01 ) {
00234                                         for(int j=0; j<3; j++) t.p_[j] += t.p_[j].Random()*0.01;
00235                                         t1 = t; t2=t; t3=t;
00236                                         t1.p_[0] = (t.p_[0]+t.p_[1]+t.p_[2])/3;
00237                                         t2.p_[1] = (t.p_[0]+t.p_[1]+t.p_[2])/3;
00238                                         t3.p_[2] = (t.p_[0]+t.p_[1]+t.p_[2])/3;
00239                                         std::cout<<"    -------------------tttttt"<<std::endl;
00240                                         c3 = (t1.kernel(m,n,p)+t2.kernel(m,n,p)+t3.kernel(m,n,p));
00241                                         std::cout<< c3<<" "<<std::abs(c3) <<std::endl<<std::endl;
00242                                 }*/
00243 
00244                                 EXPECT_NEAR(c.real(),c2.real(), 0.05);
00245                                 EXPECT_NEAR(c.imag(),c2.imag(), 0.05);
00246                                 EXPECT_TRUE(c==c);      //nan test
00247 
00248                                 tests_succeeded += (std::abs(c-c2) < 0.1&&c==c)?1:0;
00249                                 tests_num++;
00250                                 //assert(std::abs(c-c2) < 0.1);
00251                                 //assert(c==c); //nan test
00252 
00253                         }
00254                 }
00255                 else if(test==1) {
00256                         FeatureAngleComplex sum1, sum2; sum1.fill(0); sum2.fill(0);
00257                         for(size_t j=0; j<radii_.size(); j++)
00258                                 for(size_t k=0; k<num_radius_; k++) {
00259                                         sum1 += t1.f_[j].vals[k]+t2.f_[j].vals[k]+t3.f_[j].vals[k];
00260                                         sum2 += t.f_[j].vals[k];
00261 
00262                                         std::cout<<"==================="<<std::endl;
00263                                         std::cout<<std::endl<<(t1.f_[j].vals[k])<<std::endl<<std::endl;
00264                                         std::cout<<std::endl<<(t2.f_[j].vals[k])<<std::endl<<std::endl;
00265                                         std::cout<<std::endl<<(t3.f_[j].vals[k])<<std::endl<<std::endl;
00266                                         std::cout<<"###################"<<std::endl;
00267                                         std::cout<<std::endl<<(t1.f_[j].vals[k]+t2.f_[j].vals[k]+t3.f_[j].vals[k])<<std::endl<<std::endl;
00268                                         std::cout<<t.f_[j].vals[k]<<std::endl;
00269                                 }
00270 
00271                         std::cout<<"----------------------------------------"<<std::endl;
00272                         std::cout<<std::endl<<sum1<<std::endl<<std::endl;
00273                         std::cout<<sum2<<std::endl;
00274                         std::cout<<"----------------------------------------"<<std::endl;
00275                         std::cout<<sum2-sum1<<std::endl;
00276 
00277                         std::cout<<"model: "<<model.p.transpose()<<std::endl;
00278 
00279                         EXPECT_TRUE( sum1.isApprox(sum2, 0.001) );
00280                         //EXPECT_TRUE( sum1.allFinite() );      //nan test
00281 
00282                         //if(sum1!=sum2) return false;
00283                 }
00284                 else return false;
00285         }
00286 
00287         std::cout<<"Tests: "<<tests_succeeded<<"/"<<tests_num<<std::endl;
00288 
00289         return tests_succeeded==tests_num;
00290 }
00291 
00292 template<const int num_radius_, const int num_angle_, typename TSurface, typename Scalar, typename TAffine>
00293 void cob_3d_features::InvariantSurfaceFeature<num_radius_,num_angle_,TSurface,Scalar,TAffine>::Triangle::print() const {
00294         std::cout<< "Triangle "<< model_ <<std::endl;
00295         for(int i=0; i<3; i++)
00296                 std::cout<<p_[i].transpose()<<std::endl;
00297         for(int i=0; i<3; i++) {
00298                 std::cout<<p3_[i].transpose()<<std::endl;
00299                 assert(at(p_[i])==p3_[i]);
00300         }
00301 /*      for(size_t i=0; i<f_.size(); i++) {
00302                 for(size_t j=0; j<num_radius_; j++)
00303                         std::cout<<f_[i].vals[j]<<std::endl;
00304         }*/
00305 }


cob_3d_features
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:02:26