Skip to content

Commit 230e469

Browse files
Cleanup and reorganize functions between color_handler headers and common/io.h
1 parent 0bcadd5 commit 230e469

File tree

4 files changed

+105
-90
lines changed

4 files changed

+105
-90
lines changed

common/include/pcl/common/impl/io.hpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ getFields ()
102102
return fields;
103103
}
104104

105+
template <typename CloudT> inline std::vector<pcl::PCLPointField>
106+
getFields (const CloudT& cloud)
107+
{
108+
return pcl::getFields<typename CloudT::PointType>();
109+
}
110+
111+
// Template specialization for PCLPointCloud2
112+
template <> inline std::vector<pcl::PCLPointField>
113+
getFields<pcl::PCLPointCloud2> (const pcl::PCLPointCloud2& cloud) {
114+
return cloud.fields;
115+
}
116+
105117

106118
template <typename PointT> std::string
107119
getFieldsList (const pcl::PointCloud<PointT> &)
@@ -115,6 +127,60 @@ getFieldsList (const pcl::PointCloud<PointT> &)
115127
return (result);
116128
}
117129

130+
/**
131+
* \brief reinterpret a pointer as a type, dereference it, and return result as target
132+
* type
133+
*
134+
* \tparam DType Pointer type of the data
135+
* \tparam RType Return type which the dereferenced result is cast to
136+
*
137+
* \return value
138+
*/
139+
template <typename DType, typename RType> RType reinterpret_and_cast (const std::uint8_t* p)
140+
{
141+
return static_cast<RType>(*reinterpret_cast<const DType*>(p));
142+
}
143+
144+
template <typename T> T point_field_as (const std::uint8_t* data, const std::uint8_t type)
145+
{
146+
switch (type) {
147+
case pcl::PCLPointField::PointFieldTypes::FLOAT32:
148+
return reinterpret_and_cast<float, T>(data);
149+
break;
150+
case pcl::PCLPointField::PointFieldTypes::UINT8:
151+
return reinterpret_and_cast<std::uint8_t, T>(data);
152+
break;
153+
case pcl::PCLPointField::PointFieldTypes::UINT16:
154+
return reinterpret_and_cast<std::uint16_t, T>(data);
155+
break;
156+
case pcl::PCLPointField::PointFieldTypes::UINT32:
157+
return reinterpret_and_cast<std::uint32_t, T>(data);
158+
break;
159+
case pcl::PCLPointField::PointFieldTypes::UINT64:
160+
return reinterpret_and_cast<std::uint64_t, T>(data);
161+
break;
162+
case pcl::PCLPointField::PointFieldTypes::BOOL:
163+
return reinterpret_and_cast<bool, T>(data);
164+
break;
165+
case pcl::PCLPointField::PointFieldTypes::FLOAT64:
166+
return reinterpret_and_cast<double, T>(data);
167+
break;
168+
case pcl::PCLPointField::PointFieldTypes::INT16:
169+
return reinterpret_and_cast<std::int16_t, T>(data);
170+
break;
171+
case pcl::PCLPointField::PointFieldTypes::INT32:
172+
return reinterpret_and_cast<std::int32_t, T>(data);
173+
break;
174+
case pcl::PCLPointField::PointFieldTypes::INT64:
175+
return reinterpret_and_cast<std::int64_t, T>(data);
176+
break;
177+
default:
178+
return 0;
179+
break;
180+
}
181+
}
182+
183+
118184
namespace detail
119185
{
120186

@@ -459,4 +525,3 @@ copyPointCloud (const pcl::PointCloud<PointT> &cloud_in, pcl::PointCloud<PointT>
459525
}
460526

461527
} // namespace pcl
462-

common/include/pcl/common/io.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ namespace pcl
9595
template <typename PointT> inline std::vector<pcl::PCLPointField>
9696
getFields ();
9797

