|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Open Source Robotics Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | +*/ |
| 17 | + |
| 18 | +// Inspired by |
| 19 | +// https://github.com/ros/common_msgs/blob/275b09a/sensor_msgs/include/sensor_msgs/point_cloud2_iterator.h |
| 20 | + |
| 21 | +#ifndef IGNITION_MSGS_POINTCLOUDPACKEDUTILS_HH_ |
| 22 | +#define IGNITION_MSGS_POINTCLOUDPACKEDUTILS_HH_ |
| 23 | + |
| 24 | +#include <ignition/msgs/pointcloud_packed.pb.h> |
| 25 | + |
| 26 | +#include <cstdarg> |
| 27 | +#include <sstream> |
| 28 | +#include <string> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +#include "ignition/msgs/config.hh" |
| 32 | +#include "ignition/msgs/detail/PointCloudPackedUtils.hh" |
| 33 | + |
| 34 | +namespace ignition |
| 35 | +{ |
| 36 | +namespace msgs |
| 37 | +{ |
| 38 | +/// \brief Class that can iterate over a PointCloudPacked message. |
| 39 | +/// |
| 40 | +/// E.g, you create your message and reserve space for data as follows: |
| 41 | +/// \verbatim |
| 42 | +/// ignition::msgs::PointCloudPacked pcMsg; |
| 43 | +/// ignition::msgs::InitPointCloudPacked(pcMsg, "my_new_frame", false, |
| 44 | +/// {{"a", PointCloudPacked::Field::FLOAT32}}); |
| 45 | +/// pcMsg.mutable_data()->resize(numPts * pcMsg.point_step()); |
| 46 | +/// \endverbatim |
| 47 | +/// |
| 48 | +/// For iterating over "a", you do : |
| 49 | +/// |
| 50 | +/// \verbatim |
| 51 | +/// ignition::msgs::PointCloudPackedIterator<float> iterA(pcMsg, "a"); |
| 52 | +/// \endverbatim |
| 53 | +/// |
| 54 | +/// And then access it through iterA[0] or *iterA. |
| 55 | +/// |
| 56 | +/// For iterating over RGBA, you can access each element as uint8_t: |
| 57 | +/// |
| 58 | +/// \verbatim |
| 59 | +/// ignition::msgs::PointCloudPackedIterator<uint8_t> iterR(pcMsg, "r"); |
| 60 | +/// ignition::msgs::PointCloudPackedIterator<uint8_t> iterG(pcMsg, "g"); |
| 61 | +/// ignition::msgs::PointCloudPackedIterator<uint8_t> iterB(pcMsg, "b"); |
| 62 | +/// ignition::msgs::PointCloudPackedIterator<uint8_t> iterA(pcMsg, "a"); |
| 63 | +/// \endverbatim |
| 64 | +/// |
| 65 | +/// \tparam FieldType Type of the element being iterated upon |
| 66 | +template<typename FieldType> |
| 67 | +class PointCloudPackedIterator |
| 68 | + : public PointCloudPackedIteratorBase< |
| 69 | + FieldType, FieldType, char, PointCloudPacked, PointCloudPackedIterator> |
| 70 | +{ |
| 71 | + // Documentation inherited |
| 72 | + public: PointCloudPackedIterator(PointCloudPacked &_cloudMsg, |
| 73 | + const std::string &_fieldName) |
| 74 | + : PointCloudPackedIteratorBase<FieldType, FieldType, char, |
| 75 | + PointCloudPacked, PointCloudPackedIterator> |
| 76 | + ::PointCloudPackedIteratorBase(_cloudMsg, _fieldName) |
| 77 | + { |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +/// \brief Same as a PointCloudPackedIterator but for const data |
| 82 | +/// \tparam FieldType Type of the element being iterated upon |
| 83 | +template<typename FieldType> |
| 84 | +class PointCloudPackedConstIterator |
| 85 | + : public PointCloudPackedIteratorBase< |
| 86 | + FieldType, const FieldType, const char, const PointCloudPacked, |
| 87 | + PointCloudPackedConstIterator> |
| 88 | +{ |
| 89 | + public: PointCloudPackedConstIterator( |
| 90 | + const PointCloudPacked &_cloudMsg, |
| 91 | + const std::string &_fieldName) |
| 92 | + : PointCloudPackedIteratorBase<FieldType, const FieldType, const char, |
| 93 | + const PointCloudPacked, |
| 94 | + PointCloudPackedConstIterator |
| 95 | + >::PointCloudPackedIteratorBase(_cloudMsg, _fieldName) |
| 96 | + { |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +/// \brief Return the size of a datatype (which is an enum of |
| 101 | +/// ignition::msgs::PointCloudPacked::Field) in bytes. |
| 102 | +/// \param[in] _dataType One of the enums of |
| 103 | +/// ignition::msgs::PointCloudPacked::Field |
| 104 | +/// \return Size in bytes. Returns -1 if the type is unknown. |
| 105 | +inline int sizeOfPointField( |
| 106 | + PointCloudPacked::Field::DataType _dataType) |
| 107 | +{ |
| 108 | + if ((_dataType == PointCloudPacked::Field::INT8) || |
| 109 | + (_dataType == PointCloudPacked::Field::UINT8)) |
| 110 | + { |
| 111 | + return 1; |
| 112 | + } |
| 113 | + else if ((_dataType == PointCloudPacked::Field::INT16) || |
| 114 | + (_dataType == PointCloudPacked::Field::UINT16)) |
| 115 | + { |
| 116 | + return 2; |
| 117 | + } |
| 118 | + else if ((_dataType == PointCloudPacked::Field::INT32) || |
| 119 | + (_dataType == PointCloudPacked::Field::UINT32) || |
| 120 | + (_dataType == PointCloudPacked::Field::FLOAT32)) |
| 121 | + { |
| 122 | + return 4; |
| 123 | + } |
| 124 | + else if (_dataType == PointCloudPacked::Field::FLOAT64) |
| 125 | + { |
| 126 | + return 8; |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + std::cerr << "PointCloudPacked::Field of type [" << _dataType |
| 131 | + << "] does not exist" << std::endl; |
| 132 | + } |
| 133 | + return -1; |
| 134 | +} |
| 135 | +} |
| 136 | +} |
| 137 | + |
| 138 | +#endif |
0 commit comments