ROS2CameraSensorEditorComponent.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "ROS2CameraSensorEditorComponent.h"
  9. #include "CameraConstants.h"
  10. #include "CameraUtilities.h"
  11. #include "ROS2CameraSensorComponent.h"
  12. #include <AzCore/Component/TransformBus.h>
  13. #include <ROS2/Frame/ROS2FrameComponent.h>
  14. namespace ROS2
  15. {
  16. ROS2CameraSensorEditorComponent::ROS2CameraSensorEditorComponent()
  17. {
  18. m_sensorConfiguration.m_frequency = 10;
  19. m_sensorConfiguration.m_publishersConfigurations.insert(
  20. MakeTopicConfigurationPair("camera_image_color", CameraConstants::ImageMessageType, CameraConstants::ColorImageConfig));
  21. m_sensorConfiguration.m_publishersConfigurations.insert(
  22. MakeTopicConfigurationPair("camera_image_depth", CameraConstants::ImageMessageType, CameraConstants::DepthImageConfig));
  23. m_sensorConfiguration.m_publishersConfigurations.insert(
  24. MakeTopicConfigurationPair("color_camera_info", CameraConstants::CameraInfoMessageType, CameraConstants::ColorInfoConfig));
  25. m_sensorConfiguration.m_publishersConfigurations.insert(
  26. MakeTopicConfigurationPair("depth_camera_info", CameraConstants::CameraInfoMessageType, CameraConstants::DepthInfoConfig));
  27. }
  28. ROS2CameraSensorEditorComponent::ROS2CameraSensorEditorComponent(
  29. const SensorConfiguration& sensorConfiguration, const CameraSensorConfiguration& cameraConfiguration)
  30. : m_sensorConfiguration(sensorConfiguration)
  31. , m_cameraSensorConfiguration(cameraConfiguration)
  32. {
  33. }
  34. void ROS2CameraSensorEditorComponent::Reflect(AZ::ReflectContext* context)
  35. {
  36. CameraSensorConfiguration::Reflect(context);
  37. if (auto* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  38. {
  39. serialize->Class<ROS2CameraSensorEditorComponent, AzToolsFramework::Components::EditorComponentBase>()
  40. ->Version(4)
  41. ->Field("CameraSensorConfig", &ROS2CameraSensorEditorComponent::m_cameraSensorConfiguration)
  42. ->Field("SensorConfig", &ROS2CameraSensorEditorComponent::m_sensorConfiguration);
  43. if (AZ::EditContext* editContext = serialize->GetEditContext())
  44. {
  45. editContext->Class<ROS2CameraSensorEditorComponent>("ROS2 Camera Sensor", "[Camera component]")
  46. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  47. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  48. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  49. ->DataElement(
  50. AZ::Edit::UIHandlers::Default,
  51. &ROS2CameraSensorEditorComponent::m_sensorConfiguration,
  52. "Sensor configuration",
  53. "Sensor configuration")
  54. ->DataElement(
  55. AZ::Edit::UIHandlers::Default,
  56. &ROS2CameraSensorEditorComponent::m_cameraSensorConfiguration,
  57. "Camera configuration",
  58. "Camera configuration.");
  59. }
  60. }
  61. }
  62. void ROS2CameraSensorEditorComponent::Activate()
  63. {
  64. AzFramework::EntityDebugDisplayEventBus::Handler::BusConnect(this->GetEntityId());
  65. AzToolsFramework::Components::EditorComponentBase::Activate();
  66. ROS2::CameraCalibrationRequestBus::Handler::BusConnect(GetEntityId());
  67. }
  68. void ROS2CameraSensorEditorComponent::Deactivate()
  69. {
  70. AzToolsFramework::Components::EditorComponentBase::Deactivate();
  71. AzFramework::EntityDebugDisplayEventBus::Handler::BusDisconnect();
  72. ROS2::CameraCalibrationRequestBus::Handler::BusDisconnect(GetEntityId());
  73. }
  74. void ROS2CameraSensorEditorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  75. {
  76. required.push_back(AZ_CRC("ROS2Frame"));
  77. }
  78. void ROS2CameraSensorEditorComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  79. {
  80. incompatible.push_back(AZ_CRC_CE("ROS2CameraSensor"));
  81. }
  82. void ROS2CameraSensorEditorComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  83. {
  84. provided.push_back(AZ_CRC_CE("ROS2CameraSensor"));
  85. }
  86. void ROS2CameraSensorEditorComponent::BuildGameEntity(AZ::Entity* gameEntity)
  87. {
  88. gameEntity->CreateComponent<ROS2::ROS2CameraSensorComponent>(m_sensorConfiguration, m_cameraSensorConfiguration);
  89. }
  90. AZ::Matrix3x3 ROS2CameraSensorEditorComponent::GetCameraMatrix() const
  91. {
  92. return CameraUtils::MakeCameraIntrinsics(
  93. m_cameraSensorConfiguration.m_width,
  94. m_cameraSensorConfiguration.m_height,
  95. m_cameraSensorConfiguration.m_verticalFieldOfViewDeg);
  96. };
  97. int ROS2CameraSensorEditorComponent::GetWidth() const
  98. {
  99. return m_cameraSensorConfiguration.m_width;
  100. };
  101. int ROS2CameraSensorEditorComponent::GetHeight() const
  102. {
  103. return m_cameraSensorConfiguration.m_height;
  104. };
  105. float ROS2CameraSensorEditorComponent::GetVerticalFOV() const
  106. {
  107. return m_cameraSensorConfiguration.m_verticalFieldOfViewDeg;
  108. };
  109. void ROS2CameraSensorEditorComponent::DisplayEntityViewport(
  110. [[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo, AzFramework::DebugDisplayRequests& debugDisplay)
  111. {
  112. if (!m_sensorConfiguration.m_visualize)
  113. {
  114. return;
  115. }
  116. const AZ::u32 stateBefore = debugDisplay.GetState();
  117. AZ::Transform transform = GetEntity()->GetTransform()->GetWorldTM();
  118. const float distance = m_cameraSensorConfiguration.m_farClipDistance * 0.1f;
  119. const float tangent = static_cast<float>(tan(0.5f * AZ::DegToRad(m_cameraSensorConfiguration.m_verticalFieldOfViewDeg)));
  120. float height = distance * tangent;
  121. float width = height * m_cameraSensorConfiguration.m_width / m_cameraSensorConfiguration.m_height;
  122. AZ::Vector3 farPoints[4];
  123. farPoints[0] = AZ::Vector3(width, height, distance);
  124. farPoints[1] = AZ::Vector3(-width, height, distance);
  125. farPoints[2] = AZ::Vector3(-width, -height, distance);
  126. farPoints[3] = AZ::Vector3(width, -height, distance);
  127. AZ::Vector3 nearPoints[4];
  128. nearPoints[0] = farPoints[0].GetNormalizedSafe() * m_cameraSensorConfiguration.m_nearClipDistance;
  129. nearPoints[1] = farPoints[1].GetNormalizedSafe() * m_cameraSensorConfiguration.m_nearClipDistance;
  130. nearPoints[2] = farPoints[2].GetNormalizedSafe() * m_cameraSensorConfiguration.m_nearClipDistance;
  131. nearPoints[3] = farPoints[3].GetNormalizedSafe() * m_cameraSensorConfiguration.m_nearClipDistance;
  132. // dimension of drawing
  133. const float arrowRise = 0.11f;
  134. const float arrowSize = 0.05f;
  135. const AZ::Vector3 pt0(0, 0, 0);
  136. const auto ptz = AZ::Vector3::CreateAxisZ(0.2f);
  137. const auto pty = AZ::Vector3::CreateAxisY(0.2f);
  138. const auto ptx = AZ::Vector3::CreateAxisX(0.2f);
  139. debugDisplay.PushMatrix(transform);
  140. debugDisplay.SetColor(AZ::Color(0.f, 1.f, 1.f, 1.f));
  141. debugDisplay.DrawLine(nearPoints[0], farPoints[0]);
  142. debugDisplay.DrawLine(nearPoints[1], farPoints[1]);
  143. debugDisplay.DrawLine(nearPoints[2], farPoints[2]);
  144. debugDisplay.DrawLine(nearPoints[3], farPoints[3]);
  145. debugDisplay.DrawPolyLine(nearPoints, AZ_ARRAY_SIZE(nearPoints));
  146. debugDisplay.DrawPolyLine(farPoints, AZ_ARRAY_SIZE(farPoints));
  147. // up-arrow drawing
  148. const AZ::Vector3 pa1(-arrowSize * height, -arrowRise * width, 1.f);
  149. const AZ::Vector3 pa2(arrowSize * height, -arrowRise * width, 1.f);
  150. const AZ::Vector3 pa3(0, (-arrowRise - arrowSize) * width, 1.f);
  151. debugDisplay.SetColor(AZ::Color(0.f, 0.6f, 1.f, 1.f));
  152. debugDisplay.SetLineWidth(1);
  153. debugDisplay.DrawLine(pa1, pa2);
  154. debugDisplay.DrawLine(pa2, pa3);
  155. debugDisplay.DrawLine(pa3, pa1);
  156. // coordinate system drawing
  157. debugDisplay.SetColor(AZ::Color(1.f, 0.f, 0.f, 1.f));
  158. debugDisplay.SetLineWidth(2);
  159. debugDisplay.DrawLine(ptx, pt0);
  160. debugDisplay.SetColor(AZ::Color(0.f, 1.f, 0.f, 1.f));
  161. debugDisplay.SetLineWidth(2);
  162. debugDisplay.DrawLine(pty, pt0);
  163. debugDisplay.SetColor(AZ::Color(0.f, 0.f, 1.f, 1.f));
  164. debugDisplay.SetLineWidth(2);
  165. debugDisplay.DrawLine(ptz, pt0);
  166. debugDisplay.PopMatrix();
  167. debugDisplay.SetState(stateBefore);
  168. }
  169. AZStd::pair<AZStd::string, TopicConfiguration> ROS2CameraSensorEditorComponent::MakeTopicConfigurationPair(
  170. const AZStd::string& topic, const AZStd::string& messageType, const AZStd::string& configName) const
  171. {
  172. TopicConfiguration config;
  173. config.m_topic = topic;
  174. config.m_type = messageType;
  175. return AZStd::make_pair(configName, config);
  176. }
  177. } // namespace ROS2