下面是一个简单的读取PCD文件并显示的代码:

#include

#include

#include

#include

#include

void main()

{

/* Create Point Cloud */

pcl::PointCloud<:pointxyzrgba>::Ptr cloud(new pcl::PointCloud<:pointxyzrgba>);

/* Read PCD File */

/* Read Wrong */

if (- == pcl::io::loadPCDFile<:pointxyzrgba>("table_scene_lms400.pcd", *cloud))

{

return;

}

/* Then show the point cloud */

boost::shared_ptr<:visualization::pclvisualizer> viewer(new pcl::visualization::PCLVisualizer("office chair model"));

viewer->setBackgroundColor(, , );

pcl::visualization::PointCloudColorHandlerRGBField<:pointxyzrgba> rgba(cloud); //Maybe set the cloud to he handler rgba?

viewer->addPointCloud<:pointxyzrgba>(cloud, rgba, "sample cloud"); //Add a Point Cloud (templated) to screen. Q:Some questions here

viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, , "sample cloud"); //Set the rendering properties

//viewer->addCoordinateSystem(1.0); //Adds 3D axes describing a coordinate system to screen at 0,0,0

viewer->initCameraParameters (); //Initialize camera parameters with some default values.

/* Show the point cloud */

while (!viewer->wasStopped ())

{

viewer->spinOnce (); //updates the screen loop

boost::this_thread::sleep (boost::posix_time::microseconds ());

}

}

pcl::io::loadPCDFile用于读取一个PCD文件至一个PointCloud类型,这里就是将table_scene_lms400.pcd文件里的数据读取至cloud里。

在PCL文档里关于pcl::io::loadPCDFile的实现有3个,我目前只看了第一种。

下面看看loadPCDFile在namespace io里的实现:

template inline int

loadPCDFile (const std::string &file_name, pcl::PointCloud &cloud)

{

pcl::PCDReader p;

return (p.read (file_name, cloud));

}

可以看到loadPCDFile 这个内联函数,就是调用了一下pcl::PCDReader里的read函数。

继续看PCDReader函数:

template int

read (const std::string &file_name, pcl::PointCloud &cloud, const int offset = 0)

{

pcl::PCLPointCloud2 blob;

int pcd_version;

int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_, pcd_version, offset);

// If no error, convert the data

if (res == 0)

pcl::fromPCLPointCloud2 (blob, cloud);

return (res);

}

最后在pdc_io.cpp里找到代码:

int

pcl::PCDReader::read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,

Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version,

const int offset)

