From bfadafe0a9d1f0d8c5a8d70f2001d5b2ac30f2e3 Mon Sep 17 00:00:00 2001 From: Thibault Payet <monwarez@gmail.com> Date: Wed, 6 May 2020 14:51:45 +0200 Subject: [PATCH] add support to convert: x y z and x y z color --- main.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/main.cpp b/main.cpp index 1f3e1da..ccf9284 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,10 @@ #include <filesystem> #include <fstream> +#include <ios> #include <iostream> +#include <string> + +#include <pcl/io/ascii_io.h> #include <pcl/io/pcd_io.h> void txt2pcd( std::filesystem::path input, std::filesystem::path output ) @@ -11,28 +15,71 @@ void txt2pcd( std::filesystem::path input, std::filesystem::path output ) { pcl::PointCloud<pcl::PointXYZRGB> cloud; - while ( !inputFile.eof() ) + // Read header + std::string line; + auto len = inputFile.tellg(); + + char buf{'\0'}; + + do + { + inputFile.get( buf ); + if ( buf != '\0' && buf != '\n' ) + { + line.push_back( buf ); + } + } while ( buf != '\0' && buf != '\n' ); + + auto numberOfSpace = std::count( std::begin( line ), std::end( line ), ' ' ); + + if ( line.back() == ' ' ) + { + if ( numberOfSpace > 0 ) + { + --numberOfSpace; + } + } + + auto const numArgument = numberOfSpace + 1; + + inputFile.seekg( len, std::ios_base::beg ); + + pcl::PCDWriter pcdWriter{}; + if ( numArgument == 6 ) { - pcl::PointXYZRGB point; + while ( !inputFile.eof() ) + { + pcl::PointXYZRGB point; - inputFile >> point.x; - inputFile >> point.y; - inputFile >> point.z; + inputFile >> point.x; + inputFile >> point.y; + inputFile >> point.z; - int r, g, b; + int r, g, b; - inputFile >> r; - inputFile >> g; - inputFile >> b; + inputFile >> r; + inputFile >> g; + inputFile >> b; - point.r = static_cast<char>( r ); - point.g = static_cast<char>( g ); - point.b = static_cast<char>( b ); + point.r = static_cast<char>( r ); + point.g = static_cast<char>( g ); + point.b = static_cast<char>( b ); - cloud.push_back( point ); + cloud.push_back( point ); + } } + else + { + inputFile.close(); + pcl::ASCIIReader reader; - pcl::PCDWriter pcdWriter{}; + auto ext = input.extension().string(); + + reader.setExtension( ext ); + + + reader.read( input.string(), cloud ); + } pcdWriter.writeBinaryCompressed( output.string(), cloud ); } -- GitLab