CameraSensor.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #pragma once
  9. #include "CameraPublishers.h"
  10. #include <Atom/Feature/Utils/FrameCaptureBus.h>
  11. #include <AzCore/std/containers/span.h>
  12. #include <AzFramework/Components/CameraBus.h>
  13. #include <ROS2/ROS2GemUtilities.h>
  14. #include <rclcpp/publisher.hpp>
  15. #include <sensor_msgs/msg/camera_info.hpp>
  16. #include <sensor_msgs/msg/image.hpp>
  17. #include <std_msgs/msg/header.hpp>
  18. namespace ROS2
  19. {
  20. //! Class to create camera sensor using Atom renderer
  21. //! It creates dedicated rendering pipeline for each camera
  22. class CameraSensor : public Camera::CameraNotificationBus::Handler
  23. {
  24. public:
  25. //! Initializes rendering pipeline for the camera sensor.
  26. //! @param cameraSensorDescription - camera sensor description used to create camera pipeline.
  27. //! @param entityId - entityId for the owning sensor component.
  28. CameraSensor(const CameraSensorDescription& cameraSensorDescription, const AZ::EntityId& entityId);
  29. //! Deinitializes rendering pipeline for the camera sensor
  30. virtual ~CameraSensor();
  31. //! Publish Image Message frame from rendering pipeline
  32. //! @param cameraPose - current camera pose from which the rendering should take place
  33. //! @param header - header with filled message information (frame, timestamp, seq)
  34. virtual void RequestMessagePublication(const AZ::Transform& cameraPose, const std_msgs::msg::Header& header);
  35. //! Get the camera sensor description
  36. [[nodiscard]] const CameraSensorDescription& GetCameraSensorDescription() const;
  37. private:
  38. // CameraNotificationBus overrides
  39. void OnCameraRemoved(const AZ::EntityId& cameraEntityId) override;
  40. void OnActiveViewChanged(const AZ::EntityId& cameraEntityId) override;
  41. void UpdateViewAlias();
  42. AZStd::vector<AZStd::string> m_passHierarchy;
  43. AZ::RPI::ViewPtr m_view;
  44. AZ::RPI::Scene* m_scene = nullptr;
  45. const AZ::Transform AtomToRos{ AZ::Transform::CreateFromQuaternion(
  46. AZ::Quaternion::CreateFromMatrix3x3(AZ::Matrix3x3::CreateFromRows({ 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, -1 }))) };
  47. virtual AZStd::string GetPipelineTemplateName() const = 0; //! Returns name of pass template to use in pipeline
  48. virtual CameraSensorDescription::CameraChannelType GetChannelType()
  49. const = 0; //! Type of returned data eg Color, Depth, Optical flow
  50. protected:
  51. CameraSensorDescription m_cameraSensorDescription;
  52. CameraPublishers m_cameraPublishers;
  53. AZ::EntityId m_entityId;
  54. AZ::RPI::RenderPipelinePtr m_pipeline;
  55. AZStd::string m_pipelineName;
  56. //! Request a frame from the rendering pipeline
  57. //! @param cameraPose - current camera pose from which the rendering should take place
  58. //! @param callback - callback function object that will be called when capture is ready.
  59. //! It's argument is readback structure containing, among other things, a captured image
  60. void RequestFrame(
  61. const AZ::Transform& cameraPose, AZStd::function<void(const AZ::RPI::AttachmentReadback::ReadbackResult& result)> callback);
  62. //! Read and setup Atom Passes
  63. void SetupPasses();
  64. };
  65. //! Implementation of camera sensors that runs pipeline which produces depth image
  66. class CameraDepthSensor : public CameraSensor
  67. {
  68. public:
  69. CameraDepthSensor(const CameraSensorDescription& cameraSensorDescription, const AZ::EntityId& entityId);
  70. private:
  71. AZStd::string GetPipelineTemplateName() const override;
  72. CameraSensorDescription::CameraChannelType GetChannelType() const override;
  73. };
  74. //! Implementation of camera sensors that runs pipeline which produces color image
  75. class CameraColorSensor : public CameraSensor
  76. {
  77. public:
  78. CameraColorSensor(const CameraSensorDescription& cameraSensorDescription, const AZ::EntityId& entityId);
  79. private:
  80. AZStd::string GetPipelineTemplateName() const override;
  81. CameraSensorDescription::CameraChannelType GetChannelType() const override;
  82. };
  83. //! Implementation of camera sensors that runs pipeline which produces color image and readbacks a depth image from pipeline
  84. class CameraRGBDSensor : public CameraColorSensor
  85. {
  86. public:
  87. CameraRGBDSensor(const CameraSensorDescription& cameraSensorDescription, const AZ::EntityId& entityId);
  88. // CameraSensor overrides
  89. void RequestMessagePublication(const AZ::Transform& cameraPose, const std_msgs::msg::Header& header) override;
  90. private:
  91. void ReadBackDepth(AZStd::function<void(const AZ::RPI::AttachmentReadback::ReadbackResult& result)> callback);
  92. };
  93. } // namespace ROS2