CameraSensor.h 4.8 KB

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