假设我们有两个点云,一个叫做'cloud1',另一个叫做'cloud2'。我们可以使用 CGAL 的可视化功能来给其中一个点云上红色。

首先,我们需要包含 CGAL 的可视化头文件和命名空间:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Point_set_3.h>
#include <CGAL/IO/read_xyz_points.h>
#include <CGAL/IO/write_xyz_points.h>
#include <CGAL/visualization/Basic_viewer_qt.h>
#include <CGAL/Qt/manipulated_frame.h>
#include <CGAL/Qt/point_set_graphics_item.h>
#include <CGAL/Qt/Converter.h>

#include <QApplication>
#include <QMainWindow>
#include <QFileDialog>
#include <QMessageBox>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point;
typedef CGAL::Point_set_3<Point> Point_set;
typedef CGAL::Qt::Converter<Point> Converter;

然后,我们可以使用 CGAL 的读取函数来读取点云文件,并将点云添加到点集中:

// Read point clouds from files
std::string filename1 = "cloud1.xyz";
std::string filename2 = "cloud2.xyz";
Point_set cloud1, cloud2;
if (!CGAL::read_xyz_points(filename1, std::back_inserter(cloud1))) {
  std::cerr << "Error: cannot read file " << filename1 << std::endl;
  return -1;
}
if (!CGAL::read_xyz_points(filename2, std::back_inserter(cloud2))) {
  std::cerr << "Error: cannot read file " << filename2 << std::endl;
  return -1;
}

接下来,我们可以创建一个 CGAL 的 Qt 查看器,并将点集添加到查看器中:

// Create a Qt viewer
QApplication app(argc, argv);
QMainWindow mainWindow;
CGAL::Qt::Basic_viewer_qt viewer;

// Add point sets to the viewer
CGAL::Qt::Point_set_graphics_item<Point_set> item1(cloud1);
CGAL::Qt::Point_set_graphics_item<Point_set> item2(cloud2);
item1.setColor(Qt::red); // Set the color of cloud1 to red
viewer.items.push_back(&item1);
viewer.items.push_back(&item2);

// Set up the viewer
mainWindow.setCentralWidget(&viewer);
mainWindow.show();
viewer.setWindowTitle("CGAL Viewer");
viewer.show();

最后,我们可以运行应用程序并查看结果:

// Run the application
return app.exec();

完整代码如下:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Point_set_3.h>
#include <CGAL/IO/read_xyz_points.h>
#include <CGAL/IO/write_xyz_points.h>
#include <CGAL/visualization/Basic_viewer_qt.h>
#include <CGAL/Qt/manipulated_frame.h>
#include <CGAL/Qt/point_set_graphics_item.h>
#include <CGAL/Qt/Converter.h>

#include <QApplication>
#include <QMainWindow>
#include <QFileDialog>
#include <QMessageBox>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point;
typedef CGAL::Point_set_3<Point> Point_set;
typedef CGAL::Qt::Converter<Point> Converter;

int main(int argc, char** argv) {
  // Read point clouds from files
  std::string filename1 = "cloud1.xyz";
  std::string filename2 = "cloud2.xyz";
  Point_set cloud1, cloud2;
if (!CGAL::read_xyz_points(filename1, std::back_inserter(cloud1))) {
    std::cerr << "Error: cannot read file " << filename1 << std::endl;
    return -1;
  }
if (!CGAL::read_xyz_points(filename2, std::back_inserter(cloud2))) {
    std::cerr << "Error: cannot read file " << filename2 << std::endl;
    return -1;
  }

  // Create a Qt viewer
  QApplication app(argc, argv);
  QMainWindow mainWindow;
  CGAL::Qt::Basic_viewer_qt viewer;

  // Add point sets to the viewer
  CGAL::Qt::Point_set_graphics_item<Point_set> item1(cloud1);
  CGAL::Qt::Point_set_graphics_item<Point_set> item2(cloud2);
  item1.setColor(Qt::red); // Set the color of cloud1 to red
  viewer.items.push_back(&item1);
  viewer.items.push_back(&item2);

  // Set up the viewer
  mainWindow.setCentralWidget(&viewer);
  mainWindow.show();
  viewer.setWindowTitle("CGAL Viewer");
  viewer.show();

  // Run the application
  return app.exec();
}
CGAL 点云可视化:区分两种点云

原文地址: https://www.cveoy.top/t/topic/jW8b 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录