98+
/** \brief Get the list of available fields (i.e., dimension/channel) from cloud type,
99+
* not point type
100+
* \tparam CloudT cloud type, PointCloud or PCLPointCloud2
101+
* \param cloud input cloud. Unsused for PointCloud, needed for PCLPointCloud2 because the fields are not available statically
102+
* \ingroup common
103+
*/
104+
template <typename CloudT> inline std::vector<pcl::PCLPointField>
105+
getFields (const CloudT& cloud) ;
106+
107+
98108
/** \brief Get the list of all fields available in a given cloud
99109
* \param[in] cloud the point cloud message
100110
* \ingroup common
@@ -251,6 +261,19 @@ namespace pcl
251261
}
252262
}
253263

264+
/**
265+
* \brief Get the value of a point field from raw data pointer and field type.
266+
*
267+
* \tparam T return type the field will be cast as
268+
* \param data data pointer
269+
* \param type point field type
270+
* \ingroup common
271+
*
272+
* \return field value
273+
*/
274+
template <typename T> T point_field_as (const std::uint8_t* data, const std::uint8_t type);
275+
276+
254277
enum InterpolationType
255278
{
256279
BORDER_CONSTANT = 0, BORDER_REPLICATE = 1,

visualization/include/pcl/visualization/impl/point_cloud_color_handlers.hpp

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -490,21 +490,6 @@ PointCloudColorHandlerLabelField<PointT>::getColor () const
490490
// data from PointCloudColorHandlerGenericField without needing to know the
491491
// cloud type
492492

493-
// Get point fields from cloud. Could not get it to work with existing
494-
// pcl::getFields
495-
template <typename CloudT> inline std::vector<pcl::PCLPointField>
496-
getFields(const CloudT& cloud)
497-
{
498-
return pcl::getFields<typename CloudT::PointType>();
499-
}
500-
501-
template <> inline std::vector<pcl::PCLPointField>
502-
getFields<pcl::PCLPointCloud2>(const pcl::PCLPointCloud2& cloud) {
503-
return cloud.fields;
504-
}
505-
506-
507-
// Get point step. Does not directly exist in pcl::PointCloud
508493
template <typename CloudT> inline int getPointStep(const CloudT&)
509494
{
510495
return sizeof(typename CloudT::PointType);
@@ -518,14 +503,13 @@ getPointStep<pcl::PCLPointCloud2>(const pcl::PCLPointCloud2& cloud) {
518503
// Get cloud data blob
519504
template <typename CloudT> inline const std::uint8_t* getCloudData(const CloudT& cloud)
520505
{
521-
return reinterpret_cast<const std::uint8_t*>(cloud.points.data());
506+
return reinterpret_cast<const std::uint8_t*>(cloud.data());
522507
}
523508

524509
template <> inline const std::uint8_t* getCloudData<pcl::PCLPointCloud2>(const typename pcl::PCLPointCloud2& cloud) {
525-
return reinterpret_cast<const std::uint8_t*>(cloud.data.data());
510+
return cloud.data.data();
526511
}
527512

528-
529513
// copy of pcl::getFieldIndex() from impl/io.hpp, without the unused template
530514
// parameter
531515
static int getFieldIndex(const std::string& field_name,
@@ -576,64 +560,6 @@ template <> inline bool isXYZFiniteAt(const PCLPointCloud2& cloud, int index)
576560
}
577561
}
578562

579-
inline const std::uint8_t* getCloudData(const typename pcl::PCLPointCloud2& cloud)
580-
{
581-
return reinterpret_cast<const std::uint8_t*>(cloud.data.data());
582-
}
583-
584-
template <typename DType, typename RType> RType reinterpret_and_cast(const std::uint8_t* p)
585-
{
586-
return static_cast<RType>(*reinterpret_cast<const DType*>(p));
587-
}
588-
589-
/**
590-
* @brief Get the value of a point field from raw data pointer and field type.
591-
*
592-
* @tparam T return type the field will be cast as
593-
* @param data data pointer
594-
* @param type point field type
595-
*
596-
* @return field value
597-
*/
598-
template <typename T> T point_field_as(const std::uint8_t* data, const std::uint8_t type)
599-
{
600-
switch (type) {
601-
case pcl::PCLPointField::PointFieldTypes::FLOAT32:
602-
return reinterpret_and_cast<float, T>(data);
603-
break;
604-
case pcl::PCLPointField::PointFieldTypes::UINT8:
605-
return reinterpret_and_cast<std::uint8_t, T>(data);
606-
break;
607-
case pcl::PCLPointField::PointFieldTypes::UINT16:
608-
return reinterpret_and_cast<std::uint16_t, T>(data);
609-
break;
610-
case pcl::PCLPointField::PointFieldTypes::UINT32:
611-
return reinterpret_and_cast<std::uint32_t, T>(data);
612-
break;
613-
case pcl::PCLPointField::PointFieldTypes::UINT64:
614-
return reinterpret_and_cast<std::uint64_t, T>(data);
615-
break;
616-
case pcl::PCLPointField::PointFieldTypes::BOOL:
617-
return reinterpret_and_cast<bool, T>(data);
618-
break;
619-
case pcl::PCLPointField::PointFieldTypes::FLOAT64:
620-
return reinterpret_and_cast<double, T>(data);
621-
break;
622-
case pcl::PCLPointField::PointFieldTypes::INT16:
623-
return reinterpret_and_cast<std::int16_t, T>(data);
624-
break;
625-
case pcl::PCLPointField::PointFieldTypes::INT32:
626-
return reinterpret_and_cast<std::int32_t, T>(data);
627-
break;
628-
case pcl::PCLPointField::PointFieldTypes::INT64:
629-
return reinterpret_and_cast<std::int64_t, T>(data);
630-
break;
631-
default:
632-
return 0;
633-
break;
634-
}
635-
}
636-
637563
template <typename PointT>
638564
PointCloudColorHandlerGenericField<PointT>::PointCloudColorHandlerGenericField(
639565
const PointCloudConstPtr& cloud, const std::string& field_name)

visualization/include/pcl/visualization/point_cloud_color_handlers.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace pcl
5858
{
5959
//////////////////////////////////////////////////////////////////////////////////////
6060
/** \brief Base Handler class for PointCloud colors.
61-
* \author Radu B. Rusu
61+
* \author Radu B. Rusu
6262
* \ingroup visualization
6363
*/
6464
template <typename PointT>
@@ -131,7 +131,7 @@ namespace pcl
131131

132132
//////////////////////////////////////////////////////////////////////////////////////
133133
/** \brief Handler for random PointCloud colors (i.e., R, G, B will be randomly chosen)
134-
* \author Radu B. Rusu
134+
* \author Radu B. Rusu
135135
* \ingroup visualization
136136
*/
137137
template <typename PointT>
@@ -179,7 +179,7 @@ namespace pcl
179179
//////////////////////////////////////////////////////////////////////////////////////
180180
/** \brief Handler for predefined user colors. The color at each point will be drawn
181181
* as the use given R, G, B values.
182-
* \author Radu B. Rusu
182+
* \author Radu B. Rusu
183183
* \ingroup visualization
184184
*/
185185
template <typename PointT>
@@ -237,7 +237,7 @@ namespace pcl
237237
//////////////////////////////////////////////////////////////////////////////////////
238238
/** \brief RGB handler class for colors. Uses the data present in the "rgb" or "rgba"
239239
* fields as the color at each point.
240-
* \author Radu B. Rusu
240+
* \author Radu B. Rusu
241241
* \ingroup visualization
242242
*/
243243
template <typename PointT>
@@ -362,15 +362,16 @@ namespace pcl
362362

363363
vtkSmartPointer<vtkDataArray> getColor() const override;
364364

365-
protected:
365+
/** \brief Get the name of the field used. */
366+
std::string getFieldName() const override;
366367

367368
/** \brief Name of the field used to create the color handler. */
368369
std::string field_name_;
369370

370-
std::vector<pcl::PCLPointField> fields_;
371371

372-
/** \brief Get the name of the field used. */
373-
std::string getFieldName() const override;
372+
protected:
373+
374+
std::vector<pcl::PCLPointField> fields_;
374375

375376
/** \brief Class getName method. */
376377
std::string getName() const override
@@ -503,7 +504,7 @@ namespace pcl
503504

504505
//////////////////////////////////////////////////////////////////////////////////////
505506
/** \brief Base Handler class for PointCloud colors.
506-
* \author Radu B. Rusu
507+
* \author Radu B. Rusu
507508
* \ingroup visualization
508509
*/
509510
template <>
@@ -521,7 +522,7 @@ namespace pcl
521522
PointCloudColorHandler (const PointCloudConstPtr &cloud) :
522523
cloud_ (cloud), capable_ (false), field_idx_ ()
523524
{}
524-
525+
525526
/** \brief Destructor. */
526527
virtual ~PointCloudColorHandler() = default;
527528

@@ -570,7 +571,7 @@ namespace pcl
570571

571572
//////////////////////////////////////////////////////////////////////////////////////
572573
/** \brief Handler for random PointCloud colors (i.e., R, G, B will be randomly chosen)
573-
* \author Radu B. Rusu
574+
* \author Radu B. Rusu
574575
* \ingroup visualization
575576
*/
576577
template <>
@@ -606,7 +607,7 @@ namespace pcl
606607
//////////////////////////////////////////////////////////////////////////////////////
607608
/** \brief Handler for predefined user colors. The color at each point will be drawn
608609
* as the use given R, G, B values.
609-
* \author Radu B. Rusu
610+
* \author Radu B. Rusu
610611
* \ingroup visualization
611612
*/
612613
template <>
@@ -645,7 +646,7 @@ namespace pcl
645646
//////////////////////////////////////////////////////////////////////////////////////
646647
/** \brief RGB handler class for colors. Uses the data present in the "rgb" or "rgba"
647648
* fields as the color at each point.
648-
* \author Radu B. Rusu
649+
* \author Radu B. Rusu
649650
* \ingroup visualization
650651
*/
651652
template <>

0 commit comments

Comments
 (0)