add pcl
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
#ifndef COSTMAP_2D_DATA_CONVERT_H
|
||||
#define COSTMAP_2D_DATA_CONVERT_H
|
||||
#ifndef ROBOT_DATA_CONVERT_H
|
||||
#define ROBOT_DATA_CONVERT_H
|
||||
|
||||
#include <tf3/utils.h>
|
||||
#include <tf3/compat.h>
|
||||
#include <robot/time.h>
|
||||
|
||||
#include <robot_geometry_msgs/TransformStamped.h>
|
||||
#include <robot_geometry_msgs/Quaternion.h>
|
||||
#include <robot_geometry_msgs/Pose.h>
|
||||
#include <robot_geometry_msgs/PoseStamped.h>
|
||||
#include <tf3/utils.h>
|
||||
#include <tf3/compat.h>
|
||||
#include <robot/time.h>
|
||||
#include <robot_sensor_msgs/PointCloud2.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <pcl/point_cloud.h>
|
||||
#include <pcl/point_types.h>
|
||||
|
||||
namespace data_convert
|
||||
{
|
||||
@@ -191,4 +196,4 @@ namespace data_convert
|
||||
}
|
||||
}
|
||||
|
||||
#endif // DATA_CONVERT_H
|
||||
#endif // ROBOT_DATA_CONVERT_H
|
||||
374
include/data_convert/pcl_convert.h
Normal file
374
include/data_convert/pcl_convert.h
Normal file
@@ -0,0 +1,374 @@
|
||||
/*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright (c) 2013, Open Source Robotics Foundation, Inc.
|
||||
* Copyright (c) 2010-2012, Willow Garage, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimer in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
* * Neither the name of Open Source Robotics Foundation, Inc. nor
|
||||
* the names of its contributors may be used to endorse or promote
|
||||
* products derived from this software without specific prior
|
||||
* written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef ROBOT_PCL_CONVERSIONS_H__
|
||||
#define ROBOT_PCL_CONVERSIONS_H__
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <robot/robot.h>
|
||||
|
||||
#include <pcl/conversions.h>
|
||||
|
||||
#include <pcl/PCLHeader.h>
|
||||
#include <robot_std_msgs/Header.h>
|
||||
|
||||
#include <pcl/PCLImage.h>
|
||||
#include <robot_sensor_msgs/Image.h>
|
||||
|
||||
#include <pcl/PCLPointField.h>
|
||||
#include <robot_sensor_msgs/PointField.h>
|
||||
|
||||
#include <pcl/PCLPointCloud2.h>
|
||||
#include <robot_sensor_msgs/PointCloud2.h>
|
||||
|
||||
|
||||
namespace robot_pcl_conversions {
|
||||
|
||||
/** PCLHeader <=> Header **/
|
||||
|
||||
inline
|
||||
void fromPCL(const std::uint64_t &pcl_stamp, robot::Time &stamp)
|
||||
{
|
||||
stamp.fromNSec(pcl_stamp * 1000ull); // Convert from us to ns
|
||||
}
|
||||
|
||||
inline
|
||||
void toPCL(const robot::Time &stamp, std::uint64_t &pcl_stamp)
|
||||
{
|
||||
pcl_stamp = stamp.toNSec() / 1000ull; // Convert from ns to us
|
||||
}
|
||||
|
||||
inline
|
||||
robot::Time fromPCL(const std::uint64_t &pcl_stamp)
|
||||
{
|
||||
robot::Time stamp;
|
||||
fromPCL(pcl_stamp, stamp);
|
||||
return stamp;
|
||||
}
|
||||
|
||||
inline
|
||||
std::uint64_t toPCL(const robot::Time &stamp)
|
||||
{
|
||||
std::uint64_t pcl_stamp;
|
||||
toPCL(stamp, pcl_stamp);
|
||||
return pcl_stamp;
|
||||
}
|
||||
|
||||
/** PCLHeader <=> Header **/
|
||||
|
||||
inline
|
||||
void fromPCL(const pcl::PCLHeader &pcl_header, robot_std_msgs::Header &header)
|
||||
{
|
||||
fromPCL(pcl_header.stamp, header.stamp);
|
||||
header.seq = pcl_header.seq;
|
||||
header.frame_id = pcl_header.frame_id;
|
||||
}
|
||||
|
||||
inline
|
||||
void toPCL(const robot_std_msgs::Header &header, pcl::PCLHeader &pcl_header)
|
||||
{
|
||||
toPCL(header.stamp, pcl_header.stamp);
|
||||
pcl_header.seq = header.seq;
|
||||
pcl_header.frame_id = header.frame_id;
|
||||
}
|
||||
|
||||
inline
|
||||
robot_std_msgs::Header fromPCL(const pcl::PCLHeader &pcl_header)
|
||||
{
|
||||
robot_std_msgs::Header header;
|
||||
fromPCL(pcl_header, header);
|
||||
return header;
|
||||
}
|
||||
|
||||
inline
|
||||
pcl::PCLHeader toPCL(const robot_std_msgs::Header &header)
|
||||
{
|
||||
pcl::PCLHeader pcl_header;
|
||||
toPCL(header, pcl_header);
|
||||
return pcl_header;
|
||||
}
|
||||
|
||||
/** PCLImage <=> Image **/
|
||||
|
||||
inline
|
||||
void copyPCLImageMetaData(const pcl::PCLImage &pcl_image, robot_sensor_msgs::Image &image)
|
||||
{
|
||||
fromPCL(pcl_image.header, image.header);
|
||||
image.height = pcl_image.height;
|
||||
image.width = pcl_image.width;
|
||||
image.encoding = pcl_image.encoding;
|
||||
image.is_bigendian = pcl_image.is_bigendian;
|
||||
image.step = pcl_image.step;
|
||||
}
|
||||
|
||||
inline
|
||||
void fromPCL(const pcl::PCLImage &pcl_image, robot_sensor_msgs::Image &image)
|
||||
{
|
||||
copyPCLImageMetaData(pcl_image, image);
|
||||
image.data = pcl_image.data;
|
||||
}
|
||||
|
||||
inline
|
||||
void moveFromPCL(pcl::PCLImage &pcl_image, robot_sensor_msgs::Image &image)
|
||||
{
|
||||
copyPCLImageMetaData(pcl_image, image);
|
||||
image.data.swap(pcl_image.data);
|
||||
}
|
||||
|
||||
inline
|
||||
void copyImageMetaData(const robot_sensor_msgs::Image &image, pcl::PCLImage &pcl_image)
|
||||
{
|
||||
toPCL(image.header, pcl_image.header);
|
||||
pcl_image.height = image.height;
|
||||
pcl_image.width = image.width;
|
||||
pcl_image.encoding = image.encoding;
|
||||
pcl_image.is_bigendian = image.is_bigendian;
|
||||
pcl_image.step = image.step;
|
||||
}
|
||||
|
||||
inline
|
||||
void toPCL(const robot_sensor_msgs::Image &image, pcl::PCLImage &pcl_image)
|
||||
{
|
||||
copyImageMetaData(image, pcl_image);
|
||||
pcl_image.data = image.data;
|
||||
}
|
||||
|
||||
inline
|
||||
void moveToPCL(robot_sensor_msgs::Image &image, pcl::PCLImage &pcl_image)
|
||||
{
|
||||
copyImageMetaData(image, pcl_image);
|
||||
pcl_image.data.swap(image.data);
|
||||
}
|
||||
|
||||
/** PCLPointField <=> PointField **/
|
||||
|
||||
inline
|
||||
void fromPCL(const pcl::PCLPointField &pcl_pf, robot_sensor_msgs::PointField &pf)
|
||||
{
|
||||
pf.name = pcl_pf.name;
|
||||
pf.offset = pcl_pf.offset;
|
||||
pf.datatype = pcl_pf.datatype;
|
||||
pf.count = pcl_pf.count;
|
||||
}
|
||||
|
||||
inline
|
||||
void fromPCL(const std::vector<pcl::PCLPointField> &pcl_pfs, std::vector<robot_sensor_msgs::PointField> &pfs)
|
||||
{
|
||||
pfs.resize(pcl_pfs.size());
|
||||
std::vector<pcl::PCLPointField>::const_iterator it = pcl_pfs.begin();
|
||||
int i = 0;
|
||||
for(; it != pcl_pfs.end(); ++it, ++i) {
|
||||
fromPCL(*(it), pfs[i]);
|
||||
}
|
||||
std::sort(pfs.begin(), pfs.end(), [](const auto& field_a, const auto& field_b)
|
||||
{
|
||||
return field_a.offset < field_b.offset;
|
||||
});
|
||||
}
|
||||
|
||||
inline
|
||||
void toPCL(const robot_sensor_msgs::PointField &pf, pcl::PCLPointField &pcl_pf)
|
||||
{
|
||||
pcl_pf.name = pf.name;
|
||||
pcl_pf.offset = pf.offset;
|
||||
pcl_pf.datatype = pf.datatype;
|
||||
pcl_pf.count = pf.count;
|
||||
}
|
||||
|
||||
inline
|
||||
void toPCL(const std::vector<robot_sensor_msgs::PointField> &pfs, std::vector<pcl::PCLPointField> &pcl_pfs)
|
||||
{
|
||||
pcl_pfs.resize(pfs.size());
|
||||
std::vector<robot_sensor_msgs::PointField>::const_iterator it = pfs.begin();
|
||||
int i = 0;
|
||||
for(; it != pfs.end(); ++it, ++i) {
|
||||
toPCL(*(it), pcl_pfs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/** PCLPointCloud2 <=> PointCloud2 **/
|
||||
|
||||
inline
|
||||
void copyPCLPointCloud2MetaData(const pcl::PCLPointCloud2 &pcl_pc2, robot_sensor_msgs::PointCloud2 &pc2)
|
||||
{
|
||||
fromPCL(pcl_pc2.header, pc2.header);
|
||||
pc2.height = pcl_pc2.height;
|
||||
pc2.width = pcl_pc2.width;
|
||||
fromPCL(pcl_pc2.fields, pc2.fields);
|
||||
pc2.is_bigendian = pcl_pc2.is_bigendian;
|
||||
pc2.point_step = pcl_pc2.point_step;
|
||||
pc2.row_step = pcl_pc2.row_step;
|
||||
pc2.is_dense = pcl_pc2.is_dense;
|
||||
}
|
||||
|
||||
inline
|
||||
void fromPCL(const pcl::PCLPointCloud2 &pcl_pc2, robot_sensor_msgs::PointCloud2 &pc2)
|
||||
{
|
||||
copyPCLPointCloud2MetaData(pcl_pc2, pc2);
|
||||
pc2.data = pcl_pc2.data;
|
||||
}
|
||||
|
||||
inline
|
||||
void moveFromPCL(pcl::PCLPointCloud2 &pcl_pc2, robot_sensor_msgs::PointCloud2 &pc2)
|
||||
{
|
||||
copyPCLPointCloud2MetaData(pcl_pc2, pc2);
|
||||
pc2.data.swap(pcl_pc2.data);
|
||||
}
|
||||
|
||||
inline
|
||||
void copyPointCloud2MetaData(const robot_sensor_msgs::PointCloud2 &pc2, pcl::PCLPointCloud2 &pcl_pc2)
|
||||
{
|
||||
toPCL(pc2.header, pcl_pc2.header);
|
||||
pcl_pc2.height = pc2.height;
|
||||
pcl_pc2.width = pc2.width;
|
||||
toPCL(pc2.fields, pcl_pc2.fields);
|
||||
pcl_pc2.is_bigendian = pc2.is_bigendian;
|
||||
pcl_pc2.point_step = pc2.point_step;
|
||||
pcl_pc2.row_step = pc2.row_step;
|
||||
pcl_pc2.is_dense = pc2.is_dense;
|
||||
}
|
||||
|
||||
inline
|
||||
void toPCL(const robot_sensor_msgs::PointCloud2 &pc2, pcl::PCLPointCloud2 &pcl_pc2)
|
||||
{
|
||||
copyPointCloud2MetaData(pc2, pcl_pc2);
|
||||
pcl_pc2.data = pc2.data;
|
||||
}
|
||||
|
||||
inline
|
||||
void moveToPCL(robot_sensor_msgs::PointCloud2 &pc2, pcl::PCLPointCloud2 &pcl_pc2)
|
||||
{
|
||||
copyPointCloud2MetaData(pc2, pcl_pc2);
|
||||
pcl_pc2.data.swap(pc2.data);
|
||||
}
|
||||
|
||||
} // namespace robot_pcl_conversions
|
||||
|
||||
namespace pcl {
|
||||
|
||||
/** Provide pcl::toROBOTMsg **/
|
||||
|
||||
inline
|
||||
void toROBOTMsg(const robot_sensor_msgs::PointCloud2 &cloud, robot_sensor_msgs::Image &image)
|
||||
{
|
||||
pcl::PCLPointCloud2 pcl_cloud;
|
||||
robot_pcl_conversions::toPCL(cloud, pcl_cloud);
|
||||
pcl::PCLImage pcl_image;
|
||||
pcl::toPCLPointCloud2(pcl_cloud, pcl_image);
|
||||
robot_pcl_conversions::moveFromPCL(pcl_image, image);
|
||||
}
|
||||
|
||||
inline
|
||||
void moveToROBOTMsg(robot_sensor_msgs::PointCloud2 &cloud, robot_sensor_msgs::Image &image)
|
||||
{
|
||||
pcl::PCLPointCloud2 pcl_cloud;
|
||||
robot_pcl_conversions::moveToPCL(cloud, pcl_cloud);
|
||||
pcl::PCLImage pcl_image;
|
||||
pcl::toPCLPointCloud2(pcl_cloud, pcl_image);
|
||||
robot_pcl_conversions::moveFromPCL(pcl_image, image);
|
||||
}
|
||||
|
||||
template<typename T> void
|
||||
toROBOTMsg (const pcl::PointCloud<T> &cloud, robot_sensor_msgs::Image& msg)
|
||||
{
|
||||
// Ease the user's burden on specifying width/height for unorganized datasets
|
||||
if (cloud.width == 0 && cloud.height == 0)
|
||||
{
|
||||
throw std::runtime_error("Needs to be a dense like cloud!!");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cloud.points.size () != cloud.width * cloud.height)
|
||||
throw std::runtime_error("The width and height do not match the cloud size!");
|
||||
msg.height = cloud.height;
|
||||
msg.width = cloud.width;
|
||||
}
|
||||
|
||||
// robot_sensor_msgs::image_encodings::BGR8;
|
||||
msg.encoding = "bgr8";
|
||||
msg.step = msg.width * sizeof (std::uint8_t) * 3;
|
||||
msg.data.resize (msg.step * msg.height);
|
||||
for (size_t y = 0; y < cloud.height; y++)
|
||||
{
|
||||
for (size_t x = 0; x < cloud.width; x++)
|
||||
{
|
||||
std::uint8_t * pixel = &(msg.data[y * msg.step + x * 3]);
|
||||
memcpy (pixel, &cloud (x, y).rgb, 3 * sizeof(std::uint8_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Provide to/fromROBOTMsg for robot_sensor_msgs::PointCloud2 <=> pcl::PointCloud<T> **/
|
||||
|
||||
template<typename T>
|
||||
void toROBOTMsg(const pcl::PointCloud<T> &pcl_cloud, robot_sensor_msgs::PointCloud2 &cloud)
|
||||
{
|
||||
pcl::PCLPointCloud2 pcl_pc2;
|
||||
#if PCL_VERSION_COMPARE(>=, 1, 14, 1)
|
||||
// if PCL version is recent enough, request that all padding be removed to make the msg as small as possible
|
||||
pcl::toPCLPointCloud2(pcl_cloud, pcl_pc2, false);
|
||||
#else
|
||||
pcl::toPCLPointCloud2(pcl_cloud, pcl_pc2);
|
||||
#endif
|
||||
robot_pcl_conversions::moveFromPCL(pcl_pc2, cloud);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void fromROBOTMsg(const robot_sensor_msgs::PointCloud2 &cloud, pcl::PointCloud<T> &pcl_cloud)
|
||||
{
|
||||
pcl::PCLPointCloud2 pcl_pc2;
|
||||
#if PCL_VERSION_COMPARE(>=, 1, 13, 1)
|
||||
robot_pcl_conversions::copyPointCloud2MetaData(cloud, pcl_pc2); // Like robot_pcl_conversions::toPCL, but does not copy the binary data
|
||||
pcl::MsgFieldMap field_map;
|
||||
pcl::createMapping<T> (pcl_pc2.fields, field_map);
|
||||
pcl::fromPCLPointCloud2(pcl_pc2, pcl_cloud, field_map, &cloud.data[0]);
|
||||
#else
|
||||
robot_pcl_conversions::toPCL(cloud, pcl_pc2);
|
||||
pcl::fromPCLPointCloud2(pcl_pc2, pcl_cloud);
|
||||
#endif
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void moveFromROBOTMsg(robot_sensor_msgs::PointCloud2 &cloud, pcl::PointCloud<T> &pcl_cloud)
|
||||
{
|
||||
pcl::PCLPointCloud2 pcl_pc2;
|
||||
robot_pcl_conversions::moveToPCL(cloud, pcl_pc2);
|
||||
pcl::fromPCLPointCloud2(pcl_pc2, pcl_cloud);
|
||||
}
|
||||
} // namespace pcl
|
||||
|
||||
#endif /* PCL_CONVERSIONS_H__ */
|
||||
Reference in New Issue
Block a user