PCD file format is pretty basic, I didn’t bother trying to use the PCL SDK to save data:
void savePCD(const astra::DepthFrame& depthFrame, const std::string& filename)
{
std::unique_ptr<int16_t[]> depthData};
const int width = depthFrame.width();
const int height = depthFrame.height();
const int size = width * height;
const int byteLength = size * sizeof(uint16_t);
depthData = DepthPtr(new int16_t[byteLength]);
depthFrame.copy_to(&depthData[0]);
FILE* fp = fopen(filename.c_str(), "wt");
fprintf(fp, "VERSION .7\n");
fprintf(fp, "FIELDS x y z\n");
fprintf(fp, "SIZE 4 4 4\n");
fprintf(fp, "TYPE F F F\n");
fprintf(fp, "COUNT 1 1 1\n");
fprintf(fp, "WIDTH %d\n", width);
fprintf(fp, "HEIGHT %d\n", height);
fprintf(fp, "VIEWPOINT 0 0 0 1 0 0 0\n");
fprintf(fp, "POINTS %d\n", size);
fprintf(fp, "DATA ascii\n");
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
const size_t index = (width * y) + x;
const short point = depthData[index];
if (point == 0)
{
fprintf(fp, "%d %d nan\n", x, y);
}
else
{
fprintf(fp, "%d %d %d\n", x, y, point);
}
}
}
fclose(fp);
}