31 #include <pcl/io/pcd_io.h>
32 #include <pcl/point_types.h>
35 #define isnan(x) ((x) != (x))
44 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZRGBNormal> );
46 if ( pcl::io::loadPCDFile<pcl::PointXYZRGBNormal>(
filename, *cloud ) == -1)
48 std::cerr <<
"Couldn't read file “" <<
filename <<
"”." << std::endl;
53 bool has_normals =
false;
54 bool has_colors =
false;
60 for (
size_t i(0); i < cloud->points.size(); i++ )
62 if(!
isnan(cloud->points[i].x) && !
isnan(cloud->points[i].y) && !
isnan(cloud->points[i].z) )
64 points[i*3 + 0] = cloud->points[i].x;
65 points[i*3 + 1] = cloud->points[i].y;
66 points[i*3 + 2] = cloud->points[i].z;
70 points[i*3 + 0] = 0.0;
71 points[i*3 + 1] = 0.0;
72 points[i*3 + 2] = 0.0;
75 if(!
isnan(cloud->points[i].r) && !
isnan(cloud->points[i].g) && !
isnan(cloud->points[i].b) )
77 colors[i*3 + 0] = cloud->points[i].r;
78 colors[i*3 + 1] = cloud->points[i].g;
79 colors[i*3 + 2] = cloud->points[i].b;
89 if(!
isnan(cloud->points[i].normal_x) && !
isnan(cloud->points[i].normal_y) && !
isnan(cloud->points[i].normal_z) )
91 normals[i*3 + 0] = cloud->points[i].normal_x;
92 normals[i*3 + 1] = cloud->points[i].normal_y;
93 normals[i*3 + 2] = cloud->points[i].normal_z;
99 model->m_pointCloud->setPointArray( points, cloud->points.size() );
103 model->m_pointCloud->setColorArray(
colors, cloud->points.size() );
108 model->m_pointCloud->setNormalArray( normals, cloud->points.size() );
128 size_t pointcount(0), buf(0);
134 pointcount =
m_model->m_pointCloud->numPoints();
135 points =
m_model->m_pointCloud->getPointArray();
136 pointColors =
m_model->m_pointCloud->getUCharArray(
"colors", buf, w_color);
139 if ( pointcount != buf )
142 std::cerr <<
"Amount of points and color information is"
143 " not equal. Color information won't be written." << std::endl;
146 std::ofstream out(
filename.c_str() );
148 if ( !out.is_open() )
150 std::cerr <<
"Could not open file »" <<
filename <<
"«…"
155 out <<
"# .PCD v.7 - Point Cloud Data file format" << std::endl;
156 out <<
"FIELDS x y z" << ( pointColors ?
" rgb" :
"" ) << std::endl;
157 out <<
"SIZE 4 4 4" << ( pointColors ?
" 4" :
"" ) << std::endl;
158 out <<
"TYPE F F F" << ( pointColors ?
" F" :
"" ) << std::endl;
159 out <<
"WIDTH " << pointcount << std::endl;
160 out <<
"HEIGHT 1" << std::endl;
161 out <<
"POINTS " << pointcount << std::endl;
162 out <<
"DATA ascii" << std::endl;
165 for (
size_t i(0); i < pointcount; i++ )
168 out << points[i*3 + 0] <<
" " << points[i*3 + 1] <<
" " << points[i*3 + 2];
175 uint8_t* rgb = (uint8_t*)
reinterpret_cast<uint8_t*
>( &rgbf );
176 rgb[2] = pointColors[i*w_color + 0];
177 rgb[1] = pointColors[i*w_color + 1];
178 rgb[0] = pointColors[i*w_color + 2];