使用 C++ 从 URL 中读取文件而不是使用点云库读取本地文件

Read files from URL in C++ instead of local files using Point Cloud Library

我是点云库 (PCL) 的新手,对指针如何工作的 C++ 知识有限。虽然我们可以从文件中加载文件并将其可视化(使用 this 教程),但我们如何从 HTTP URL?

int main () {
        pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
        pcl::io::loadPCDFile ("my_point_cloud.pcd", *cloud);

    pcl::visualization::CloudViewer viewer("Cloud Viewer");

    //blocks until the cloud is actually rendered
    viewer.showCloud(cloud);

    //use the following functions to get access to the underlying more advanced/powerful
    //PCLVisualizer

    while (!viewer.wasStopped ())
    {
    }
    return 0;
}

我不知道 PCL 是否直接执行此操作,但您可以将 cpr or urdl C++ libraries either to download the file 用于本地临时文件,或者实际在流上工作。

Urdl 示例:

// For urdl::url.
#include <urdl/url.hpp>

// etc...    

urdl::url url("http://somehost/path");
urdl::istream is("http://somehost/path");

并且这个 istream 可以直接使用(如果 PCL 支持),或者你可以 write the data on the stream to a file.

使用 cpr 的示例程序(a.k.a。C++ 请求;基于 C 库 libcurl):

#include <cpr/cpr.h>

int main(int argc, char** argv) {
    auto r = cpr::Get(cpr::Url{"https://api.github.com/repos/whoshuu/cpr/contributors"},
                      cpr::Authentication{"user", "pass"},
                      cpr::Parameters{{"anon", "true"}, {"key", "value"}});
    r.status_code;                  // 200
    r.header["content-type"];       // application/json; charset=utf-8
    r.text;                         // JSON text string
}

(摘自cpr官网)