|
| 1 | +#include <iostream> |
| 2 | +#include <pcl/ModelCoefficients.h> |
| 3 | +#include <pcl/io/pcd_io.h> |
| 4 | +#include <pcl/point_types.h> |
| 5 | +#include <pcl/sample_consensus/method_types.h> |
| 6 | +#include <pcl/sample_consensus/model_types.h> |
| 7 | +#include <pcl/segmentation/sac_segmentation.h> |
| 8 | +#include <pcl/filters/voxel_grid.h> |
| 9 | +#include <pcl/filters/extract_indices.h> |
| 10 | +#include <ed/include/ed/io/json_reader.h> |
| 11 | + |
| 12 | +Eigen::Matrix4f ReadJson(std::string 2022-04-05-13-28-28, float *xout, float *yout, float *zout) { |
| 13 | + |
| 14 | + std::string json_filename = boost::filesystem::change_extension(2022-04-05-13-28-28, ".json").string(); |
| 15 | + // read json metadata |
| 16 | + tue::config::DataPointer meta_data; |
| 17 | + |
| 18 | + try |
| 19 | + { |
| 20 | + meta_data = tue::config::fromFile(json_filename); |
| 21 | + } |
| 22 | + catch (tue::config::ParseException& e) |
| 23 | + { |
| 24 | + std::cerr << "Could not open '" << json_filename << "'.\n\n" << e.what() << std::endl; |
| 25 | + //return 0; |
| 26 | + } |
| 27 | + |
| 28 | + tue::config::Reader r(meta_data); |
| 29 | + // Read sensor pose |
| 30 | + geo::Pose3D sensor_pose; |
| 31 | + if (!ed::deserialize(r, "sensor_pose", sensor_pose)) |
| 32 | + { |
| 33 | + std::cerr << "No field 'sensor_pose' specified." << std::endl; |
| 34 | + //return 0; |
| 35 | + } |
| 36 | + // convert from geolib coordinates to ros coordinates #TODO remove geolib coordinates for camera pose |
| 37 | + sensor_pose.R = sensor_pose.R * geo::Mat3(1, 0, 0, 0, -1, 0, 0, 0, -1); |
| 38 | + |
| 39 | + float x = sensor_pose.t.x; |
| 40 | + float y = sensor_pose.t.y; |
| 41 | + float z = sensor_pose.t.z; |
| 42 | + float xx = sensor_pose.R.xx; |
| 43 | + float xy = sensor_pose.R.xy; |
| 44 | + float xz = sensor_pose.R.xz; |
| 45 | + float yx = sensor_pose.R.yx; |
| 46 | + float yy = sensor_pose.R.yy; |
| 47 | + float yz = sensor_pose.R.yz; |
| 48 | + float zx = sensor_pose.R.zx; |
| 49 | + float zy = sensor_pose.R.zy; |
| 50 | + float zz = sensor_pose.R.zz; |
| 51 | + |
| 52 | + *xout = x; |
| 53 | + *yout = y; |
| 54 | + *zout = z; |
| 55 | + |
| 56 | + //float qx, qy, qz, qw; |
| 57 | + |
| 58 | + //const float n = 2.0f/(qx*qx+qy*qy+qz*qz+qw*qw); |
| 59 | + Eigen::Matrix4f Transform = Eigen::Matrix4f::Identity();/* { |
| 60 | + {1.0f - n*qy*qy - n*qz*qz, n*qx*qy - n*qz*qw, n*qx*qz + n*qy*qw, x}, |
| 61 | + {n*qx*qy + n*qz*qw, 1.0f - n*qx*qx - n*qz*qz, n*qy*qz - n*qx*qw, y}, |
| 62 | + {n*qx*qz - n*qy*qw, n*qy*qz + n*qx*qw, 1.0f - n*qx*qx - n*qy*qy, z}, |
| 63 | + {0.0f, 0.0f, 0.0f, 1.0f}}; */ |
| 64 | + |
| 65 | + Transform(0,0) = xx; //1.0f - n*qy*qy - n*qz*qz; |
| 66 | + Transform(0,1) = xy; //n*qx*qy - n*qz*qw; |
| 67 | + Transform(0,2) = xz; //n*qx*qz + n*qy*qw; |
| 68 | + Transform(0,3) = x; |
| 69 | + Transform(1,0) = yx; //n*qx*qy + n*qz*qw; |
| 70 | + Transform(1,1) = yy; //1.0f - n*qx*qx - n*qz*qz; |
| 71 | + Transform(1,2) = yz; //n*qy*qz - n*qx*qw; |
| 72 | + Transform(1,3) = y; |
| 73 | + Transform(2,0) = zx; //n*qx*qz - n*qy*qw; |
| 74 | + Transform(2,1) = zy; //n*qy*qz + n*qx*qw; |
| 75 | + Transform(2,2) = zz; //1.0f - n*qx*qx - n*qy*qy; |
| 76 | + Transform(2,3) = z; |
| 77 | + |
| 78 | + std::cout << Transform << std::endl; |
| 79 | + return Transform; |
| 80 | +} |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +int |
| 85 | +main () |
| 86 | +{ |
| 87 | + pcl::PCLPointCloud2::Ptr cloud_blob (new pcl::PCLPointCloud2), cloud_filtered_blob (new pcl::PCLPointCloud2); |
| 88 | + pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>), cloud_p (new pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>); |
| 89 | + |
| 90 | + // Fill in the cloud data |
| 91 | + pcl::PCDReader reader; |
| 92 | + reader.read ("2022-04-05-13-28-28.pcd", *cloud_blob); |
| 93 | + |
| 94 | + std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl; |
| 95 | + |
| 96 | + // Create the filtering object: downsample the dataset using a leaf size of 1cm |
| 97 | + pcl::VoxelGrid<pcl::PCLPointCloud2> sor; |
| 98 | + sor.setInputCloud (cloud_blob); |
| 99 | + sor.setLeafSize (0.0025f, 0.0025f, 0.0025f); |
| 100 | + sor.filter (*cloud_filtered_blob); |
| 101 | + |
| 102 | + // Convert to the templated PointCloud |
| 103 | + pcl::fromPCLPointCloud2 (*cloud_filtered_blob, *cloud_filtered); |
| 104 | + |
| 105 | + std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl; |
| 106 | + |
| 107 | + // Write the downsampled version to disk |
| 108 | + pcl::PCDWriter writer; |
| 109 | + writer.write<pcl::PointXYZ> ("table_scene_lms400_downsampled.pcd", *cloud_filtered, false); |
| 110 | + |
| 111 | + pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ()); |
| 112 | + pcl::PointIndices::Ptr inliers (new pcl::PointIndices ()); |
| 113 | + // Create the segmentation object |
| 114 | + pcl::SACSegmentation<pcl::PointXYZ> seg; |
| 115 | + // Optional |
| 116 | + seg.setOptimizeCoefficients (true); |
| 117 | + // Mandatory |
| 118 | + seg.setModelType (pcl::SACMODEL_PLANE); |
| 119 | + seg.setMethodType (pcl::SAC_RANSAC); |
| 120 | + seg.setMaxIterations (1000); |
| 121 | + seg.setDistanceThreshold (0.01); |
| 122 | + |
| 123 | + // Create the filtering object |
| 124 | + pcl::ExtractIndices<pcl::PointXYZ> extract; |
| 125 | + |
| 126 | + int i = 0, nr_points = (int) cloud_filtered->size (); |
| 127 | + // While 30% of the original cloud is still there |
| 128 | + while (cloud_filtered->size () > 0.3 * nr_points) |
| 129 | + { |
| 130 | + // Segment the largest planar component from the remaining cloud |
| 131 | + seg.setInputCloud (cloud_filtered); |
| 132 | + seg.segment (*inliers, *coefficients); |
| 133 | + if (inliers->indices.size () == 0) |
| 134 | + { |
| 135 | + std::cerr << "Could not estimate a planar model for the given dataset." << std::endl; |
| 136 | + break; |
| 137 | + } |
| 138 | + |
| 139 | + // Extract the inliers |
| 140 | + extract.setInputCloud (cloud_filtered); |
| 141 | + extract.setIndices (inliers); |
| 142 | + extract.setNegative (false); |
| 143 | + extract.filter (*cloud_p); |
| 144 | + std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height << " data points." << std::endl; |
| 145 | + |
| 146 | + std::stringstream ss; |
| 147 | + ss << "table_scene_lms400_plane_" << i << ".pcd"; |
| 148 | + writer.write<pcl::PointXYZ> (ss.str (), *cloud_p, false); |
| 149 | + |
| 150 | + // Create the filtering object |
| 151 | + extract.setNegative (true); |
| 152 | + extract.filter (*cloud_f); |
| 153 | + cloud_filtered.swap (cloud_f); |
| 154 | + i++; |
| 155 | + } |
| 156 | + |
| 157 | + return (0); |
| 158 | + |
| 159 | + |
0 commit comments