Program Listing for File cloud.hpp

Return to documentation for file (/tmp/ws/src/point_cloud_transport_plugins/draco_point_cloud_transport/include/draco_point_cloud_transport/cloud/impl/cloud.hpp)

// Copyright (c) 2023, Open Source Robotics Foundation, Inc.
// Copyright (c) 2023, Czech Technical University in Prague
//
// 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 the copyright holder 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 HOLDER 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 DRACO_POINT_CLOUD_TRANSPORT__CLOUD__IMPL__CLOUD_HPP_
#define DRACO_POINT_CLOUD_TRANSPORT__CLOUD__IMPL__CLOUD_HPP_

#include <string>

#include <sensor_msgs/msg/point_field.hpp>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <sensor_msgs/point_cloud2_iterator.hpp>

namespace cras
{

namespace impl
{

template<typename T, typename TT, typename U, typename C, template<typename> class V>
class GenericCloudIteratorBase : public ::sensor_msgs::impl::PointCloud2IteratorBase<T, TT, U, C, V>
{
public:
  GenericCloudIteratorBase(C & cloudMsg, const ::std::string & fieldName);

  inline size_t getFieldSize() const {return this->fieldSize;}

  U * rawData() const;

protected:
  size_t fieldSize = 0;
};

template<typename T = unsigned char>
class GenericCloudConstIterator
  : public GenericCloudIteratorBase<T, const T, const unsigned char,
    const ::sensor_msgs::msg::PointCloud2, GenericCloudConstIterator>
{
public:
  GenericCloudConstIterator(
    const ::sensor_msgs::msg::PointCloud2 & cloud_msg,
    const ::std::string & field_name)
  : GenericCloudIteratorBase<T, const T, const unsigned char, const ::sensor_msgs::msg::PointCloud2,
      GenericCloudConstIterator>::GenericCloudIteratorBase(cloud_msg, field_name)
  {
  }

  template<typename D>
  const D * dataAs() const
  {
    if (sizeof(D) != this->getFieldSize()) {
      throw ::std::runtime_error(
              "Cannot convert field of size " + ::std::to_string(this->getFieldSize()) +
              " to a type of size " + ::std::to_string(sizeof(D)));
    }
    return reinterpret_cast<const D *>(this->rawData());
  }
};

template<typename T = unsigned char>
class GenericCloudIterator
  : public GenericCloudIteratorBase<T, T, unsigned char, ::sensor_msgs::msg::PointCloud2,
    GenericCloudIterator>
{
public:
  GenericCloudIterator(
    ::sensor_msgs::msg::PointCloud2 & cloud_msg,
    const ::std::string & field_name)
  : GenericCloudIteratorBase<T, T, unsigned char, ::sensor_msgs::msg::PointCloud2,
      GenericCloudIterator>::GenericCloudIteratorBase(cloud_msg, field_name)
  {
  }

  template<typename D>
  D * dataAs() const
  {
    if (sizeof(D) != this->getFieldSize()) {
      throw ::std::runtime_error(
              "Cannot convert field of size " + ::std::to_string(this->getFieldSize()) +
              " to a type of size " + ::std::to_string(sizeof(D)));
    }
    return reinterpret_cast<D *>(this->rawData());
  }

  void copyData(const ::cras::impl::GenericCloudConstIterator<T> & otherIter) const;

  void copyData(const ::cras::impl::GenericCloudIterator<T> & otherIter) const;
};

}  // namespace impl
}  // namespace cras

#endif  // DRACO_POINT_CLOUD_TRANSPORT__CLOUD__IMPL__CLOUD_HPP_