66 : input_cloud(
new pcl::PCLPointCloud2()), output_cloud(
new pcl::PCLPointCloud2())
69 ~Modifier() =
default;
78 bool load(
const std::string & file_path,
79 pointcloudcrafter::tools::formats::FileFormat format)
82 case pointcloudcrafter::tools::formats::FileFormat::PCD:
83 return load_pcd(file_path);
84 case pointcloudcrafter::tools::formats::FileFormat::PLY:
85 return load_ply(file_path);
86 case pointcloudcrafter::tools::formats::FileFormat::TXT:
87 return load_txt(file_path);
88 case pointcloudcrafter::tools::formats::FileFormat::KITTI:
89 return load_kitti(file_path);
90 case pointcloudcrafter::tools::formats::FileFormat::NUSCENES:
91 return load_nuscenes(file_path);
92 case pointcloudcrafter::tools::formats::FileFormat::OBJ:
93 return load_obj(file_path);
95 std::cerr <<
"Error: Unknown file format" << std::endl;
105 bool save(
const std::string & file_path,
106 pointcloudcrafter::tools::formats::FileFormat format)
109 case pointcloudcrafter::tools::formats::FileFormat::PCD:
110 return save_pcd(file_path);
111 case pointcloudcrafter::tools::formats::FileFormat::PLY:
112 return save_ply(file_path);
113 case pointcloudcrafter::tools::formats::FileFormat::TXT:
114 return save_txt(file_path);
115 case pointcloudcrafter::tools::formats::FileFormat::KITTI:
116 return save_kitti(file_path);
117 case pointcloudcrafter::tools::formats::FileFormat::NUSCENES:
118 return save_nuscenes(file_path);
120 std::cerr <<
"Error: Unknown file format" << std::endl;
132 Modifier &
cropBox(
const std::vector<double> & box_params,
const bool & negative =
false)
135 if (box_params.size() != 6) {
136 std::cerr <<
"Error: box_params must contain exactly 6 elements." << std::endl;
140 pcl::CropBox<PointCloud> boxFilter;
141 boxFilter.setMin(Eigen::Vector4f(box_params[0], box_params[1], box_params[2], 1.0));
142 boxFilter.setMax(Eigen::Vector4f(box_params[3], box_params[4], box_params[5], 1.0));
143 boxFilter.setInputCloud(output_cloud);
144 boxFilter.setNegative(negative);
145 boxFilter.filter(*output_cloud);
154 Modifier &
cropSphere(
const double & sphere_params,
const bool & negative =
false)
156 const auto radius_sq = sphere_params * sphere_params;
157 pcl::PointCloud<pcl::PointXYZ>::Ptr tmp(
new pcl::PointCloud<pcl::PointXYZ>);
158 pcl::fromPCLPointCloud2(*output_cloud, *tmp);
160 pcl::PointIndices::Ptr inliers(
new pcl::PointIndices());
161 inliers->indices.reserve(tmp->size());
163 for (
size_t i = 0; i < tmp->size(); ++i) {
164 if (tmp->points[i].getVector3fMap().squaredNorm() < radius_sq) {
165 inliers->indices.push_back(i);
169 pcl::ExtractIndices<pcl::PCLPointCloud2> extract;
170 extract.setInputCloud(output_cloud);
171 extract.setIndices(inliers);
172 extract.setNegative(negative);
173 extract.filter(*output_cloud);
183 Modifier &
cropCylinder(
const double & zylinder_params,
const bool & negative =
false)
185 const auto radius_sq = zylinder_params * zylinder_params;
186 pcl::PointCloud<pcl::PointXYZ>::Ptr tmp(
new pcl::PointCloud<pcl::PointXYZ>);
187 pcl::fromPCLPointCloud2(*output_cloud, *tmp);
189 pcl::PointIndices::Ptr inliers(
new pcl::PointIndices());
190 inliers->indices.reserve(tmp->size());
192 for (
size_t i = 0; i < tmp->size(); ++i) {
193 auto point = tmp->points[i].getVector3fMap();
194 auto r = point.x() * point.x() + point.y() * point.y();
196 inliers->indices.push_back(i);
200 pcl::ExtractIndices<pcl::PCLPointCloud2> extract;
201 extract.setInputCloud(output_cloud);
202 extract.setIndices(inliers);
203 extract.setNegative(negative);
204 extract.filter(*output_cloud);
216 if (voxel.size() != 3) {
217 std::cerr <<
"Error: voxel must contain exactly 3 elements." << std::endl;
221 pcl::PointCloud<pcl::PointXYZ>::Ptr tmp(
new pcl::PointCloud<pcl::PointXYZ>);
222 pcl::fromPCLPointCloud2(*output_cloud, *tmp);
225 pcl::PointXYZ min_pt, max_pt;
226 pcl::getMinMax3D(*tmp, min_pt, max_pt);
229 const auto num_x =
static_cast<uint64_t
>(std::floor((max_pt.x - min_pt.x) / voxel[0]));
230 const auto num_y =
static_cast<uint64_t
>(std::floor((max_pt.y - min_pt.y) / voxel[1]));
231 const auto num_z =
static_cast<uint64_t
>(std::floor((max_pt.z - min_pt.z) / voxel[2]));
232 const auto total = num_x * num_y * num_z;
235 if (total >
static_cast<uint64_t
>(INT32_MAX)) {
236 applySubVoxelFilter(voxel, output_cloud, min_pt, max_pt, num_x, num_y, num_z);
238 applyVoxelFilter(voxel, output_cloud);
252 if (radius <= 0.0 || min_neighbors < 1) {
253 std::cerr <<
"Error: radius must be positive and min_neighbors must be at least 1."
258 pcl::RadiusOutlierRemoval<PointCloud> radiusFilter;
259 radiusFilter.setRadiusSearch(radius);
260 radiusFilter.setMinNeighborsInRadius(min_neighbors);
261 radiusFilter.setInputCloud(output_cloud);
262 radiusFilter.filter(*output_cloud);
274 if (threshold <= 0.0 || mean < 1) {
275 std::cerr <<
"Error: threshold must be positive and mean must be at least 1." << std::endl;
279 pcl::StatisticalOutlierRemoval<PointCloud> statFilter;
280 statFilter.setMeanK(mean);
281 statFilter.setStddevMulThresh(threshold);
282 statFilter.setInputCloud(output_cloud);
283 statFilter.filter(*output_cloud);
293 Modifier &
transform(
const Eigen::Affine3d & transformation)
296 int x_offset = -1, y_offset = -1, z_offset = -1;
297 for (
const auto & field : output_cloud->fields) {
298 if (field.name ==
"x") x_offset = field.offset;
299 else if (field.name ==
"y") y_offset = field.offset;
300 else if (field.name ==
"z") z_offset = field.offset;
303 Eigen::Affine3f transform_f = transformation.cast<
float>();
306 for (
size_t i = 0; i < output_cloud->data.size(); i += output_cloud->point_step) {
308 memcpy(&x, &output_cloud->data[i + x_offset],
sizeof(
float));
309 memcpy(&y, &output_cloud->data[i + y_offset],
sizeof(
float));
310 memcpy(&z, &output_cloud->data[i + z_offset],
sizeof(
float));
312 Eigen::Vector3f pt(x, y, z);
313 Eigen::Vector3f pt_transformed = transform_f * pt;
315 memcpy(&output_cloud->data[i + x_offset], &pt_transformed.x(),
sizeof(
float));
316 memcpy(&output_cloud->data[i + y_offset], &pt_transformed.y(),
sizeof(
float));
317 memcpy(&output_cloud->data[i + z_offset], &pt_transformed.z(),
sizeof(
float));
333 std::map<std::pair<int, int>, Modifier>
split(
334 const double & grid_size,
337 std::map<std::pair<int, int>, Modifier> result;
339 if (output_cloud->data.empty()) {
340 std::cerr <<
"Error: Cannot split empty point cloud" << std::endl;
343 if (grid_size <= 0.0) {
344 std::cerr <<
"Error: grid_size must be positive" << std::endl;
348 int x_offset = -1, y_offset = -1;
349 for (
const auto & field : output_cloud->fields) {
350 if (field.name ==
"x") x_offset =
static_cast<int>(field.offset);
351 else if (field.name ==
"y") y_offset =
static_cast<int>(field.offset);
353 if (x_offset == -1 || y_offset == -1) {
354 std::cerr <<
"Error: Point cloud must have x and y fields" << std::endl;
358 const size_t num_points = output_cloud->width * output_cloud->height;
359 const size_t point_step = output_cloud->point_step;
362 std::map<std::pair<int, int>, std::vector<size_t>> grid_index;
363 const size_t bin_report_step = std::max<size_t>(num_points / 100, 1);
364 for (
size_t i = 0; i < num_points; ++i) {
365 const uint8_t * pt_data = output_cloud->data.data() + i * point_step;
367 memcpy(&x, pt_data + x_offset,
sizeof(
float));
368 memcpy(&y, pt_data + y_offset,
sizeof(
float));
369 int ix =
static_cast<int>(std::floor(x / grid_size));
370 int iy =
static_cast<int>(std::floor(y / grid_size));
371 grid_index[{ix, iy}].push_back(i);
372 if (on_progress && ((i + 1) % bin_report_step == 0 || i + 1 == num_points)) {
373 on_progress(
"Binning points", i + 1, num_points);
377 const size_t total_cells = grid_index.size();
378 size_t cells_built = 0;
379 for (
const auto & cell : grid_index) {
380 const auto & indices = cell.second;
381 PointCloud::Ptr cell_cloud = std::make_shared<PointCloud>();
382 cell_cloud->header = output_cloud->header;
383 cell_cloud->fields = output_cloud->fields;
384 cell_cloud->is_bigendian = output_cloud->is_bigendian;
385 cell_cloud->point_step = point_step;
386 cell_cloud->width = indices.size();
387 cell_cloud->height = 1;
388 cell_cloud->is_dense =
true;
389 cell_cloud->row_step = cell_cloud->point_step * cell_cloud->width;
390 cell_cloud->data.resize(indices.size() * point_step);
392 for (
size_t j = 0; j < indices.size(); ++j) {
394 cell_cloud->data.data() + j * point_step,
395 output_cloud->data.data() + indices[j] * point_step,
399 Modifier cell_modifier;
401 result.emplace(cell.first, std::move(cell_modifier));
405 on_progress(
"Building cells", cells_built, total_cells);
421 const std::map<std::pair<int, int>, Modifier> & cells,
422 const double & grid_size,
423 const std::string & file_path,
424 const std::string & extension =
".pcd")
426 std::ofstream file(file_path);
427 if (!file.is_open()) {
428 std::cerr <<
"Error: Failed to write metadata file: " << file_path << std::endl;
432 auto format_coord = [](
double val) -> std::string {
433 if (val == std::floor(val)) {
434 return std::to_string(
static_cast<int64_t
>(val));
436 std::ostringstream oss;
441 file <<
"x_resolution: " << grid_size <<
"\n";
442 file <<
"y_resolution: " << grid_size <<
"\n";
444 for (
const auto & cell : cells) {
445 double coord_x = cell.first.first * grid_size;
446 double coord_y = cell.first.second * grid_size;
447 std::string sx = format_coord(coord_x);
448 std::string sy = format_coord(coord_y);
449 file << sx <<
"_" << sy << extension <<
": [" << sx <<
", " << sy <<
"]\n";
463 std::vector<double> time_float{};
464 std::vector<size_t> time_int{};
465 for (
size_t i = 0; i < output_cloud->data.size(); i += output_cloud->point_step) {
466 for (
size_t j = 0; j < output_cloud->fields.size(); ++j) {
469 pcl::PCLPointField & field = output_cloud->fields[j];
470 size_t point_offset = i + field.offset;
471 if (field.name ==
"timestamp" || field.name ==
"time_stamp" || field.name ==
"t") {
472 if (field.datatype == pcl::PCLPointField::FLOAT32) {
474 memcpy(&tmp_stamp, &output_cloud->data[point_offset],
sizeof(
float));
475 tf =
static_cast<double>(tmp_stamp);
476 }
else if (field.datatype == pcl::PCLPointField::FLOAT64) {
477 memcpy(&tf, &output_cloud->data[point_offset],
sizeof(
double));
478 }
else if (field.datatype == pcl::PCLPointField::UINT32) {
479 memcpy(&ti, &output_cloud->data[point_offset],
sizeof(uint32_t));
480 }
else if (field.datatype == pcl::PCLPointField::UINT8 && field.count == 8) {
482 for (
size_t k = 0; k < 8; ++k) {
483 ti |=
static_cast<uint64_t
>(output_cloud->data[point_offset + k]) << (8 * k);
485 }
else if (field.datatype == pcl::PCLPointField::UINT8 && field.count == 4) {
487 for (
size_t k = 0; k < 4; ++k) {
488 ti |=
static_cast<uint32_t
>(output_cloud->data[point_offset + k]) << (8 * k);
491 std::cerr <<
"Error: Unknown datatype for timestamp field" << std::endl;
496 time_float.push_back(tf);
498 time_int.push_back(ti);
503 if (!time_float.empty()) {
504 saveTimestamps<double>(time_float, file_path);
507 std::cout <<
"Float timestamps: " << time_float.size() <<
" entries" << std::endl;
508 if (time_float.size() > 1) {
509 double min_ts = *std::min_element(time_float.begin(), time_float.end());
510 double max_ts = *std::max_element(time_float.begin(), time_float.end());
511 std::cout <<
" Min: " << min_ts <<
"\n Max: " << max_ts
512 <<
"\n Duration: " << (max_ts - min_ts) << std::endl;
516 if (!time_int.empty()) {
517 saveTimestamps<size_t>(time_int, file_path);
520 std::cout <<
"Integer timestamps: " << time_int.size() <<
" entries" << std::endl;
521 if (time_int.size() > 1) {
522 uint64_t min_ts = *std::min_element(time_int.begin(), time_int.end());
523 uint64_t max_ts = *std::max_element(time_int.begin(), time_int.end());
524 std::cout <<
" Min: " << min_ts <<
"\n Max: " << max_ts
525 <<
"\n Duration: " << (max_ts - min_ts) << std::endl;
539 pcl::PointCloud<pcl::PointXYZ>::Ptr input_xyz(
new pcl::PointCloud<pcl::PointXYZ>());
540 pcl::PointCloud<pcl::PointXYZ>::Ptr output_xyz(
new pcl::PointCloud<pcl::PointXYZ>());
542 pcl::fromPCLPointCloud2(*input_cloud, *input_xyz);
543 pcl::fromPCLPointCloud2(*output_cloud, *output_xyz);
545 pcl::visualization::PCLVisualizer viewer(
"Point Cloud Viewer");
549 pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> input_color(
550 input_xyz, 255, 255, 255);
551 viewer.addPointCloud(input_xyz, input_color,
"input_cloud");
552 viewer.setPointCloudRenderingProperties(
553 pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
"input_cloud");
556 pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> output_color(
557 output_xyz, 230, 20, 20);
558 viewer.addPointCloud(output_xyz, output_color,
"output_cloud");
559 viewer.setPointCloudRenderingProperties(
560 pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
"output_cloud");
563 viewer.addCoordinateSystem(1.0,
"cloud", 0);
564 viewer.setBackgroundColor(0.15, 0.15, 0.15, 0);
566 std::cout <<
"Input_cloud: white | Output_cloud: red" << std::endl;
579 *output_cloud = *cloud;
580 *input_cloud = *cloud;
596 PointCloud::Ptr input_cloud;
597 PointCloud::Ptr output_cloud;
604 bool load_pcd(
const std::string & file_path)
606 pcl::PCDReader reader;
607 if (reader.read(file_path, *input_cloud) == -1) {
608 std::cerr <<
"Error: Failed to load PCD file: " << file_path << std::endl;
611 *output_cloud = *input_cloud;
619 bool load_ply(
const std::string & file_path)
621 pcl::PLYReader reader;
622 if (reader.read(file_path, *input_cloud) == -1) {
623 std::cerr <<
"Error: Failed to load PLY file: " << file_path << std::endl;
626 *output_cloud = *input_cloud;
639 bool load_txt(
const std::string & file_path)
641 std::ifstream file(file_path);
642 if (!file.is_open()) {
643 std::cerr <<
"Error: Failed to open TXT file: " << file_path << std::endl;
648 std::vector<std::string> field_names;
649 std::vector<std::vector<float>> data;
652 if (!std::getline(file, line)) {
653 std::cerr <<
"Error: Empty TXT file: " << file_path << std::endl;
658 std::istringstream first_line(line);
660 bool is_header =
false;
662 while (first_line >> token) {
665 std::strtof(token.c_str(), &end);
670 field_names.push_back(token);
676 size_t num_fields = 0;
679 std::istringstream data_line(line);
680 std::vector<float> point_data;
682 while (data_line >> value) {
683 point_data.push_back(value);
686 data.push_back(point_data);
689 if (num_fields >= 3) {
690 field_names = {
"x",
"y",
"z"};
691 for (
size_t i = 3; i < num_fields; ++i) {
692 field_names.push_back(
"field_" + std::to_string(i));
695 for (
size_t i = 0; i < num_fields; ++i) {
696 field_names.push_back(
"field_" + std::to_string(i));
702 while (std::getline(file, line)) {
703 if (line.empty() || line[0] ==
'#')
continue;
705 std::istringstream iss(line);
706 std::vector<float> point_data;
709 while (iss >> value) {
710 point_data.push_back(value);
713 if (point_data.size() == field_names.size()) {
714 data.push_back(point_data);
719 std::cerr <<
"Error: No valid points in TXT file: " << file_path << std::endl;
724 const size_t num_points = data.size();
725 const size_t num_fields = field_names.size();
727 input_cloud->width = num_points;
728 input_cloud->height = 1;
729 input_cloud->is_dense =
true;
730 input_cloud->is_bigendian =
false;
733 input_cloud->fields.clear();
734 input_cloud->fields.resize(num_fields);
737 for (
size_t i = 0; i < num_fields; ++i) {
738 input_cloud->fields[i].name = field_names[i];
739 input_cloud->fields[i].offset = offset;
740 input_cloud->fields[i].datatype = pcl::PCLPointField::FLOAT32;
741 input_cloud->fields[i].count = 1;
742 offset +=
sizeof(float);
745 input_cloud->point_step = offset;
746 input_cloud->row_step = input_cloud->point_step * input_cloud->width;
747 input_cloud->data.resize(input_cloud->row_step * input_cloud->height);
750 for (
size_t i = 0; i < num_points; ++i) {
751 uint8_t* pt_data = input_cloud->data.data() + i * input_cloud->point_step;
752 for (
size_t j = 0; j < num_fields; ++j) {
753 float value = data[i][j];
754 memcpy(pt_data + input_cloud->fields[j].offset, &value,
sizeof(
float));
758 *output_cloud = *input_cloud;
769 bool load_kitti(
const std::string & file_path)
771 std::ifstream file(file_path, std::ios::binary);
772 if (!file.is_open()) {
773 std::cerr <<
"Error: Failed to open KITTI file: " << file_path << std::endl;
778 file.seekg(0, std::ios::end);
779 const size_t file_size = file.tellg();
780 file.seekg(0, std::ios::beg);
783 constexpr size_t point_size = 4 *
sizeof(float);
784 const size_t num_points = file_size / point_size;
787 std::vector<float> buffer(num_points * 4);
788 file.read(
reinterpret_cast<char*
>(buffer.data()), file_size);
791 pcl::PointCloud<pcl::PointXYZI>::Ptr tmp(
new pcl::PointCloud<pcl::PointXYZI>);
792 tmp->reserve(num_points);
794 for (
size_t i = 0; i < num_points; ++i) {
795 pcl::PointXYZI point;
796 point.x = buffer[i * 4 + 0];
797 point.y = buffer[i * 4 + 1];
798 point.z = buffer[i * 4 + 2];
799 point.intensity = buffer[i * 4 + 3];
800 tmp->push_back(point);
803 tmp->width = tmp->size();
805 tmp->is_dense =
true;
807 pcl::toPCLPointCloud2(*tmp, *input_cloud);
808 *output_cloud = *input_cloud;
819 bool load_nuscenes(
const std::string & file_path)
821 std::ifstream file(file_path, std::ios::binary);
822 if (!file.is_open()) {
823 std::cerr <<
"Error: Failed to open nuScenes file: " << file_path << std::endl;
828 file.seekg(0, std::ios::end);
829 const size_t file_size = file.tellg();
830 file.seekg(0, std::ios::beg);
833 constexpr size_t point_size = 5 *
sizeof(float);
834 const size_t num_points = file_size / point_size;
837 std::vector<float> buffer(num_points * 5);
838 file.read(
reinterpret_cast<char*
>(buffer.data()), file_size);
842 pcl::PointCloud<pcl::PointXYZI>::Ptr tmp(
new pcl::PointCloud<pcl::PointXYZI>);
843 tmp->reserve(num_points);
845 for (
size_t i = 0; i < num_points; ++i) {
846 pcl::PointXYZI point;
847 point.x = buffer[i * 5 + 0];
848 point.y = buffer[i * 5 + 1];
849 point.z = buffer[i * 5 + 2];
850 point.intensity = buffer[i * 5 + 3];
853 tmp->push_back(point);
856 tmp->width = tmp->size();
858 tmp->is_dense =
true;
860 pcl::toPCLPointCloud2(*tmp, *input_cloud);
861 *output_cloud = *input_cloud;
869 bool load_obj(
const std::string & file_path)
871 pcl::OBJReader reader;
872 if (reader.read(file_path, *input_cloud) == -1) {
873 std::cerr <<
"Error: Failed to load OBJ file: " << file_path << std::endl;
876 *output_cloud = *input_cloud;
885 bool save_pcd(
const std::string & file_path)
887 pcl::PCDWriter writer;
888 if (writer.write(file_path, *output_cloud, Eigen::Vector4f::Zero(),
889 Eigen::Quaternionf::Identity(),
true) == -1) {
890 std::cerr <<
"Error: Failed to save PCD file: " << file_path << std::endl;
900 bool save_ply(
const std::string & file_path)
902 pcl::PLYWriter writer;
903 if (writer.write(file_path, *output_cloud, Eigen::Vector4f::Zero(),
904 Eigen::Quaternionf::Identity(),
true,
false) == -1) {
905 std::cerr <<
"Error: Failed to save PLY file: " << file_path << std::endl;
919 bool save_txt(
const std::string & file_path)
921 std::ofstream file(file_path);
922 if (!file.is_open()) {
923 std::cerr <<
"Error: Failed to open TXT file for writing: " << file_path << std::endl;
928 for (
size_t i = 0; i < output_cloud->fields.size(); ++i) {
929 file << output_cloud->fields[i].name;
930 if (i < output_cloud->fields.size() - 1) {
937 const size_t num_points = output_cloud->width * output_cloud->height;
938 file << std::fixed << std::setprecision(6);
940 for (
size_t i = 0; i < num_points; ++i) {
941 const uint8_t* pt_data = output_cloud->data.data() + i * output_cloud->point_step;
943 for (
size_t j = 0; j < output_cloud->fields.size(); ++j) {
944 const auto & field = output_cloud->fields[j];
945 float value = getFieldValue(pt_data, field);
947 if (j < output_cloud->fields.size() - 1) {
964 bool save_kitti(
const std::string & file_path)
966 std::ofstream file(file_path, std::ios::binary);
967 if (!file.is_open()) {
968 std::cerr <<
"Error: Failed to open KITTI file for writing: " << file_path << std::endl;
973 int idx_x = -1, idx_y = -1, idx_z = -1, idx_intensity = -1;
974 for (
size_t i = 0; i < output_cloud->fields.size(); ++i) {
975 std::string name = output_cloud->fields[i].name;
976 std::transform(name.begin(), name.end(), name.begin(),
977 [](
unsigned char c){ return std::tolower(c); });
978 if (name ==
"x") idx_x =
static_cast<int>(i);
979 else if (name ==
"y") idx_y =
static_cast<int>(i);
980 else if (name ==
"z") idx_z =
static_cast<int>(i);
981 else if (name ==
"intensity") idx_intensity =
static_cast<int>(i);
984 const size_t num_points = output_cloud->width * output_cloud->height;
986 for (
size_t i = 0; i < num_points; ++i) {
987 uint8_t* pt_data = output_cloud->data.data() + i * output_cloud->point_step;
989 float x = *
reinterpret_cast<float*
>(pt_data + output_cloud->fields[idx_x].offset);
990 float y = *
reinterpret_cast<float*
>(pt_data + output_cloud->fields[idx_y].offset);
991 float z = *
reinterpret_cast<float*
>(pt_data + output_cloud->fields[idx_z].offset);
993 float intensity = 0.0f;
994 if (idx_intensity != -1) {
995 intensity = getFieldValue(pt_data, output_cloud->fields[idx_intensity]);
998 float data[4] = {x, y, z, intensity};
999 file.write(
reinterpret_cast<char*
>(data),
sizeof(data));
1012 bool save_nuscenes(
const std::string & file_path)
1014 std::ofstream file(file_path, std::ios::binary);
1015 if (!file.is_open()) {
1016 std::cerr <<
"Error: Failed to open nuScenes file for writing: " << file_path << std::endl;
1021 int idx_x = -1, idx_y = -1, idx_z = -1, idx_intensity = -1, idx_ring = -1;
1022 for (
size_t i = 0; i < output_cloud->fields.size(); ++i) {
1023 std::string name = output_cloud->fields[i].name;
1024 std::transform(name.begin(), name.end(), name.begin(),
1025 [](
unsigned char c){ return std::tolower(c); });
1026 if (name ==
"x") idx_x =
static_cast<int>(i);
1027 else if (name ==
"y") idx_y =
static_cast<int>(i);
1028 else if (name ==
"z") idx_z =
static_cast<int>(i);
1029 else if (name ==
"intensity") idx_intensity =
static_cast<int>(i);
1030 else if (name ==
"ring" || name ==
"channel") idx_ring =
static_cast<int>(i);
1033 const size_t num_points = output_cloud->width * output_cloud->height;
1035 for (
size_t i = 0; i < num_points; ++i) {
1036 uint8_t* pt_data = output_cloud->data.data() + i * output_cloud->point_step;
1038 float x = *
reinterpret_cast<float*
>(pt_data + output_cloud->fields[idx_x].offset);
1039 float y = *
reinterpret_cast<float*
>(pt_data + output_cloud->fields[idx_y].offset);
1040 float z = *
reinterpret_cast<float*
>(pt_data + output_cloud->fields[idx_z].offset);
1042 float intensity = (idx_intensity != -1) ?
1043 getFieldValue(pt_data, output_cloud->fields[idx_intensity]) : 0.0f;
1044 float ring = (idx_ring != -1) ?
1045 getFieldValue(pt_data, output_cloud->fields[idx_ring]) : 0.0f;
1047 float data[5] = {x, y, z, intensity, ring};
1048 file.write(
reinterpret_cast<char*
>(data),
sizeof(data));
1059 float getFieldValue(
const uint8_t* pt_data,
const pcl::PCLPointField & field)
1061 switch (field.datatype) {
1062 case pcl::PCLPointField::INT8:
1063 return static_cast<float>(*
reinterpret_cast<const int8_t*
>(pt_data + field.offset));
1064 case pcl::PCLPointField::UINT8:
1065 return static_cast<float>(*
reinterpret_cast<const uint8_t*
>(pt_data + field.offset));
1066 case pcl::PCLPointField::INT16:
1067 return static_cast<float>(*
reinterpret_cast<const int16_t*
>(pt_data + field.offset));
1068 case pcl::PCLPointField::UINT16:
1069 return static_cast<float>(*
reinterpret_cast<const uint16_t*
>(pt_data + field.offset));
1070 case pcl::PCLPointField::INT32:
1071 return static_cast<float>(*
reinterpret_cast<const int32_t*
>(pt_data + field.offset));
1072 case pcl::PCLPointField::UINT32:
1073 return static_cast<float>(*
reinterpret_cast<const uint32_t*
>(pt_data + field.offset));
1074 case pcl::PCLPointField::FLOAT32:
1075 return *
reinterpret_cast<const float*
>(pt_data + field.offset);
1076 case pcl::PCLPointField::FLOAT64:
1077 return static_cast<float>(*
reinterpret_cast<const double*
>(pt_data + field.offset));
1093 void applySubVoxelFilter(
1094 const std::vector<double> & voxel, PointCloud::Ptr & cloud,
const pcl::PointXYZ & min_pt,
1095 const pcl::PointXYZ & max_pt,
const uint64_t num_x,
const uint64_t num_y,
const uint64_t num_z)
1097 PointCloud::Ptr tpc = std::make_shared<PointCloud>();
1098 PointCloud::Ptr apc = std::make_shared<PointCloud>();
1099 uint64_t num = std::floor(std::cbrt((num_x * num_y * num_z) / INT32_MAX)) + 1;
1102 for (uint64_t ix = 0; ix < num; ix++) {
1103 for (uint64_t iy = 0; iy < num; iy++) {
1104 for (uint64_t iz = 0; iz < num; iz++) {
1105 std::cout <<
"\rSubvoxel split: " << ++iter <<
" of "
1106 <<
static_cast<uint64_t
>(std::pow(num, 3)) << std::flush;
1107 pcl::CropBox<PointCloud> box;
1108 pcl::PointXYZ tmin = {
1109 min_pt.x + (ix * (max_pt.x - min_pt.x) / num),
1110 min_pt.y + (iy * (max_pt.y - min_pt.y) / num),
1111 min_pt.z + (iz * (max_pt.z - min_pt.z) / num)};
1112 pcl::PointXYZ tmax = {
1113 tmin.x + (max_pt.x - min_pt.x) / num, tmin.y + (max_pt.y - min_pt.y) / num,
1114 tmin.z + (max_pt.z - min_pt.z) / num};
1116 box.setMin(Eigen::Vector4f(tmin.x, tmin.y, tmin.z, 1.0));
1117 box.setMax(Eigen::Vector4f(tmax.x, tmax.y, tmax.z, 1.0));
1118 box.setInputCloud(cloud);
1121 pcl::VoxelGrid<PointCloud> grid;
1122 grid.setInputCloud(tpc);
1123 grid.setLeafSize(voxel[0], voxel[1], voxel[2]);
1130 std::cout << std::endl;
1131 *output_cloud = *apc;
1139 void applyVoxelFilter(
const std::vector<double> & voxel, PointCloud::Ptr & cloud)
1142 voxelFilter.setLeafSize(voxel[0], voxel[1], voxel[2]);
1153 template <
typename T>
1154 bool saveTimestamps(
const std::vector<T> & timestamps,
const std::string & output_path)
1156 std::ofstream outfile;
1157 outfile.open(output_path);
1159 if (!outfile.is_open()) {
1160 std::cerr <<
"Failed to open file for writing: " << output_path << std::endl;
1164 for (
const auto & ts : timestamps) {
1165 outfile << std::fixed << std::setprecision(9) << ts << std::endl;
1169 std::cout <<
"Timestamps saved to: " << output_path << std::endl;