GeoreferenceLevelComponent.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "GeoreferenceInternalStructures.h"
  11. #include <AzCore/Component/TransformBus.h>
  12. #include <AzCore/Math/Matrix4x4.h>
  13. #include <AzCore/RTTI/BehaviorContext.h>
  14. #include <AzCore/Serialization/EditContext.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <Georeferencing/GeoreferenceStructures.h>
  17. namespace Georeferencing
  18. {
  19. void GeoReferenceLevelConfig::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (auto* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serialize->Class<GeoReferenceLevelConfig, AZ::ComponentConfig>()
  24. ->Version(1)
  25. ->Field("EnuOriginWGS84", &GeoReferenceLevelConfig::m_originLocation)
  26. ->Field("EnuOriginLocationEntityId", &GeoReferenceLevelConfig::m_enuOriginLocationEntityId);
  27. if (auto* editContext = serialize->GetEditContext())
  28. {
  29. editContext->Class<GeoReferenceLevelConfig>("Georeference config", "Georeference config")
  30. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  31. ->DataElement(
  32. AZ::Edit::UIHandlers::Default,
  33. &GeoReferenceLevelConfig::m_enuOriginLocationEntityId,
  34. "ENU Origin Transform",
  35. "ENU (East-North-Up) origin in the level")
  36. ->DataElement(
  37. AZ::Edit::UIHandlers::Default,
  38. &GeoReferenceLevelConfig::m_originLocation,
  39. "ENU Origin Coordinates in WGS84",
  40. "ENU Origin Coordinates in WGS84");
  41. }
  42. }
  43. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  44. {
  45. behaviorContext->EBus<GeoreferenceRequestsBus>("GeoreferenceRequestsBus")
  46. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  47. ->Attribute(AZ::Script::Attributes::Category, "GeoreferenceRequests")
  48. ->Attribute(AZ::Script::Attributes::Module, "GeoreferenceRequests")
  49. ->Event("ConvertFromLevelToWGS84", &GeoreferenceRequests::ConvertFromLevelToWGS84)
  50. ->Event("ConvertFromWGS84ToLevel", &GeoreferenceRequests::ConvertFromWGS84ToLevel)
  51. ->Event("GetRotationFromLevelToENU", &GeoreferenceRequests::GetRotationFromLevelToENU);
  52. }
  53. }
  54. void GeoReferenceLevelController::Reflect(AZ::ReflectContext* context)
  55. {
  56. GeoReferenceLevelConfig::Reflect(context);
  57. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  58. {
  59. serializeContext->Class<GeoReferenceLevelController>()->Version(1)->Field(
  60. "Configuration", &GeoReferenceLevelController::m_config);
  61. AZ::EditContext* editContext = serializeContext->GetEditContext();
  62. if (editContext)
  63. {
  64. editContext->Class<GeoReferenceLevelController>("GeoReferenceLevelController", "Controller for GeoReferenceLevelComponent")
  65. ->ClassElement(AZ::Edit::ClassElements::EditorData, "Manages Georeferencing of the level")
  66. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  67. ->DataElement(AZ::Edit::UIHandlers::Default, &GeoReferenceLevelController::m_config);
  68. }
  69. }
  70. }
  71. GeoReferenceLevelController::GeoReferenceLevelController(const GeoReferenceLevelConfig& config)
  72. : m_config(config)
  73. {
  74. }
  75. void GeoReferenceLevelController::Init()
  76. {
  77. }
  78. void GeoReferenceLevelController::Activate(AZ::EntityId entityId)
  79. {
  80. AZ::EntityBus::Handler::BusConnect(m_config.m_enuOriginLocationEntityId);
  81. AZ::TransformNotificationBus::Handler::BusConnect(m_config.m_enuOriginLocationEntityId);
  82. GeoreferenceRequestsBus::Handler::BusConnect();
  83. GeoreferenceConfigurationRequestsBus::Handler::BusConnect();
  84. }
  85. void GeoReferenceLevelController::Deactivate()
  86. {
  87. GeoreferenceConfigurationRequestsBus::Handler::BusDisconnect();
  88. GeoreferenceRequestsBus::Handler::BusDisconnect();
  89. AZ::TransformNotificationBus::Handler::BusDisconnect();
  90. AZ::EntityBus::Handler::BusDisconnect();
  91. }
  92. void GeoReferenceLevelController::OnEntityActivated(const AZ::EntityId& entityId)
  93. {
  94. AZ_Assert(entityId == m_config.m_enuOriginLocationEntityId, "Entity activated is not the origin entity");
  95. m_enuOriginTransform = AZ::Transform::CreateIdentity();
  96. AZ::TransformBus::EventResult(m_enuOriginTransform, m_config.m_enuOriginLocationEntityId, &AZ::TransformBus::Events::GetWorldTM);
  97. m_enuOriginTransform.Invert();
  98. AZ::TransformNotificationBus::Handler::BusConnect(m_config.m_enuOriginLocationEntityId);
  99. AZ::EntityBus::Handler::BusDisconnect();
  100. }
  101. void GeoReferenceLevelController::OnTransformChanged([[maybe_unused]] const AZ::Transform& local, const AZ::Transform& world)
  102. {
  103. ApplyOriginTransform(world);
  104. }
  105. void GeoReferenceLevelController::ApplyOriginTransform(const AZ::Transform& worldTransform)
  106. {
  107. m_enuOriginTransform = worldTransform.GetInverse();
  108. }
  109. WGS::WGS84Coordinate GeoReferenceLevelController::ConvertFromLevelToWGS84(const AZ::Vector3& xyz)
  110. {
  111. using namespace Georeferencing::Utils::GeodeticConversions;
  112. const auto enu = WGS::Vector3d(m_enuOriginTransform.TransformPoint(xyz));
  113. const auto ecef = ENUToECEF(m_config.m_originLocation, enu);
  114. return ECEFToWGS84(ecef);
  115. }
  116. AZ::Vector3 GeoReferenceLevelController::ConvertFromWGS84ToLevel(const WGS::WGS84Coordinate& latLon)
  117. {
  118. using namespace Georeferencing::Utils::GeodeticConversions;
  119. const auto ecef = WGS84ToECEF(latLon);
  120. const auto enu = ECEFToENU(m_config.m_originLocation, ecef);
  121. return m_enuOriginTransform.GetInverse().TransformPoint(enu.ToVector3f());
  122. };
  123. AZ::Quaternion GeoReferenceLevelController::GetRotationFromLevelToENU()
  124. {
  125. return m_enuOriginTransform.GetRotation();
  126. };
  127. void GeoReferenceLevelController::SetConfiguration(const GeoReferenceLevelConfig& config)
  128. {
  129. m_config = config;
  130. SetOriginEntity(m_config.m_enuOriginLocationEntityId);
  131. }
  132. void GeoReferenceLevelController::SetOriginEntity(const AZ::EntityId& entityId)
  133. {
  134. m_config.m_enuOriginLocationEntityId = entityId;
  135. AZ::Transform transform = AZ::Transform::CreateIdentity();
  136. AZ::TransformBus::EventResult(transform, m_config.m_enuOriginLocationEntityId, &AZ::TransformBus::Events::GetWorldTM);
  137. ApplyOriginTransform(transform);
  138. if (AZ::EntityBus::Handler::BusIsConnected())
  139. {
  140. AZ::EntityBus::Handler::BusDisconnect();
  141. }
  142. if (AZ::TransformNotificationBus::Handler::BusIsConnected())
  143. {
  144. AZ::TransformNotificationBus::Handler::BusDisconnect();
  145. }
  146. AZ::EntityBus::Handler::BusConnect(m_config.m_enuOriginLocationEntityId);
  147. AZ::TransformNotificationBus::Handler::BusConnect(m_config.m_enuOriginLocationEntityId);
  148. }
  149. void GeoReferenceLevelController::SetOriginCoordinates(const WGS::WGS84Coordinate& origin)
  150. {
  151. m_config.m_originLocation = origin;
  152. }
  153. AZ::EntityId GeoReferenceLevelController::GetOriginEntity()
  154. {
  155. return m_config.m_enuOriginLocationEntityId;
  156. }
  157. WGS::WGS84Coordinate GeoReferenceLevelController::GetOriginCoordinates()
  158. {
  159. return m_config.m_originLocation;
  160. }
  161. const GeoReferenceLevelConfig& GeoReferenceLevelController::GetConfiguration() const
  162. {
  163. return m_config;
  164. }
  165. GeoReferenceLevelComponent::GeoReferenceLevelComponent(const GeoReferenceLevelConfig& config)
  166. : GeoReferenceLevelComponentBase(config)
  167. {
  168. }
  169. void GeoReferenceLevelComponent::Reflect(AZ::ReflectContext* context)
  170. {
  171. GeoReferenceLevelComponentBase::Reflect(context);
  172. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  173. {
  174. serialize->Class<GeoReferenceLevelComponent, GeoReferenceLevelComponentBase>()->Version(1);
  175. }
  176. }
  177. void GeoReferenceLevelComponent::Activate()
  178. {
  179. GeoReferenceLevelComponentBase::Activate();
  180. }
  181. void GeoReferenceLevelComponent::Deactivate()
  182. {
  183. GeoReferenceLevelComponentBase::Deactivate();
  184. }
  185. } // namespace Georeferencing