PointCloudMessageBuilder.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/std/string/string.h>
  9. #include <Lidar/PointCloudMessageBuilder.h>
  10. #include <sensor_msgs/point_cloud2_iterator.hpp>
  11. namespace ROS2Sensors
  12. {
  13. PointCloud2MessageBuilder::PointCloud2MessageBuilder(
  14. const AZStd::string& frameId, builtin_interfaces::msg::Time timeStamp, size_t count)
  15. {
  16. sensor_msgs::msg::PointCloud2 message{};
  17. message.header.frame_id = frameId.data();
  18. message.header.stamp = timeStamp;
  19. message.height = 1;
  20. message.width = count;
  21. m_message = message;
  22. }
  23. PointCloud2MessageBuilder& PointCloud2MessageBuilder::AddField(const char* name, uint8_t dataType, size_t count)
  24. {
  25. sensor_msgs::msg::PointField point_field;
  26. point_field.name = name;
  27. point_field.count = count;
  28. point_field.datatype = dataType;
  29. point_field.offset = m_offset;
  30. m_message.fields.push_back(point_field);
  31. m_offset += point_field.count * sizeOfPointField(dataType);
  32. return *this;
  33. }
  34. sensor_msgs::msg::PointCloud2 PointCloud2MessageBuilder::Get()
  35. {
  36. m_message.point_step = m_offset;
  37. m_message.row_step = m_message.width * m_message.point_step;
  38. m_message.data.resize(m_message.row_step);
  39. return m_message;
  40. }
  41. } // namespace ROS2Sensors