LidarRegistrarSystemComponent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/Serialization/EditContext.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <Lidar/LidarRegistrarSystemComponent.h>
  11. #include <ROS2Sensors/Lidar/LidarTemplate.h>
  12. namespace ROS2Sensors
  13. {
  14. LidarRegistrarSystemComponent::LidarRegistrarSystemComponent()
  15. {
  16. if (!LidarRegistrarInterface::Get())
  17. {
  18. LidarRegistrarInterface::Register(this);
  19. }
  20. }
  21. LidarRegistrarSystemComponent::~LidarRegistrarSystemComponent()
  22. {
  23. if (LidarRegistrarInterface::Get() == this)
  24. {
  25. LidarRegistrarInterface::Unregister(this);
  26. }
  27. }
  28. void LidarRegistrarSystemComponent::Init()
  29. {
  30. }
  31. void LidarRegistrarSystemComponent::Activate()
  32. {
  33. m_physxLidarSystem.Activate();
  34. }
  35. void LidarRegistrarSystemComponent::Deactivate()
  36. {
  37. m_physxLidarSystem.Deactivate();
  38. }
  39. void LidarRegistrarSystemComponent::Reflect(AZ::ReflectContext* context)
  40. {
  41. LidarTemplate::Reflect(context);
  42. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  43. {
  44. serializeContext->Class<LidarRegistrarSystemComponent, AZ::Component>()->Version(0);
  45. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  46. {
  47. editContext
  48. ->Class<LidarRegistrarSystemComponent>(
  49. "Lidar Registrar", "Manages the LidarSystem registration and stores their metadata.")
  50. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  51. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
  52. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  53. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  54. }
  55. }
  56. }
  57. void LidarRegistrarSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  58. {
  59. provided.push_back(AZ_CRC_CE("LidarRegistrarService"));
  60. }
  61. void LidarRegistrarSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  62. {
  63. incompatible.push_back(AZ_CRC_CE("LidarRegistrarService"));
  64. }
  65. void LidarRegistrarSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  66. {
  67. required.push_back(AZ_CRC_CE("ROS2Service"));
  68. }
  69. void LidarRegistrarSystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  70. {
  71. }
  72. void LidarRegistrarSystemComponent::RegisterLidarSystem(const char* name, const char* description, const LidarSystemFeatures& features)
  73. {
  74. AZ_Assert(
  75. !m_registeredLidarSystems.contains(AZ_CRC(name)),
  76. "A lidar system with the provided name already exists. Please choose a different name.");
  77. m_registeredLidarSystems.emplace(AZ_CRC(name), LidarSystemMetaData{ name, description, features });
  78. }
  79. AZStd::vector<AZStd::string> LidarRegistrarSystemComponent::GetRegisteredLidarSystems() const
  80. {
  81. AZStd::vector<AZStd::string> lidarSystemList;
  82. lidarSystemList.reserve(m_registeredLidarSystems.size());
  83. for (const auto& lidarSystem : m_registeredLidarSystems)
  84. {
  85. lidarSystemList.push_back(lidarSystem.second.m_name);
  86. }
  87. return lidarSystemList;
  88. }
  89. const LidarSystemMetaData* LidarRegistrarSystemComponent::GetLidarSystemMetaData(const AZStd::string& name) const
  90. {
  91. if (auto lidarSystem = m_registeredLidarSystems.find(AZ_CRC(name)); lidarSystem != m_registeredLidarSystems.end())
  92. {
  93. return &lidarSystem->second;
  94. }
  95. return nullptr;
  96. }
  97. AZStd::string Details::GetDefaultLidarSystem()
  98. {
  99. const auto& lidarInterface = LidarRegistrarInterface::Get();
  100. AZ_Assert(lidarInterface, "LidarRegistrarInterface is not registered.");
  101. const auto& lidarSystemList = lidarInterface->GetRegisteredLidarSystems();
  102. if (lidarSystemList.empty())
  103. {
  104. AZ_Warning("ROS2LidarSensorComponent", false, "No LIDAR system for the sensor to use.");
  105. return {};
  106. }
  107. return lidarSystemList.front();
  108. }
  109. } // namespace ROS2Sensors