#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int num = 3;
int lengthPointCloud = 3 * sizeof(float) + 3 * sizeof(unsigned char);
//头内容Ascii码格式
stringstream header_binary;
header_binary << "ply" << endl
<< "format binary_little_endian 1.0" << endl
<< "comment PCL generated" << endl
<< "element vertex " << num << endl
<< "property float x" << endl
<< "property float y" << endl
<< "property float z" << endl
<< "property uchar red" << endl
<< "property uchar green" << endl
<< "property uchar blue" << endl
<< "end_header" << endl;
//点云数据Binary格式
char *pointCloud_binary = new char[num * lengthPointCloud];
float *X = new float[num] {0};
float *Y = new float[num] {0};
float *Z = new float[num] {0};
unsigned char *R = new unsigned char[num] {0};
unsigned char *G = new unsigned char[num] {0};
unsigned char *B = new unsigned char[num] {0};
X[0] = -11.122746;
X[1] = -2.725141;
X[2] = 12.04;
Y[0] = 10.255;
Y[1] = 7.017;
Y[2] = -12.04;
Z[0] = -19;
Z[1] = -13;
Z[2] = -18;
for (int i = 0; i < num; i++) {
R[i] = 255;
G[i] = 0;
B[i] = 0;
}
for (int i = 0; i < num; i++) {
memcpy(pointCloud_binary + i * 15, &X[i], sizeof(float));
memcpy(pointCloud_binary + i * 15 + 4, &Y[i], sizeof(float));
memcpy(pointCloud_binary + i * 15 + 8, &Z[i], sizeof(float));
memcpy(pointCloud_binary + i * 15 + 12, &R[i], sizeof(unsigned char));
memcpy(pointCloud_binary + i * 15 + 13, &G[i], sizeof(unsigned char));
memcpy(pointCloud_binary + i * 15 + 14, &B[i], sizeof(unsigned char));
}
string pointCloud;
pointCloud = header_binary.str();
string pointCloudToString(num * lengthPointCloud, ' ');
for (int i = 0; i < num * lengthPointCloud; i++)
pointCloudToString[i] = pointCloud_binary[i];
pointCloud += pointCloudToString;
return 0;
}
C++char*转string,同时输出Ascii码类型和Binary类型
最新推荐文章于 2024-04-16 00:12:25 发布