【问题标题】:Read files from URL in C++ instead of local files using Point Cloud Library使用点云库从 C++ 中的 URL 读取文件,而不是本地文件
【发布时间】:2017-07-16 06:41:58
【问题描述】:

我是点云库 (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;
}

【问题讨论】:

  • C++ 没有直接从 HTTP URL 打开文件的本地工具。您需要使用第三方库。

标签: c++ pointers networking point-cloud-library


【解决方案1】:

我不知道 PCL 是否直接执行此操作,但您可以使用 cprurdl C++ 库将 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 的示例程序(又名 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官网)

【讨论】:

  • 谢谢。这很好。但我需要做的可能是将它复制到内存中,一旦完成,将其可视化。 CURL 看起来不错,但如何将istream*cloud 连接起来?
  • @Ariana:用更好的东西更新了我的答案,包装了 libcurl。回答您的问题:希望 PCL 可以从 istream 中获取输入;否则,可能来自 C 字符串(然后您可以在 cpr 示例中使用类似 r.text.begin() 的东西)。如果没有,请将数据保存到文件并使用与示例相同的加载函数。
猜你喜欢
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多