41 #include <boost/filesystem.hpp> 53 const int &xPos,
const int& yPos,
const int& zPos,
54 const int &rPos,
const int& gPos,
const int& bPos,
const int &iPos)
57 boost::filesystem::path selectedFile(filename);
58 string extension(selectedFile.extension().string());
60 if ( extension !=
".pts" && extension !=
".3d" && extension !=
".xyz" && extension !=
".txt" )
62 cout <<
"»" << extension <<
"« is not a valid file extension." << endl;
68 if ( lines_in_file < 2 )
70 cout <<
timestamp <<
"AsciiIO: Too few lines in file (has to be > 2)." << endl;
76 in.open(filename.c_str());
80 in.getline(buffer, 2048);
81 in.getline(buffer, 2048);
88 in.open(filename.c_str());
91 in.getline(buffer, 2048);
101 numPoints = lines_in_file - 1;
102 points =
floatArr(
new float[ numPoints * 3 ] );
107 if(rPos > num_columns || gPos > num_columns || bPos > num_columns || iPos > num_columns)
109 cout <<
timestamp <<
"Error: At least one attribute index is larger than the number of columns" << endl;
115 bool has_color = (rPos > -1 && gPos > -1 && bPos > -1);
116 bool has_intensity = (iPos > -1);
121 pointColors =
ucharArr(
new uint8_t[ numPoints * 3 ] );
126 pointIntensities =
floatArr(
new float[ numPoints ] );
131 while (in.good() && c < numPoints)
133 float x, y, z, intensity, dummy;
134 unsigned int r, g, b;
136 for(
int i = 0; i < num_columns; i++)
176 pointColors[ c * 3 ] = (
unsigned char) r;
177 pointColors[ c * 3 + 1 ] = (
unsigned char) g;
178 pointColors[ c * 3 + 2 ] = (
unsigned char) b;
184 pointIntensities[c] = intensity;
189 points[ c * 3 + 1 ] = y;
190 points[ c * 3 + 2 ] = z;
197 cout <<
timestamp <<
"Warning: Point count / line count mismatch: " 198 << numPoints <<
" / " << c << endl;
202 size_t numColors = 0;
203 size_t numIntensities = 0;
207 numColors = numPoints;
208 model->m_pointCloud->setColorArray(pointColors, numColors);
214 numIntensities = numPoints;
215 model->m_pointCloud->addFloatChannel(pointIntensities,
"intensities", numIntensities, 1);
220 model->m_pointCloud->setPointArray(points, numPoints);
230 boost::filesystem::path selectedFile(filename);
231 string extension(selectedFile.extension().string());
233 if ( extension !=
".pts" && extension !=
".3d" && extension !=
".xyz" && extension !=
".txt" )
235 cout <<
"»" << extension <<
"« is not a valid file extension." << endl;
241 if ( lines_in_file < 2 )
243 cout <<
timestamp <<
"AsciiIO: Too few lines in file (has to be > 2)." << endl;
256 in.open(filename.c_str());
260 in.getline(buffer, 2048);
261 in.getline(buffer, 2048);
265 bool has_color = (num_attributes == 3) || (num_attributes == 4);
266 bool has_intensity = (num_attributes == 1) || (num_attributes == 4);
268 if(has_color || has_intensity)
270 cout <<
timestamp <<
"Autodetected the following attributes" << endl;
271 cout <<
timestamp <<
"Color: " << has_color << endl;
272 cout <<
timestamp <<
"Intensity: " << has_intensity << endl;
274 if(has_color && has_intensity)
276 return read(filename, 0, 1, 2, 4, 5, 6, 3);
280 return read(filename, 0, 1, 2, 3, 4, 5);
282 else if(has_intensity)
284 if(num_attributes == 1)
286 return read(filename, 0, 1, 2, -1, -1, -1, 3);
290 return read(filename, 0, 1, 2, -1, -1, -1, 6);
294 return read(filename, 0, 1, 2);
303 if ( !this->
m_model->m_pointCloud ) {
304 std::cerr <<
"No point buffer available for output." << std::endl;
308 size_t pointcount( 0 ), buf ( 0 );
315 pointcount = this->
m_model->m_pointCloud->numPoints();
316 points = this->
m_model->m_pointCloud->getPointArray();
322 auto colors = this->
m_model->m_pointCloud->getChannel<
unsigned char>(
"colors");
325 pointColors = (*colors).dataPtr();
326 buf = (*colors).numElements();
328 if ( pointcount != buf )
331 std::cerr <<
"Amount of points and color information is" 332 " not equal. Color information won't be written" << std::endl;
336 auto intensity = this->
m_model->m_pointCloud->getChannel<
float>(
"intensities");
339 pointIntensities = (*intensity).dataPtr();
340 buf = (*intensity).numElements();
344 if ( pointcount != buf )
346 pointIntensities.reset();
347 std::cerr <<
"Amount of points and intensity values are" 348 " not equal. Intensity information will not be written." << std::endl;
353 std::ofstream out( filename.c_str() );
355 if ( !out.is_open() ) {
356 std::cerr <<
"Could not open file »" << filename
357 <<
"« for output." << std::endl;
361 for (
size_t i(0); i < pointcount; i++ )
363 out << points[i * 3] <<
" " 364 << points[i * 3 + 1] <<
" " 365 << points[i * 3 + 2];
366 if ( pointIntensities )
368 out <<
" " << pointIntensities[i];
374 out <<
" " << (
unsigned int) pointColors[i * 3]
375 <<
" " << (
unsigned int) pointColors[i * 3 + 1]
376 <<
" " << (
unsigned int) pointColors[i * 3 + 2];
389 ifstream in(filename.c_str());
396 in.getline(line, 1024);
407 ifstream in(filename.c_str());
411 in.getline(line, 1024);
414 in.getline(line, 1024);
420 char* pch = strtok(line,
" ");
423 pch = strtok(
NULL,
" ");
A class to handle point information with an arbitrarily large number of attribute channels...
static int getEntriesInLine(string filename)
Helper method. Returns the number of columns in the given file.
static Timestamp timestamp
A global time stamp object for program runtime measurement.
boost::shared_array< unsigned char > ucharArr
std::shared_ptr< PointBuffer > PointBufferPtr
Read and write pointclouds from .pts and .3d files.
static size_t countLines(string filename)
TODO: Coordinate mapping for ascii files.
boost::shared_array< float > floatArr
std::shared_ptr< Model > ModelPtr
virtual ModelPtr read(string filename)
Reads the given file and stores point and color information in the given parameters.
virtual void save(string filename)