AppleDetectionGroundTruth.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "AppleDetectionGroundTruth.h"
  9. #include "ROS2/Utilities/ROS2Conversions.h"
  10. #include <ROS2/Frame/ROS2FrameComponent.h>
  11. #include <ROS2/ROS2Bus.h>
  12. #include <ROS2/Utilities/ROS2Names.h>
  13. #include <rclcpp/qos.hpp>
  14. using namespace ROS2;
  15. namespace AppleKraken
  16. {
  17. AppleDetectionGroundTruth::AppleDetectionGroundTruth(const AZStd::string& rosNamespace, const AZStd::string& namespacedFrameId)
  18. : m_frameId(namespacedFrameId)
  19. {
  20. auto defaultQoS = rclcpp::QoS(10);
  21. auto ros2Node = ROS2Interface::Get()->GetNode();
  22. m_appleDetections3DMessage.header.frame_id = m_frameId.c_str();
  23. AZStd::string full3DTopic = ROS2Names::GetNamespacedName(rosNamespace, "ground_truth_3D_detection");
  24. m_detection3DPublisher = ros2Node->create_publisher<vision_msgs::msg::Detection3DArray>(full3DTopic.data(), defaultQoS);
  25. m_appleDetections2DMessage.header.frame_id = m_frameId.c_str();
  26. AZStd::string full2DTopic = ROS2Names::GetNamespacedName(rosNamespace, "ground_truth_2D_detection");
  27. m_detection2DPublisher = ros2Node->create_publisher<vision_msgs::msg::Detection2DArray>(full2DTopic.data(), defaultQoS);
  28. }
  29. void AppleDetectionGroundTruth::UpdateGroundTruth(const AZStd::vector<PickAppleTask>& apples)
  30. {
  31. ConstructROS2Detection2DMessage(apples);
  32. ConstructROS2Detection3DMessage(apples);
  33. }
  34. void AppleDetectionGroundTruth::Publish()
  35. {
  36. auto timestamp = ROS2Interface::Get()->GetROSTimestamp();
  37. m_appleDetections2DMessage.header.stamp = timestamp;
  38. for (auto& detection : m_appleDetections2DMessage.detections)
  39. {
  40. detection.header.stamp = timestamp;
  41. }
  42. m_detection2DPublisher->publish(m_appleDetections2DMessage);
  43. m_appleDetections3DMessage.header.stamp = timestamp;
  44. for (auto& detection : m_appleDetections3DMessage.detections)
  45. {
  46. detection.header.stamp = timestamp;
  47. }
  48. m_detection3DPublisher->publish(m_appleDetections3DMessage);
  49. }
  50. vision_msgs::msg::Detection3D AppleDetectionGroundTruth::Construct3DDetection(const PickAppleTask& apple)
  51. {
  52. vision_msgs::msg::Detection3D detection;
  53. detection.header.frame_id = m_frameId.c_str();
  54. detection.id = "Apple"; // duh
  55. auto middleVector = ROS2Conversions::ToROS2Vector3(apple.m_middle);
  56. detection.bbox.center.position.x = middleVector.x;
  57. detection.bbox.center.position.y = middleVector.y;
  58. detection.bbox.center.position.z = middleVector.z;
  59. detection.bbox.size = ROS2Conversions::ToROS2Vector3(apple.m_appleBoundingBox.GetExtents());
  60. return detection;
  61. }
  62. void AppleDetectionGroundTruth::ConstructROS2Detection3DMessage(const AZStd::vector<PickAppleTask>& apples)
  63. {
  64. m_appleDetections3DMessage.detections.clear();
  65. for (const auto& apple : apples)
  66. {
  67. m_appleDetections3DMessage.detections.push_back(Construct3DDetection(apple));
  68. }
  69. }
  70. vision_msgs::msg::Detection2D AppleDetectionGroundTruth::Construct2DDetection(const PickAppleTask& apple)
  71. {
  72. vision_msgs::msg::Detection2D detection;
  73. detection.header.frame_id = m_frameId.c_str();
  74. detection.id = "Apple";
  75. // TODO - warning, these APIs changed between Humble and Galactic!
  76. // In Galactic, center has no position, just x and y. In Humble, it has position.
  77. // detection.bbox.center.position.x = apple.m_middle.GetX();
  78. // detection.bbox.center.position.y = apple.m_middle.GetY();
  79. detection.bbox.size_x = apple.m_appleBoundingBox.GetExtents().GetX();
  80. detection.bbox.size_y = apple.m_appleBoundingBox.GetExtents().GetY();
  81. return detection;
  82. }
  83. void AppleDetectionGroundTruth::ConstructROS2Detection2DMessage(const AZStd::vector<PickAppleTask>& apples)
  84. {
  85. m_appleDetections2DMessage.detections.clear();
  86. for (const auto& apple : apples)
  87. {
  88. m_appleDetections2DMessage.detections.push_back(Construct2DDetection(apple));
  89. }
  90. }
  91. } // namespace AppleKraken