00001 
00058 #include <fstream>
00059 #include <ros/ros.h>
00060 #include <pcl/point_types.h>
00061 #include <pcl/io/pcd_io.h>
00062 #include <sensor_msgs/Image.h>
00063 #include <pcl/io/ply_io.h>
00064 #include <pcl/io/vtk_lib_io.h>
00065 
00066 #include <iostream>
00067 #include <Eigen/Geometry>
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 #include "cob_3d_features/impl/invariant_surface_feature.hpp"
00080 
00081 #include "cob_3d_features/impl/invariant_surface_feature_debug.hpp"
00082 #include <cob_3d_segmentation/quad_regression/quad_regression.h>
00083 #include "cob_3d_segmentation/quad_regression/polygon.h"
00084  
00085 
00086 template<class Samples, class Values>
00087 void simple_test(Eigen::Vector3d n, const Samples &samples, Values &vals) {
00088         vals.resize(samples.size());
00089 double s=0;
00090         for(size_t i=0; i<samples.size(); i++) {
00091                 for(size_t j=0; j<samples[i].size(); j++) {
00092 
00093 
00094                         vals[i].push_back(std::complex<double>(samples[i][j].dot(n), 0));
00095 
00096 
00097 s+=std::abs(vals[i].back());
00098 
00099 
00100                 }
00101 
00102         }
00103 std::cout<<"sum "<<s<<std::endl;
00104 }
00105 
00106 int main(int argc, char **argv)
00107 {
00108 
00109         std::string fn = "/home/josh/Downloads/MaleLow.obj";
00110         
00111         fn = "/home/josh/Downloads/bunny.ply";
00112         
00113         
00114         fn = "/home/josh/Downloads/cylinder.obj";
00115         fn = "/home/josh/Downloads/sphere.obj";
00116         
00117         
00118         std::string fn2 = "/home/josh/Downloads/Beautiful Girl.obj";
00119         
00120         fn2 = "/home/josh/Downloads/sphere.obj";
00121         
00122         
00123 
00124         typedef float Real;
00125         typedef double Scalar;
00126         typedef Sampler<Real, Scalar> S;
00127 
00128         srand(time(NULL));
00129 
00130         const int num_radii = 8;
00131         const int num_angles = 32;
00132         S gedt((Real)num_radii , num_radii , num_angles);
00133         S gedt2((Real)num_radii , num_radii , num_angles);
00134         std::vector< FourierKeyS2< Real > > sKeys;
00135         S::Samples samples;
00136         gedt.getSamples(samples);
00137 
00138         Eigen::Vector3d p1 = Eigen::Vector3d::Random(), p2 = Eigen::Vector3d::Random(), p3 = Eigen::Vector3d::Random(), off = Eigen::Vector3d::Random(), n1=Eigen::Vector3d::Random(),n2, off2;
00139         Eigen::Vector3d p4 = Eigen::Vector3d::Random(), p5 = Eigen::Vector3d::Random(), p6 = Eigen::Vector3d::Random();
00140         
00141         off2(0)=off2(1)=off2(2)=0;
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155         Eigen::AngleAxisd rotA(0.33*M_PI, Eigen::Vector3d::Random().normalized());
00156         Eigen::Matrix3d rot = rotA.matrix();
00157 
00158         n2 = rot*n1;
00159 
00160         if(1) {         
00161                 cob_3d_features::invariant_surface_feature::SingleTriangle<Scalar, S::Samples, S::Values> tri(p1, p2, p3);
00162                 cob_3d_features::invariant_surface_feature::SingleTriangle<Scalar, S::Samples, S::Values> tri_opp(-p1, -p2, -p3);
00163                 
00164                 cob_3d_features::invariant_surface_feature::SingleTriangle<Scalar, S::Samples, S::Values> tri3(p4, p5, p6);
00165                 cob_3d_features::invariant_surface_feature::SingleTriangle<Scalar, S::Samples, S::Values> tri5(p4*2, p5, p6+p2);
00166 
00167                 
00168                 cob_3d_features::invariant_surface_feature::SingleTriangle<Scalar, S::Samples, S::Values> tri2(rot*p1+off, rot*p2+off, rot*p3+off);
00169                 cob_3d_features::invariant_surface_feature::SingleTriangle<Scalar, S::Samples, S::Values> tri4(rot*p4+off, rot*p5+off, rot*p6+off);
00170                 cob_3d_features::invariant_surface_feature::SingleTriangle<Scalar, S::Samples, S::Values> tri6(rot*(2*p4)+off, rot*p5+off, rot*(p6+p2)+off);
00171 
00172                 std::cout<<"nd1 "<<n1.dot(p1)<<std::endl;
00173                 std::cout<<"nd2 "<<n2.dot(rot*p1)<<std::endl;
00174 
00175                 std::cout<<"area "<<(p2-p1).cross(p3-p1).norm()/2<<std::endl;
00176                 std::cout<<"0 val. "<<std::abs(tri.kernel_lin_tri(Eigen::Vector3d::Zero())+tri3.kernel_lin_tri(Eigen::Vector3d::Zero())+tri5.kernel_lin_tri(Eigen::Vector3d::Zero()))<<std::endl;
00177                 std::cout<<"0 val. "<<std::abs(tri2.kernel_lin_tri(Eigen::Vector3d::Zero())+tri4.kernel_lin_tri(Eigen::Vector3d::Zero())+tri6.kernel_lin_tri(Eigen::Vector3d::Zero()))<<std::endl;
00178 
00179                 std::cout<<"single val. "<<std::abs(tri.kernel_lin_tri(n1)+tri3.kernel_lin_tri(n1)+tri5.kernel_lin_tri(n1))<<std::endl;
00180                 
00181                 
00182                 std::cout<<"single val. "<<std::abs(tri2.kernel_lin_tri(n2)+tri4.kernel_lin_tri(n2)+tri6.kernel_lin_tri(n2))<<std::endl;
00183 
00184                 std::cout<<"single val. "<<std::abs( tri.kernel_lin_tri(n1,true))<<std::endl;
00185                 std::cout<<"single val. "<<std::abs( tri_opp.kernel_lin_tri(n1,true))<<std::endl;
00186                 std::cout<<"single val. "<<std::abs(tri2.kernel_lin_tri(n2,true))<<std::endl;
00187                 
00188                 
00189                 std::cout<<"sum val. "<<std::abs( tri.kernel_lin_tri(n1)+tri_opp.kernel_lin_tri(n1))<<std::endl;
00190                 
00191                 std::cout<<"t "<<std::polar<float>(1,0)<<std::endl;
00192                 std::cout<<"t "<<std::polar<float>(1,PI)<<std::endl;
00193                 std::cout<<"t "<<std::polar<float>(1,0)+std::polar<float>(1,PI)<<std::endl;
00194 
00195                 tri.compute(samples);
00196                 tri2.compute(samples);
00197                 exit(0);
00198         }
00199 
00200         
00201 
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00210 
00211 
00212 
00213         const bool test=false;
00214         const int M=atoi(argv[2]), Start=atoi(argv[1]);
00215         if(!test) {
00216                 FILE *fp = fopen("/tmp/cmp_prec", "wb");
00217                 pcl::PolygonMesh mesh;
00218                 pcl::io::loadPolygonFile(fn, mesh);
00219                 std::cout<<"step 1 finished"<<std::endl;
00220 
00221                 pcl::PointCloud<pcl::PointXYZ> points;
00222                 pcl::fromROSMsg(mesh.cloud, points);
00223                 time_t start = time(0);
00224                 for(size_t i=Start; i<std::min(mesh.polygons.size(), (size_t)M); i++) {
00225                         for(int j=0; j<(int)mesh.polygons[i].vertices.size()-2; j++) {
00226 
00227 
00228 
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 
00237 
00238                                 cob_3d_features::invariant_surface_feature::SingleTriangle<Scalar, S::Samples, S::Values> tri(
00239                                         points[mesh.polygons[i].vertices[j  ]].getVector3fMap().cast<double>()+off2,
00240                                         points[mesh.polygons[i].vertices[j+1]].getVector3fMap().cast<double>()+off2,
00241                                         points[mesh.polygons[i].vertices[j+2]].getVector3fMap().cast<double>()+off2
00242                                 );
00243                                 tri.compute(samples);
00244                                 gedt += tri;
00245 
00246 
00247 
00248 
00249                                 
00250 
00251 
00252 
00253 
00254                         }
00255                 }
00256 for(size_t l=0; l<gedt.complex_vals_[7].size(); l++) {
00257         double d;
00258         d=std::abs(gedt.complex_vals_[7][l]);
00259         fwrite(&d, sizeof(double), 1, fp);
00260 }
00261 fclose(fp);
00262                 std::cout<<"step 2 finished: "<<(time(0)-start)<<"s"<<std::endl;
00263         }
00264 
00265         if(test) {S::Values vals;
00266         simple_test(n1, samples, vals);
00267         gedt+=vals;}
00268         gedt.sample(sKeys);
00269 
00270         const int bw = sKeys[0].bandWidth();
00271         Signature< Real > sig( (bw) * int( sKeys.size() ) );
00272         for( int i=0 ; i<sKeys.size() ; i++ )
00273         {
00274                 std::cout<<"Key:"<<std::endl;
00275                 for( int b=0 ; b<bw ; b++ )
00276                 {
00277                         Real _norm2 = sKeys[i](b,0).squareNorm();
00278                         
00279                         for( int j=1 ; j<=b ; j++ ) {
00280                                 _norm2 += sKeys[i](b,j).squareNorm()*2;
00281                                 
00282                         }
00283                         sig[i*bw+b] = Real( sqrt(_norm2) );
00284                         if(b%2==0)
00285                                 std::cout<<sig[i*bw+b]<<" ";
00286                 }
00287                 std::cout<<std::endl;
00288         }
00289         sig.write( "/tmp/sig1"  );
00290 
00291         for(size_t i=0; i<sKeys.size(); i++)
00292         {
00293                 char fn[512];
00294                 sprintf(fn, "/tmp/key1_%d", i);
00295                 sKeys[i].write(fn);
00296         }
00297 
00298 
00299         if(!test) {
00300                 FILE *fp = fopen("/tmp/cmp_prec2", "wb");
00301                 pcl::PolygonMesh mesh;
00302                 pcl::io::loadPolygonFile(fn2, mesh);
00303                 std::cout<<"step 1 finished"<<std::endl;
00304 
00305                 pcl::PointCloud<pcl::PointXYZ> points;
00306                 pcl::fromROSMsg(mesh.cloud, points);
00307                 time_t start = time(0);
00308                 for(size_t i=Start; i<std::min(mesh.polygons.size(), (size_t)M); i++) {
00309                         for(int j=0; j<(int)mesh.polygons[i].vertices.size()-2; j++) {
00310                                 cob_3d_features::invariant_surface_feature::SingleTriangle<Scalar, S::Samples, S::Values> tri(
00311                                         rot*points[mesh.polygons[i].vertices[j  ]].getVector3fMap().cast<double>()+off+off2,
00312                                         rot*points[mesh.polygons[i].vertices[j+1]].getVector3fMap().cast<double>()+off+off2,
00313                                         rot*points[mesh.polygons[i].vertices[j+2]].getVector3fMap().cast<double>()+off+off2
00314                                 );
00315                                 tri.compute(samples);
00316                                 gedt2 += tri;
00317 
00318 
00319 
00320 
00321                                 
00322 
00323 
00324 
00325 
00326                                 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340                         }
00341                 }
00342 for(size_t l=0; l<gedt2.complex_vals_[7].size(); l++) {
00343         double d;
00344         d=std::abs(gedt2.complex_vals_[7][l]);
00345         fwrite(&d, sizeof(double), 1, fp);
00346 }
00347 fclose(fp);
00348         }
00349 
00350         if(test) {
00351                 FILE *fp1 = fopen("/tmp/cmp_prec", "rb");
00352                 FILE *fp2 = fopen("/tmp/cmp_prec2", "rb");
00353                 double m=0;
00354                 int i=0;
00355                 while(!feof(fp1) && !feof(fp2)) {
00356                         double d1,d2;
00357                         fread(&d1, sizeof(double), 1, fp1);
00358                         fread(&d2, sizeof(double), 1, fp2);
00359                         std::cout<<i<<":\t"<<d1-d2<<" "<<d1<<" "<<d2<<" \t    "<<samples[7][i++].transpose()<<"\n";
00360                         m = std::max(std::abs(d1-d2), m);
00361                 }
00362                 std::cout<<std::endl<<"MAX "<<m<<std::endl;
00363                 fclose(fp1);
00364                 fclose(fp2);
00365         }
00366 
00367 
00368         if(test) {S::Values vals;
00369         simple_test(n2, samples, vals);
00370         gedt2+=vals;}
00371         gedt2.sample(sKeys);
00372 
00373         
00374 
00375 
00376 
00377 
00378 
00379 
00380 
00381 
00382 
00383 
00384 
00385         {
00386         Signature< Real > sig( (bw) * int( sKeys.size() ) );
00387         for( int i=0 ; i<sKeys.size() ; i++ )
00388         {
00389                 std::cout<<"Key:"<<std::endl;
00390                 for( int b=0 ; b<bw ; b++ )
00391                 {
00392                         Real _norm2 = sKeys[i](b,0).squareNorm();
00393                         
00394                         for( int j=1 ; j<=b ; j++ ) {
00395                                 _norm2 += sKeys[i](b,j).squareNorm()*2;
00396                                 
00397                         }
00398                         sig[i*bw+b] = Real( sqrt(_norm2) );
00399                         if(b%2==0) std::cout<<sig[i*bw+b]<<" ";
00400                 }
00401                 std::cout<<std::endl;
00402         }
00403         sig.write( "/tmp/sig2"  );}
00404 
00405         for(size_t i=0; i<sKeys.size(); i++)
00406         {
00407                 char fn[512];
00408                 sprintf(fn, "/tmp/key2_%d", i);
00409                 sKeys[i].write(fn);
00410         }
00411         
00412         {
00413                 enum {DEGREE=2};
00414                 typedef cob_3d_features::InvariantSurfaceFeature<Segmentation::S_POLYGON<DEGREE> > ISF;
00415                 ISF isf(16,16);
00416                 
00417                 
00418                 isf.addRadius(0.25);
00419                 
00420                 
00421                 std::ifstream fis("/tmp/blub");
00422                 Segmentation::Segmentation_QuadRegression<pcl::PointXYZ, pcl::PointXYZRGB, Segmentation::QPPF::QuadRegression<DEGREE, pcl::PointXYZ, Segmentation::QPPF::CameraModel_Kinect<pcl::PointXYZ> > > seg;
00423                 seg.serialize(fis);
00424                 
00425                 ISF::PTSurfaceList input(new ISF::TSurfaceList);
00426                 *input = seg.getPolygons();
00427                 std::cout<<"read data"<<std::endl;
00428                 
00429                 
00430                 isf.setInput(input);
00431                 std::cout<<"setInput"<<std::endl;
00432                 isf.compute();
00433                 std::cout<<"compute"<<std::endl;
00434                 ISF::PResultConst oldR = isf.getResult();
00435                 std::cout<<"getResult"<<std::endl;
00436                 
00437                 pcl::io::savePLYFile("map1.ply",                *isf.dbg_Mesh_of_Map());
00438         }
00439 }
00440 
00441 #if 0
00442 #include <fstream>
00443 #include <ros/ros.h>
00444 #include <pcl/point_types.h>
00445 #include <pcl/io/pcd_io.h>
00446 #include <sensor_msgs/Image.h>
00447 #include <pcl/io/ply_io.h>
00448 
00449 
00450 #include "cob_3d_features/impl/invariant_surface_feature.hpp"
00451 #include "cob_3d_features/impl/invariant_surface_feature_unit_tests.hpp"
00452 #include "cob_3d_features/impl/invariant_surface_feature_debug.hpp"
00453 #include <cob_3d_segmentation/quad_regression/quad_regression.h>
00454 #include "cob_3d_segmentation/quad_regression/polygon.h"
00455 
00456 
00457 TEST(InvariantSurfaceFeature, sampleSurfaces) {
00458         typedef cob_3d_features::InvariantSurfaceFeature<4,4, Segmentation::S_POLYGON<2>, double, Eigen::Affine3d> ISF;
00459 
00460         
00461         std::ifstream fis("/tmp/blub");
00462         Segmentation::Segmentation_QuadRegression<pcl::PointXYZ, pcl::PointXYZRGB, Segmentation::QPPF::QuadRegression<2, pcl::PointXYZ, Segmentation::QPPF::CameraModel_Kinect<pcl::PointXYZ> > > seg;
00463         seg.serialize(fis);
00464         ISF::PTSurfaceList input(new ISF::TSurfaceList);
00465         *input = seg.getPolygons();
00466 
00467         
00468         std::ifstream fis2("/tmp/blub2");
00469         seg.serialize(fis2);
00470         ISF::PTSurfaceList input2(new ISF::TSurfaceList);
00471         *input2 = seg.getPolygons();
00472 
00473         ISF isf;
00474 
00475         
00476         
00477         isf.addRadius(0.5);
00478         
00479 
00480         
00481         isf.setInput(input);
00482         isf.compute();
00483         ISF::PResultVectorListConst oldR = isf.getResult();
00484         pcl::io::savePLYFile("map1.ply",                *isf.dbg_Mesh_of_Map());
00485 
00486         
00487         isf.setInput(input2);
00488         isf.compute();
00489         ISF::PResultVectorListConst newR = isf.getResult();
00490         pcl::io::savePLYFile("map2.ply",                *isf.dbg_Mesh_of_Map());
00491 
00492         
00493         Eigen::MatrixXd cor(oldR->size(),oldR->size());
00494         for(size_t i=0; i<oldR->size(); i++) {
00495                 for(size_t k=0; k<oldR->size(); k++) {
00496 
00497                         cor(i,k)=0;
00498                         for(size_t j=0; j<(*oldR)[i].ft.size(); j++)
00499                                 cor(i,k) += (*oldR)[i].ft[j]-(*oldR)[k].ft[j];
00500 
00501                 }
00502         }
00503 
00504         std::cout<<"Correlation\n"<<cor<<std::endl;
00505 
00506 
00507         
00508         Eigen::MatrixXd cor2(oldR->size(),newR->size());
00509         for(size_t i=0; i<oldR->size(); i++) {
00510                 for(size_t k=0; k<newR->size(); k++) {
00511 
00512                         cor2(i,k)=0;
00513                         for(size_t j=0; j<(*oldR)[i].ft.size(); j++)
00514                                 cor2(i,k) += (*oldR)[i].ft[j]-(*newR)[k].ft[j];
00515 
00516                 }
00517         }
00518 
00519         std::cout<<"Correlation\n"<<cor2<<std::endl;
00520 }
00521 
00522 
00523 
00524 
00525 
00526 
00527 
00528 
00529 
00530 
00531 
00532 
00533 
00534 
00535 
00536 
00537 
00538 
00539 
00540 
00541 
00542 
00543 
00544 
00545 
00546 
00547 
00548 
00549 
00550 
00551 
00552 
00553 
00554 
00555 
00556 
00557 
00558 
00559 
00560 
00561 
00562 
00563 
00564 
00565 
00566 
00567 
00568 
00569 
00570 
00571 
00572 
00573 
00574 
00575 
00576 
00577 
00578 
00579 
00580 int main(int argc, char **argv)
00581 {
00582   ::testing::InitGoogleTest(&argc, argv);
00583   return RUN_ALL_TESTS();
00584 }
00585 #endif