{

pcl::console::TicToc tt;

tt.tic ();

int data_type;

unsigned int data_idx;

int res = readHeader (file_name, cloud, origin, orientation, pcd_version, data_type, data_idx, offset);

if (res < )

return (res);

unsigned int idx = ;

// Get the number of points the cloud should have

unsigned int nr_points = cloud.width * cloud.height;

// Setting the is_dense property to true by default

cloud.is_dense = true;

if (file_name == "" || !boost::filesystem::exists (file_name))

{

PCL_ERROR ("[pcl::PCDReader::read] Could not find file '%s'.\n", file_name.c_str ());

return (-);

}

// if ascii

if (data_type == )

{

// Re-open the file (readHeader closes it)

std::ifstream fs;

fs.open (file_name.c_str ());

if (!fs.is_open () || fs.fail ())

{

PCL_ERROR ("[pcl::PCDReader::read] Could not open file %s.\n", file_name.c_str ());

return (-);

}

fs.seekg (data_idx);

std::string line;

std::vector<:string> st;

// Read the rest of the file

try

{

while (idx < nr_points && !fs.eof ())

{

getline (fs, line);

// Ignore empty lines

if (line == "")

continue;

// Tokenize the line

boost::trim (line);

boost::split (st, line, boost::is_any_of ("\t\r "), boost::token_compress_on);

if (idx >= nr_points)

{

PCL_WARN ("[pcl::PCDReader::read] input file %s has more points (%d) than advertised (%d)!\n", file_name.c_str (), idx, nr_points);

break;

}

size_t total = ;

// Copy data

for (unsigned int d = ; d < static_cast (cloud.fields.size ()); ++d)

{

// Ignore invalid padded dimensions that are inherited from binary data

if (cloud.fields[d].name == "_")

{

total += cloud.fields[d].count; // jump over this many elements in the string token

continue;

}

for (unsigned int c = ; c < cloud.fields[d].count; ++c)

{

switch (cloud.fields[d].datatype)

{

case pcl::PCLPointField::INT8:

{

copyStringValue<:traits::astype>::type> (

st.at (total + c), cloud, idx, d, c);

break;

}

case pcl::PCLPointField::UINT8:

{

copyStringValue<:traits::astype>::type> (

st.at (total + c), cloud, idx, d, c);

break;

}

case pcl::PCLPointField::INT16:

{

copyStringValue<:traits::astype>::type> (

st.at (total + c), cloud, idx, d, c);

break;

}

case pcl::PCLPointField::UINT16:

{

copyStringValue<:traits::astype>::type> (

st.at (total + c), cloud, idx, d, c);

break;

}

case pcl::PCLPointField::INT32:

{

copyStringValue<:traits::astype>::type> (

st.at (total + c), cloud, idx, d, c);

break;

}

case pcl::PCLPointField::UINT32:

{

copyStringValue<:traits::astype>::type> (

st.at (total + c), cloud, idx, d, c);

break;

}

case pcl::PCLPointField::FLOAT32:

{

copyStringValue<:traits::astype>::type> (

st.at (total + c), cloud, idx, d, c);

break;

}

case pcl::PCLPointField::FLOAT64:

{

copyStringValue<:traits::astype>::type> (

st.at (total + c), cloud, idx, d, c);

break;

}

default:

PCL_WARN ("[pcl::PCDReader::read] Incorrect field data type specified (%d)!\n",cloud.fields[d].datatype);

break;

}

}

total += cloud.fields[d].count; // jump over this many elements in the string token

}

idx++;

}

}

catch (const char *exception)

{

PCL_ERROR ("[pcl::PCDReader::read] %s\n", exception);

fs.close ();

return (-);

}

// Close file

fs.close ();

}

else

/// ---[ Binary mode only

/// We must re-open the file and read with mmap () for binary

{

// Open for reading

int fd = pcl_open (file_name.c_str (), O_RDONLY);

if (fd == -)

{

PCL_ERROR ("[pcl::PCDReader::read] Failure to open file %s\n", file_name.c_str () );

return (-);

}

// Seek at the given offset

off_t result = pcl_lseek (fd, offset, SEEK_SET);

if (result < )

{

pcl_close (fd);

PCL_ERROR ("[pcl::PCDReader::read] lseek errno: %d strerror: %s\n", errno, strerror (errno));

PCL_ERROR ("[pcl::PCDReader::read] Error during lseek ()!\n");

return (-);

}

size_t data_size = data_idx + cloud.data.size ();

// Prepare the map

#ifdef _WIN32

// As we don't know the real size of data (compressed or not),

// we set dwMaximumSizeHigh = dwMaximumSizeLow = 0 so as to map the whole file

HANDLE fm = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL, PAGE_READONLY, , , NULL);

// As we don't know the real size of data (compressed or not),

// we set dwNumberOfBytesToMap = 0 so as to map the whole file

char *map = static_cast(MapViewOfFile (fm, FILE_MAP_READ, , , ));

if (map == NULL)

{

CloseHandle (fm);

pcl_close (fd);

PCL_ERROR ("[pcl::PCDReader::read] Error mapping view of file, %s\n", file_name.c_str ());

return (-);

}

#else

char *map = static_cast (mmap (, data_size, PROT_READ, MAP_SHARED, fd, ));

if (map == reinterpret_cast (-)) // MAP_FAILED

{

pcl_close (fd);

PCL_ERROR ("[pcl::PCDReader::read] Error preparing mmap for binary PCD file.\n");

return (-);

}

#endif

/// ---[ Binary compressed mode only

if (data_type == )

{

// Uncompress the data first

unsigned int compressed_size, uncompressed_size;

memcpy (&compressed_size, &map[data_idx + ], sizeof (unsigned int));

memcpy (&uncompressed_size, &map[data_idx + ], sizeof (unsigned int));

PCL_DEBUG ("[pcl::PCDReader::read] Read a binary compressed file with %u bytes compressed and %u original.\n", compressed_size, uncompressed_size);

// For all those weird situations where the compressed data is actually LARGER than the uncompressed one

// (we really ought to check this in the compressor and copy the original data in those cases)

if (data_size < compressed_size || uncompressed_size < compressed_size)

{

PCL_DEBUG ("[pcl::PCDReader::read] Allocated data size (%zu) or uncompressed size (%zu) smaller than compressed size (%u). Need to remap.\n", data_size, uncompressed_size, compressed_size);

#ifdef _WIN32

UnmapViewOfFile (map);

data_size = compressed_size + data_idx + ;

map = static_cast(MapViewOfFile (fm, FILE_MAP_READ, , , data_size));

#else

munmap (map, data_size);

data_size = compressed_size + data_idx + ;

map = static_cast (mmap (, data_size, PROT_READ, MAP_SHARED, fd, ));

#endif

}

if (uncompressed_size != cloud.data.size ())

{

PCL_WARN ("[pcl::PCDReader::read] The estimated cloud.data size (%u) is different than the saved uncompressed value (%u)! Data corruption?\n",

cloud.data.size (), uncompressed_size);

cloud.data.resize (uncompressed_size);

}

char *buf = static_cast (malloc (data_size));

// The size of the uncompressed data better be the same as what we stored in the header

unsigned int tmp_size = pcl::lzfDecompress (&map[data_idx + ], compressed_size, buf, static_cast (data_size));

if (tmp_size != uncompressed_size)

{

free (buf);

pcl_close (fd);

PCL_ERROR ("[pcl::PCDReader::read] Size of decompressed lzf data (%u) does not match value stored in PCD header (%u). Errno: %d\n", tmp_size, uncompressed_size, errno);

return (-);

}

// Get the fields sizes

std::vector<:pclpointfield> fields (cloud.fields.size ());

std::vector fields_sizes (cloud.fields.size ());

int nri = , fsize = ;

for (size_t i = ; i < cloud.fields.size (); ++i)

{

if (cloud.fields[i].name == "_")

continue;

fields_sizes[nri] = cloud.fields[i].count * pcl::getFieldSize (cloud.fields[i].datatype);

fsize += fields_sizes[nri];

fields[nri] = cloud.fields[i];

++nri;

}

fields.resize (nri);

fields_sizes.resize (nri);

// Unpack the xxyyzz to xyz

std::vector pters (fields.size ());

int toff = ;

for (size_t i = ; i < pters.size (); ++i)

{

pters[i] = &buf[toff];

toff += fields_sizes[i] * cloud.width * cloud.height;

}

// Copy it to the cloud

for (size_t i = ; i < cloud.width * cloud.height; ++i)

{

for (size_t j = ; j < pters.size (); ++j)

{

memcpy (&cloud.data[i * fsize + fields[j].offset], pters[j], fields_sizes[j]);

// Increment the pointer

pters[j] += fields_sizes[j];

}

}

//memcpy (&cloud.data[0], &buf[0], uncompressed_size);

free (buf);

}

else

// Copy the data

memcpy (&cloud.data[], &map[] + data_idx, cloud.data.size ());

// Unmap the pages of memory

#ifdef _WIN32

UnmapViewOfFile (map);

CloseHandle (fm);

#else

if (munmap (map, data_size) == -)

{

pcl_close (fd);

PCL_ERROR ("[pcl::PCDReader::read] Munmap failure\n");

return (-);

}

#endif

pcl_close (fd);

}

