Point Cloud Library (PCL) 1.15.0
Loading...
Searching...
No Matches
io.h
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2010-2011, Willow Garage, Inc.
6 * Copyright (c) 2012-, Open Perception, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 * * Neither the name of the copyright holder(s) nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 * $Id$
38 *
39 */
40
41#pragma once
42
43#include <numeric>
44#include <string>
45
46#include <pcl/point_cloud.h>
47#include <pcl/PointIndices.h>
48#include <pcl/pcl_macros.h>
49#include <pcl/PolygonMesh.h>
50#include <locale>
51
52namespace pcl
53{
54 /** \brief Get the index of a specified field (i.e., dimension/channel)
55 * \param[in] cloud the point cloud message
56 * \param[in] field_name the string defining the field name
57 * \return the index of the field or a negative integer if no field with the given name exists
58 * \ingroup common
59 */
60 inline int
61 getFieldIndex (const pcl::PCLPointCloud2 &cloud, const std::string &field_name)
62 {
63 // Get the index we need
64 const auto result = std::find_if(cloud.fields.begin (), cloud.fields.end (),
65 [&field_name](const auto field) { return field.name == field_name; });
66 if (result == cloud.fields.end ())
67 return -1;
68 return std::distance(cloud.fields.begin (), result);
69 }
70
71 /** \brief Get the index of a specified field (i.e., dimension/channel)
72 * \tparam PointT datatype for which fields is being queries
73 * \param[in] field_name the string defining the field name
74 * \param[out] fields a vector to the original \a PCLPointField vector that the raw PointCloud message contains
75 * \return the index of the field or a negative integer if no field with the given name exists
76 * \ingroup common
77 */
78 template <typename PointT> inline int
79 getFieldIndex (const std::string &field_name,
80 std::vector<pcl::PCLPointField> &fields);
81 /** \brief Get the index of a specified field (i.e., dimension/channel)
82 * \tparam PointT datatype for which fields is being queries
83 * \param[in] field_name the string defining the field name
84 * \param[in] fields a vector to the original \a PCLPointField vector that the raw PointCloud message contains
85 * \ingroup common
86 */
87 template <typename PointT> inline int
88 getFieldIndex (const std::string &field_name,
89 const std::vector<pcl::PCLPointField> &fields);
90
91 /** \brief Get the list of available fields (i.e., dimension/channel)
92 * \tparam PointT datatype whose details are requested
93 * \ingroup common
94 */
95 template <typename PointT> inline std::vector<pcl::PCLPointField>
96 getFields ();
97
98 /** \brief Get the list of all fields available in a given cloud
99 * \param[in] cloud the point cloud message
100 * \ingroup common
101 */
102 template <typename PointT> inline std::string
104
105 /** \brief Get the available point cloud fields as a space separated string
106 * \param[in] cloud a pointer to the PointCloud message
107 * \ingroup common
108 */
109 inline std::string
111 {
112 if (cloud.fields.empty())
113 {
114 return "";
115 } else
116 {
117 return std::accumulate(std::next (cloud.fields.begin ()), cloud.fields.end (), cloud.fields[0].name,
118 [](const auto& acc, const auto& field) { return acc + " " + field.name; });
119 }
120 }
121
122 /** \brief Obtains the size of a specific field data type in bytes
123 * \param[in] datatype the field data type (see PCLPointField.h)
124 * \ingroup common
125 */
126 inline int
127 getFieldSize (const int datatype)
128 {
129 switch (datatype)
130 {
132 return sizeof(bool);
133
136 return (1);
137
140 return (2);
141
145 return (4);
146
150 return (8);
151
152 default:
153 return (0);
154 }
155 }
156
157 /** \brief Obtain a vector with the sizes of all valid fields (e.g., not "_")
158 * \param[in] fields the input vector containing the fields
159 * \param[out] field_sizes the resultant field sizes in bytes
160 */
161 PCL_EXPORTS void
162 getFieldsSizes (const std::vector<pcl::PCLPointField> &fields,
163 std::vector<int> &field_sizes);
164
165 /** \brief Obtains the type of the PCLPointField from a specific size and type
166 * \param[in] size the size in bytes of the data field
167 * \param[in] type a char describing the type of the field ('B' = bool, 'F' = float, 'I' = signed, 'U' = unsigned)
168 * \ingroup common
169 */
170 inline int
171 getFieldType (const int size, char type)
172 {
173 type = std::toupper (type, std::locale::classic ());
174
175 // extra logic for bool because its size is undefined
176 if (type == 'B') {
177 if (size == sizeof(bool)) {
179 } else {
180 return -1;
181 }
182 }
183
184 switch (size)
185 {
186 case 1:
187 if (type == 'I')
189 if (type == 'U')
191 break;
192
193 case 2:
194 if (type == 'I')
196 if (type == 'U')
198 break;
199
200 case 4:
201 if (type == 'I')
203 if (type == 'U')
205 if (type == 'F')
207 break;
208
209 case 8:
210 if (type == 'I')
212 if (type == 'U')
214 if (type == 'F')
216 break;
217 }
218 return (-1);
219 }
220
221 /** \brief Obtains the type of the PCLPointField from a specific PCLPointField as a char
222 * \param[in] type the PCLPointField field type
223 * \ingroup common
224 */
225 inline char
226 getFieldType (const int type)
227 {
228 switch (type)
229 {
231 return ('B');
232
237 return ('I');
238
243 return ('U');
244
247 return ('F');
248
249 default:
250 return ('?');
251 }
252 }
253
261
262 /** \brief \return the right index according to the interpolation type.
263 * \note this is adapted from OpenCV
264 * \param p the index of point to interpolate
265 * \param length the top/bottom row or left/right column index
266 * \param type the requested interpolation
267 * \throws pcl::BadArgumentException if type is unknown
268 */
269 PCL_EXPORTS int
270 interpolatePointIndex (int p, int length, InterpolationType type);
271
272 /** \brief Concatenate two pcl::PointCloud<PointT>
273 * \param[in] cloud1 the first input point cloud dataset
274 * \param[in] cloud2 the second input point cloud dataset
275 * \param[out] cloud_out the resultant output point cloud dataset
276 * \return true if successful, false otherwise
277 * \ingroup common
278 */
279 template <typename PointT>
280 bool
282 const pcl::PointCloud<PointT> &cloud2,
283 pcl::PointCloud<PointT> &cloud_out)
284 {
285 return pcl::PointCloud<PointT>::concatenate(cloud1, cloud2, cloud_out);
286 }
287
288 /** \brief Concatenate two pcl::PCLPointCloud2
289 *
290 * \warning This function will concatenate IFF the non-skip fields are in the correct
291 * order and same in number.
292 * \param[in] cloud1 the first input point cloud dataset
293 * \param[in] cloud2 the second input point cloud dataset
294 * \param[out] cloud_out the resultant output point cloud dataset
295 * \return true if successful, false otherwise
296 * \ingroup common
297 */
298 inline bool
300 const pcl::PCLPointCloud2 &cloud2,
301 pcl::PCLPointCloud2 &cloud_out)
302 {
303 return pcl::PCLPointCloud2::concatenate(cloud1, cloud2, cloud_out);
304 }
305
306 /** \brief Concatenate two pcl::PolygonMesh
307 * \param[in] mesh1 the first input mesh
308 * \param[in] mesh2 the second input mesh
309 * \param[out] mesh_out the resultant output mesh
310 * \return true if successful, false otherwise
311 * \ingroup common
312 */
313 inline bool
315 const pcl::PolygonMesh &mesh2,
316 pcl::PolygonMesh &mesh_out)
317 {
318 return pcl::PolygonMesh::concatenate(mesh1, mesh2, mesh_out);
319 }
320
321 /** \brief Extract the indices of a given point cloud as a new point cloud
322 * \param[in] cloud_in the input point cloud dataset
323 * \param[in] indices the vector of indices representing the points to be copied from \a cloud_in
324 * \param[out] cloud_out the resultant output point cloud dataset
325 * \note Assumes unique indices.
326 * \ingroup common
327 */
328 PCL_EXPORTS void
330 const Indices &indices,
331 pcl::PCLPointCloud2 &cloud_out);
332
333 /** \brief Extract the indices of a given point cloud as a new point cloud
334 * \param[in] cloud_in the input point cloud dataset
335 * \param[in] indices the vector of indices representing the points to be copied from \a cloud_in
336 * \param[out] cloud_out the resultant output point cloud dataset
337 * \note Assumes unique indices.
338 * \ingroup common
339 */
340 PCL_EXPORTS void
342 const IndicesAllocator< Eigen::aligned_allocator<index_t> > &indices,
343 pcl::PCLPointCloud2 &cloud_out);
344
345 /** \brief Copy fields and point cloud data from \a cloud_in to \a cloud_out
346 * \param[in] cloud_in the input point cloud dataset
347 * \param[out] cloud_out the resultant output point cloud dataset
348 * \ingroup common
349 */
350 PCL_EXPORTS void
352 pcl::PCLPointCloud2 &cloud_out);
353
354 /** \brief Check if two given point types are the same or not. */
355template <typename Point1T, typename Point2T> constexpr bool
357 {
358 return (std::is_same<remove_cvref_t<Point1T>, remove_cvref_t<Point2T>>::value);
359 }
360
361 /** \brief Extract the indices of a given point cloud as a new point cloud
362 * \param[in] cloud_in the input point cloud dataset
363 * \param[in] indices the vector of indices representing the points to be copied from \a cloud_in
364 * \param[out] cloud_out the resultant output point cloud dataset
365 * \note Assumes unique indices.
366 * \ingroup common
367 */
368 template <typename PointT, typename IndicesVectorAllocator = std::allocator<index_t>> void
370 const IndicesAllocator< IndicesVectorAllocator> &indices,
371 pcl::PointCloud<PointT> &cloud_out);
372
373 /** \brief Extract the indices of a given point cloud as a new point cloud
374 * \param[in] cloud_in the input point cloud dataset
375 * \param[in] indices the PointIndices structure representing the points to be copied from cloud_in
376 * \param[out] cloud_out the resultant output point cloud dataset
377 * \note Assumes unique indices.
378 * \ingroup common
379 */
380 template <typename PointT> void
382 const PointIndices &indices,
383 pcl::PointCloud<PointT> &cloud_out);
384
385 /** \brief Extract the indices of a given point cloud as a new point cloud
386 * \param[in] cloud_in the input point cloud dataset
387 * \param[in] indices the vector of indices representing the points to be copied from \a cloud_in
388 * \param[out] cloud_out the resultant output point cloud dataset
389 * \note Assumes unique indices.
390 * \ingroup common
391 */
392 template <typename PointT> void
394 const std::vector<pcl::PointIndices> &indices,
395 pcl::PointCloud<PointT> &cloud_out);
396
397 /** \brief Copy all the fields from a given point cloud into a new point cloud
398 * \param[in] cloud_in the input point cloud dataset
399 * \param[out] cloud_out the resultant output point cloud dataset
400 * \ingroup common
401 */
402 template <typename PointInT, typename PointOutT> void
404 pcl::PointCloud<PointOutT> &cloud_out);
405
406 /** \brief Extract the indices of a given point cloud as a new point cloud
407 * \param[in] cloud_in the input point cloud dataset
408 * \param[in] indices the vector of indices representing the points to be copied from \a cloud_in
409 * \param[out] cloud_out the resultant output point cloud dataset
410 * \note Assumes unique indices.
411 * \ingroup common
412 */
413 template <typename PointInT, typename PointOutT, typename IndicesVectorAllocator = std::allocator<index_t>> void
415 const IndicesAllocator<IndicesVectorAllocator> &indices,
416 pcl::PointCloud<PointOutT> &cloud_out);
417
418 /** \brief Extract the indices of a given point cloud as a new point cloud
419 * \param[in] cloud_in the input point cloud dataset
420 * \param[in] indices the PointIndices structure representing the points to be copied from cloud_in
421 * \param[out] cloud_out the resultant output point cloud dataset
422 * \note Assumes unique indices.
423 * \ingroup common
424 */
425 template <typename PointInT, typename PointOutT> void
427 const PointIndices &indices,
428 pcl::PointCloud<PointOutT> &cloud_out);
429
430 /** \brief Extract the indices of a given point cloud as a new point cloud
431 * \param[in] cloud_in the input point cloud dataset
432 * \param[in] indices the vector of indices representing the points to be copied from cloud_in
433 * \param[out] cloud_out the resultant output point cloud dataset
434 * \note Assumes unique indices.
435 * \ingroup common
436 */
437 template <typename PointInT, typename PointOutT> void
439 const std::vector<pcl::PointIndices> &indices,
440 pcl::PointCloud<PointOutT> &cloud_out);
441
442 /** \brief Copy a point cloud inside a larger one interpolating borders.
443 * \param[in] cloud_in the input point cloud dataset
444 * \param[out] cloud_out the resultant output point cloud dataset
445 * \param top
446 * \param bottom
447 * \param left
448 * \param right
449 * Position of cloud_in inside cloud_out is given by \a top, \a left, \a bottom \a right.
450 * \param[in] border_type the interpolating method (pcl::BORDER_XXX)
451 * BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh
452 * BORDER_REFLECT: fedcba|abcdefgh|hgfedcb
453 * BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba
454 * BORDER_WRAP: cdefgh|abcdefgh|abcdefg
455 * BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified 'i'
456 * BORDER_TRANSPARENT: mnopqr|abcdefgh|tuvwxyz where m-r and t-z are original values of cloud_out
457 * \param value
458 * \throw pcl::BadArgumentException if any of top, bottom, left or right is negative.
459 * \ingroup common
460 */
461 template <typename PointT> void
463 pcl::PointCloud<PointT> &cloud_out,
464 int top, int bottom, int left, int right,
465 pcl::InterpolationType border_type, const PointT& value);
466
467 /** \brief Concatenate two datasets representing different fields.
468 *
469 * \note If the input datasets have overlapping fields (i.e., both contain
470 * the same fields), then the data in the second cloud (cloud2_in) will
471 * overwrite the data in the first (cloud1_in).
472 *
473 * \param[in] cloud1_in the first input dataset
474 * \param[in] cloud2_in the second input dataset (overwrites the fields of the first dataset for those that are shared)
475 * \param[out] cloud_out the resultant output dataset created by the concatenation of all the fields in the input datasets
476 * \ingroup common
477 */
478 template <typename PointIn1T, typename PointIn2T, typename PointOutT> void
480 const pcl::PointCloud<PointIn2T> &cloud2_in,
481 pcl::PointCloud<PointOutT> &cloud_out);
482
483 /** \brief Concatenate two datasets representing different fields.
484 *
485 * \note If the input datasets have overlapping fields (i.e., both contain
486 * the same fields), then the data in the second cloud (cloud2_in) will
487 * overwrite the data in the first (cloud1_in).
488 *
489 * \param[in] cloud1_in the first input dataset
490 * \param[in] cloud2_in the second input dataset (overwrites the fields of the first dataset for those that are shared)
491 * \param[out] cloud_out the output dataset created by concatenating all the fields in the input datasets
492 * \ingroup common
493 */
494 PCL_EXPORTS bool
496 const pcl::PCLPointCloud2 &cloud2_in,
497 pcl::PCLPointCloud2 &cloud_out);
498
499 /** \brief Copy the XYZ dimensions of a pcl::PCLPointCloud2 into Eigen format
500 * \param[in] in the point cloud message
501 * \param[out] out the resultant Eigen MatrixXf format containing XYZ0 / point
502 * \ingroup common
503 */
504 PCL_EXPORTS bool
505 getPointCloudAsEigen (const pcl::PCLPointCloud2 &in, Eigen::MatrixXf &out);
506
507 /** \brief Copy the XYZ dimensions from an Eigen MatrixXf into a pcl::PCLPointCloud2 message
508 * \param[in] in the Eigen MatrixXf format containing XYZ0 / point
509 * \param[out] out the resultant point cloud message
510 * \note the method assumes that the PCLPointCloud2 message already has the fields set up properly !
511 * \ingroup common
512 */
513 PCL_EXPORTS bool
514 getEigenAsPointCloud (Eigen::MatrixXf &in, pcl::PCLPointCloud2 &out);
515
516 namespace io
517 {
518 /** \brief swap bytes order of a char array of length N
519 * \param bytes char array to swap
520 * \ingroup common
521 */
522 template <std::size_t N> void
523 swapByte (char* bytes);
524
525 /** \brief specialization of swapByte for dimension 1
526 * \param bytes char array to swap
527 */
528 template <> inline void
529 swapByte<1> (char* bytes) { bytes[0] = bytes[0]; }
530
531
532 /** \brief specialization of swapByte for dimension 2
533 * \param bytes char array to swap
534 */
535 template <> inline void
536 swapByte<2> (char* bytes) { std::swap (bytes[0], bytes[1]); }
537
538 /** \brief specialization of swapByte for dimension 4
539 * \param bytes char array to swap
540 */
541 template <> inline void
542 swapByte<4> (char* bytes)
543 {
544 std::swap (bytes[0], bytes[3]);
545 std::swap (bytes[1], bytes[2]);
546 }
547
548 /** \brief specialization of swapByte for dimension 8
549 * \param bytes char array to swap
550 */
551 template <> inline void
552 swapByte<8> (char* bytes)
553 {
554 std::swap (bytes[0], bytes[7]);
555 std::swap (bytes[1], bytes[6]);
556 std::swap (bytes[2], bytes[5]);
557 std::swap (bytes[3], bytes[4]);
558 }
559
560 /** \brief swaps byte of an arbitrary type T casting it to char*
561 * \param value the data you want its bytes swapped
562 */
563 template <typename T> void
564 swapByte (T& value)
565 {
566 pcl::io::swapByte<sizeof(T)> (reinterpret_cast<char*> (&value));
567 }
568 }
569}
570
571#include <pcl/common/impl/io.hpp>
PointCloud represents the base class in PCL for storing collections of 3D points.
static bool concatenate(pcl::PointCloud< PointT > &cloud1, const pcl::PointCloud< PointT > &cloud2)
bool concatenate(const pcl::PointCloud< PointT > &cloud1, const pcl::PointCloud< PointT > &cloud2, pcl::PointCloud< PointT > &cloud_out)
Concatenate two pcl::PointCloud<PointT>
Definition io.h:281
void swapByte(char *bytes)
swap bytes order of a char array of length N
PCL_EXPORTS bool getPointCloudAsEigen(const pcl::PCLPointCloud2 &in, Eigen::MatrixXf &out)
Copy the XYZ dimensions of a pcl::PCLPointCloud2 into Eigen format.
PCL_EXPORTS bool getEigenAsPointCloud(Eigen::MatrixXf &in, pcl::PCLPointCloud2 &out)
Copy the XYZ dimensions from an Eigen MatrixXf into a pcl::PCLPointCloud2 message.
int getFieldSize(const int datatype)
Obtains the size of a specific field data type in bytes.
Definition io.h:127
std::string getFieldsList(const pcl::PointCloud< PointT > &)
Get the list of all fields available in a given cloud.
Definition io.hpp:107
int getFieldType(const int size, char type)
Obtains the type of the PCLPointField from a specific size and type.
Definition io.h:171
void concatenateFields(const pcl::PointCloud< PointIn1T > &cloud1_in, const pcl::PointCloud< PointIn2T > &cloud2_in, pcl::PointCloud< PointOutT > &cloud_out)
Concatenate two datasets representing different fields.
Definition io.hpp:303
std::vector< pcl::PCLPointField > getFields()
Get the list of available fields (i.e., dimension/channel)
Definition io.hpp:97
#define PCL_FALLTHROUGH
Macro to add a no-op or a fallthrough attribute based on compiler feature.
Definition pcl_macros.h:438
void copyPointCloud(const pcl::PointCloud< PointInT > &cloud_in, pcl::PointCloud< PointOutT > &cloud_out)
Copy all the fields from a given point cloud into a new point cloud.
Definition io.hpp:142
void swapByte< 1 >(char *bytes)
specialization of swapByte for dimension 1
Definition io.h:529
void swapByte< 4 >(char *bytes)
specialization of swapByte for dimension 4
Definition io.h:542
void swapByte< 2 >(char *bytes)
specialization of swapByte for dimension 2
Definition io.h:536
void swapByte< 8 >(char *bytes)
specialization of swapByte for dimension 8
Definition io.h:552
int getFieldIndex(const pcl::PointCloud< PointT > &, const std::string &field_name, std::vector< pcl::PCLPointField > &fields)
Definition io.hpp:52
InterpolationType
Definition io.h:255
@ BORDER_REFLECT
Definition io.h:257
@ BORDER_REFLECT_101
Definition io.h:258
@ BORDER_TRANSPARENT
Definition io.h:258
@ BORDER_DEFAULT
Definition io.h:259
@ BORDER_CONSTANT
Definition io.h:256
@ BORDER_WRAP
Definition io.h:257
@ BORDER_REPLICATE
Definition io.h:256
PCL_EXPORTS int interpolatePointIndex(int p, int length, InterpolationType type)
constexpr bool isSamePointType() noexcept
Check if two given point types are the same or not.
Definition io.h:356
std::vector< index_t, Allocator > IndicesAllocator
Type used for indices in PCL.
Definition types.h:128
std::remove_cv_t< std::remove_reference_t< T > > remove_cvref_t
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
PCL_EXPORTS void getFieldsSizes(const std::vector< pcl::PCLPointField > &fields, std::vector< int > &field_sizes)
Obtain a vector with the sizes of all valid fields (e.g., not "_")
Defines all the PCL and non-PCL macros used.
std::vector<::pcl::PCLPointField > fields
static bool concatenate(pcl::PCLPointCloud2 &cloud1, const pcl::PCLPointCloud2 &cloud2)
Inplace concatenate two pcl::PCLPointCloud2.
A point structure representing Euclidean xyz coordinates, and the RGB color.
static bool concatenate(pcl::PolygonMesh &mesh1, const pcl::PolygonMesh &mesh2)
Inplace concatenate two pcl::PolygonMesh.
Definition PolygonMesh.h:30