WhiteBoxTestUtil.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "WhiteBoxTestUtil.h"
  9. namespace WhiteBox
  10. {
  11. std::ostream& operator<<(std::ostream& os, const Api::FaceHandle faceHandle)
  12. {
  13. return os << Api::ToString(faceHandle).c_str();
  14. }
  15. std::ostream& operator<<(std::ostream& os, const Api::VertexHandle vertexHandle)
  16. {
  17. return os << Api::ToString(vertexHandle).c_str();
  18. }
  19. std::ostream& operator<<(std::ostream& os, const Api::PolygonHandle& polygonHandle)
  20. {
  21. return os << Api::ToString(polygonHandle).c_str();
  22. }
  23. std::ostream& operator<<(std::ostream& os, const Api::EdgeHandle edgeHandle)
  24. {
  25. return os << Api::ToString(edgeHandle).c_str();
  26. }
  27. std::ostream& operator<<(std::ostream& os, const Api::HalfedgeHandle halfedgeHandle)
  28. {
  29. return os << Api::ToString(halfedgeHandle).c_str();
  30. }
  31. std::ostream& operator<<(std::ostream& os, const Api::FaceVertHandles& faceVertHandles)
  32. {
  33. return os << Api::ToString(faceVertHandles).c_str();
  34. }
  35. std::ostream& operator<<(std::ostream& os, const Api::FaceVertHandlesList& faceVertHandlesList)
  36. {
  37. return os << Api::ToString(faceVertHandlesList).c_str();
  38. }
  39. AZStd::string VerticesToString(const WhiteBoxMesh* whiteBox, const AZ::Transform& localToWorld)
  40. {
  41. const auto vertexPositions = Api::MeshVertexPositions(*whiteBox);
  42. AZStd::string vertexString = "\n----------------------------------------------\n";
  43. for (size_t i = 0; i < vertexPositions.size(); i++)
  44. {
  45. const auto& p = vertexPositions[i];
  46. const auto v = localToWorld.TransformPoint(p);
  47. vertexString += AZStd::string::format("Vertex %zu: %f, %f, %f\n", i, v.GetX(), v.GetY(), v.GetZ());
  48. }
  49. vertexString += "----------------------------------------------\n";
  50. return vertexString;
  51. }
  52. AZStd::string FacesMidPointsToString(const WhiteBoxMesh* whiteBox, const AZ::Transform& localToWorld)
  53. {
  54. std::stringstream ss;
  55. const auto faceHandles = Api::MeshFaceHandles(*whiteBox);
  56. AZStd::string vertexString = "\n----------------------------------------------\n";
  57. for (size_t i = 0; i < faceHandles.size(); i++)
  58. {
  59. const auto p = Api::PolygonMidpoint(
  60. *whiteBox, Api::FacePolygonHandle(*whiteBox, Api::FaceHandle(aznumeric_cast<int>(i))));
  61. const auto v = localToWorld.TransformPoint(p);
  62. vertexString += AZStd::string::format("Face midpoint %zu: %f, %f, %f\n", i, v.GetX(), v.GetY(), v.GetZ());
  63. }
  64. vertexString += "----------------------------------------------\n";
  65. return vertexString;
  66. }
  67. } // namespace WhiteBox
  68. namespace UnitTest
  69. {
  70. void Create2x2CubeGrid(WhiteBox::WhiteBoxMesh& whiteBox)
  71. {
  72. namespace Api = WhiteBox::Api;
  73. Api::InitializeAsUnitCube(whiteBox);
  74. Initialize2x2CubeGrid(whiteBox);
  75. }
  76. void Initialize2x2CubeGrid(WhiteBox::WhiteBoxMesh& whiteBox)
  77. {
  78. namespace Api = WhiteBox::Api;
  79. // form a 2x2 grid of connected cubes
  80. Api::TranslatePolygonAppend(whiteBox, Api::FacePolygonHandle(whiteBox, Api::FaceHandle{4}), 1.0f);
  81. Api::HideEdge(whiteBox, Api::EdgeHandle{12});
  82. Api::TranslatePolygonAppend(whiteBox, Api::FacePolygonHandle(whiteBox, Api::FaceHandle{5}), 1.0f);
  83. }
  84. void Create3x3CubeGrid(WhiteBox::WhiteBoxMesh& whiteBox)
  85. {
  86. namespace Api = WhiteBox::Api;
  87. Api::InitializeAsUnitCube(whiteBox);
  88. Initialize3x3CubeGrid(whiteBox);
  89. }
  90. void Initialize3x3CubeGrid(WhiteBox::WhiteBoxMesh& whiteBox)
  91. {
  92. namespace Api = WhiteBox::Api;
  93. // form a 3x3 grid of connected cubes
  94. Api::TranslatePolygonAppend(whiteBox, Api::FacePolygonHandle(whiteBox, Api::FaceHandle{4}), 1.0f);
  95. Api::TranslatePolygonAppend(whiteBox, Api::FacePolygonHandle(whiteBox, Api::FaceHandle{11}), 1.0f);
  96. Api::HideEdge(whiteBox, Api::EdgeHandle{21});
  97. Api::HideEdge(whiteBox, Api::EdgeHandle{12});
  98. Api::TranslatePolygonAppend(whiteBox, Api::FacePolygonHandle(whiteBox, Api::FaceHandle{27}), 1.0f);
  99. Api::TranslatePolygonAppend(whiteBox, Api::FacePolygonHandle(whiteBox, Api::FaceHandle{26}), 1.0f);
  100. }
  101. void HideAllTopUserEdgesFor2x2Grid(WhiteBox::WhiteBoxMesh& whiteBox)
  102. {
  103. namespace Api = WhiteBox::Api;
  104. for (const auto edgeHandle : {43, 12, 4})
  105. {
  106. Api::HideEdge(whiteBox, Api::EdgeHandle{edgeHandle});
  107. }
  108. }
  109. void HideAllTopUserEdgesFor3x3Grid(WhiteBox::WhiteBoxMesh& whiteBox)
  110. {
  111. namespace Api = WhiteBox::Api;
  112. // hide all top 'user' edges (top is one polygon)
  113. for (const auto edgeHandle : {41, 12, 59, 47, 4, 48, 27, 45})
  114. {
  115. Api::HideEdge(whiteBox, Api::EdgeHandle{edgeHandle});
  116. }
  117. }
  118. MultiSpacePoint::MultiSpacePoint(
  119. const AZ::Vector3& local, const AZ::Transform& toWorld, const AzFramework::CameraState& cameraState)
  120. {
  121. m_local = local;
  122. m_world = toWorld.TransformPoint(m_local);
  123. m_screen = AzFramework::WorldToScreen(m_world, cameraState);
  124. }
  125. const AZ::Vector3& MultiSpacePoint::GetLocalSpace() const
  126. {
  127. return m_local;
  128. }
  129. const AZ::Vector3& MultiSpacePoint::GetWorldSpace() const
  130. {
  131. return m_world;
  132. }
  133. const AzFramework::ScreenPoint& MultiSpacePoint::GetScreenSpace() const
  134. {
  135. return m_screen;
  136. }
  137. } // namespace UnitTest