if ((idx != nr_points) && (data_type == ))

{

PCL_ERROR ("[pcl::PCDReader::read] Number of points read (%d) is different than expected (%d)\n", idx, nr_points);

return (-);

}

// No need to do any extra checks if the data type is ASCII

if (data_type != )

{

int point_size = static_cast (cloud.data.size () / (cloud.height * cloud.width));

// Once copied, we need to go over each field and check if it has NaN/Inf values and assign cloud.is_dense to true or false

for (uint32_t i = ; i < cloud.width * cloud.height; ++i)

{

for (unsigned int d = ; d < static_cast (cloud.fields.size ()); ++d)

{

for (uint32_t c = ; c < cloud.fields[d].count; ++c)

{

switch (cloud.fields[d].datatype)

{

case pcl::PCLPointField::INT8:

{

if (!isValueFinite<:traits::astype>::type>(cloud, i, point_size, d, c))

cloud.is_dense = false;

break;

}

case pcl::PCLPointField::UINT8:

{

if (!isValueFinite<:traits::astype>::type>(cloud, i, point_size, d, c))

cloud.is_dense = false;

break;

}

case pcl::PCLPointField::INT16:

{

if (!isValueFinite<:traits::astype>::type>(cloud, i, point_size, d, c))

cloud.is_dense = false;

break;

}

case pcl::PCLPointField::UINT16:

{

if (!isValueFinite<:traits::astype>::type>(cloud, i, point_size, d, c))

cloud.is_dense = false;

break;

}

case pcl::PCLPointField::INT32:

{

if (!isValueFinite<:traits::astype>::type>(cloud, i, point_size, d, c))

cloud.is_dense = false;

break;

}

case pcl::PCLPointField::UINT32:

{

if (!isValueFinite<:traits::astype>::type>(cloud, i, point_size, d, c))

cloud.is_dense = false;

break;

}

case pcl::PCLPointField::FLOAT32:

{

if (!isValueFinite<:traits::astype>::type>(cloud, i, point_size, d, c))

cloud.is_dense = false;

break;

}

case pcl::PCLPointField::FLOAT64:

{

if (!isValueFinite<:traits::astype>::type>(cloud, i, point_size, d, c))

cloud.is_dense = false;

break;

}

}

}

}

}

}

