GeoreferenceLevelComponent.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "GeoreferenceLevelComponent.h"
  9. #include "GNSSFormatConversions.h"
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Math/Matrix4x4.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <ROS2/Georeference/GeoreferenceStructures.h>
  15. namespace ROS2
  16. {
  17. void GeoReferenceLevelConfig::Reflect(AZ::ReflectContext* context)
  18. {
  19. WGS::WGS84Coordinate::Reflect(context);
  20. if (auto* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  21. {
  22. serialize->Class<GeoReferenceLevelConfig, AZ::ComponentConfig>()
  23. ->Version(1)
  24. ->Field("EnuOriginWGS84", &GeoReferenceLevelConfig::m_originLocation)
  25. ->Field("EnuOriginLocationEntityId", &GeoReferenceLevelConfig::m_enuOriginLocationEntityId);
  26. if (auto* editContext = serialize->GetEditContext())
  27. {
  28. editContext->Class<GeoReferenceLevelConfig>("Georeference config", "Georeference config")
  29. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  30. ->DataElement(
  31. AZ::Edit::UIHandlers::Default,
  32. &GeoReferenceLevelConfig::m_enuOriginLocationEntityId,
  33. "ENU Origin Transform",
  34. "ENU (East-North-Up) origin in the level")
  35. ->DataElement(
  36. AZ::Edit::UIHandlers::Default,
  37. &GeoReferenceLevelConfig::m_originLocation,
  38. "ENU Origin Coordinates in WGS84",
  39. "ENU Origin Coordinates in WGS84");
  40. }
  41. }
  42. }
  43. void GeoReferenceLevelController::Reflect(AZ::ReflectContext* context)
  44. {
  45. GeoReferenceLevelConfig::Reflect(context);
  46. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  47. {
  48. serializeContext->Class<GeoReferenceLevelController>()->Version(1)->Field(
  49. "Configuration", &GeoReferenceLevelController::m_config);
  50. AZ::EditContext* editContext = serializeContext->GetEditContext();
  51. if (editContext)
  52. {
  53. editContext->Class<GeoReferenceLevelController>("GeoReferenceLevelController", "Controller for GeoReferenceLevelComponent")
  54. ->ClassElement(AZ::Edit::ClassElements::EditorData, "Manages spawning of robots in configurable locations")
  55. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  56. ->DataElement(AZ::Edit::UIHandlers::Default, &GeoReferenceLevelController::m_config);
  57. }
  58. }
  59. }
  60. GeoReferenceLevelController::GeoReferenceLevelController(const GeoReferenceLevelConfig& config)
  61. : m_config(config)
  62. {
  63. }
  64. void GeoReferenceLevelController::Init()
  65. {
  66. }
  67. void GeoReferenceLevelController::Activate(AZ::EntityId entityId)
  68. {
  69. AZ::EntityBus::Handler::BusConnect(m_config.m_enuOriginLocationEntityId);
  70. GeoreferenceRequestsBus::Handler::BusConnect();
  71. }
  72. void GeoReferenceLevelController::Deactivate()
  73. {
  74. GeoreferenceRequestsBus::Handler::BusDisconnect();
  75. AZ::EntityBus::Handler::BusDisconnect();
  76. }
  77. void GeoReferenceLevelController::OnEntityActivated(const AZ::EntityId& entityId)
  78. {
  79. m_enuOriginTransform = AZ::Transform::CreateIdentity();
  80. AZ::TransformBus::EventResult(m_enuOriginTransform, m_config.m_enuOriginLocationEntityId, &AZ::TransformBus::Events::GetWorldTM);
  81. m_enuOriginTransform.Invert();
  82. AZ::EntityBus::Handler::BusDisconnect();
  83. }
  84. WGS::WGS84Coordinate GeoReferenceLevelController::ConvertFromLevelToWGS84(const AZ::Vector3& xyz)
  85. {
  86. using namespace ROS2::Utils::GeodeticConversions;
  87. const auto enu = WGS::Vector3d(m_enuOriginTransform.TransformPoint(xyz));
  88. const auto ecef = ENUToECEF(m_config.m_originLocation, enu);
  89. return ECEFToWGS84(ecef);
  90. }
  91. AZ::Vector3 GeoReferenceLevelController::ConvertFromWGS84ToLevel(const WGS::WGS84Coordinate& latLon)
  92. {
  93. using namespace ROS2::Utils::GeodeticConversions;
  94. const auto ecef = WGS84ToECEF(latLon);
  95. const auto enu = ECEFToENU(m_config.m_originLocation, ecef);
  96. return m_enuOriginTransform.GetInverse().TransformPoint(enu.ToVector3f());
  97. };
  98. AZ::Quaternion GeoReferenceLevelController::GetRotationFromLevelToENU()
  99. {
  100. return m_enuOriginTransform.GetRotation();
  101. };
  102. void GeoReferenceLevelController::SetConfiguration(const GeoReferenceLevelConfig& config)
  103. {
  104. m_config = config;
  105. }
  106. const GeoReferenceLevelConfig& GeoReferenceLevelController::GetConfiguration() const
  107. {
  108. return m_config;
  109. }
  110. GeoReferenceLevelComponent::GeoReferenceLevelComponent(const GeoReferenceLevelConfig& config)
  111. : GeoReferenceLevelComponentBase(config)
  112. {
  113. }
  114. void GeoReferenceLevelComponent::Reflect(AZ::ReflectContext* context)
  115. {
  116. GeoReferenceLevelComponentBase::Reflect(context);
  117. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  118. {
  119. serialize->Class<GeoReferenceLevelComponent, GeoReferenceLevelComponentBase>()->Version(1);
  120. }
  121. }
  122. void GeoReferenceLevelComponent::Activate()
  123. {
  124. GeoReferenceLevelComponentBase::Activate();
  125. }
  126. void GeoReferenceLevelComponent::Deactivate()
  127. {
  128. GeoReferenceLevelComponentBase::Deactivate();
  129. }
  130. } // namespace ROS2