You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PCL Viewer中同时绘制点云曲线与样条曲线?

How to Display Point Cloud and Spline Curve Together in PCL Viewer

Hey there! Since PCL's Visualizer is built on top of VTK, you don't need separate windows for your point cloud and spline. You can directly inject your VTK-generated spline actor into the PCL Viewer's rendering pipeline. Here's a modified version of your code that combines both visualizations:

#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <vtkPoints.h>
#include <vtkParametricSpline.h>
#include <vtkParametricFunctionSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkInteractorStyleSwitch.h>

int main() {
    // Load the point cloud
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_curve(new pcl::PointCloud<pcl::PointXYZ>);
    if (pcl::io::loadPCDFile<pcl::PointXYZ>("line.pcd", *cloud_curve) == -1) {
        PCL_ERROR("Couldn't read the pcd file.\n");
        exit(1);
    }

    // Initialize PCL Viewer
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("Point Cloud + Spline Viewer"));
    viewer->setBackgroundColor(0, 0, 0);
    
    // Add point cloud to viewer
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> handler(cloud_curve, 255, 0, 0);
    viewer->addPointCloud(cloud_curve, handler, "cloud");

    // --------------------------
    // VTK Spline Creation Logic
    // --------------------------
    double p0[3] = {-5.91152, 4.65316, 28063.7};
    double p1[3] = {-6.2126, 3.8805, 28034.9};
    double p2[3] = {-6.54915, 4.6517, 28007};

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(p0);
    points->InsertNextPoint(p1);
    points->InsertNextPoint(p2);

    vtkSmartPointer<vtkParametricSpline> spline = vtkSmartPointer<vtkParametricSpline>::New();
    spline->SetPoints(points);

    vtkSmartPointer<vtkParametricFunctionSource> functionSource = vtkSmartPointer<vtkParametricFunctionSource>::New();
    functionSource->SetParametricFunction(spline);
    functionSource->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(functionSource->GetOutputPort());

    vtkSmartPointer<vtkActor> splineActor = vtkSmartPointer<vtkActor>::New();
    splineActor->SetMapper(mapper);
    splineActor->GetProperty()->SetColor(0, 255, 0); // Green spline to contrast red point cloud
    splineActor->GetProperty()->SetLineWidth(2); // Thicker line for better visibility

    // --------------------------
    // Add VTK Actor to PCL Viewer
    // --------------------------
    // Access PCL Viewer's underlying VTK renderer
    vtkRenderer* renderer = viewer->getRendererCollection()->GetFirstRenderer();
    renderer->AddActor(splineActor);

    // Set up camera and interactor style
    viewer->initCameraParameters();
    
    // Optional: Match VTK's trackball camera navigation style
    vtkInteractorStyleSwitch* style = vtkInteractorStyleSwitch::New();
    style->SetCurrentStyleToTrackballCamera();
    viewer->getRenderWindow()->GetInteractor()->SetInteractorStyle(style);
    style->Delete(); // Clean up unmanaged VTK object

    // Start rendering loop
    while (!viewer->wasStopped()) {
        viewer->spinOnce(100);
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }

    return 0;
}

Key Details to Note:

  • PCL-VTK Interoperability: PCL Visualizer relies on VTK internally, so we can grab its vtkRenderer and add any VTK actor directly to it—no need to maintain separate windows.
  • Visibility Tweaks: I adjusted the spline color and line width to make it easy to distinguish from the point cloud; feel free to tweak these values to your preference.
  • Interactor Consistency: The optional trackball camera style ensures navigation behaves the same way as your standalone VTK window, making the experience more familiar.
  • Memory Management: Be sure to clean up VTK objects that aren't handled by smart pointers (like the interactor style here) to avoid memory leaks.

内容的提问来源于stack exchange,提问作者GStav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:02:37