double total_time = tt.toc ();

PCL_DEBUG ("[pcl::PCDReader::read] Loaded %s as a %s cloud in %g ms with %d points. Available dimensions: %s.\n",

file_name.c_str (), cloud.is_dense ? "dense" : "non-dense", total_time,

cloud.width * cloud.height, pcl::getFieldsList (cloud).c_str ());

return ();

}

这里的大致流程就是:

1.读取PCD和Header;

2.Header里的data有ascii还是binary两种情况,根据其不同采取不同的方法读取剩余的内容;

3.binary数据的情况还需要对数据进行check;

这段代码的细节处理暂时先这样了,以后再看看为什么ascii和binary的处理不一样,有什么不一样。

PCD文件格式详解及在PCL下读取PCD文件

一.PCD简介 1.1 PCD版本 在点云库PCL 1.0发布之前,PCD文件格式就已经发展更新了许多版本.这些新旧不同的版本用PCD_Vx来编号(例如PCD_V5.PCD_V6和PCD_V7等),分 ...

PCL读取PCD文件的数据

1.pcd文件——rabbit.pcd 链接:https://pan.baidu.com/s/1v6mjPjwd7fIqUSjlIGTIGQ提取码:zspx 新建项目pcl rabbit.pcd 和p ...

PCL点云库中怎样读取指定的PCD文件,又一次命名,处理后保存到指定目录

我一直想把处理后的pcd文件重命名,然后放到指定的目录,尝试了好久最终做到了: 比方我想读取  "table_scene_lms400.pcd" 把它进行滤波处理,重命名为 &qu ...

从PCD文件写入和读取点云数据

(1)学习向PCD文件写入点云数据 建立工程文件ch2,然后新建write_pcd.cpp  CMakeLists.txt两个文件 write_pcd.cpp : #include

从PCD文件中读取点云数据

博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=84 在本小节我们学习如何从PCD文件中读取点云数据. 代码 章例1文件夹中, ...

Unity3D移动平台动态读取外部文件全解析

