Skip to content

Instantly share code, notes, and snippets.

@keijiro
Created February 8, 2019 09:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keijiro/585e86987e3da420245d122d1d796799 to your computer and use it in GitHub Desktop.
Save keijiro/585e86987e3da420245d122d1d796799 to your computer and use it in GitHub Desktop.
A small utility that converts .asc point cloud file into binary-LE .ply file.
#include <deque>
#include <fstream>
#include <iostream>
#include <memory>
namespace
{
struct Point
{
float x, y, z;
unsigned char r, g, b;
};
void convert_asc_to_ply(std::istream& input, std::ostream& output)
{
std::deque<Point> points;
while (input.good())
{
float x, y, z, w;
int r, g, b;
input >> x >> y >> z >> r >> g >> b >> w;
points.push_back(Point{
.x = x, .y = y, .z = z,
.r = (unsigned char)r,
.g = (unsigned char)g,
.b = (unsigned char)b
});
}
output << "ply" << std::endl;
output << "format binary_little_endian 1.0" << std::endl;
output << "element vertex " << points.size() << std::endl;
output << "property float x" << std::endl;
output << "property float y" << std::endl;
output << "property float z" << std::endl;
output << "property uchar red" << std::endl;
output << "property uchar green" << std::endl;
output << "property uchar blue" << std::endl;
output << "end_header" << std::endl;
for (auto p : points)
{
output.write(reinterpret_cast<const char*>(&p.x), sizeof(float) * 3);
output.write(reinterpret_cast<const char*>(&p.r), sizeof(unsigned char) * 3);
}
}
}
int main(int argc, char* argv[])
{
std::unique_ptr<std::istream> ifs;
std::unique_ptr<std::ostream> ofs;
if (argc > 1)
ifs = std::make_unique<std::ifstream>(argv[1]);
if (argc > 2)
ofs = std::make_unique<std::ofstream>(argv[2]);
convert_asc_to_ply(ifs ? *ifs : std::cin, ofs ? *ofs : std::cout);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment