我正在处理点云,即点的向量,作为计算的结果,其包含重复点(高达云的大小的10%).
我的实现是根据x,y和z值对这些点进行排序,然后使用该std::unique
函数.然而,生成的云仍然包含重复项,即使排序本身似乎有效.
这是至关重要的代码
bool comparePoint(pcl::PointXYZINormal p1, pcl::PointXYZINormal p2){
if (p1.x != p2.x)
return p1.x > p2.x;
else if (p1.y != p2.y)
return p1.y > p2.y;
else
return p1.z > p2.z;
}
bool equalPoint(pcl::PointXYZINormal p1, pcl::PointXYZINormal p2){
if (p1.x == p2.x && p1.y == p2.y && p1.z == p2.z)
return true;
return false;
}
void KDsearch::cullDuplePoints(){
std::sort(points.begin(), points.end(), comparePoint);
std::unique(points.begin(), points.end(), equalPoint);
}
Run Code Online (Sandbox Code Playgroud)
这里是输出pointcloud(x,y和z坐标)的部分提取:
1.96828 -535.09515 2794.8391
1.96627 -636.95264 2914.0366
1.96627 -636.95264 2914.0366
1.9651 108.77433 2350.9841
1.9651 108.77433 2350.9841 …
Run Code Online (Sandbox Code Playgroud) 我想将坐标向量中的数据写入二进制.stl 3d几何文件.
我需要一个80位的标题,24位数的三角形.每个三角形应该由3个点和一个法线定义,每个三角形的坐标值是32位.此外,每个三角形可能有一个属性,我想留空.(见维基)
我想我仍然对char和二进制模式有误解.
最终生成的文件与原始文件大小相同,但图形程序无法读取,因此仍然存在逻辑错误......
在转换为char*之前,我的点坐标是双值的,这样做可以吗?
我的代码:
void write_stl(std::string filename, std::vector<tri> triangles){
//binary file
std::string header_info = "solid " + filename + "-output";
char head[80];
std::strncpy(head,header_info.c_str(),sizeof(head)-1);
char attribute[2] = "0";
unsigned long nTriLong = triangles.size() ;
std::ofstream myfile;
myfile.open((Filename + "-out.stl").c_str(), std::ios::out | std::ios::binary);
myfile.write(head,sizeof(head));
myfile.write((char*)&nTriLong,4);
//write down every triangle
for (std::vector<tri>::iterator it = triangles.begin(); it!=triangles.end(); it++){
//normal vector coordinates
myfile.write((char*)&it->m_n.m_x, 4);
myfile.write((char*)&it->m_n.m_y, 4);
myfile.write((char*)&it->m_n.m_z, 4);
//p1 coordinates
myfile.write((char*)&it->m_p1.m_x, 4);
myfile.write((char*)&it->m_p1.m_y, 4);
myfile.write((char*)&it->m_p1.m_z, 4);
//p2 coordinates
myfile.write((char*)&it->m_p2.m_x, 4); …
Run Code Online (Sandbox Code Playgroud)