diff --git a/apps/console_tools/laz_to_pcd.cpp b/apps/console_tools/laz_to_pcd.cpp index ca537744..e6603033 100644 --- a/apps/console_tools/laz_to_pcd.cpp +++ b/apps/console_tools/laz_to_pcd.cpp @@ -42,7 +42,7 @@ bool convert_and_save(const char* from, const char* to) } laszip_point* point = nullptr; - if (laszip_get_point_pointer(header, &point)) + if (laszip_get_point_pointer(laszip_reader, &point)) { return false; } @@ -58,7 +58,7 @@ bool convert_and_save(const char* from, const char* to) while (point_read_count < point_count) { - if (laszip_read_point(header)) + if (laszip_read_point(laszip_reader)) { return false; } @@ -66,10 +66,10 @@ bool convert_and_save(const char* from, const char* to) uint8_t* pointerTimstamp = pointDataPtr + sizeof(float) * 4; uint8_t* pointDataEndPtr = pointDataPtr + PointSizeBytes; - std::span xyzi(reinterpret_cast(pointDataPtr), 3); + std::span xyzi(reinterpret_cast(pointDataPtr), 4); xyzi[0] = static_cast(header->x_offset + header->x_scale_factor * static_cast(point->X)); - xyzi[1] = static_cast(header->y_offset + header->x_scale_factor * static_cast(point->Y)); - xyzi[2] = static_cast(header->z_offset + header->x_scale_factor * static_cast(point->Z)); + xyzi[1] = static_cast(header->y_offset + header->y_scale_factor * static_cast(point->Y)); + xyzi[2] = static_cast(header->z_offset + header->z_scale_factor * static_cast(point->Z)); xyzi[3] = static_cast(point->intensity); const double ts = point->gps_time; memcpy(pointerTimstamp, &ts, sizeof(double)); @@ -78,6 +78,12 @@ bool convert_and_save(const char* from, const char* to) } progress.finish(static_cast(point_read_count)); + if (laszip_close_reader(laszip_reader) || laszip_destroy(laszip_reader)) + { + spdlog::error("Failed closing LASzip reader"); + return false; + } + if (std::ofstream to_file = std::ofstream(to, std::ios::out | std::ios::binary)) { to_file << "# .PCD v0.7\n"; @@ -93,6 +99,12 @@ bool convert_and_save(const char* from, const char* to) to_file << "DATA binary\n"; to_file.write((const char*)pointsBinary.data(), pointsBinary.size()); + to_file.close(); + if (!to_file) + { + spdlog::error("Failed writing PCD output {}", to); + return false; + } } else { diff --git a/apps/console_tools/laz_to_txt.cpp b/apps/console_tools/laz_to_txt.cpp index 274892fc..d34185d1 100644 --- a/apps/console_tools/laz_to_txt.cpp +++ b/apps/console_tools/laz_to_txt.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include @@ -47,7 +49,7 @@ bool convert_and_save(const char* from, const char* to) } laszip_point* point = nullptr; - if (laszip_get_point_pointer(header, &point)) + if (laszip_get_point_pointer(laszip_reader, &point)) { return false; } @@ -60,7 +62,7 @@ bool convert_and_save(const char* from, const char* to) laszip_I64 point_read_count = 0; while (point_read_count < point_count) { - if (laszip_read_point(header)) + if (laszip_read_point(laszip_reader)) { return false; } @@ -76,18 +78,34 @@ bool convert_and_save(const char* from, const char* to) } progress.finish(static_cast(point_read_count)); + if (laszip_close_reader(laszip_reader) || laszip_destroy(laszip_reader)) + { + spdlog::error("Failed closing LASzip reader"); + return false; + } + if (std::ofstream to_file = std::ofstream(to, std::ios::out)) { + // The default 6 significant digits would print every timestamp as e.g. 1.731e+09. + const auto coordinate_precision = to_file.precision(); + const auto timestamp_precision = std::numeric_limits::max_digits10; + ConsoleProgress write_progress("Writing points", points.size()); uint64_t points_written = 0; for (const auto& point : points) { - to_file << point.x << ' ' << point.y << ' ' << point.z << ' ' << point.intensity << ' ' << point.timestamp << '\n'; + to_file << std::setprecision(coordinate_precision) << point.x << ' ' << point.y << ' ' << point.z << ' ' << point.intensity + << ' ' << std::setprecision(timestamp_precision) << point.timestamp << '\n'; write_progress.update(++points_written); } write_progress.finish(points_written); - to_file.write((const char*)points.data(), points.size() * sizeof(Point)); + to_file.close(); + if (!to_file) + { + spdlog::error("Failed writing TXT output {}", to); + return false; + } } else {