用liblas将点云写入las文件
首先配置liblas库,可以见上一篇博客
inline int writeLas(string lasPath, Points3Ds pt3Ds) {//point3ds是我自己定义的结构std::ofstream ofs;ofs.open(lasPath, std::ios::out | std::ios::binary);if (!ofs.is_open()) {std::cerr << "无法创建 LAS 文件" << std::endl;return 1;}// 设置 LAS 文件的头部信息liblas::Header header;header.SetVersionMajor(1);header.SetVersionMinor(2);header.SetDataFormatId(liblas::ePointFormat2);header.SetScale(0.00001, 0.00001, 0.00001);//要设置,不然保存的就是整形xyzint pointsNum = pt3Ds.size(); //获取点数header.SetPointRecordsCount(pointsNum); //设置点数// 创建一个 LAS 写入器liblas::Writer writer(ofs, header);liblas::Point point(&header);//记录数据范围double minx = 99999999;double miny = 99999999;double minz = 99999999;double maxx = -99999999;double maxy = -99999999;double maxz = -99999999;// 遍历点云数据,添加到 LAS 文件中for (const auto& pd : pt3Ds) {// 设置点的坐标double tx = pd.second.x;double ty = pd.second.y;double tz = pd.second.z;minx = minx > tx ? tx : minx;miny = miny > ty ? ty : miny;minz = minz > tz ? tz : minz;maxx = maxx < tx ? tx : maxx;maxy = maxy < ty ? ty : maxy;maxz = maxz < tz ? tz : maxz;point.SetX(tx);point.SetY(ty);point.SetZ(tz);// 设置点的颜色liblas::Color color;color.SetRed(pd.second.r);color.SetGreen(pd.second.g);color.SetBlue(pd.second.b);point.SetColor(color);// 将点写入文件writer.WritePoint(point);}header.SetMax(maxx, maxy, maxz); //设置数据范围header.SetMin(minx, miny, minz);//(若中间处理过程点数有变化的话,可以通过此方法进行修改)//(若无变动,则可以在一开始就设置点数)writer.SetHeader(header); //设置文件头writer.WriteHeader(); //重写文件头// 关闭文件ofs.close();std::cout << "LAS 文件创建成功。" << std::endl;return 0;
},
其中一定要SetScale啊,搞了很久!
在debug模式下可以顺利保存点云,但是release下报内存问题,没找到原因!
放弃准备用laslib试试