GeoreferenceComponentTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/Asset/AssetManagerComponent.h>
  9. #include <AzCore/Component/ComponentApplication.h>
  10. #include <AzCore/Component/ComponentApplicationBus.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzCore/Component/EntityId.h>
  13. #include <AzCore/RTTI/RTTIMacros.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Slice/SliceAssetHandler.h>
  16. #include <AzCore/UserSettings/UserSettingsComponent.h>
  17. #include <AzCore/std/containers/array.h>
  18. #include <AzCore/std/string/string_view.h>
  19. #include <AzQtComponents/Utilities/QtPluginPaths.h>
  20. #include <AzTest/GemTestEnvironment.h>
  21. #include <AzToolsFramework/Entity/EditorEntityContextComponent.h>
  22. #include <AzToolsFramework/ToolsComponents/TransformComponent.h>
  23. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  24. #include <AzToolsFramework/UnitTest/ToolsTestApplication.h>
  25. #include <QApplication>
  26. #include <gtest/gtest.h>
  27. #include <memory>
  28. #include <string>
  29. #include <string_view>
  30. #include <vector>
  31. #include "Clients/GeoreferenceLevelComponent.h"
  32. #include "Clients/GeoreferencingSystemComponent.h"
  33. #include <Georeferencing/GeoreferenceBus.h>
  34. namespace UnitTest
  35. {
  36. constexpr float OneMillimeter = 0.001f;
  37. constexpr double MaxWGSError = 0.001 * (1.0 / 60.0 / 60.0); // 0.001 arc second
  38. class GeoreferenceComponentTestEnvironment : public AZ::Test::GemTestEnvironment
  39. {
  40. // AZ::Test::GemTestEnvironment overrides ...
  41. void AddGemsAndComponents() override;
  42. AZ::ComponentApplication* CreateApplicationInstance() override;
  43. void PostSystemEntityActivate() override;
  44. public:
  45. GeoreferenceComponentTestEnvironment() = default;
  46. ~GeoreferenceComponentTestEnvironment() override = default;
  47. };
  48. void GeoreferenceComponentTestEnvironment::AddGemsAndComponents()
  49. {
  50. AddActiveGems(AZStd::to_array<AZStd::string_view>({ "LevelGeoreferencing" }));
  51. AddDynamicModulePaths({});
  52. AddComponentDescriptors(AZStd::initializer_list<AZ::ComponentDescriptor*>{
  53. Georeferencing::GeoReferenceLevelComponent::CreateDescriptor(),
  54. Georeferencing::GeoreferencingSystemComponent::CreateDescriptor(),
  55. });
  56. AddRequiredComponents(AZStd::to_array<AZ::TypeId const>({ Georeferencing::GeoreferencingSystemComponent::TYPEINFO_Uuid() }));
  57. }
  58. AZ::ComponentApplication* GeoreferenceComponentTestEnvironment::CreateApplicationInstance()
  59. {
  60. // Using ToolsTestApplication to have AzFramework and AzToolsFramework components.
  61. return aznew UnitTest::ToolsTestApplication("GeoreferenceComponentTestEnvironment");
  62. }
  63. void GeoreferenceComponentTestEnvironment::PostSystemEntityActivate()
  64. {
  65. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  66. }
  67. class GeoreferenceComponentTestFixture : public ::testing::Test
  68. {
  69. protected:
  70. void SetUp() override;
  71. void TearDown() override;
  72. AZ::Entity* m_levelEntity;
  73. AZ::Entity* m_originEntity;
  74. };
  75. void GeoreferenceComponentTestFixture::SetUp()
  76. {
  77. m_levelEntity = aznew AZ::Entity();
  78. m_originEntity = aznew AZ::Entity();
  79. AZ_Assert(m_levelEntity, "Failed to create level entity");
  80. AZ_Assert(m_originEntity, "Failed to create origin entity");
  81. auto component = m_levelEntity->CreateComponent<Georeferencing::GeoReferenceLevelComponent>();
  82. AZ_Assert(component, "Failed to create GeoreferenceLevelComponent");
  83. auto transformComponent = m_originEntity->CreateComponent<AzFramework::TransformComponent>();
  84. AZ_Assert(transformComponent, "Failed to create TransformComponent");
  85. m_levelEntity->Init();
  86. m_originEntity->Init();
  87. m_levelEntity->Activate();
  88. m_originEntity->Activate();
  89. Georeferencing::GeoreferenceConfigurationRequestsBus::Broadcast(
  90. &Georeferencing::GeoreferenceConfigurationRequests::SetOriginEntity, m_originEntity->GetId());
  91. }
  92. void GeoreferenceComponentTestFixture::TearDown()
  93. {
  94. delete m_levelEntity;
  95. delete m_originEntity;
  96. }
  97. TEST_F(GeoreferenceComponentTestFixture, ComponentSmokeTest)
  98. {
  99. EXPECT_TRUE(m_levelEntity->GetState() == AZ::Entity::State::Active);
  100. EXPECT_TRUE(m_originEntity->GetState() == AZ::Entity::State::Active);
  101. }
  102. TEST_F(GeoreferenceComponentTestFixture, TestConfiguration)
  103. {
  104. // Set the origin entity as the origin of the level entity
  105. Georeferencing::GeoreferenceConfigurationRequestsBus::Broadcast(
  106. &Georeferencing::GeoreferenceConfigurationRequests::SetOriginEntity, m_originEntity->GetId());
  107. // Set origin coordinates
  108. Georeferencing::WGS::WGS84Coordinate originCoordinate;
  109. originCoordinate.SetAltitude(0.0);
  110. originCoordinate.SetLatitude(25.0);
  111. originCoordinate.SetAltitude(35.0);
  112. Georeferencing::GeoreferenceConfigurationRequestsBus::Broadcast(
  113. &Georeferencing::GeoreferenceConfigurationRequests::SetOriginCoordinates, originCoordinate);
  114. AZ::EntityId originEntityId;
  115. Georeferencing::GeoreferenceConfigurationRequestsBus::BroadcastResult(
  116. originEntityId, &Georeferencing::GeoreferenceConfigurationRequests::GetOriginEntity);
  117. EXPECT_TRUE(originEntityId == m_originEntity->GetId());
  118. Georeferencing::WGS::WGS84Coordinate originCoordinateResult;
  119. Georeferencing::GeoreferenceConfigurationRequestsBus::BroadcastResult(
  120. originCoordinateResult, &Georeferencing::GeoreferenceConfigurationRequests::GetOriginCoordinates);
  121. EXPECT_TRUE(originCoordinateResult == originCoordinate);
  122. }
  123. TEST_F(GeoreferenceComponentTestFixture, TestConversionToLevelAtOrigin)
  124. {
  125. // Set origin coordinates
  126. const Georeferencing::WGS::WGS84Coordinate originCoordinate{ 25.0, 35.0, 0.0 };
  127. Georeferencing::GeoreferenceConfigurationRequestsBus::Broadcast(
  128. &Georeferencing::GeoreferenceConfigurationRequests::SetOriginCoordinates, originCoordinate);
  129. // convert to level
  130. AZ::Vector3 levelCoordinate;
  131. Georeferencing::GeoreferenceRequestsBus::BroadcastResult(
  132. levelCoordinate, &Georeferencing::GeoreferenceRequests::ConvertFromWGS84ToLevel, originCoordinate);
  133. EXPECT_NEAR(levelCoordinate.GetX(), 0.0, OneMillimeter);
  134. EXPECT_NEAR(levelCoordinate.GetY(), 0.0, OneMillimeter);
  135. EXPECT_NEAR(levelCoordinate.GetZ(), 0.0, OneMillimeter);
  136. }
  137. TEST_F(GeoreferenceComponentTestFixture, TestConversionToWGS84AtOrigin)
  138. {
  139. // Set origin coordinates
  140. const Georeferencing::WGS::WGS84Coordinate originCoordinate{ 25.0, 35.0, 0.0 };
  141. Georeferencing::GeoreferenceConfigurationRequestsBus::Broadcast(
  142. &Georeferencing::GeoreferenceConfigurationRequests::SetOriginCoordinates, originCoordinate);
  143. Georeferencing::WGS::WGS84Coordinate resultCoordinate;
  144. Georeferencing::GeoreferenceRequestsBus::BroadcastResult(
  145. resultCoordinate, &Georeferencing::GeoreferenceRequests::ConvertFromLevelToWGS84, AZ::Vector3::CreateZero());
  146. EXPECT_NEAR(resultCoordinate.GetLatitude(), 25.0, MaxWGSError);
  147. EXPECT_NEAR(resultCoordinate.GetLongitude(), 35.0, MaxWGSError);
  148. EXPECT_NEAR(resultCoordinate.GetAltitude(), 0.0, MaxWGSError);
  149. }
  150. TEST_F(GeoreferenceComponentTestFixture, TestNavigationInDirectionENU)
  151. {
  152. // Set origin coordinates
  153. const Georeferencing::WGS::WGS84Coordinate originCoordinate{ 25.0, 35.0, 0.0 };
  154. Georeferencing::GeoreferenceConfigurationRequestsBus::Broadcast(
  155. &Georeferencing::GeoreferenceConfigurationRequests::SetOriginCoordinates, originCoordinate);
  156. const AZ::Vector3 north = AZ::Vector3::CreateAxisY(); // One meter north
  157. Georeferencing::WGS::WGS84Coordinate resultCoordinate;
  158. Georeferencing::GeoreferenceRequestsBus::BroadcastResult(
  159. resultCoordinate, &Georeferencing::GeoreferenceRequests::ConvertFromLevelToWGS84, north);
  160. EXPECT_GT(resultCoordinate.GetLatitude(), 25.0);
  161. EXPECT_NEAR(resultCoordinate.GetLongitude(), 35.0, MaxWGSError);
  162. EXPECT_NEAR(resultCoordinate.GetAltitude(), 0.0, MaxWGSError);
  163. }
  164. TEST_F(GeoreferenceComponentTestFixture, TestNavigationInDirectionRotatedENU)
  165. {
  166. // Set origin coordinates
  167. const Georeferencing::WGS::WGS84Coordinate originCoordinate{ 25.0, 35.0, 0.0 };
  168. Georeferencing::GeoreferenceConfigurationRequestsBus::Broadcast(
  169. &Georeferencing::GeoreferenceConfigurationRequests::SetOriginCoordinates, originCoordinate);
  170. // level is rotated 90 degrees around Z axis
  171. // positive X direction is of the level is now pointing east
  172. // negative Y direction is of the level is now pointing north
  173. const AZ::Transform transform = AZ::Transform::CreateRotationZ(AZ::DegToRad(90.0));
  174. AZ_Assert(m_originEntity, "No origin entity");
  175. AZ_Assert(m_originEntity->GetState() == AZ::Entity::State::Active, "Origin entity is not active");
  176. AZ::TransformBus::Event(m_originEntity->GetId(), &AZ::TransformBus::Events::SetWorldTM, transform);
  177. // Query is point straight north
  178. const Georeferencing::WGS::WGS84Coordinate queryCoordinate{ 25.001, 35.0, 0.0 };
  179. AZ::Vector3 resultLevel;
  180. Georeferencing::GeoreferenceRequestsBus::BroadcastResult(
  181. resultLevel, &Georeferencing::GeoreferenceRequests::ConvertFromWGS84ToLevel, queryCoordinate);
  182. EXPECT_LT(resultLevel.GetX(), 0.0);
  183. EXPECT_NEAR(resultLevel.GetY(), 0.0, OneMillimeter);
  184. }
  185. } // namespace UnitTest
  186. // required to support running integration tests with Qt and PhysX
  187. AZTEST_EXPORT int AZ_UNIT_TEST_HOOK_NAME(int argc, char** argv)
  188. {
  189. ::testing::InitGoogleMock(&argc, argv);
  190. AzQtComponents::PrepareQtPaths();
  191. QApplication app(argc, argv);
  192. AZ::Test::printUnusedParametersWarning(argc, argv);
  193. AZ::Test::addTestEnvironments({ new UnitTest::GeoreferenceComponentTestEnvironment() });
  194. int result = RUN_ALL_TESTS();
  195. return result;
  196. }
  197. IMPLEMENT_TEST_EXECUTABLE_MAIN();