Skip to content

Commit 1d39403

Browse files
authored
Merge pull request PointCloudLibrary#5608 from mvieth/fromPCLPointCloud2_overload
Add overload to fromPCLPointCloud2 to avoid copying data
2 parents 693c7a8 + b438c9b commit 1d39403

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

common/include/pcl/conversions.h

+27-12
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,17 @@ namespace pcl
151151
}
152152

153153
/** \brief Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map.
154-
* \param[in] msg the PCLPointCloud2 binary blob
154+
* \param[in] msg the PCLPointCloud2 binary blob (note that the binary point data in msg.data will not be used!)
155155
* \param[out] cloud the resultant pcl::PointCloud<T>
156156
* \param[in] field_map a MsgFieldMap object
157+
* \param[in] msg_data pointer to binary blob data, used instead of msg.data
157158
*
158-
* \note Use fromPCLPointCloud2 (PCLPointCloud2, PointCloud<T>) directly or create you
159-
* own MsgFieldMap using:
160-
*
161-
* \code
162-
* MsgFieldMap field_map;
163-
* createMapping<PointT> (msg.fields, field_map);
164-
* \endcode
159+
* \note Use fromPCLPointCloud2 (PCLPointCloud2, PointCloud<T>) instead, except if you have a binary blob of
160+
* point data that you do not want to copy into a pcl::PCLPointCloud2 in order to use fromPCLPointCloud2.
165161
*/
166162
template <typename PointT> void
167163
fromPCLPointCloud2 (const pcl::PCLPointCloud2& msg, pcl::PointCloud<PointT>& cloud,
168-
const MsgFieldMap& field_map)
164+
const MsgFieldMap& field_map, const std::uint8_t* msg_data)
169165
{
170166
// Copy info fields
171167
cloud.header = msg.header;
@@ -187,11 +183,10 @@ namespace pcl
187183
field_map[0].size == sizeof(PointT))
188184
{
189185
const auto cloud_row_step = (sizeof (PointT) * cloud.width);
190-
const std::uint8_t* msg_data = &msg.data[0];
191186
// Should usually be able to copy all rows at once
192187
if (msg.row_step == cloud_row_step)
193188
{
194-
std::copy(msg.data.cbegin(), msg.data.cend(), cloud_data);
189+
memcpy (cloud_data, msg_data, msg.width * msg.height * sizeof(PointT));
195190
}
196191
else
197192
{
@@ -205,7 +200,7 @@ namespace pcl
205200
// If not, memcpy each group of contiguous fields separately
206201
for (uindex_t row = 0; row < msg.height; ++row)
207202
{
208-
const std::uint8_t* row_data = &msg.data[row * msg.row_step];
203+
const std::uint8_t* row_data = msg_data + row * msg.row_step;
209204
for (uindex_t col = 0; col < msg.width; ++col)
210205
{
211206
const std::uint8_t* msg_data = row_data + col * msg.point_step;
@@ -220,6 +215,26 @@ namespace pcl
220215
}
221216
}
222217

218+
/** \brief Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map.
219+
* \param[in] msg the PCLPointCloud2 binary blob
220+
* \param[out] cloud the resultant pcl::PointCloud<T>
221+
* \param[in] field_map a MsgFieldMap object
222+
*
223+
* \note Use fromPCLPointCloud2 (PCLPointCloud2, PointCloud<T>) directly or create you
224+
* own MsgFieldMap using:
225+
*
226+
* \code
227+
* MsgFieldMap field_map;
228+
* createMapping<PointT> (msg.fields, field_map);
229+
* \endcode
230+
*/
231+
template <typename PointT> void
232+
fromPCLPointCloud2 (const pcl::PCLPointCloud2& msg, pcl::PointCloud<PointT>& cloud,
233+
const MsgFieldMap& field_map)
234+
{
235+
fromPCLPointCloud2 (msg, cloud, field_map, msg.data.data());
236+
}
237+
223238
/** \brief Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object.
224239
* \param[in] msg the PCLPointCloud2 binary blob
225240
* \param[out] cloud the resultant pcl::PointCloud<T>

0 commit comments

Comments
 (0)