#include "data/pcviewer.h" #include // #include #include std::shared_ptr CustomColorVis( pcl::PointCloud::ConstPtr pc) { // -------------------------------------------- // -----Open 3D viewer and add point cloud----- // -------------------------------------------- std::shared_ptr viewer( new pcl::visualization::PCLVisualizer("PointCloud Viewer")); viewer->setBackgroundColor(0, 0, 0); pcl::visualization::PointCloudColorHandlerCustom single_color( pc, 255, 255, 255); viewer->addPointCloud(pc, single_color, "point cloud"); viewer->setPointCloudRenderingProperties( pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "point cloud"); // viewer->addCoordinateSystem(1.0); viewer->addCoordinateSystem(1000.0); viewer->initCameraParameters(); viewer->setCameraPosition(0, 0, -150, 0, 1, 0); return (viewer); } PCViewer::PCViewer() : viewer_(nullptr) { VLOG(2) << __func__; } PCViewer::~PCViewer() { VLOG(2) << __func__; if (viewer_) { // viewer_->saveCameraParameters("pcl_camera_params.txt"); viewer_->close(); viewer_ == nullptr; } } void PCViewer::Draw(const cv::Mat &xyz) { pcl::PointCloud::Ptr pc(new pcl::PointCloud); ConvertMatToPointCloud(xyz, pc); Draw(pc); } void PCViewer::Draw(pcl::PointCloud::ConstPtr pc) { if (viewer_ == nullptr) { viewer_ = CustomColorVis(pc); } viewer_->updatePointCloud(pc, "point cloud"); viewer_->spinOnce(); } bool PCViewer::WasDrew() const { return viewer_ != nullptr; } bool PCViewer::WasStopped() const { return viewer_ == nullptr || viewer_->wasStopped(); } void PCViewer::ConvertMatToPointCloud( const cv::Mat &xyz, pcl::PointCloud::Ptr pc) { // cv::Mat channels[3]; // cv::split(xyz, channels); // double min, max; // cv::minMaxLoc(channels[2], &min, &max); for (int i = 0; i < xyz.rows; i++) { for (int j = 0; j < xyz.cols; j++) { auto &&p = xyz.at(i, j); if (std::isfinite(p.x) && std::isfinite(p.y) && std::isfinite(p.z)) { // LOG(INFO) << "[" << i << "," << j << "] x: " << p.x << ", y: " << p.y // << ", z: " << p.z; pcl::PointXYZ point; point.x = p.x; point.y = p.y; point.z = p.z; // point.z = p.z - min; pc->points.push_back(point); } } } pc->width = static_cast(pc->points.size()); pc->height = 1; }