前言: 一直有个想法,就是把工作中遇到的坑通过自己的深挖,总结成一套相同问题的解决方案供各位同行拍砖探讨.眼瞅着2015年第一个工作日就要来到了,小匹夫也休息的差不多了,寻思着也该写点东西活动活动大脑 ...

PCD文件去除曲率的脚本

在写一个重建算法的时候需要用到点坐标和法向的数据文件,于是向利用pcl中的法向计算模块来生成法向.输出后法向文件中包含曲率信息,但是这是不需要的.于是自己写了一个python小脚本实现格式转换. #- ...

python读取caffemodel文件

caffemodel是二进制的protobuf文件,利用protobuf的python接口可以读取它,解析出需要的内容 不少算法都是用预训练模型在自己数据上微调,即加载"caffemodel ...

informatica读取FTP文件

以下为一个完整的informatica读取ftp文件,并导入到系统中. 第一步: 通过shell脚本下载压缩包文件 /server/infa_shared/crm_prod/shell/ftpFrom ...

随机推荐

那些年因为粗心导致的外链css无效

css文件三种引用的三种方式: 1.外链: 注:如果使用外链式绝对不可以忘记 re ...

MySQL数据库集群进行正确配置步骤

MySQL数据库集群进行正确配置步骤 2010-06-09 10:47 arrowcat 博客园 字号:T | T 我们今天是要和大家一起分享的是对MySQL数据库集群进行正确配置,我前两天在相关网站 ...

jquery利用event&period;which方法获取键盘输入值的代码

