test_io.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id: test_io.cpp 4968 2012-03-08 06:39:52Z rusu $
00037  *
00038  */
00039 
00040 #include <gtest/gtest.h>
00041 #include <sensor_msgs/PointCloud2.h>
00042 #include <pcl/point_traits.h>
00043 #include <pcl/point_types.h>
00044 #include <pcl/common/io.h>
00045 #include <pcl/console/print.h>
00046 #include <pcl/io/pcd_io.h>
00047 #include <pcl/io/ply_io.h>
00048 #include <fstream>
00049 #include <locale>
00050 #include <stdexcept>
00051 
00052 using namespace pcl;
00053 using namespace pcl::io;
00054 
00056 TEST (PCL, ComplexPCDFileASCII)
00057 {
00058   std::ofstream fs;
00059   fs.open ("complex_ascii.pcd");
00060   fs << "# .PCD v0.7 - Point Cloud Data file format\n"
00061         "VERSION 0.7\n"
00062         "FIELDS fpfh _ x y z\n"
00063         "SIZE 4 1 4 4 4\n"
00064         "TYPE F F F F F\n"
00065         "COUNT 33 10 1 1 1\n"
00066         "WIDTH 1\n"
00067         "HEIGHT 1\n"
00068         "VIEWPOINT 0 0 0 1 0 0 0\n"
00069         "POINTS 1\n"
00070         "DATA ascii\n"
00071         "0 0 0 0 0 100 0 0 0 0 0 0 0 0 0 0 100 0 0 0 0 0 0 0 0 0 0 100 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 -69.234001 -65.460999 19.173";
00072   fs.close ();
00073 
00074   sensor_msgs::PointCloud2 blob;
00075   int res = loadPCDFile ("complex_ascii.pcd", blob);
00076   EXPECT_NE (int (res), -1);
00077   EXPECT_EQ (blob.width, 1);
00078   EXPECT_EQ (blob.height, 1);
00079   EXPECT_EQ (blob.is_dense, true);
00080   EXPECT_EQ (blob.data.size (), 4 * 33 + 10 * 1 + 4 * 3);
00081 
00082   // Check fields
00083   EXPECT_EQ (blob.fields[0].name, "fpfh");
00084   EXPECT_EQ (blob.fields[0].offset, 0);
00085   EXPECT_EQ (blob.fields[0].count, 33);
00086   EXPECT_EQ (blob.fields[0].datatype, sensor_msgs::PointField::FLOAT32);
00087 
00088   EXPECT_EQ (blob.fields[1].name, "_");
00089   EXPECT_EQ (blob.fields[1].offset, 4 * 33);
00090   EXPECT_EQ (blob.fields[1].count, 10);
00091   EXPECT_EQ (blob.fields[1].datatype, sensor_msgs::PointField::FLOAT32);
00092   
00093   EXPECT_EQ (blob.fields[2].name, "x");
00094   EXPECT_EQ (blob.fields[2].offset, 4 * 33 + 10 * 1);
00095   EXPECT_EQ (blob.fields[2].count, 1);
00096   EXPECT_EQ (blob.fields[2].datatype, sensor_msgs::PointField::FLOAT32);
00097   
00098   EXPECT_EQ (blob.fields[3].name, "y");
00099   EXPECT_EQ (blob.fields[3].offset, 4 * 33 + 10 * 1 + 4);
00100   EXPECT_EQ (blob.fields[3].count, 1);
00101   EXPECT_EQ (blob.fields[3].datatype, sensor_msgs::PointField::FLOAT32);
00102   
00103   EXPECT_EQ (blob.fields[4].name, "z");
00104   EXPECT_EQ (blob.fields[4].offset, 4 * 33 + 10 * 1 + 4 + 4);
00105   EXPECT_EQ (blob.fields[4].count, 1);
00106   EXPECT_EQ (blob.fields[4].datatype, sensor_msgs::PointField::FLOAT32);
00107 
00108   int x_idx = pcl::getFieldIndex (blob, "x");
00109   EXPECT_EQ (x_idx, 2);
00110   float x, y, z;
00111   memcpy (&x, &blob.data[0 * blob.point_step + blob.fields[x_idx + 0].offset], sizeof (float));
00112   memcpy (&y, &blob.data[0 * blob.point_step + blob.fields[x_idx + 1].offset], sizeof (float));
00113   memcpy (&z, &blob.data[0 * blob.point_step + blob.fields[x_idx + 2].offset], sizeof (float));
00114   EXPECT_FLOAT_EQ (x, -69.234001f);
00115   EXPECT_FLOAT_EQ (y, -65.460999f);
00116   EXPECT_FLOAT_EQ (z, 19.173f);
00117 
00118   int fpfh_idx = pcl::getFieldIndex (blob, "fpfh");
00119   EXPECT_EQ (fpfh_idx, 0);
00120   float val[33];
00121   for (size_t i = 0; i < blob.fields[fpfh_idx].count; ++i)
00122     memcpy (&val[i], &blob.data[0 * blob.point_step + blob.fields[fpfh_idx + 0].offset + i * sizeof (float)], sizeof (float));
00123 
00124   EXPECT_EQ (val[0], 0); 
00125   EXPECT_EQ (val[1], 0); 
00126   EXPECT_EQ (val[2], 0); 
00127   EXPECT_EQ (val[3], 0); 
00128   EXPECT_EQ (val[4], 0); 
00129   EXPECT_EQ (val[5], 100); 
00130   EXPECT_EQ (val[6], 0); 
00131   EXPECT_EQ (val[7], 0); 
00132   EXPECT_EQ (val[8], 0); 
00133   EXPECT_EQ (val[9], 0); 
00134   EXPECT_EQ (val[10], 0); 
00135   EXPECT_EQ (val[11], 0); 
00136   EXPECT_EQ (val[12], 0); 
00137   EXPECT_EQ (val[13], 0); 
00138   EXPECT_EQ (val[14], 0); 
00139   EXPECT_EQ (val[15], 0); 
00140   EXPECT_EQ (val[16], 100); 
00141   EXPECT_EQ (val[17], 0); 
00142   EXPECT_EQ (val[18], 0); 
00143   EXPECT_EQ (val[19], 0); 
00144   EXPECT_EQ (val[20], 0); 
00145   EXPECT_EQ (val[21], 0); 
00146   EXPECT_EQ (val[22], 0); 
00147   EXPECT_EQ (val[23], 0); 
00148   EXPECT_EQ (val[24], 0); 
00149   EXPECT_EQ (val[25], 0); 
00150   EXPECT_EQ (val[26], 0); 
00151   EXPECT_EQ (val[27], 100); 
00152   EXPECT_EQ (val[28], 0); 
00153   EXPECT_EQ (val[29], 0); 
00154   EXPECT_EQ (val[30], 0); 
00155   EXPECT_EQ (val[31], 0); 
00156   EXPECT_EQ (val[32], 0); 
00157 }
00158 
00160 TEST (PCL, AllTypesPCDFile)
00161 {
00162   std::ofstream fs;
00163   fs.open ("all_types.pcd");
00164   fs << "# .PCD v0.7 - Point Cloud Data file format\n"
00165         "VERSION 0.7\n"
00166         "FIELDS a1 a2 a3 a4 a5 a6 a7 a8\n"
00167         "SIZE    1  1  2  2  4  4  4  8\n"
00168         "TYPE    I  U  I  U  I  U  F  F\n"
00169         "COUNT   1  2  1  2  1  2  1  2\n"
00170         "WIDTH 1\n"
00171         "HEIGHT 1\n"
00172         "VIEWPOINT 0 0 0 1 0 0 0\n"
00173         "POINTS 1\n"
00174         "DATA ascii\n"
00175         "-50 250 251 -250 2500 2501 -250000 250000 250001 250.05 -250.05 -251.05";
00176   fs.close ();
00177 
00178   sensor_msgs::PointCloud2 blob;
00179   int res = loadPCDFile ("all_types.pcd", blob);
00180   EXPECT_NE (int (res), -1);
00181   EXPECT_EQ (blob.width, 1);
00182   EXPECT_EQ (blob.height, 1);
00183   EXPECT_EQ (blob.data.size (), 1 + 1 * 2 + 2 * 1 + 2 * 2 + 4 * 1 + 4 * 2 + 4 * 1 + 8 * 2);
00184   EXPECT_EQ (blob.is_dense, true);
00185 
00186   EXPECT_EQ (blob.fields.size (), 8);
00187   // Check fields
00188   EXPECT_EQ (blob.fields[0].name, "a1");
00189   EXPECT_EQ (blob.fields[1].name, "a2");
00190   EXPECT_EQ (blob.fields[2].name, "a3");
00191   EXPECT_EQ (blob.fields[3].name, "a4");
00192   EXPECT_EQ (blob.fields[4].name, "a5");
00193   EXPECT_EQ (blob.fields[5].name, "a6");
00194   EXPECT_EQ (blob.fields[6].name, "a7");
00195   EXPECT_EQ (blob.fields[7].name, "a8");
00196 
00197   EXPECT_EQ (blob.fields[0].offset, 0);
00198   EXPECT_EQ (blob.fields[1].offset, 1);
00199   EXPECT_EQ (blob.fields[2].offset, 1 + 1 * 2);
00200   EXPECT_EQ (blob.fields[3].offset, 1 + 1 * 2 + 2 * 1);
00201   EXPECT_EQ (blob.fields[4].offset, 1 + 1 * 2 + 2 * 1 + 2 * 2);
00202   EXPECT_EQ (blob.fields[5].offset, 1 + 1 * 2 + 2 * 1 + 2 * 2 + 4 * 1);
00203   EXPECT_EQ (blob.fields[6].offset, 1 + 1 * 2 + 2 * 1 + 2 * 2 + 4 * 1 + 4 * 2);
00204   EXPECT_EQ (blob.fields[7].offset, 1 + 1 * 2 + 2 * 1 + 2 * 2 + 4 * 1 + 4 * 2 + 4 * 1);
00205 
00206   EXPECT_EQ (blob.fields[0].count, 1);
00207   EXPECT_EQ (blob.fields[1].count, 2);
00208   EXPECT_EQ (blob.fields[2].count, 1);
00209   EXPECT_EQ (blob.fields[3].count, 2);
00210   EXPECT_EQ (blob.fields[4].count, 1);
00211   EXPECT_EQ (blob.fields[5].count, 2);
00212   EXPECT_EQ (blob.fields[6].count, 1);
00213   EXPECT_EQ (blob.fields[7].count, 2);
00214 
00215   EXPECT_EQ (blob.fields[0].datatype, sensor_msgs::PointField::INT8);
00216   EXPECT_EQ (blob.fields[1].datatype, sensor_msgs::PointField::UINT8);
00217   EXPECT_EQ (blob.fields[2].datatype, sensor_msgs::PointField::INT16);
00218   EXPECT_EQ (blob.fields[3].datatype, sensor_msgs::PointField::UINT16);
00219   EXPECT_EQ (blob.fields[4].datatype, sensor_msgs::PointField::INT32);
00220   EXPECT_EQ (blob.fields[5].datatype, sensor_msgs::PointField::UINT32);
00221   EXPECT_EQ (blob.fields[6].datatype, sensor_msgs::PointField::FLOAT32);
00222   EXPECT_EQ (blob.fields[7].datatype, sensor_msgs::PointField::FLOAT64);
00223 
00224   int8_t b1;
00225   uint8_t b2;
00226   int16_t b3;
00227   uint16_t b4;
00228   int32_t b5;
00229   uint32_t b6;
00230   float b7;
00231   double b8;
00232   memcpy (&b1, &blob.data[blob.fields[0].offset], sizeof (int8_t));
00233   EXPECT_FLOAT_EQ (b1, -50);
00234   memcpy (&b2, &blob.data[blob.fields[1].offset], sizeof (uint8_t));
00235   EXPECT_FLOAT_EQ (b2, 250);
00236   memcpy (&b2, &blob.data[blob.fields[1].offset + sizeof (uint8_t)], sizeof (uint8_t));
00237   EXPECT_FLOAT_EQ (b2, 251);
00238   memcpy (&b3, &blob.data[blob.fields[2].offset], sizeof (int16_t));
00239   EXPECT_FLOAT_EQ (b3, -250);
00240   memcpy (&b4, &blob.data[blob.fields[3].offset], sizeof (uint16_t));
00241   EXPECT_FLOAT_EQ (b4, 2500);
00242   memcpy (&b4, &blob.data[blob.fields[3].offset + sizeof (uint16_t)], sizeof (uint16_t));
00243   EXPECT_FLOAT_EQ (b4, 2501);
00244   memcpy (&b5, &blob.data[blob.fields[4].offset], sizeof (int32_t));
00245   EXPECT_FLOAT_EQ (float (b5), float (-250000));
00246   memcpy (&b6, &blob.data[blob.fields[5].offset], sizeof (uint32_t));
00247   EXPECT_FLOAT_EQ (float (b6), float (250000));
00248   memcpy (&b6, &blob.data[blob.fields[5].offset + sizeof (uint32_t)], sizeof (uint32_t));
00249   EXPECT_FLOAT_EQ (float (b6), float (250001));
00250   memcpy (&b7, &blob.data[blob.fields[6].offset], sizeof (float));
00251   EXPECT_FLOAT_EQ (b7, 250.05f);
00252   memcpy (&b8, &blob.data[blob.fields[7].offset], sizeof (double));
00253   EXPECT_FLOAT_EQ (float (b8), -250.05f);
00254   memcpy (&b8, &blob.data[blob.fields[7].offset + sizeof (double)], sizeof (double));
00255   EXPECT_FLOAT_EQ (float (b8), -251.05f);
00256 }
00257 
00259 TEST (PCL, ConcatenatePoints)
00260 {
00261   pcl::PointCloud<pcl::PointXYZ> cloud_a, cloud_b, cloud_c;
00262 
00263   // Fill in the cloud data
00264   cloud_a.width  = 5;
00265   cloud_b.width  = 3;
00266   cloud_a.height = cloud_b.height = 1;
00267   cloud_a.points.resize (cloud_a.width * cloud_a.height);
00268   cloud_b.points.resize (cloud_b.width * cloud_b.height);
00269 
00270   for (size_t i = 0; i < cloud_a.points.size (); ++i)
00271   {
00272     cloud_a.points[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00273     cloud_a.points[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00274     cloud_a.points[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00275   }
00276 
00277   for (size_t i = 0; i < cloud_b.points.size (); ++i)
00278   {
00279     cloud_b.points[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00280     cloud_b.points[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00281     cloud_b.points[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00282   }
00283 
00284   // Copy the point cloud data
00285   cloud_c  = cloud_a;
00286   cloud_c += cloud_b;
00287   EXPECT_EQ (cloud_c.points.size (), cloud_a.points.size () + cloud_b.points.size ());
00288   EXPECT_EQ (cloud_c.width, cloud_a.width + cloud_b.width);
00289   EXPECT_EQ (int (cloud_c.height), 1);
00290 
00291   for (size_t i = 0; i < cloud_a.points.size (); ++i)
00292   {
00293     EXPECT_FLOAT_EQ (cloud_c.points[i].x, cloud_a.points[i].x);
00294     EXPECT_FLOAT_EQ (cloud_c.points[i].y, cloud_a.points[i].y);
00295     EXPECT_FLOAT_EQ (cloud_c.points[i].z, cloud_a.points[i].z);
00296   }
00297   for (size_t i = cloud_a.points.size (); i < cloud_c.points.size (); ++i)
00298   {
00299     EXPECT_FLOAT_EQ (cloud_c.points[i].x, cloud_b.points[i - cloud_a.points.size ()].x);
00300     EXPECT_FLOAT_EQ (cloud_c.points[i].y, cloud_b.points[i - cloud_a.points.size ()].y);
00301     EXPECT_FLOAT_EQ (cloud_c.points[i].z, cloud_b.points[i - cloud_a.points.size ()].z);
00302   }
00303 }
00304 
00306 TEST (PCL, ConcatenateFields)
00307 {
00308   pcl::PointCloud<pcl::PointXYZ> cloud_a;
00309   pcl::PointCloud<pcl::Normal> cloud_b;
00310   pcl::PointCloud<pcl::PointNormal> cloud_c;
00311 
00312   // Fill in the cloud data
00313   cloud_a.width  = cloud_b.width  = 5;
00314   cloud_a.height = cloud_b.height = 1;
00315   cloud_a.points.resize (cloud_a.width * cloud_a.height);
00316   cloud_b.points.resize (cloud_b.width * cloud_b.height);
00317 
00318   for (size_t i = 0; i < cloud_a.points.size (); ++i)
00319   {
00320     cloud_a[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00321     cloud_a[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00322     cloud_a[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00323   }
00324 
00325   for (size_t i = 0; i < cloud_b.points.size (); ++i)
00326   {
00327     cloud_b[i].normal_x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00328     cloud_b[i].normal_y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00329     cloud_b[i].normal_z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00330   }
00331 
00332   pcl::concatenateFields (cloud_a, cloud_b, cloud_c);
00333   EXPECT_EQ (cloud_c.points.size (), cloud_a.points.size ());
00334   EXPECT_EQ (cloud_c.width, cloud_a.width);
00335   EXPECT_EQ (cloud_c.height, cloud_a.height);
00336 
00337   for (size_t i = 0; i < cloud_a.points.size (); ++i)
00338   {
00339     EXPECT_FLOAT_EQ (cloud_c.points[i].x, cloud_a.points[i].x);
00340     EXPECT_FLOAT_EQ (cloud_c.points[i].y, cloud_a.points[i].y);
00341     EXPECT_FLOAT_EQ (cloud_c.points[i].z, cloud_a.points[i].z);
00342     EXPECT_FLOAT_EQ (cloud_c.points[i].normal[0], cloud_b.points[i].normal[0]);
00343     EXPECT_FLOAT_EQ (cloud_c.points[i].normal[1], cloud_b.points[i].normal[1]);
00344     EXPECT_FLOAT_EQ (cloud_c.points[i].normal[2], cloud_b.points[i].normal[2]);
00345   }
00346 }
00347 
00349 TEST (PCL, IO)
00350 {
00351   sensor_msgs::PointCloud2 cloud_blob;
00352   PointCloud<PointXYZI> cloud;
00353 
00354   cloud.width  = 640;
00355   cloud.height = 480;
00356   cloud.points.resize (cloud.width * cloud.height);
00357   cloud.is_dense = true;
00358 
00359   srand (static_cast<unsigned int> (time (NULL)));
00360   size_t nr_p = cloud.points.size ();
00361   // Randomly create a new point cloud
00362   for (size_t i = 0; i < nr_p; ++i)
00363   {
00364     cloud[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00365     cloud[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00366     cloud[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00367     cloud.points[i].intensity = static_cast<float> (i);
00368   }
00369   PointXYZI first, last;
00370   first.x = cloud.points[0].x;       first.y = cloud.points[0].y;       first.z = cloud.points[0].z;       first.intensity = cloud.points[0].intensity;
00371   last.x = cloud.points[nr_p - 1].x; last.y = cloud.points[nr_p - 1].y; last.z = cloud.points[nr_p - 1].z; last.intensity  = cloud.points[nr_p - 1].intensity;
00372 
00373   // Tests for PointCloud::operator()
00374   EXPECT_FLOAT_EQ (first.x, cloud (0, 0).x);
00375   EXPECT_FLOAT_EQ (first.y, cloud (0, 0).y);
00376   EXPECT_FLOAT_EQ (first.z, cloud (0, 0).z);
00377   EXPECT_FLOAT_EQ (first.intensity, 0.0f);
00378   EXPECT_FLOAT_EQ (last.x, cloud (cloud.width-1, cloud.height-1).x);
00379   EXPECT_FLOAT_EQ (last.y, cloud (cloud.width-1, cloud.height-1).y);
00380   EXPECT_FLOAT_EQ (last.z, cloud (cloud.width-1, cloud.height-1).z);
00381   EXPECT_FLOAT_EQ (last.intensity, static_cast<float> (nr_p - 1));
00382 
00383   // Test getFieldIndex
00384   std::vector<sensor_msgs::PointField> fields;
00385   pcl::getFields (cloud, fields);
00386   EXPECT_EQ (fields.size (), size_t (4));
00387   int x_idx = pcl::getFieldIndex (cloud, "x", fields);
00388   EXPECT_EQ (x_idx, 0);
00389   EXPECT_EQ (fields[x_idx].offset, uint32_t (0));
00390   EXPECT_EQ (fields[x_idx].name, "x");
00391   EXPECT_EQ (fields[x_idx].datatype, sensor_msgs::PointField::FLOAT32);
00392   EXPECT_EQ (fields[x_idx].count, uint32_t (1));
00393 
00394   int y_idx = pcl::getFieldIndex (cloud, "y", fields);
00395   EXPECT_EQ (y_idx, 1);
00396   EXPECT_EQ (fields[y_idx].offset, uint32_t (4));
00397   EXPECT_EQ (fields[y_idx].name, "y");
00398   EXPECT_EQ (fields[y_idx].datatype, sensor_msgs::PointField::FLOAT32);
00399   EXPECT_EQ (fields[y_idx].count, uint32_t (1));
00400 
00401   int z_idx = pcl::getFieldIndex (cloud, "z", fields);
00402   EXPECT_EQ (z_idx, 2);
00403   EXPECT_EQ (fields[z_idx].offset, uint32_t (8));
00404   EXPECT_EQ (fields[z_idx].name, "z");
00405   EXPECT_EQ (fields[z_idx].datatype, sensor_msgs::PointField::FLOAT32);
00406   EXPECT_EQ (fields[z_idx].count, uint32_t (1));
00407 
00408   int intensity_idx = pcl::getFieldIndex (cloud, "intensity", fields);
00409   EXPECT_EQ (intensity_idx, 3);
00410   EXPECT_EQ (fields[intensity_idx].offset, uint32_t (16));      // NOTE: intensity_idx.offset should be 12, but we are padding in PointXYZ (!)
00411   EXPECT_EQ (fields[intensity_idx].name, "intensity");
00412   EXPECT_EQ (fields[intensity_idx].datatype, sensor_msgs::PointField::FLOAT32);
00413   EXPECT_EQ (fields[intensity_idx].count, uint32_t (1));
00414 
00415   // Convert from data type to blob
00416   toROSMsg (cloud, cloud_blob);
00417 
00418   // Test getFieldIndex
00419   x_idx = pcl::getFieldIndex (cloud_blob, "x");
00420   EXPECT_EQ (x_idx, 0);
00421   EXPECT_EQ (cloud_blob.fields[x_idx].offset, uint32_t (0));
00422   EXPECT_EQ (cloud_blob.fields[x_idx].name, "x");
00423   EXPECT_EQ (cloud_blob.fields[x_idx].datatype, sensor_msgs::PointField::FLOAT32);
00424   EXPECT_EQ (cloud_blob.fields[x_idx].count, uint32_t (1));
00425   y_idx = pcl::getFieldIndex (cloud_blob, "y");
00426   EXPECT_EQ (y_idx, 1);
00427   EXPECT_EQ (cloud_blob.fields[y_idx].offset, uint32_t (4));
00428   EXPECT_EQ (cloud_blob.fields[y_idx].name, "y");
00429   EXPECT_EQ (cloud_blob.fields[y_idx].datatype, sensor_msgs::PointField::FLOAT32);
00430   EXPECT_EQ (cloud_blob.fields[y_idx].count, uint32_t (1));
00431   z_idx = pcl::getFieldIndex (cloud_blob, "z");
00432   EXPECT_EQ (z_idx, 2);
00433   EXPECT_EQ (cloud_blob.fields[z_idx].offset, uint32_t (8));
00434   EXPECT_EQ (cloud_blob.fields[z_idx].name, "z");
00435   EXPECT_EQ (cloud_blob.fields[z_idx].datatype, sensor_msgs::PointField::FLOAT32);
00436   EXPECT_EQ (cloud_blob.fields[z_idx].count, uint32_t (1));
00437   intensity_idx = pcl::getFieldIndex (cloud_blob, "intensity");
00438   EXPECT_EQ (intensity_idx, 3);
00439   //EXPECT_EQ (cloud_blob.fields[intensity_idx].offset, (uint32_t)12);      // NOTE: the fields.offset is 16 in PointCloud<PointXYZI>, but we are obtaining the correct offset in toROSMsg
00440   EXPECT_EQ (cloud_blob.fields[intensity_idx].offset, uint32_t (16));      // NOTE: the fields.offset is 16 in PointCloud<PointXYZI>, but we are obtaining the correct offset in toROSMsg
00441   EXPECT_EQ (cloud_blob.fields[intensity_idx].name, "intensity");
00442   EXPECT_EQ (cloud_blob.fields[intensity_idx].datatype, sensor_msgs::PointField::FLOAT32);
00443   EXPECT_EQ (cloud_blob.fields[intensity_idx].count, uint32_t (1));
00444   
00445   fromROSMsg (cloud_blob, cloud);
00446   for (size_t i = 0; i < nr_p; ++i)
00447     EXPECT_EQ (cloud.points[i].intensity, i);
00448 
00449   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);    // test for toROSMsg ()
00450   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);  // test for toROSMsg ()
00451   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);  // test for toROSMsg ()
00452   //EXPECT_EQ ((size_t)cloud_blob.data.size () * 2,         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00453   //           cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for toROSMsg ()
00454   EXPECT_EQ (size_t (cloud_blob.data.size ()),             // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00455              cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for toROSMsg ()
00456 
00457   // Make sure we have permissions to write there
00458   PCDWriter w;
00459   int res = w.writeASCII ("test_pcl_io.pcd", cloud_blob, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), 10);
00460   EXPECT_EQ (int (res), 0);                            // test for savePCDFileASCII ()
00461 
00462   // Please make sure that this file exists, otherwise the test will fail.
00463   res = loadPCDFile ("test_pcl_io.pcd", cloud_blob);
00464   EXPECT_NE (int (res), -1);                               // test for loadPCDFile ()
00465   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);    // test for loadPCDFile ()
00466   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);  // test for loadPCDFile ()
00467   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);  // test for loadPCDFile ()
00468   EXPECT_EQ (size_t (cloud_blob.data.size () * 2),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00469               cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for loadPCDFile ()
00470 
00471   // Convert from blob to data type
00472   fromROSMsg (cloud_blob, cloud);
00473 
00474   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00475   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00476   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00477   EXPECT_EQ (size_t (cloud.points.size ()), nr_p);         // test for fromROSMsg ()
00478 
00479   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00480   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00481   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00482   EXPECT_FLOAT_EQ (cloud.points[0].intensity, first.intensity);  // test for fromROSMsg ()
00483 
00484   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].x, last.x);    // test for fromROSMsg ()
00485   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].y, last.y);    // test for fromROSMsg ()
00486   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].z, last.z);    // test for fromROSMsg ()
00487   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].intensity, last.intensity); // test for fromROSMsg ()
00488 
00489   // Make sure we have permissions to write there
00490   res = savePCDFile ("test_pcl_io.pcd", cloud_blob, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), true);
00491   EXPECT_EQ (int (res), 0);                            // test for savePCDFileBinary ()
00492 
00493   // Please make sure that this file exists, otherwise the test will fail.
00494   res = loadPCDFile ("test_pcl_io.pcd", cloud_blob);
00495   EXPECT_NE (int (res), -1);                               // test for loadPCDFile ()
00496   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);    // test for loadPCDFile ()
00497   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);  // test for loadPCDFile ()
00498   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);
00499   EXPECT_EQ (size_t (cloud_blob.data.size () * 2),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00500               cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for loadPCDFile ()
00501 
00502   // Convert from blob to data type
00503   fromROSMsg (cloud_blob, cloud);
00504 
00505   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00506   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00507   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00508   EXPECT_EQ (size_t (cloud.points.size ()), nr_p);         // test for fromROSMsg ()
00509 
00510   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00511   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00512   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00513   EXPECT_FLOAT_EQ (cloud.points[0].intensity, first.intensity);  // test for fromROSMsg ()
00514 
00515   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].x, last.x);    // test for fromROSMsg ()
00516   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].y, last.y);    // test for fromROSMsg ()
00517   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].z, last.z);    // test for fromROSMsg ()
00518   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].intensity, last.intensity); // test for fromROSMsg ()
00519 
00520   // Save as ASCII
00521   try
00522   {
00523     w.write<PointXYZI> ("test_pcl_io_binary.pcd", cloud, true);
00524   }
00525   catch (pcl::IOException &e)
00526   {
00527     std::cerr << e.detailedMessage () << std::endl;
00528   }
00529   res = loadPCDFile ("test_pcl_io_binary.pcd", cloud_blob);
00530   EXPECT_NE (int (res), -1);                               // test for loadPCDFile ()
00531   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);    // test for loadPCDFile ()
00532   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);  // test for loadPCDFile ()
00533   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);
00534   EXPECT_EQ (size_t (cloud_blob.data.size () * 2),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00535               cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for loadPCDFile ()
00536 
00537   // Convert from blob to data type
00538   fromROSMsg (cloud_blob, cloud);
00539 
00540   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00541   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00542   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00543   EXPECT_EQ (size_t (cloud.points.size ()), nr_p);         // test for fromROSMsg ()
00544 
00545   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00546   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00547   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00548   EXPECT_FLOAT_EQ (cloud.points[0].intensity, first.intensity);  // test for fromROSMsg ()
00549 
00550   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].x, last.x);    // test for fromROSMsg ()
00551   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].y, last.y);    // test for fromROSMsg ()
00552   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].z, last.z);    // test for fromROSMsg ()
00553   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].intensity, last.intensity); // test for fromROSMsg ()
00554 
00555   // Save as ASCII
00556   try
00557   {
00558     w.write<PointXYZI> ("test_pcl_io_ascii.pcd", cloud, false);
00559   }
00560   catch (pcl::IOException &e)
00561   {
00562     std::cerr << e.detailedMessage () << std::endl;
00563   }
00564   res = loadPCDFile ("test_pcl_io_ascii.pcd", cloud_blob);
00565   EXPECT_NE (int (res), -1);                               // test for loadPCDFile ()
00566   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);    // test for loadPCDFile ()
00567   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);  // test for loadPCDFile ()
00568   EXPECT_EQ (bool (cloud_blob.is_dense), true);
00569   EXPECT_EQ (size_t (cloud_blob.data.size () * 2),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00570               cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for loadPCDFile ()
00571 
00572   // Convert from blob to data type
00573   fromROSMsg (cloud_blob, cloud);
00574 
00575   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00576   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00577   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00578   EXPECT_EQ (size_t (cloud.points.size ()), nr_p);         // test for fromROSMsg ()
00579 
00580   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00581   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00582   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00583   EXPECT_FLOAT_EQ (cloud.points[0].intensity, first.intensity);  // test for fromROSMsg ()
00584 
00585   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].x, last.x);    // test for fromROSMsg ()
00586   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].y, last.y);    // test for fromROSMsg ()
00587   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].z, last.z);    // test for fromROSMsg ()
00588   EXPECT_FLOAT_EQ (float (cloud.points[nr_p - 1].intensity), float (last.intensity)); // test for fromROSMsg ()
00589 
00590   std::vector<int> indices (cloud.width * cloud.height / 2);
00591   for (int i = 0; i < static_cast<int> (indices.size ()); ++i) indices[i] = i;
00592   // Save as ASCII
00593   try
00594   {
00595     w.write<PointXYZI> ("test_pcl_io_binary.pcd", cloud, indices, true);
00596   }
00597   catch (pcl::IOException &e)
00598   {
00599     std::cerr << e.detailedMessage () << std::endl;
00600   }
00601   res = loadPCDFile ("test_pcl_io_binary.pcd", cloud_blob);
00602   EXPECT_NE (int (res), -1);                               // test for loadPCDFile ()
00603   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width * cloud.height / 2);    // test for loadPCDFile ()
00604   EXPECT_EQ (uint32_t (cloud_blob.height), 1);  // test for loadPCDFile ()
00605   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);
00606   EXPECT_EQ (size_t (cloud_blob.data.size () * 2),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00607               cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for loadPCDFile ()
00608 
00609   // Convert from blob to data type
00610   fromROSMsg (cloud_blob, cloud);
00611 
00612   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00613   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00614   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00615   EXPECT_EQ (size_t (cloud.points.size ()), nr_p / 2);         // test for fromROSMsg ()
00616 
00617   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00618   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00619   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00620   EXPECT_FLOAT_EQ (float (cloud.points[0].intensity), float (first.intensity));  // test for fromROSMsg ()
00621 
00622   indices.resize (cloud.width * cloud.height / 2);
00623   for (int i = 0; i < static_cast<int> (indices.size ()); ++i) indices[i] = i;
00624   // Save as ASCII
00625   try
00626   {
00627     w.write<PointXYZI> ("test_pcl_io_ascii.pcd", cloud, indices, false);
00628   }
00629   catch (pcl::IOException &e)
00630   {
00631     std::cerr << e.detailedMessage () << std::endl;
00632   }
00633   res = loadPCDFile ("test_pcl_io_ascii.pcd", cloud_blob);
00634   EXPECT_NE (int (res), -1);                               // test for loadPCDFile ()
00635   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width * cloud.height / 2);    // test for loadPCDFile ()
00636   EXPECT_EQ (uint32_t (cloud_blob.height), 1);  // test for loadPCDFile ()
00637   EXPECT_EQ (bool (cloud_blob.is_dense), true);
00638   EXPECT_EQ (size_t (cloud_blob.data.size () * 2),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00639               cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for loadPCDFile ()
00640 
00641   // Convert from blob to data type
00642   fromROSMsg (cloud_blob, cloud);
00643 
00644   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00645   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00646   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00647   EXPECT_EQ (size_t (cloud.points.size ()), nr_p / 4);         // test for fromROSMsg ()
00648 
00649   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00650   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00651   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00652   EXPECT_FLOAT_EQ (cloud.points[0].intensity, first.intensity);  // test for fromROSMsg ()
00653 }
00654 
00656 TEST (PCL, PCDReaderWriter)
00657 {
00658   sensor_msgs::PointCloud2 cloud_blob;
00659   PointCloud<PointXYZI> cloud;
00660 
00661   cloud.width  = 640;
00662   cloud.height = 480;
00663   cloud.points.resize (cloud.width * cloud.height);
00664   cloud.is_dense = true;
00665 
00666   srand (static_cast<unsigned int> (time (NULL)));
00667   size_t nr_p = cloud.points.size ();
00668   // Randomly create a new point cloud
00669   for (size_t i = 0; i < nr_p; ++i)
00670   {
00671     cloud.points[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00672     cloud.points[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00673     cloud.points[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00674     cloud.points[i].intensity = static_cast<float> (i);
00675   }
00676   PointXYZI first, last;
00677   first.x = cloud.points[0].x;       first.y = cloud.points[0].y;       first.z = cloud.points[0].z;       first.intensity = cloud.points[0].intensity;
00678   last.x = cloud.points[nr_p - 1].x; last.y = cloud.points[nr_p - 1].y; last.z = cloud.points[nr_p - 1].z; last.intensity  = cloud.points[nr_p - 1].intensity;
00679 
00680   // Convert from data type to blob
00681   toROSMsg (cloud, cloud_blob);
00682 
00683   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);    // test for toROSMsg ()
00684   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);  // test for toROSMsg ()
00685   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);  // test for toROSMsg ()
00686   //EXPECT_EQ ((size_t)cloud_blob.data.size () * 2,         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00687   //           cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for toROSMsg ()
00688   EXPECT_EQ (size_t (cloud_blob.data.size ()),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00689              cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for toROSMsg ()
00690 
00691   PCDWriter writer;
00692   writer.write ("test_pcl_io.pcd", cloud_blob, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), true);
00693 
00694   PCDReader reader;
00695   reader.read ("test_pcl_io.pcd", cloud_blob);
00696   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);
00697   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);
00698   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);
00699   //EXPECT_EQ ((size_t)cloud_blob.data.size () * 2,         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00700   //           cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for loadPCDFile ()
00701   EXPECT_EQ (size_t (cloud_blob.data.size ()),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00702              cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  // test for loadPCDFile ()
00703 
00704   // Convert from blob to data type
00705   fromROSMsg (cloud_blob, cloud);
00706 
00707   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00708   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00709   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00710   EXPECT_EQ (size_t (cloud.points.size ()), nr_p);         // test for fromROSMsg ()
00711 
00712   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00713   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00714   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00715   EXPECT_FLOAT_EQ (cloud.points[0].intensity, first.intensity);  // test for fromROSMsg ()
00716 
00717   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].x, last.x);    // test for fromROSMsg ()
00718   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].y, last.y);    // test for fromROSMsg ()
00719   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].z, last.z);    // test for fromROSMsg ()
00720   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].intensity, last.intensity); // test for fromROSMsg ()
00721 }
00722 
00724 TEST (PCL, PCDReaderWriterEigen)
00725 {
00726   sensor_msgs::PointCloud2 cloud_blob;
00727   PointCloud<PointXYZI> cloud;
00728   pcl::PointCloud<Eigen::MatrixXf> cloud_eigen1, cloud_eigen2, cloud_eigen3;
00729 
00730   cloud.width  = 640;
00731   cloud.height = 480;
00732   cloud.points.resize (cloud.width * cloud.height);
00733   cloud.is_dense = true;
00734 
00735   srand (static_cast<unsigned int> (time (NULL)));
00736   size_t nr_p = cloud.points.size ();
00737   // Randomly create a new point cloud
00738   for (size_t i = 0; i < nr_p; ++i)
00739   {
00740     cloud.points[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00741     cloud.points[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00742     cloud.points[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00743     cloud.points[i].intensity = static_cast<float> (i);
00744   }
00745   PointXYZI first, last;
00746   first.x = cloud.points[0].x;       first.y = cloud.points[0].y;       first.z = cloud.points[0].z;       first.intensity = cloud.points[0].intensity;
00747   last.x = cloud.points[nr_p - 1].x; last.y = cloud.points[nr_p - 1].y; last.z = cloud.points[nr_p - 1].z; last.intensity  = cloud.points[nr_p - 1].intensity;
00748 
00749   // Convert from data type to blob
00750   toROSMsg (cloud, cloud_blob);
00751 
00752   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);    // test for toROSMsg ()
00753   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);  // test for toROSMsg ()
00754   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);  // test for toROSMsg ()
00755   EXPECT_EQ (size_t (cloud_blob.data.size ()),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00756              cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));
00757 
00758   // Convert to Eigen
00759   cloud_eigen1 = cloud;
00760 
00761   PCDWriter writer;
00762   writer.writeBinaryCompressedEigen ("test_pcl_io.pcd", cloud_eigen1);
00763 
00764   PCDReader reader;
00765   reader.read ("test_pcl_io.pcd", cloud_blob);
00766 
00767   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);
00768   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);
00769   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);
00770   EXPECT_EQ (size_t (cloud_blob.data.size ()),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00771              cloud_blob.width * cloud_blob.height * sizeof (PointXYZI) / 2);  // we already got rid of the padding here
00772 
00773   // Convert from blob to data type
00774   fromROSMsg (cloud_blob, cloud);
00775 
00776   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00777   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00778   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00779   EXPECT_EQ (size_t (cloud.points.size ()), nr_p);         // test for fromROSMsg ()
00780 
00781   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00782   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00783   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00784   EXPECT_FLOAT_EQ (cloud.points[0].intensity, first.intensity);  // test for fromROSMsg ()
00785 
00786   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].x, last.x);    // test for fromROSMsg ()
00787   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].y, last.y);    // test for fromROSMsg ()
00788   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].z, last.z);    // test for fromROSMsg ()
00789   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].intensity, last.intensity); // test for fromROSMsg ()
00790 
00791   cloud_eigen2 = cloud;
00792   writer.writeBinaryEigen ("test_pcl_io.pcd", cloud_eigen2);
00793 
00794   reader.read ("test_pcl_io.pcd", cloud_blob);
00795 
00796   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);
00797   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);
00798   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);
00799   EXPECT_EQ (size_t (cloud_blob.data.size ()),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00800              cloud_blob.width * cloud_blob.height * sizeof (PointXYZI) / 2);  // we already got rid of the padding here
00801 
00802   // Convert from blob to data type
00803   fromROSMsg (cloud_blob, cloud);
00804 
00805   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00806   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00807   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00808   EXPECT_EQ (size_t (cloud.points.size ()), nr_p);         // test for fromROSMsg ()
00809 
00810   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00811   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00812   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00813   EXPECT_FLOAT_EQ (cloud.points[0].intensity, first.intensity);  // test for fromROSMsg ()
00814 
00815   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].x, last.x);    // test for fromROSMsg ()
00816   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].y, last.y);    // test for fromROSMsg ()
00817   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].z, last.z);    // test for fromROSMsg ()
00818   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].intensity, last.intensity); // test for fromROSMsg ()
00819 
00820   cloud_eigen3 = cloud;
00821   writer.writeASCIIEigen ("test_pcl_io.pcd", cloud_eigen3);
00822 
00823   reader.read ("test_pcl_io.pcd", cloud_blob);
00824 
00825   EXPECT_EQ (uint32_t (cloud_blob.width), cloud.width);
00826   EXPECT_EQ (uint32_t (cloud_blob.height), cloud.height);
00827   EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);
00828   EXPECT_EQ (size_t (cloud_blob.data.size ()),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00829              cloud_blob.width * cloud_blob.height * sizeof (PointXYZI) / 2);  // we already got rid of the padding here
00830 
00831   // Convert from blob to data type
00832   fromROSMsg (cloud_blob, cloud);
00833 
00834   EXPECT_EQ (uint32_t (cloud.width), cloud_blob.width);    // test for fromROSMsg ()
00835   EXPECT_EQ (uint32_t (cloud.height), cloud_blob.height);  // test for fromROSMsg ()
00836   EXPECT_EQ (int (cloud.is_dense), cloud_blob.is_dense);   // test for fromROSMsg ()
00837   EXPECT_EQ (size_t (cloud.points.size ()), nr_p);         // test for fromROSMsg ()
00838 
00839   EXPECT_FLOAT_EQ (cloud.points[0].x, first.x);     // test for fromROSMsg ()
00840   EXPECT_FLOAT_EQ (cloud.points[0].y, first.y);     // test for fromROSMsg ()
00841   EXPECT_FLOAT_EQ (cloud.points[0].z, first.z);     // test for fromROSMsg ()
00842   EXPECT_FLOAT_EQ (cloud.points[0].intensity, first.intensity);  // test for fromROSMsg ()
00843 
00844   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].x, last.x);    // test for fromROSMsg ()
00845   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].y, last.y);    // test for fromROSMsg ()
00846   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].z, last.z);    // test for fromROSMsg ()
00847   EXPECT_FLOAT_EQ (cloud.points[nr_p - 1].intensity, last.intensity); // test for fromROSMsg ()
00848 
00849   pcl::PointCloud<Eigen::MatrixXf> cloud_eigen4, cloud_eigen5, cloud_eigen6;
00850   reader.readEigen ("test_pcl_io.pcd", cloud_eigen4);
00851 
00852   EXPECT_EQ (uint32_t (cloud_eigen4.width), cloud.width);
00853   EXPECT_EQ (uint32_t (cloud_eigen4.height), cloud.height);
00854   EXPECT_EQ (bool (cloud_eigen4.is_dense), cloud.is_dense);
00855 
00856   for (int i = 0; i < cloud_eigen4.points.rows (); ++i)
00857   {
00858     EXPECT_FLOAT_EQ (cloud_eigen4.points (i, 0), cloud.points[i].x);
00859     EXPECT_FLOAT_EQ (cloud_eigen4.points (i, 1), cloud.points[i].y);
00860     EXPECT_FLOAT_EQ (cloud_eigen4.points (i, 2), cloud.points[i].z);
00861     EXPECT_FLOAT_EQ (cloud_eigen4.points (i, 3), cloud.points[i].intensity);
00862   }
00863 
00864   writer.writeBinaryEigen ("test_pcl_io.pcd", cloud_eigen1);
00865 
00866   reader.readEigen ("test_pcl_io.pcd", cloud_eigen5);
00867 
00868   EXPECT_EQ (uint32_t (cloud_eigen5.width), cloud.width);
00869   EXPECT_EQ (uint32_t (cloud_eigen5.height), cloud.height);
00870   EXPECT_EQ (bool (cloud_eigen5.is_dense), cloud.is_dense);
00871 
00872   for (int i = 0; i < cloud_eigen5.points.rows (); ++i)
00873   {
00874     EXPECT_FLOAT_EQ (cloud_eigen5.points (i, 0), cloud.points[i].x);
00875     EXPECT_FLOAT_EQ (cloud_eigen5.points (i, 1), cloud.points[i].y);
00876     EXPECT_FLOAT_EQ (cloud_eigen5.points (i, 2), cloud.points[i].z);
00877     EXPECT_FLOAT_EQ (cloud_eigen5.points (i, 3), cloud.points[i].intensity);
00878   }
00879 }
00880 
00882 TEST (PCL, PLYReaderWriter)
00883 {
00884   sensor_msgs::PointCloud2 cloud_blob, cloud_blob2;
00885   PointCloud<PointXYZI> cloud, cloud2;
00886 
00887   cloud.width  = 640;
00888   cloud.height = 480;
00889   cloud.resize (cloud.width * cloud.height);
00890   cloud.is_dense = true;
00891 
00892   srand (static_cast<unsigned int> (time (NULL)));
00893   size_t nr_p = cloud.size ();
00894   // Randomly create a new point cloud
00895   for (size_t i = 0; i < nr_p; ++i)
00896   {
00897     cloud[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00898     cloud[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00899     cloud[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
00900     cloud[i].intensity = static_cast<float> (i);
00901   }
00902 
00903   // Convert from data type to blob
00904   toROSMsg (cloud, cloud_blob);
00905 
00906   EXPECT_EQ (cloud_blob.width, cloud.width);    // test for toROSMsg ()
00907   EXPECT_EQ (cloud_blob.height, cloud.height);  // test for toROSMsg ()
00908   EXPECT_EQ (cloud_blob.is_dense, cloud.is_dense);  // test for toROSMsg ()
00909   EXPECT_EQ (cloud_blob.data.size (), 
00910              cloud_blob.width * cloud_blob.height * sizeof (PointXYZI));  
00911 
00912   // test for toROSMsg ()
00913   PLYWriter writer;
00914   writer.write ("test_pcl_io.ply", cloud_blob, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), true, true);
00915 
00916   PLYReader reader;
00917   reader.read ("test_pcl_io.ply", cloud_blob2);
00918   //PLY DOES preserve organiziation
00919   EXPECT_EQ (cloud_blob.width * cloud_blob.height, cloud_blob2.width * cloud_blob2.height);
00920   EXPECT_EQ (cloud_blob.is_dense, cloud.is_dense);   
00921   EXPECT_EQ (size_t (cloud_blob2.data.size ()),         // PointXYZI is 16*2 (XYZ+1, Intensity+3)
00922              cloud_blob2.width * cloud_blob2.height * sizeof (PointXYZ));  // test for loadPLYFile ()
00923 
00924   // Convert from blob to data type
00925   fromROSMsg (cloud_blob2, cloud2);
00926 
00927   // EXPECT_EQ (cloud.width, cloud2.width);    // test for fromROSMsg ()
00928   // EXPECT_EQ (cloud.height, cloud2.height);  // test for fromROSMsg ()
00929   // EXPECT_EQ (cloud.is_dense, cloud2.is_dense);   // test for fromROSMsg ()
00930   EXPECT_EQ (cloud.size (), cloud2.size ());         // test for fromROSMsg ()
00931 
00932   for (uint32_t counter = 0; counter < cloud.size (); ++counter)
00933   {
00934     EXPECT_FLOAT_EQ (cloud[counter].x, cloud2[counter].x);     // test for fromROSMsg ()
00935     EXPECT_FLOAT_EQ (cloud[counter].y, cloud2[counter].y);     // test for fromROSMsg ()
00936     EXPECT_FLOAT_EQ (cloud[counter].z, cloud2[counter].z);     // test for fromROSMsg ()
00937     EXPECT_FLOAT_EQ (cloud[counter].intensity, cloud2[counter].intensity);  // test for fromROSMsg ()
00938   }
00939 }
00940 
00942 
00943 struct PointXYZFPFH33
00944 {
00945   float x, y, z;
00946   float histogram[33];
00947 };
00948 POINT_CLOUD_REGISTER_POINT_STRUCT (PointXYZFPFH33,
00949     (float, x, x) (float, y, y) (float, z, z)
00950     (float[33], histogram, fpfh))
00951 
00952 inline std::ostream& operator << (std::ostream& os, const PointXYZFPFH33& p)
00953 {
00954   os << "(" << p.x << "," << p.y << "," << p.z << ")";
00955   for (int i = 0; i < 33; ++i) 
00956     os << (i == 0 ? "(" :"") << p.histogram[i] << (i < 32 ? ", " : ")");
00957   return os;
00958 }
00959 
00961 TEST (PCL, ExtendedIO)
00962 {
00963   PointCloud<PointXYZFPFH33> cloud;
00964   cloud.width = 2; cloud.height = 1;
00965   cloud.points.resize (2);
00966 
00967   cloud.points[0].x = cloud.points[0].y = cloud.points[0].z = 1;
00968   cloud.points[1].x = cloud.points[1].y = cloud.points[1].z = 2;
00969   for (int i = 0; i < 33; ++i)
00970   {
00971     cloud.points[0].histogram[i] = static_cast<float> (i);
00972     cloud.points[1].histogram[i] = 33.0f - static_cast<float> (i);
00973   }
00974 
00975   savePCDFile ("v.pcd", cloud);
00976   cloud.points.clear ();
00977   loadPCDFile ("v.pcd", cloud);
00978 
00979   EXPECT_EQ (int (cloud.width), 2);
00980   EXPECT_EQ (int (cloud.height), 1);
00981   EXPECT_EQ (cloud.is_dense, true);
00982   EXPECT_EQ (int (cloud.points.size ()), 2);
00983   
00984   EXPECT_EQ (cloud.points[0].x, 1); EXPECT_EQ (cloud.points[0].y, 1); EXPECT_EQ (cloud.points[0].z, 1);
00985   EXPECT_EQ (cloud.points[1].x, 2); EXPECT_EQ (cloud.points[1].y, 2); EXPECT_EQ (cloud.points[1].z, 2);
00986   for (int i = 0; i < 33; ++i)
00987   {
00988     ASSERT_EQ (cloud.points[0].histogram[i], i);
00989     ASSERT_EQ (cloud.points[1].histogram[i], 33-i);
00990   }
00991 }
00992 
00994 TEST (PCL, EigenConversions)
00995 {
00996   PointCloud<PointXYZ> cloud;
00997   cloud.points.resize (5);
00998 
00999   for (int i = 0; i < int (cloud.points.size ()); ++i)
01000     cloud.points[i].x = cloud.points[i].y = cloud.points[i].z = static_cast<float> (i);
01001 
01002   sensor_msgs::PointCloud2 blob;
01003   toROSMsg (cloud, blob);
01004 
01005   Eigen::MatrixXf mat;
01006   getPointCloudAsEigen (blob, mat);
01007   EXPECT_EQ (mat.cols (), int (cloud.points.size ()));
01008   EXPECT_EQ (mat.rows (), 4);
01009   
01010   for (size_t i = 0; i < cloud.points.size (); ++i)
01011   {
01012     EXPECT_EQ (mat (0, i), cloud.points[i].x);
01013     EXPECT_EQ (mat (1, i), cloud.points[i].y);
01014     EXPECT_EQ (mat (2, i), cloud.points[i].z);
01015     EXPECT_EQ (mat (3, i), 1);
01016   }
01017   
01018   getEigenAsPointCloud (mat, blob);
01019   fromROSMsg (blob, cloud);
01020   for (size_t i = 0; i < cloud.points.size (); ++i)
01021   {
01022     EXPECT_EQ (cloud.points[i].x, i);
01023     EXPECT_EQ (cloud.points[i].y, i);
01024     EXPECT_EQ (cloud.points[i].z, i);
01025   }
01026 
01027   getPointCloudAsEigen (blob, mat);
01028   EXPECT_EQ (mat.cols (), int (cloud.points.size ()));
01029   EXPECT_EQ (mat.rows (), 4);
01030   
01031   for (size_t i = 0; i < cloud.points.size (); ++i)
01032   {
01033     EXPECT_EQ (mat (0, i), cloud.points[i].x);
01034     EXPECT_EQ (mat (1, i), cloud.points[i].y);
01035     EXPECT_EQ (mat (2, i), cloud.points[i].z);
01036     EXPECT_EQ (mat (3, i), 1);
01037   }
01038 }
01039 
01041 TEST (PCL, CopyPointCloud)
01042 {
01043   pcl::PointCloud<pcl::PointXYZ> cloud_a;
01044   pcl::PointCloud<pcl::PointXYZRGBA> cloud_b;
01045 
01046   // Fill in the cloud data
01047   cloud_a.width  = cloud_b.width  = 3;
01048   cloud_a.height = cloud_b.height = 1;
01049   cloud_a.points.resize (cloud_a.width * cloud_a.height);
01050   cloud_b.points.resize (cloud_b.width * cloud_b.height);
01051 
01052   for (size_t i = 0; i < cloud_a.points.size (); ++i)
01053   {
01054     cloud_a.points[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01055     cloud_a.points[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01056     cloud_a.points[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01057     cloud_b.points[i].rgba = 255;
01058   }
01059 
01060   pcl::copyPointCloud<pcl::PointXYZ, pcl::PointXYZRGBA> (cloud_a, cloud_b);
01061 
01062   for (size_t i = 0; i < cloud_a.points.size (); ++i)
01063   {
01064     EXPECT_EQ (cloud_b.points[i].x, cloud_a.points[i].x);
01065     EXPECT_EQ (cloud_b.points[i].y, cloud_a.points[i].y);
01066     EXPECT_EQ (cloud_b.points[i].z, cloud_a.points[i].z);
01067     EXPECT_EQ (cloud_b.points[i].rgba, 255);
01068     cloud_a.points[i].x = cloud_a.points[i].y = cloud_a.points[i].z = 0;
01069   }
01070 
01071   pcl::copyPointCloud<pcl::PointXYZRGBA, pcl::PointXYZ> (cloud_b, cloud_a);
01072 
01073   for (size_t i = 0; i < cloud_a.points.size (); ++i)
01074   {
01075     EXPECT_EQ (cloud_b.points[i].x, cloud_a.points[i].x);
01076     EXPECT_EQ (cloud_b.points[i].y, cloud_a.points[i].y);
01077     EXPECT_EQ (cloud_b.points[i].z, cloud_a.points[i].z);
01078     EXPECT_EQ (cloud_b.points[i].rgba, 255);
01079   }
01080 }
01081 
01083 TEST (PCL, LZF)
01084 {
01085   PointCloud<PointXYZ> cloud, cloud2;
01086   cloud.width  = 640;
01087   cloud.height = 480;
01088   cloud.points.resize (cloud.width * cloud.height);
01089   cloud.is_dense = true;
01090 
01091   srand (static_cast<unsigned int> (time (NULL)));
01092   size_t nr_p = cloud.points.size ();
01093   // Randomly create a new point cloud
01094   for (size_t i = 0; i < nr_p; ++i)
01095   {
01096     cloud.points[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01097     cloud.points[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01098     cloud.points[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01099   }
01100   PCDWriter writer;
01101   int res = writer.writeBinaryCompressed<PointXYZ> ("test_pcl_io_compressed.pcd", cloud);
01102   EXPECT_EQ (res, 0);
01103 
01104   PCDReader reader;
01105   reader.read<PointXYZ> ("test_pcl_io_compressed.pcd", cloud2);
01106 
01107   EXPECT_EQ (cloud2.width, cloud.width);
01108   EXPECT_EQ (cloud2.height, cloud.height);
01109   EXPECT_EQ (cloud2.is_dense, cloud.is_dense);
01110   EXPECT_EQ (cloud2.points.size (), cloud.points.size ());
01111 
01112   for (size_t i = 0; i < cloud2.points.size (); ++i)
01113   {
01114     ASSERT_EQ (cloud2.points[i].x, cloud.points[i].x);
01115     ASSERT_EQ (cloud2.points[i].y, cloud.points[i].y);
01116     ASSERT_EQ (cloud2.points[i].z, cloud.points[i].z);
01117   }
01118 
01119   sensor_msgs::PointCloud2 blob;
01120   pcl::toROSMsg (cloud, blob);
01121   res = writer.writeBinaryCompressed ("test_pcl_io_compressed.pcd", blob);
01122   EXPECT_EQ (res, 0);
01123 
01124   reader.read<PointXYZ> ("test_pcl_io_compressed.pcd", cloud2);
01125 
01126   EXPECT_EQ (cloud2.width, blob.width);
01127   EXPECT_EQ (cloud2.height, blob.height);
01128   EXPECT_EQ (cloud2.is_dense, cloud.is_dense);
01129   EXPECT_EQ (cloud2.points.size (), cloud.points.size ());
01130 
01131   for (size_t i = 0; i < cloud2.points.size (); ++i)
01132   {
01133     EXPECT_EQ (cloud2.points[i].x, cloud.points[i].x);
01134     EXPECT_EQ (cloud2.points[i].y, cloud.points[i].y);
01135     EXPECT_EQ (cloud2.points[i].z, cloud.points[i].z);
01136   }
01137 }
01138 
01140 TEST (PCL, LZFExtended)
01141 {
01142   PointCloud<PointXYZRGBNormal> cloud, cloud2;
01143   cloud.width  = 640;
01144   cloud.height = 480;
01145   cloud.points.resize (cloud.width * cloud.height);
01146   cloud.is_dense = true;
01147 
01148   srand (static_cast<unsigned int> (time (NULL)));
01149   size_t nr_p = cloud.points.size ();
01150   // Randomly create a new point cloud
01151   for (size_t i = 0; i < nr_p; ++i)
01152   {
01153     cloud.points[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01154     cloud.points[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01155     cloud.points[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01156     cloud.points[i].normal_x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01157     cloud.points[i].normal_y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01158     cloud.points[i].normal_z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01159     cloud.points[i].rgb = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01160   }
01161 
01162   sensor_msgs::PointCloud2 blob;
01163   pcl::toROSMsg (cloud, blob);
01164 
01165   PCDWriter writer;
01166   int res = writer.writeBinaryCompressed ("test_pcl_io_compressed.pcd", blob);
01167   EXPECT_EQ (res, 0);
01168 
01169   PCDReader reader;
01170   reader.read<PointXYZRGBNormal> ("test_pcl_io_compressed.pcd", cloud2);
01171 
01172   EXPECT_EQ (cloud2.width, blob.width);
01173   EXPECT_EQ (cloud2.height, blob.height);
01174   EXPECT_EQ (cloud2.is_dense, cloud.is_dense);
01175   EXPECT_EQ (cloud2.points.size (), cloud.points.size ());
01176 
01177   for (size_t i = 0; i < cloud2.points.size (); ++i)
01178   {
01179     EXPECT_EQ (cloud2.points[i].x, cloud.points[i].x);
01180     EXPECT_EQ (cloud2.points[i].y, cloud.points[i].y);
01181     EXPECT_EQ (cloud2.points[i].z, cloud.points[i].z);
01182     EXPECT_EQ (cloud2.points[i].normal_x, cloud.points[i].normal_x);
01183     EXPECT_EQ (cloud2.points[i].normal_y, cloud.points[i].normal_y);
01184     EXPECT_EQ (cloud2.points[i].normal_z, cloud.points[i].normal_z);
01185     EXPECT_EQ (cloud2.points[i].rgb, cloud.points[i].rgb);
01186   }
01187 }
01188 
01190 TEST (PCL, Locale)
01191 {
01192 #ifndef __APPLE__
01193   try
01194   {
01195     PointCloud<PointXYZ> cloud, cloud2;
01196     cloud.width  = 640;
01197     cloud.height = 480;
01198     cloud.points.resize (cloud.width * cloud.height);
01199     cloud.is_dense = true;
01200 
01201     srand (static_cast<unsigned int> (time (NULL)));
01202     size_t nr_p = cloud.points.size ();
01203     // Randomly create a new point cloud
01204     cloud.points[0].x = std::numeric_limits<float>::quiet_NaN ();
01205     cloud.points[0].y = std::numeric_limits<float>::quiet_NaN ();
01206     cloud.points[0].z = std::numeric_limits<float>::quiet_NaN ();
01207   
01208     for (size_t i = 1; i < nr_p; ++i)
01209     {
01210       cloud.points[i].x = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01211       cloud.points[i].y = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01212       cloud.points[i].z = static_cast<float> (1024 * rand () / (RAND_MAX + 1.0));
01213     }
01214     PCDWriter writer;
01215     try
01216     {
01217 #ifdef _WIN32
01218       std::locale::global (std::locale ("German_germany"));
01219 #else
01220       std::locale::global (std::locale ("de_DE.UTF-8"));
01221 #endif
01222     }
01223     catch (std::runtime_error e)
01224     {
01225       PCL_WARN ("Failed to set locale, skipping test.\n");
01226     }
01227     int res = writer.writeASCII<PointXYZ> ("test_pcl_io_ascii.pcd", cloud);
01228     EXPECT_EQ (res, 0);
01229 
01230     PCDReader reader;
01231     try
01232     {
01233 #ifdef _WIN32
01234       std::locale::global (std::locale ("English_US"));
01235 #else
01236       std::locale::global (std::locale ("en_US.UTF-8"));
01237 #endif
01238     }
01239     catch (std::runtime_error e)
01240     {
01241       PCL_WARN ("Failed to set locale, skipping test.\n");
01242     }
01243     reader.read<PointXYZ> ("test_pcl_io_ascii.pcd", cloud2);
01244     std::locale::global (std::locale::classic ());
01245 
01246     EXPECT_EQ (cloud2.width, cloud.width);
01247     EXPECT_EQ (cloud2.height, cloud.height);
01248     EXPECT_EQ (cloud2.is_dense, false);
01249     EXPECT_EQ (cloud2.points.size (), cloud.points.size ());
01250   
01251     EXPECT_TRUE (pcl_isnan(cloud2.points[0].x));
01252     EXPECT_TRUE (pcl_isnan(cloud2.points[0].y));
01253     EXPECT_TRUE (pcl_isnan(cloud2.points[0].z));
01254     for (size_t i = 1; i < cloud2.points.size (); ++i)
01255     {
01256       ASSERT_FLOAT_EQ (cloud2.points[i].x, cloud.points[i].x);
01257       ASSERT_FLOAT_EQ (cloud2.points[i].y, cloud.points[i].y);
01258       ASSERT_FLOAT_EQ (cloud2.points[i].z, cloud.points[i].z);
01259     }
01260   }
01261   catch(std::exception& e)
01262   {
01263   }
01264 #endif
01265 }
01266 
01267 /* ---[ */
01268 int
01269   main (int argc, char** argv)
01270 {
01271   testing::InitGoogleTest (&argc, argv);
01272   return (RUN_ALL_TESTS ());
01273 }
01274 /* ]--- */


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