jquery利用event.which方法获取键盘输入值的代码,需要的朋友可以参考下. 实例 显示按了哪个键: $("input").keydown(function(event) ...

leetcode Search Insert Position Python

#Given a sorted array and a target value, return the index if the target is found. If #not, return t ...

OpenCV 开发环境环境搭建(win10&plus;vs2015&plus;opencv 3&period;0)

OpenCV 3.0 for windows(下载地址:http://opencv.org/): 本测试中,OpenCV安装目录:D:\Program Files\opencv,笔者操作系统为64位. ...

ASP&period;NET Web API消息处理管道:Self Host下的消息处理管道&lbrack;下篇&rsqb;

ASP.NET Web API消息处理管道:Self Host下的消息处理管道[下篇] 我们知道ASP.NET Web API借助于HttpSelfHostServer以Self Host模式寄宿于当 ...

笔记之monkey自定义脚本

自定义脚本的稳定性测试 常规MOnkey测试执行的是随机的事件流,但如果只是想让Monkey测试某个特定场景者时候就需要用到自定义脚本,Monkey支持执行用户自定义脚本的测试,用户之需要按照Monk ...

Cocos Creator下删除AnySDK步骤

1.删除 frameworks/runtime-src/Classes 下的 jsb_anysdk_basic_conversions.cpp manualanysdkbindings.cpp jsb ...

python---tornado初识(1)

# coding:utf8 # __author: Administrator # date: 2018/3/6 0006 # /usr/bin/env python import tornado.i ...

python读取pcd文件_(一)读取PCD文件相关推荐

  1. java并发读取相同的文件_高效读取大文件,再也不用担心 OOM 了!

    最近阿粉接到一个需求,需要从文件读取数据,然后经过业务处理之后存储到数据库中.这个需求,说实话不是很难,阿粉很快完成了第一个版本. 内存读取 第一个版本,阿粉采用内存读取的方式,所有的数据首先读读取到 ...

  2. java+读取source资源_如何从JavaJAR文件中读取资源文件?

    如何从JavaJAR文件中读取资源文件? 我试图从一个单独的JAR文件中访问一个XML文件,这个JAR是作为桌面应用程序运行的.我可以获得我需要的文件的URL,但是当我将它传递给FileReader( ...

  3. 大数据之-Hadoop之HDFS的API操作_定位读取文件_只读取某个block的内容_案例---大数据之hadoop工作笔记0065

    然后我们再来看看,如果我们的文件比较大,比如一个文件有10g,这个如果是个日志文件的话, 我们只读取最新的128M可以嘛? 因为我们只关心最新的日志对吧. 是可以的. 首先我们上传一个大点的文件,去h ...

  4. java 异步写文件_异步读取文件实例

    1.目的: 通过异步的方式,读取sdcard中的文件,并显示读取进度,最后将读取的文件显示在指定的位置 2.代码 1 packagecom.example.myfile;2 3 importjava. ...

  5. matlab读取 xdr文件_如何打开XDR文件?

    下载通用文件查看器(File Magic) 安装可选产品 - File Magic (Solvusoft) | EULA | Privacy Policy | Terms | Uninstall 步骤 ...

  6. python保存dat文件_将数据存入文件

    要常常喜乐,不住地祷告,凡事谢恩,因为这是神在基督耶稣里向你们所定的旨意.不要消灭圣灵的感动,不要藐视先知的讲论.但要凡事察验,善美的要持守,各样的恶事要禁戒不作.(1 THESSALONIANS 5 ...

  7. python怎么运行ipynb文件_如何运行.ipynb文件的图文讲解

    如何运行.ipynb文件的图文讲解 首先cmd下面输入: pip install jupyter notebook,安装慢的改下pip的源为国内的源 然后cmd中输入: jupyter noteboo ...

  8. python 图片对比文件夹_初学Python-找出文件夹下的所有图片

    这个命题,有2种含义. 1.找出指定某个文件夹目录下的图片 2.找出指定某个文件夹及其子文件夹下的图片 两者的区别在于,子文件夹的图片是否需要找出来.对应的处理方法也略微有所区别,下面具体讲解一下.找 ...

  9. cmd 将文件夹下文件剪切到另外一个文件_总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇...

    本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  10. linux 一切都是文件_一切都是文件

    linux 一切都是文件 这是给您一个技巧性的问题:以下哪些文件? 目录 Shell脚本 LibreOffice文件 串口 内核数据结构 内核调整参数 硬盘驱动器 隔断 逻辑卷(LVM) 印表机 插座 ...

最新文章

  1. shell编程面试必会30题
  2. 什么是动态语言(转)
  3. go的http服务报错accept4: too many open files
  4. python顺序表代码_python实现顺序表的简单代码
  5. VTK:相互作用之MoveAVertexUnstructuredGrid
  6. java mapstring_ object 遍历_ListMapString,Object使用Java代码遍历以获取String,Object的值...
  7. 使用MultipartFile实现文件上传_SpringMVC
  8. P2Psim分析笔记(5)-EventGenerator and Observer
  9. python绘制气象等值线图_利用Python插值绘制等值线图
  10. Mysql数据库drop表不用跑路,表空间传输助你恢复数据
  11. django2连接mysql_Django2.2 连接mySQL数据库
  12. 【异常】git提示Ask a project Owner or Maintainer to create a default branch
  13. 待忧伤开满山岗,等青春散场
  14. 分享几款UI设计师快速提升工作效率的辅助设计软件
  15. NLP - Gensim
  16. VSCODE无法使用unordered_map解决
  17. 2020牛客国庆集训派对day2 补题J
  18. ubuntu16.04开机自动挂载nas盘
  19. OSChina 周一乱弹 —— 济南源创会特刊
  20. 原子物理与原子核物理知识结构(含链接)

热门文章

  1. 卸载python2.7_98%的人这样卸载软件,真的卸载干净了吗?这才是正确的卸载方式...
  2. 服务器维护以后多久刷潮汐护符,魔兽世界怀旧服潮汐护符怎么获得
  3. Android Studio 按钮样式实现
  4. vba python 基金历史排名_“科技基金”万里挑一:近三年各阶段排名前10“科技基金”全在这...
  5. java 支付宝支付 demo_java开发支付宝支付详细流程_demo的运行
  6. 软件开发管理之:编码负责人及标准代码库机制(转)--有同样的想法
  7. python 操作word页眉表格_Python 如何对word文档(.docx)的页眉和页脚进行编辑?
  8. GO语言-panic和recover
  9. 携创教育:自考英语二相当于什么水平?可以不考吗?
  10. 腾讯掀起史上最大一轮管理干部裁撤:从月入2万+到领4千工资的中年们