WhiteBoxTest.cpp 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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 "WhiteBoxTestFixtures.h"
  9. #include "WhiteBoxTestUtil.h"
  10. #include <AzCore/Math/Transform.h>
  11. #include <AzCore/Memory/SystemAllocator.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <AzCore/std/containers/array.h>
  14. #include <AzCore/std/containers/vector.h>
  15. #include <AzQtComponents/Utilities/QtPluginPaths.h>
  16. #include <AzTest/AzTest.h>
  17. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  18. #include <QApplication>
  19. #include <WhiteBox/WhiteBoxToolApi.h>
  20. namespace UnitTest
  21. {
  22. TEST(WhiteBoxTest, HandlesInitializedInvalid)
  23. {
  24. namespace Api = WhiteBox::Api;
  25. Api::VertexHandle vertexHandle;
  26. Api::FaceHandle faceHandle;
  27. Api::HalfedgeHandle halfedgeHandle;
  28. Api::EdgeHandle edgeHandle;
  29. EXPECT_FALSE(vertexHandle.IsValid());
  30. EXPECT_FALSE(faceHandle.IsValid());
  31. EXPECT_FALSE(halfedgeHandle.IsValid());
  32. EXPECT_FALSE(edgeHandle.IsValid());
  33. }
  34. TEST(WhiteBoxTest, VertexHandlesNotEqual)
  35. {
  36. namespace Api = WhiteBox::Api;
  37. Api::VertexHandle firstVertexHandle{1};
  38. Api::VertexHandle secondVertexHandle{2};
  39. EXPECT_TRUE(firstVertexHandle != secondVertexHandle);
  40. EXPECT_FALSE(firstVertexHandle == secondVertexHandle);
  41. }
  42. TEST(WhiteBoxTest, FaceHandlesNotEqual)
  43. {
  44. namespace Api = WhiteBox::Api;
  45. Api::FaceHandle firstFaceHandle{1};
  46. Api::FaceHandle secondFaceHandle{2};
  47. EXPECT_TRUE(firstFaceHandle != secondFaceHandle);
  48. EXPECT_FALSE(firstFaceHandle == secondFaceHandle);
  49. }
  50. TEST(WhiteBoxTest, HalfedgeHandlesNotEqual)
  51. {
  52. namespace Api = WhiteBox::Api;
  53. Api::HalfedgeHandle firstHalfedgeHandle{1};
  54. Api::HalfedgeHandle secondHalfedgeHandle{2};
  55. EXPECT_TRUE(firstHalfedgeHandle != secondHalfedgeHandle);
  56. EXPECT_FALSE(firstHalfedgeHandle == secondHalfedgeHandle);
  57. }
  58. TEST(WhiteBoxTest, EdgeHandlesNotEqual)
  59. {
  60. namespace Api = WhiteBox::Api;
  61. Api::EdgeHandle firstEdgeHandle{1};
  62. Api::EdgeHandle secondEdgeHandle{2};
  63. EXPECT_TRUE(firstEdgeHandle != secondEdgeHandle);
  64. EXPECT_FALSE(firstEdgeHandle == secondEdgeHandle);
  65. }
  66. TEST_F(WhiteBoxTestFixture, ClearRemovesMeshData)
  67. {
  68. namespace Api = WhiteBox::Api;
  69. Api::InitializeAsUnitCube(*m_whiteBox);
  70. Api::Clear(*m_whiteBox);
  71. const auto faceHandles = Api::MeshFaceHandles(*m_whiteBox);
  72. const auto faceCount = Api::MeshFaceCount(*m_whiteBox);
  73. const auto vertexCount = Api::MeshVertexCount(*m_whiteBox);
  74. const auto vertexHandles = Api::MeshVertexHandles(*m_whiteBox);
  75. const auto halfedgeHandleCount = Api::MeshHalfedgeCount(*m_whiteBox);
  76. const auto polygonHandles = Api::MeshPolygonHandles(*m_whiteBox);
  77. EXPECT_EQ(faceCount, 0);
  78. EXPECT_EQ(faceHandles.size(), 0);
  79. EXPECT_EQ(vertexCount, 0);
  80. EXPECT_EQ(vertexHandles.size(), 0);
  81. EXPECT_EQ(halfedgeHandleCount, 0);
  82. EXPECT_EQ(polygonHandles.size(), 0);
  83. }
  84. TEST_F(WhiteBoxTestFixture, FirstFaceOfCubeIsTop)
  85. {
  86. namespace Api = WhiteBox::Api;
  87. Api::InitializeAsUnitCube(*m_whiteBox);
  88. const AZ::Vector3 normal = Api::FaceNormal(*m_whiteBox, Api::FaceHandle{0});
  89. EXPECT_THAT(normal, IsClose(AZ::Vector3::CreateAxisZ()));
  90. }
  91. TEST_F(WhiteBoxTestFixture, FaceEdgeHandlesEmptyEdgeHandlesReturnedWithInvalidInput)
  92. {
  93. namespace Api = WhiteBox::Api;
  94. using ::testing::Eq;
  95. Api::InitializeAsUnitQuad(*m_whiteBox);
  96. const Api::EdgeHandles edgeHandles = Api::FaceEdgeHandles(*m_whiteBox, Api::FaceHandle{});
  97. EXPECT_THAT(edgeHandles.empty(), Eq(true));
  98. }
  99. TEST_F(WhiteBoxTestFixture, FaceVertexHandlesEmptyVertexHandlesReturnedWithInvalidInput)
  100. {
  101. namespace Api = WhiteBox::Api;
  102. using ::testing::Eq;
  103. Api::InitializeAsUnitQuad(*m_whiteBox);
  104. const Api::VertexHandles vertexHandles = Api::FaceVertexHandles(*m_whiteBox, Api::FaceHandle{});
  105. EXPECT_THAT(vertexHandles.empty(), Eq(true));
  106. }
  107. TEST_F(WhiteBoxTestFixture, ConnectedPolyFacesWithSameNormalReturned)
  108. {
  109. namespace Api = WhiteBox::Api;
  110. using ::testing::ElementsAreArray;
  111. AZStd::array<Api::VertexHandle, 8> vhandles;
  112. // verts must be added in CCW order
  113. vhandles[0] = Api::AddVertex(*m_whiteBox, AZ::Vector3(-1, 1, 0));
  114. vhandles[1] = Api::AddVertex(*m_whiteBox, AZ::Vector3(-2, 0, 0));
  115. vhandles[2] = Api::AddVertex(*m_whiteBox, AZ::Vector3(-1, -1, 0));
  116. vhandles[3] = Api::AddVertex(*m_whiteBox, AZ::Vector3(0, -3, 0));
  117. vhandles[4] = Api::AddVertex(*m_whiteBox, AZ::Vector3(1, -1, 0));
  118. vhandles[5] = Api::AddVertex(*m_whiteBox, AZ::Vector3(2, 0, 0));
  119. vhandles[6] = Api::AddVertex(*m_whiteBox, AZ::Vector3(1, 1, 0));
  120. vhandles[7] = Api::AddVertex(*m_whiteBox, AZ::Vector3(0, 3, 0));
  121. AZStd::vector<Api::FaceHandle> fhandles;
  122. for (size_t i = 1; i < vhandles.size() - 1; ++i)
  123. {
  124. // triangle fan topology setup
  125. fhandles.push_back(Api::AddFace(*m_whiteBox, vhandles[0], vhandles[i], vhandles[i + 1]));
  126. }
  127. Api::CalculateNormals(*m_whiteBox);
  128. Api::ZeroUVs(*m_whiteBox);
  129. const auto sideFaceHandles = Api::SideFaceHandles(*m_whiteBox, Api::FaceHandle{0});
  130. const auto sideVertexHandles = Api::SideVertexHandles(*m_whiteBox, Api::FaceHandle{0});
  131. const auto faceNormal = Api::FaceNormal(*m_whiteBox, Api::FaceHandle{0});
  132. EXPECT_THAT(sideFaceHandles, ElementsAreArray(fhandles));
  133. EXPECT_THAT(sideVertexHandles, ElementsAreArray(vhandles));
  134. EXPECT_THAT(faceNormal, IsClose(AZ::Vector3::CreateAxisZ()));
  135. }
  136. TEST_F(WhiteBoxTestFixture, OutgoingHalfedgesFromVertex)
  137. {
  138. namespace Api = WhiteBox::Api;
  139. using ::testing::ElementsAreArray;
  140. Api::InitializeAsUnitCube(*m_whiteBox);
  141. AZStd::vector<Api::HalfedgeHandle> outgoingHalfedgeHandles =
  142. Api::VertexOutgoingHalfedgeHandles(*m_whiteBox, Api::VertexHandle{0});
  143. const Api::HalfedgeHandle expectedHalfedgeHandles[] = {
  144. Api::HalfedgeHandle{9}, Api::HalfedgeHandle{34}, Api::HalfedgeHandle{24}, Api::HalfedgeHandle{0},
  145. Api::HalfedgeHandle{5}};
  146. EXPECT_THAT(
  147. outgoingHalfedgeHandles, ElementsAreArray(expectedHalfedgeHandles, std::size(expectedHalfedgeHandles)));
  148. }
  149. TEST_F(WhiteBoxTestFixture, IncomingHalfedgesFromVertex)
  150. {
  151. namespace Api = WhiteBox::Api;
  152. using ::testing::ElementsAreArray;
  153. Api::InitializeAsUnitCube(*m_whiteBox);
  154. AZStd::vector<Api::HalfedgeHandle> incomingHalfedgeHandles =
  155. Api::VertexIncomingHalfedgeHandles(*m_whiteBox, Api::VertexHandle{0});
  156. const Api::HalfedgeHandle expectedHalfedgeHandles[] = {
  157. Api::HalfedgeHandle{8}, Api::HalfedgeHandle{35}, Api::HalfedgeHandle{25}, Api::HalfedgeHandle{1},
  158. Api::HalfedgeHandle{4}};
  159. EXPECT_THAT(
  160. incomingHalfedgeHandles, ElementsAreArray(expectedHalfedgeHandles, std::size(expectedHalfedgeHandles)));
  161. }
  162. TEST_F(WhiteBoxTestFixture, AllHalfedgesFromVertex)
  163. {
  164. namespace Api = WhiteBox::Api;
  165. using ::testing::ElementsAreArray;
  166. Api::InitializeAsUnitCube(*m_whiteBox);
  167. AZStd::vector<Api::HalfedgeHandle> allHalfedgeHandles =
  168. Api::VertexHalfedgeHandles(*m_whiteBox, Api::VertexHandle{0});
  169. const Api::HalfedgeHandle expectedHalfedgeHandles[] = {
  170. Api::HalfedgeHandle{9}, Api::HalfedgeHandle{34}, Api::HalfedgeHandle{24}, Api::HalfedgeHandle{0},
  171. Api::HalfedgeHandle{5}, Api::HalfedgeHandle{8}, Api::HalfedgeHandle{35}, Api::HalfedgeHandle{25},
  172. Api::HalfedgeHandle{1}, Api::HalfedgeHandle{4}};
  173. EXPECT_THAT(allHalfedgeHandles, ElementsAreArray(expectedHalfedgeHandles, std::size(expectedHalfedgeHandles)));
  174. }
  175. TEST_F(WhiteBoxTestFixture, VerticesForFace)
  176. {
  177. namespace Api = WhiteBox::Api;
  178. using ::testing::ElementsAreArray;
  179. Api::InitializeAsUnitCube(*m_whiteBox);
  180. const auto vertexHandlesForFace = Api::FaceVertexHandles(*m_whiteBox, Api::FaceHandle{0});
  181. const Api::VertexHandle expectedVertexHandles[] = {
  182. Api::VertexHandle{0}, Api::VertexHandle{1}, Api::VertexHandle{2}};
  183. EXPECT_THAT(vertexHandlesForFace, ElementsAreArray(expectedVertexHandles, std::size(expectedVertexHandles)));
  184. }
  185. TEST_F(WhiteBoxTestFixture, SideHalfedgesForFace)
  186. {
  187. namespace Api = WhiteBox::Api;
  188. using ::testing::ElementsAreArray;
  189. using ::testing::Eq;
  190. Api::InitializeAsUnitCube(*m_whiteBox);
  191. const auto sideHalfEdgeHandlesCollection = Api::SideBorderHalfedgeHandles(*m_whiteBox, Api::FaceHandle{1});
  192. const Api::HalfedgeHandle expectedHalfedgeHandles[] = {
  193. Api::HalfedgeHandle{2}, Api::HalfedgeHandle{6}, Api::HalfedgeHandle{8}, Api::HalfedgeHandle{0}};
  194. EXPECT_THAT(sideHalfEdgeHandlesCollection.size(), Eq(1));
  195. EXPECT_THAT(
  196. sideHalfEdgeHandlesCollection.front(),
  197. ElementsAreArray(expectedHalfedgeHandles, std::size(expectedHalfedgeHandles)));
  198. }
  199. TEST_F(WhiteBoxTestFixture, VerticesOrderedForSide)
  200. {
  201. namespace Api = WhiteBox::Api;
  202. using ::testing::ElementsAreArray;
  203. using ::testing::Eq;
  204. Api::InitializeAsUnitCube(*m_whiteBox);
  205. const auto vertexHandlesCollection = Api::SideBorderVertexHandles(*m_whiteBox, Api::FaceHandle{0});
  206. const Api::VertexHandle vhs[] = {
  207. Api::VertexHandle{0}, Api::VertexHandle{1}, Api::VertexHandle{2}, Api::VertexHandle{3}};
  208. EXPECT_THAT(vertexHandlesCollection.size(), Eq(1));
  209. EXPECT_THAT(vertexHandlesCollection.front(), ElementsAreArray(vhs, std::size(vhs)));
  210. }
  211. TEST_F(WhiteBoxTestFixture, VertexPositionsFromFaceHandle)
  212. {
  213. namespace Api = WhiteBox::Api;
  214. using ::testing::Pointwise;
  215. AZStd::vector<AZ::Vector3> vertices = {
  216. AZ::Vector3(0.0f, 0.0f, 0.0f), AZ::Vector3(1.0f, 0.0f, 0.0f), AZ::Vector3(1.0f, 1.0f, 0.0f)};
  217. Api::VertexHandles vhs;
  218. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[0]));
  219. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[1]));
  220. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[2]));
  221. auto faceHandle = Api::AddFace(*m_whiteBox, vhs[0], vhs[1], vhs[2]);
  222. EXPECT_THAT(vertices, Pointwise(ContainerIsClose(), Api::FaceVertexPositions(*m_whiteBox, faceHandle)));
  223. }
  224. TEST_F(WhiteBoxTestFixture, VertexPositionsFromPolygonHandle)
  225. {
  226. namespace Api = WhiteBox::Api;
  227. using ::testing::Eq;
  228. using ::testing::Pointwise;
  229. AZStd::vector<AZ::Vector3> vertices = {
  230. AZ::Vector3(0.0f, 0.0f, 0.0f), AZ::Vector3(1.0f, 0.0f, 0.0f), AZ::Vector3(1.0f, 1.0f, 0.0f),
  231. AZ::Vector3(0.0f, 1.0f, 0.0f)};
  232. Api::VertexHandles vhs;
  233. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[0]));
  234. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[1]));
  235. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[2]));
  236. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[3]));
  237. Api::PolygonHandle polygonHandle{Api::FaceHandles{
  238. Api::AddFace(*m_whiteBox, vhs[0], vhs[1], vhs[2]), Api::AddFace(*m_whiteBox, vhs[0], vhs[2], vhs[3])}};
  239. const auto vertexPositions = Api::PolygonVertexPositions(*m_whiteBox, polygonHandle);
  240. EXPECT_THAT(vertices.size(), Eq(vertexPositions.size()));
  241. EXPECT_THAT(vertices, Pointwise(ContainerIsClose(), vertexPositions));
  242. }
  243. TEST_F(WhiteBoxTestFixture, VertexPositionsFromVertexHandles)
  244. {
  245. namespace Api = WhiteBox::Api;
  246. using ::testing::Pointwise;
  247. AZStd::vector<AZ::Vector3> vertices = {AZ::Vector3(0, 0, 0), AZ::Vector3(1, 0, 0), AZ::Vector3(1, 1, 0)};
  248. Api::VertexHandles vhs;
  249. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[0]));
  250. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[1]));
  251. vhs.push_back(Api::AddVertex(*m_whiteBox, vertices[2]));
  252. Api::AddFace(*m_whiteBox, vhs[0], vhs[1], vhs[2]);
  253. EXPECT_THAT(vertices, Pointwise(ContainerIsClose(), Api::VertexPositions(*m_whiteBox, vhs)));
  254. }
  255. TEST_F(WhiteBoxTestFixture, PolygonHandlesFromUnitQuad)
  256. {
  257. namespace Api = WhiteBox::Api;
  258. Api::InitializeAsUnitQuad(*m_whiteBox);
  259. auto polygonHandles = Api::MeshPolygonHandles(*m_whiteBox);
  260. EXPECT_EQ(polygonHandles.size(), 1);
  261. }
  262. TEST_F(WhiteBoxTestFixture, PolygonHandlesFromUnitCube)
  263. {
  264. namespace Api = WhiteBox::Api;
  265. Api::InitializeAsUnitCube(*m_whiteBox);
  266. auto polygonHandles = Api::MeshPolygonHandles(*m_whiteBox);
  267. EXPECT_EQ(polygonHandles.size(), 6);
  268. }
  269. TEST_F(WhiteBoxTestFixture, UniqueVertexPositionsFromUnitQuad)
  270. {
  271. namespace Api = WhiteBox::Api;
  272. Api::InitializeAsUnitQuad(*m_whiteBox);
  273. auto vertexPositions =
  274. Api::PolygonVertexPositions(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0}));
  275. EXPECT_EQ(vertexPositions.size(), 4);
  276. }
  277. TEST_F(WhiteBoxTestFixture, MultiplePolygonExtrusions)
  278. {
  279. namespace Api = WhiteBox::Api;
  280. using Vh = Api::VertexHandle;
  281. using ::testing::ElementsAreArray;
  282. Api::InitializeAsUnitCube(*m_whiteBox);
  283. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{7}), 1.0f);
  284. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{11}), 1.0f);
  285. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{14}), 1.0f);
  286. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{17}), 1.0f);
  287. const auto polygonHandles = Api::MeshPolygonHandles(*m_whiteBox);
  288. const auto faceHandles = Api::MeshFaceHandles(*m_whiteBox);
  289. const auto faceCount = Api::MeshFaceCount(*m_whiteBox);
  290. const auto vertexCount = Api::MeshVertexCount(*m_whiteBox);
  291. const auto halfedgeHandleCount = Api::MeshHalfedgeCount(*m_whiteBox);
  292. const auto vertexHandles = Api::MeshVertexHandles(*m_whiteBox);
  293. EXPECT_EQ(polygonHandles.size(), 22);
  294. EXPECT_EQ(faceCount, 44);
  295. EXPECT_EQ(faceHandles.size(), 44);
  296. EXPECT_EQ(vertexCount, 24);
  297. EXPECT_EQ(vertexHandles.size(), 24);
  298. EXPECT_THAT(vertexHandles, ElementsAreArray({Vh{0}, Vh{1}, Vh{2}, Vh{3}, Vh{4}, Vh{5}, Vh{6}, Vh{7},
  299. Vh{8}, Vh{9}, Vh{10}, Vh{11}, Vh{12}, Vh{13}, Vh{14}, Vh{15},
  300. Vh{16}, Vh{17}, Vh{18}, Vh{19}, Vh{20}, Vh{21}, Vh{22}, Vh{23}}));
  301. EXPECT_EQ(halfedgeHandleCount, 132);
  302. }
  303. TEST_F(WhiteBoxTestFixture, PolygonExtrusionEmptyWithEmptyMesh)
  304. {
  305. namespace Api = WhiteBox::Api;
  306. const auto polygon = Api::TranslatePolygonAppend(*m_whiteBox, Api::PolygonHandle{}, 1.0f);
  307. EXPECT_EQ(polygon.m_faceHandles.size(), 0);
  308. }
  309. TEST_F(WhiteBoxTestFixture, MeshSerializedAndDeserialized)
  310. {
  311. namespace Api = WhiteBox::Api;
  312. Api::InitializeAsUnitCube(*m_whiteBox);
  313. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0}), 1.0f);
  314. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{15}), 1.0f);
  315. {
  316. const auto polygonHandles = Api::MeshPolygonHandles(*m_whiteBox);
  317. const auto faceHandles = Api::MeshFaceHandles(*m_whiteBox);
  318. const auto faceCount = Api::MeshFaceCount(*m_whiteBox);
  319. const auto vertexCount = Api::MeshVertexCount(*m_whiteBox);
  320. const auto halfedgeHandleCount = Api::MeshHalfedgeCount(*m_whiteBox);
  321. const auto vertexHandles = Api::MeshVertexHandles(*m_whiteBox);
  322. EXPECT_EQ(polygonHandles.size(), 14);
  323. EXPECT_EQ(faceCount, 28);
  324. EXPECT_EQ(faceHandles.size(), 28);
  325. EXPECT_EQ(vertexCount, 16);
  326. EXPECT_EQ(vertexHandles.size(), 16);
  327. EXPECT_EQ(halfedgeHandleCount, 84);
  328. }
  329. AZStd::vector<AZ::u8> whiteBoxMeshData;
  330. Api::WriteMesh(*m_whiteBox, whiteBoxMeshData);
  331. m_whiteBox.reset();
  332. m_whiteBox = Api::CreateWhiteBoxMesh();
  333. Api::ReadMesh(*m_whiteBox, whiteBoxMeshData);
  334. {
  335. const auto polygonHandles = Api::MeshPolygonHandles(*m_whiteBox);
  336. const auto faceHandles = Api::MeshFaceHandles(*m_whiteBox);
  337. const auto faceCount = Api::MeshFaceCount(*m_whiteBox);
  338. const auto vertexCount = Api::MeshVertexCount(*m_whiteBox);
  339. const auto halfedgeHandleCount = Api::MeshHalfedgeCount(*m_whiteBox);
  340. const auto vertexHandles = Api::MeshVertexHandles(*m_whiteBox);
  341. EXPECT_EQ(polygonHandles.size(), 14);
  342. EXPECT_EQ(faceCount, 28);
  343. EXPECT_EQ(faceHandles.size(), 28);
  344. EXPECT_EQ(vertexCount, 16);
  345. EXPECT_EQ(vertexHandles.size(), 16);
  346. EXPECT_EQ(halfedgeHandleCount, 84);
  347. }
  348. }
  349. TEST_F(WhiteBoxTestFixture, MeshNotDeserializedWithSkipWhiteSpaceStream)
  350. {
  351. namespace Api = WhiteBox::Api;
  352. using testing::Eq;
  353. Api::InitializeAsUnitCube(*m_whiteBox);
  354. AZStd::vector<AZ::u8> serializedWhiteBox;
  355. Api::WriteMesh(*m_whiteBox, serializedWhiteBox);
  356. std::string serializedWhiteBoxStr;
  357. serializedWhiteBoxStr.reserve(serializedWhiteBox.size());
  358. AZStd::copy(
  359. serializedWhiteBox.cbegin(), serializedWhiteBox.cend(), AZStd::back_inserter(serializedWhiteBoxStr));
  360. std::stringstream whiteBoxStream;
  361. whiteBoxStream.str(serializedWhiteBoxStr);
  362. // note: std::stringstream will default to skip white space characters
  363. AZ_TEST_START_TRACE_SUPPRESSION;
  364. EXPECT_THAT(Api::ReadMesh(*m_whiteBox, whiteBoxStream), Eq(Api::ReadResult::Error));
  365. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  366. }
  367. TEST_F(WhiteBoxTestFixture, InitialiseAsUnitQuad)
  368. {
  369. namespace Api = WhiteBox::Api;
  370. using ::testing::Pointwise;
  371. Api::InitializeAsUnitQuad(*m_whiteBox);
  372. AZStd::vector<AZ::Vector3> vertexPositions = Api::MeshVertexPositions(*m_whiteBox);
  373. AZStd::vector<AZ::Vector3> expectedVertexPositions = {
  374. AZ::Vector3(-0.5f, 0.0f, -0.5f), AZ::Vector3(0.5f, 0.0f, -0.5f), AZ::Vector3(0.5f, 0.0f, 0.5f),
  375. AZ::Vector3(-0.5f, 0.0f, 0.5f)};
  376. EXPECT_THAT(vertexPositions, Pointwise(ContainerIsClose(), expectedVertexPositions));
  377. }
  378. TEST_F(WhiteBoxTestFixture, MeshScalePolygon)
  379. {
  380. namespace Api = WhiteBox::Api;
  381. using ::testing::Pointwise;
  382. const auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  383. const auto midpoint = Api::PolygonMidpoint(*m_whiteBox, polygonHandle);
  384. Api::ScalePolygonRelative(*m_whiteBox, polygonHandle, midpoint, 0.5f);
  385. const auto vertexPositions = Api::PolygonVertexPositions(*m_whiteBox, polygonHandle);
  386. // result of scaling each vertex by 0.5 towards the midpoint of the quad
  387. const AZStd::vector<AZ::Vector3> scaledUnitQuad = {
  388. AZ::Vector3(-0.75f, 0.0f, -0.75f), AZ::Vector3(0.75f, 0.0f, -0.75f), AZ::Vector3(0.75f, 0.0f, 0.75f),
  389. AZ::Vector3(-0.75f, 0.0f, 0.75f)};
  390. EXPECT_THAT(vertexPositions, Pointwise(ContainerIsClose(), scaledUnitQuad));
  391. EXPECT_EQ(Api::MeshPolygonHandles(*m_whiteBox).size(), 1);
  392. }
  393. TEST_F(WhiteBoxTestFixture, MeshScalePolygonAppend)
  394. {
  395. namespace Api = WhiteBox::Api;
  396. using ::testing::Pointwise;
  397. auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  398. polygonHandle = Api::ScalePolygonAppendRelative(*m_whiteBox, polygonHandle, 0.5f);
  399. const auto vertexPositions = Api::PolygonVertexPositions(*m_whiteBox, polygonHandle);
  400. // result of scaling each vertex by 0.5 towards the midpoint of the quad
  401. const AZStd::vector<AZ::Vector3> scaledUnitQuad = {
  402. AZ::Vector3(-0.75f, 0.0f, -0.75f), AZ::Vector3(0.75f, 0.0f, -0.75f), AZ::Vector3(0.75f, 0.0f, 0.75f),
  403. AZ::Vector3(-0.75f, 0.0f, 0.75f)};
  404. EXPECT_THAT(vertexPositions, Pointwise(ContainerIsClose(), scaledUnitQuad));
  405. EXPECT_EQ(Api::MeshPolygonHandles(*m_whiteBox).size(), 5);
  406. }
  407. TEST_F(WhiteBoxTestFixture, MeshPolygonUniqueVertexHandles)
  408. {
  409. namespace Api = WhiteBox::Api;
  410. const auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  411. const auto vertexHandles = Api::PolygonVertexHandles(*m_whiteBox, polygonHandle);
  412. EXPECT_EQ(vertexHandles.size(), 4);
  413. }
  414. TEST_F(WhiteBoxTestFixture, MeshMidPointOfPolygon)
  415. {
  416. namespace Api = WhiteBox::Api;
  417. auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  418. EXPECT_THAT(AZ::Vector3::CreateZero(), IsClose(Api::PolygonMidpoint(*m_whiteBox, polygonHandle)));
  419. }
  420. TEST_F(WhiteBoxTestFixture, MeshMidPointOfEdge)
  421. {
  422. namespace Api = WhiteBox::Api;
  423. // given
  424. const auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  425. const auto edgeHandles = Api::PolygonBorderEdgeHandlesFlattened(*m_whiteBox, polygonHandle);
  426. for (const auto& edgeHandle : edgeHandles)
  427. {
  428. // when
  429. const auto tail = Api::HalfedgeVertexPositionAtTail(
  430. *m_whiteBox, Api::EdgeHalfedgeHandle(*m_whiteBox, edgeHandle, Api::EdgeHalfedge::First));
  431. const auto tip = Api::HalfedgeVertexPositionAtTip(
  432. *m_whiteBox, Api::EdgeHalfedgeHandle(*m_whiteBox, edgeHandle, Api::EdgeHalfedge::First));
  433. // computed differently to EdgeMidpoint
  434. const auto midpoint = tail + (tip - tail) * 0.5f;
  435. // then
  436. EXPECT_THAT(midpoint, IsClose(Api::EdgeMidpoint(*m_whiteBox, edgeHandle)));
  437. }
  438. }
  439. TEST_F(WhiteBoxTestFixture, MeshMidPointOfFace)
  440. {
  441. namespace Api = WhiteBox::Api;
  442. Api::InitializeAsUnitCube(*m_whiteBox);
  443. const auto faceMidpoint = Api::FaceMidpoint(*m_whiteBox, Api::FaceHandle{0});
  444. EXPECT_THAT(faceMidpoint, IsClose(AZ::Vector3(0.1666f, -0.1666, 0.5f)));
  445. }
  446. TEST_F(WhiteBoxTestFixture, MeshFacesReturned)
  447. {
  448. namespace Api = WhiteBox::Api;
  449. using ::testing::Pointwise;
  450. Api::InitializeAsUnitQuad(*m_whiteBox);
  451. const auto faces = Api::MeshFaces(*m_whiteBox);
  452. EXPECT_EQ(faces.size(), 2);
  453. const AZStd::vector<AZ::Vector3> vertexPositions = {
  454. AZ::Vector3(-0.5f, 0.0f, -0.5f), AZ::Vector3(0.5f, 0.0f, -0.5f), AZ::Vector3(0.5f, 0.0f, 0.5f),
  455. AZ::Vector3(-0.5f, 0.0f, -0.5f), AZ::Vector3(0.5f, 0.0f, 0.5f), AZ::Vector3(-0.5f, 0.0f, 0.5f),
  456. };
  457. AZStd::vector<AZ::Vector3> faceVertexPositions;
  458. faceVertexPositions.insert(faceVertexPositions.end(), faces[0].begin(), faces[0].end());
  459. faceVertexPositions.insert(faceVertexPositions.end(), faces[1].begin(), faces[1].end());
  460. EXPECT_THAT(faceVertexPositions, Pointwise(ContainerIsClose(), vertexPositions));
  461. }
  462. // note: here we sum and then normalize unit normals of each face (the normals are not weighted)
  463. TEST_F(WhiteBoxTestFixture, PolygonNormalIsAverageOfFaces)
  464. {
  465. namespace Api = WhiteBox::Api;
  466. // given
  467. const auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  468. const auto vertexHandles = Api::PolygonVertexHandles(*m_whiteBox, polygonHandle);
  469. const auto vertexPositions = Api::VertexPositions(*m_whiteBox, vertexHandles);
  470. // update the position of a single vertex to make the faces in the polygon not co-planar
  471. Api::SetVertexPosition(*m_whiteBox, vertexHandles[0], vertexPositions[0] + AZ::Vector3::CreateAxisY());
  472. // ensure we refresh normals after modifications
  473. Api::CalculateNormals(*m_whiteBox);
  474. // when
  475. const auto polygonNormal = PolygonNormal(*m_whiteBox, polygonHandle);
  476. // then
  477. const auto faceNormal22 = FaceNormal(*m_whiteBox, polygonHandle.m_faceHandles[0]);
  478. const auto faceNormal23 = FaceNormal(*m_whiteBox, polygonHandle.m_faceHandles[1]);
  479. const auto averageNormal = (faceNormal22 + faceNormal23).GetNormalized();
  480. EXPECT_THAT(polygonNormal, IsClose(averageNormal));
  481. }
  482. TEST_F(WhiteBoxTestFixture, PolygonTranslateAlongNormal)
  483. {
  484. namespace Api = WhiteBox::Api;
  485. using ::testing::Pointwise;
  486. // given
  487. // use default position (Y is at origin)
  488. const auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  489. // when
  490. Api::TranslatePolygon(*m_whiteBox, polygonHandle, 1.0f);
  491. AZStd::vector<AZ::Vector3> expectedVertexPositions = {
  492. AZ::Vector3(-0.5f, -1.0f, -0.5f), AZ::Vector3(0.5f, -1.0f, -0.5f), AZ::Vector3(0.5f, -1.0f, 0.5f),
  493. AZ::Vector3(-0.5f, -1.0f, 0.5f)};
  494. const AZStd::vector<AZ::Vector3> polygonVertexPositions =
  495. Api::PolygonVertexPositions(*m_whiteBox, polygonHandle);
  496. // then
  497. EXPECT_THAT(expectedVertexPositions, Pointwise(ContainerIsClose(), polygonVertexPositions));
  498. }
  499. TEST_F(WhiteBoxTestFixture, SpaceCreatedForPolygonIsOrthogonal)
  500. {
  501. namespace Api = WhiteBox::Api;
  502. // given
  503. const auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  504. // when
  505. const AZ::Vector3 polygonMidpoint = Api::PolygonMidpoint(*m_whiteBox, polygonHandle);
  506. const AZ::Transform polygonSpace = Api::PolygonSpace(*m_whiteBox, polygonHandle, polygonMidpoint);
  507. // then
  508. EXPECT_TRUE(polygonSpace.IsOrthogonal());
  509. }
  510. TEST_F(WhiteBoxTestFixture, SpaceCreatedForEdgeIsOrthogonal)
  511. {
  512. namespace Api = WhiteBox::Api;
  513. // given
  514. const auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  515. const auto edgeHandles = Api::PolygonBorderEdgeHandlesFlattened(*m_whiteBox, polygonHandle);
  516. // when
  517. const AZ::Vector3 edgeMidpoint = Api::EdgeMidpoint(*m_whiteBox, edgeHandles[0]);
  518. const AZ::Transform edgeSpace = Api::EdgeSpace(*m_whiteBox, edgeHandles[0], edgeMidpoint);
  519. // then
  520. EXPECT_TRUE(edgeSpace.IsOrthogonal());
  521. }
  522. TEST_F(WhiteBoxTestFixture, EdgeToHalfEdgeConversionsMapCorrectly)
  523. {
  524. namespace Api = WhiteBox::Api;
  525. Api::InitializeAsUnitQuad(*m_whiteBox);
  526. // given
  527. const auto edgeHandles = Api::MeshEdgeHandles(*m_whiteBox);
  528. for (const auto& edgeHandle : edgeHandles)
  529. {
  530. const auto firstHalfedgeHandle = Api::EdgeHalfedgeHandle(*m_whiteBox, edgeHandle, Api::EdgeHalfedge::First);
  531. const auto secondHalfedgeHandle =
  532. Api::EdgeHalfedgeHandle(*m_whiteBox, edgeHandle, Api::EdgeHalfedge::First);
  533. // when
  534. const auto edgeHandleFromFirst = Api::HalfedgeEdgeHandle(*m_whiteBox, firstHalfedgeHandle);
  535. const auto edgeHandleFromSecond = Api::HalfedgeEdgeHandle(*m_whiteBox, secondHalfedgeHandle);
  536. // then
  537. EXPECT_EQ(edgeHandle, edgeHandleFromFirst);
  538. EXPECT_EQ(edgeHandle, edgeHandleFromSecond);
  539. }
  540. }
  541. class WhiteBoxTestUpdateVerticesFixture : public WhiteBoxTestFixture
  542. {
  543. public:
  544. WhiteBoxTestUpdateVerticesFixture()
  545. : WhiteBoxTestFixture()
  546. {
  547. }
  548. void SetUpEditorFixtureImpl() override
  549. {
  550. namespace Api = WhiteBox::Api;
  551. Api::InitializeAsUnitCube(*m_whiteBox);
  552. // triangle A of the unit cube's left face tri pair
  553. WhiteBox::Api::FaceHandle leftFaceHandle = WhiteBox::Api::FaceHandle{0};
  554. // triangle A of the unit cube's top face tri pair
  555. WhiteBox::Api::FaceHandle topFaceHandle = WhiteBox::Api::FaceHandle{10};
  556. // top face polygon comprised of triangles A and B
  557. WhiteBox::Api::PolygonHandle topFacePolyHandle =
  558. WhiteBox::Api::FacePolygonHandle(*m_whiteBox, topFaceHandle);
  559. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, leftFaceHandle), 1.0f);
  560. m_polygonVertexHandles = Api::PolygonVertexHandles(*m_whiteBox, topFacePolyHandle);
  561. const auto vertexPositions = Api::VertexPositions(*m_whiteBox, m_polygonVertexHandles);
  562. // translate top face upwards one unit
  563. size_t index = 0;
  564. for (auto vertexHandle : m_polygonVertexHandles)
  565. {
  566. Api::SetVertexPositionAndUpdateUVs(
  567. *m_whiteBox, vertexHandle, vertexPositions[index++] + AZ::Vector3::CreateAxisZ());
  568. }
  569. }
  570. void TearDownEditorFixtureImpl()
  571. {
  572. // ensure we deallocate memory for the vector before the allocator is destroyed
  573. m_polygonVertexHandles = {};
  574. }
  575. WhiteBox::Api::VertexHandles m_polygonVertexHandles;
  576. };
  577. TEST_F(WhiteBoxTestUpdateVerticesFixture, MeshCanUpdateVertexPositions)
  578. {
  579. namespace Api = WhiteBox::Api;
  580. const AZ::Vector3 updatedVertexPositions[] = {
  581. AZ::Vector3{-0.5f, -0.5f, 2.5f}, AZ::Vector3{0.5f, -0.5f, 2.5f}, AZ::Vector3{0.5f, 0.5f, 2.5f},
  582. AZ::Vector3{-0.5f, 0.5f, 2.5f}};
  583. size_t index = 0;
  584. for (auto vertexHandle : m_polygonVertexHandles)
  585. {
  586. const auto vertexPosition = Api::VertexPosition(*m_whiteBox, vertexHandle);
  587. EXPECT_THAT(vertexPosition, IsClose(updatedVertexPositions[index++]));
  588. }
  589. }
  590. TEST_F(WhiteBoxTestUpdateVerticesFixture, MeshCanUpdateVertexUVs)
  591. {
  592. namespace Api = WhiteBox::Api;
  593. // iterate over all vertex handles associated with a polygon and get all outgoing
  594. // half edges - check the uvs at each halfedge at the outer edge of the extruded
  595. // face (halfedges are on the lateral faces, opposite of halfedges on the extruded
  596. // polygon/face) - we lookup the uv from the halfedge handle and verify the tiling
  597. using Heh = Api::HalfedgeHandle;
  598. const Heh topHalfedgeHandles[] = {Heh{35}, Heh{37}, Heh{41}, Heh{43}};
  599. // expected uv coordinates given the z-axis translation applied to the top face
  600. const AZ::Vector2 expectedUVs[] = {
  601. AZ::Vector2(-2.0f, 0.0f), AZ::Vector2(1.0f, -2.0f), AZ::Vector2(-2.0f, 1.0f), AZ::Vector2(0.0f, -2.0f)};
  602. unsigned i = 0;
  603. for (auto vertexHandle : m_polygonVertexHandles)
  604. {
  605. const auto outgoingHalfedges = Api::VertexOutgoingHalfedgeHandles(*m_whiteBox, vertexHandle);
  606. for (auto halfedgeHandle : outgoingHalfedges)
  607. {
  608. auto halfedgeHandleIt =
  609. AZStd::find(std::begin(topHalfedgeHandles), std::end(topHalfedgeHandles), halfedgeHandle);
  610. if (halfedgeHandleIt != std::end(topHalfedgeHandles))
  611. {
  612. const AZ::Vector2 uv = Api::HalfedgeUV(*m_whiteBox, halfedgeHandle);
  613. EXPECT_THAT(uv, IsClose(expectedUVs[i++]));
  614. }
  615. }
  616. }
  617. }
  618. TEST_F(WhiteBoxTestFixture, EdgeCanBeAppendedToWhiteBoxCubeConnectedByQuadPolygons)
  619. {
  620. namespace Api = WhiteBox::Api;
  621. using ::testing::Eq;
  622. auto polygonHandles = Api::InitializeAsUnitCube(*m_whiteBox);
  623. const auto polygonCountBefore = Api::MeshPolygonHandles(*m_whiteBox).size();
  624. const auto faceCountBefore = Api::MeshFaceCount(*m_whiteBox);
  625. const auto nextEdgeHandle =
  626. Api::TranslateEdgeAppend(*m_whiteBox, Api::EdgeHandle{1}, AZ::Vector3{-0.5f, 0.0f, 0.5f});
  627. const auto edgeMidpoint = Api::EdgeMidpoint(*m_whiteBox, nextEdgeHandle);
  628. const auto polygonCountAfter = Api::MeshPolygonHandles(*m_whiteBox).size();
  629. const auto faceCountAfter = Api::MeshFaceCount(*m_whiteBox);
  630. EXPECT_THAT(nextEdgeHandle, Eq(Api::EdgeHandle{19}));
  631. EXPECT_THAT(edgeMidpoint, IsClose(AZ::Vector3{0.0f, 0.0f, 1.0f}));
  632. EXPECT_THAT(polygonCountAfter - polygonCountBefore, Eq(3));
  633. EXPECT_THAT(faceCountAfter - faceCountBefore, Eq(4));
  634. }
  635. TEST_F(WhiteBoxTestFixture, EdgeCanBeAppendedToWhiteBoxCubeConnectedByTriPolygons)
  636. {
  637. namespace Api = WhiteBox::Api;
  638. using ::testing::Eq;
  639. auto polygonHandles = Api::InitializeAsUnitCube(*m_whiteBox);
  640. // quad edge extrusion - same as EdgeCanBeAppendedToWhiteBoxCubeConnectedByTwoQuadPolygons
  641. Api::TranslateEdgeAppend(*m_whiteBox, Api::EdgeHandle{1}, AZ::Vector3{-0.5f, 0.0f, 0.5f});
  642. const auto polygonCountBefore = Api::MeshPolygonHandles(*m_whiteBox).size();
  643. const auto faceCountBefore = Api::MeshFaceCount(*m_whiteBox);
  644. // triangle edge extrusion
  645. const auto nextEdgeHandle =
  646. Api::TranslateEdgeAppend(*m_whiteBox, Api::EdgeHandle{0}, AZ::Vector3{0.0f, -0.25f, 0.25f});
  647. const auto edgeMidpoint = Api::EdgeMidpoint(*m_whiteBox, nextEdgeHandle);
  648. const auto polygonCountAfter = Api::MeshPolygonHandles(*m_whiteBox).size();
  649. const auto faceCountAfter = Api::MeshFaceCount(*m_whiteBox);
  650. EXPECT_THAT(nextEdgeHandle, Eq(Api::EdgeHandle{26}));
  651. EXPECT_THAT(edgeMidpoint, IsClose(AZ::Vector3{0.0f, -0.75f, 0.75f}));
  652. EXPECT_THAT(polygonCountAfter - polygonCountBefore, Eq(3));
  653. EXPECT_THAT(faceCountAfter - faceCountBefore, Eq(4));
  654. }
  655. TEST_F(WhiteBoxTestFixture, HidingEdgeCreatesNewPolygonHandleWithCombinedFaceHandles)
  656. {
  657. namespace Api = WhiteBox::Api;
  658. [[maybe_unused]] auto polygonHandles = Api::InitializeAsUnitCube(*m_whiteBox);
  659. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{7}), 1.0f);
  660. const auto beforeHidePolygonHandleFromFaceHandle_0 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0});
  661. const auto beforeHidePolygonHandleFromFaceHandle_1 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{1});
  662. const auto beforeHidePolygonHandleFromFaceHandle_16 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{16});
  663. const auto beforeHidePolygonHandleFromFaceHandle_17 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{17});
  664. // hide top edge
  665. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{1});
  666. const auto afterHidePolygonHandleFromFaceHandle_0 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0});
  667. const auto afterHidePolygonHandleFromFaceHandle_1 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{1});
  668. const auto afterHidePolygonHandleFromFaceHandle_16 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{16});
  669. const auto afterHidePolygonHandleFromFaceHandle_17 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{17});
  670. const auto beforeHidePolygonHandlesFaceHandle_0_1_Expected =
  671. Api::PolygonHandle{Api::FaceHandles{Api::FaceHandle{0}, Api::FaceHandle{1}}};
  672. const auto beforeHidePolygonHandlesFaceHandle_16_17_Expected =
  673. Api::PolygonHandle{Api::FaceHandles{Api::FaceHandle{16}, Api::FaceHandle{17}}};
  674. // two separate top polygons after append/extrusion
  675. EXPECT_EQ(beforeHidePolygonHandleFromFaceHandle_0, beforeHidePolygonHandlesFaceHandle_0_1_Expected);
  676. EXPECT_EQ(beforeHidePolygonHandleFromFaceHandle_1, beforeHidePolygonHandlesFaceHandle_0_1_Expected);
  677. EXPECT_EQ(beforeHidePolygonHandleFromFaceHandle_16, beforeHidePolygonHandlesFaceHandle_16_17_Expected);
  678. EXPECT_EQ(beforeHidePolygonHandleFromFaceHandle_17, beforeHidePolygonHandlesFaceHandle_16_17_Expected);
  679. const auto afterHidePolygonHandlesFaceHandle_1_0_16_17_Expected = Api::PolygonHandle{
  680. Api::FaceHandles{{Api::FaceHandle{0}, Api::FaceHandle{1}, Api::FaceHandle{16}, Api::FaceHandle{17}}}};
  681. // single top polygon after hiding edge
  682. EXPECT_EQ(afterHidePolygonHandleFromFaceHandle_0, afterHidePolygonHandlesFaceHandle_1_0_16_17_Expected);
  683. EXPECT_EQ(afterHidePolygonHandleFromFaceHandle_1, afterHidePolygonHandlesFaceHandle_1_0_16_17_Expected);
  684. EXPECT_EQ(afterHidePolygonHandleFromFaceHandle_16, afterHidePolygonHandlesFaceHandle_1_0_16_17_Expected);
  685. EXPECT_EQ(afterHidePolygonHandleFromFaceHandle_17, afterHidePolygonHandlesFaceHandle_1_0_16_17_Expected);
  686. }
  687. TEST_F(WhiteBoxTestFixture, FlipInternalEdgeOfQuadSucceeds)
  688. {
  689. namespace Api = WhiteBox::Api;
  690. [[maybe_unused]] auto polygonHandles = Api::InitializeAsUnitQuad(*m_whiteBox);
  691. const auto beforeFlipVertexHandles =
  692. AZStd::array<Api::VertexHandle, 2>{Api::VertexHandle{2}, Api::VertexHandle{0}};
  693. const auto afterFlipVertexHandles =
  694. AZStd::array<Api::VertexHandle, 2>{Api::VertexHandle{3}, Api::VertexHandle{1}};
  695. const auto beforeFlipExpectedVertexHandles = Api::EdgeVertexHandles(*m_whiteBox, Api::EdgeHandle{2});
  696. EXPECT_EQ(beforeFlipExpectedVertexHandles, beforeFlipVertexHandles);
  697. // flip diagonal edge
  698. bool result = Api::FlipEdge(*m_whiteBox, Api::EdgeHandle{2});
  699. EXPECT_TRUE(result);
  700. const auto afterFlipExpectedVertexHandles = Api::EdgeVertexHandles(*m_whiteBox, Api::EdgeHandle{2});
  701. EXPECT_EQ(afterFlipExpectedVertexHandles, afterFlipVertexHandles);
  702. }
  703. TEST_F(WhiteBoxTestFixture, FlipOuterEdgeOfQuadReturnsFalse)
  704. {
  705. namespace Api = WhiteBox::Api;
  706. [[maybe_unused]] auto polygonHandles = Api::InitializeAsUnitQuad(*m_whiteBox);
  707. // attempt to flip outer edge
  708. bool result = Api::FlipEdge(*m_whiteBox, Api::EdgeHandle{0});
  709. EXPECT_EQ(result, false);
  710. }
  711. TEST_F(WhiteBoxTestFixture, FlipVisibleEdgeReturnsFalse)
  712. {
  713. namespace Api = WhiteBox::Api;
  714. [[maybe_unused]] auto polygonHandles = Api::InitializeAsUnitQuad(*m_whiteBox);
  715. {
  716. Api::EdgeHandles restoringEdgeHandles;
  717. Api::RestoreEdge(*m_whiteBox, Api::EdgeHandle{2}, restoringEdgeHandles);
  718. }
  719. // attempt to flip outer edge
  720. bool result = Api::FlipEdge(*m_whiteBox, Api::EdgeHandle{2});
  721. EXPECT_EQ(result, false);
  722. }
  723. TEST_F(WhiteBoxTestFixture, EdgeCannotBeAppendedWhenPolygonHasMoreThanTwoFaces)
  724. {
  725. namespace Api = WhiteBox::Api;
  726. using ::testing::Eq;
  727. const auto polygonHandles = Api::InitializeAsUnitCube(*m_whiteBox);
  728. // quad face extrusion
  729. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{7}), 1.0f);
  730. // hide top edge
  731. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{1});
  732. const auto polygonCountBefore = Api::MeshPolygonHandles(*m_whiteBox).size();
  733. // attempt to perform an edge append
  734. const auto nextEdgeHandle =
  735. Api::TranslateEdgeAppend(*m_whiteBox, Api::EdgeHandle{20}, AZ::Vector3{-0.5f, 0.0f, 0.5f});
  736. const auto polygonCountAfter = Api::MeshPolygonHandles(*m_whiteBox).size();
  737. // same edge handle is returned, no append/extrusion is performed
  738. EXPECT_THAT(nextEdgeHandle, Eq(Api::EdgeHandle{20}));
  739. EXPECT_THAT(polygonCountBefore, Eq(polygonCountAfter));
  740. }
  741. TEST_F(WhiteBoxTestFixture, PolygonCannotBeAppendedWithNoEdges)
  742. {
  743. namespace Api = WhiteBox::Api;
  744. using ::testing::Eq;
  745. const auto polygonHandles = Api::InitializeAsUnitCube(*m_whiteBox);
  746. // hide all 'logical'/'visible' edges (those that define the bounds of a polygon)
  747. for (const auto& edgeHandle :
  748. {Api::EdgeHandle{1}, Api::EdgeHandle{3}, Api::EdgeHandle{4}, Api::EdgeHandle{0}, Api::EdgeHandle{6}})
  749. {
  750. Api::HideEdge(*m_whiteBox, edgeHandle);
  751. }
  752. const auto polygonHandle = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0});
  753. const auto polygonCount = Api::MeshPolygonHandles(*m_whiteBox).size();
  754. const auto borderPolygonVertexHandlesCollection = Api::PolygonBorderVertexHandles(*m_whiteBox, polygonHandle);
  755. // attempt appending a polygon
  756. const auto nextPolygonHandle =
  757. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0}), 1.0f);
  758. // mesh is unchanged, polygon count is as before, same polygon handle is returned
  759. EXPECT_THAT(polygonCount, Eq(1));
  760. EXPECT_THAT(nextPolygonHandle, Eq(polygonHandle));
  761. EXPECT_THAT(borderPolygonVertexHandlesCollection, Eq(Api::VertexHandlesCollection{}));
  762. }
  763. TEST_F(WhiteBoxTestFixture, MeshReturnsBothBordersOfPolygonWithHole)
  764. {
  765. namespace Api = WhiteBox::Api;
  766. using ::testing::Eq;
  767. using ::testing::UnorderedElementsAreArray;
  768. const auto polygonHandles = Api::InitializeAsUnitCube(*m_whiteBox);
  769. const auto polygonHandle = Api::ScalePolygonAppendRelative(
  770. *m_whiteBox, Api::PolygonHandle{Api::FaceHandles{{Api::FaceHandle{4}, Api::FaceHandle{5}}}}, -0.25f);
  771. // hide all 'logical'/'visible' edges for scale appended face
  772. for (const auto& edgeHandle : {Api::EdgeHandle{25}, Api::EdgeHandle{27}, Api::EdgeHandle{24}})
  773. {
  774. Api::HideEdge(*m_whiteBox, edgeHandle);
  775. }
  776. const auto expectedLoopFaceHandles =
  777. Api::FaceHandles{Api::FaceHandle{16}, Api::FaceHandle{17}, Api::FaceHandle{14}, Api::FaceHandle{15},
  778. Api::FaceHandle{12}, Api::FaceHandle{13}, Api::FaceHandle{19}, Api::FaceHandle{18}};
  779. const auto expectedFirstBorderVertexHandles = Api::VertexHandles{
  780. Api::VertexHandle{11}, Api::VertexHandle{10}, Api::VertexHandle{9}, Api::VertexHandle{8}};
  781. const auto expectedSecondBorderVertexHandles =
  782. Api::VertexHandles{Api::VertexHandle{0}, Api::VertexHandle{1}, Api::VertexHandle{5}, Api::VertexHandle{4}};
  783. const auto loopPolygonHandle = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{13});
  784. const auto borderVertexHandlesCollection = Api::PolygonBorderVertexHandles(*m_whiteBox, loopPolygonHandle);
  785. EXPECT_THAT(borderVertexHandlesCollection.size(), Eq(2));
  786. EXPECT_THAT(
  787. loopPolygonHandle.m_faceHandles,
  788. UnorderedElementsAreArray(expectedLoopFaceHandles.cbegin(), expectedLoopFaceHandles.cend()));
  789. EXPECT_THAT(
  790. borderVertexHandlesCollection[0],
  791. UnorderedElementsAreArray(
  792. expectedFirstBorderVertexHandles.cbegin(), expectedFirstBorderVertexHandles.cend()));
  793. EXPECT_THAT(
  794. borderVertexHandlesCollection[1],
  795. UnorderedElementsAreArray(
  796. expectedSecondBorderVertexHandles.cbegin(), expectedSecondBorderVertexHandles.cend()));
  797. }
  798. TEST_F(WhiteBoxTestFixture, MeshReturnsMultipleBordersOfHollowCylinderPolygon)
  799. {
  800. namespace Api = WhiteBox::Api;
  801. using ::testing::Eq;
  802. using ::testing::UnorderedElementsAreArray;
  803. Api::InitializeAsUnitCube(*m_whiteBox);
  804. // hide all vertical 'logical'/'visible' edges
  805. for (const auto& edgeHandle : {Api::EdgeHandle{13}, Api::EdgeHandle{15}, Api::EdgeHandle{12}})
  806. {
  807. Api::HideEdge(*m_whiteBox, edgeHandle);
  808. }
  809. const auto expectedLoopFaceHandles =
  810. Api::FaceHandles{Api::FaceHandle{9}, Api::FaceHandle{8}, Api::FaceHandle{7}, Api::FaceHandle{6},
  811. Api::FaceHandle{5}, Api::FaceHandle{4}, Api::FaceHandle{11}, Api::FaceHandle{10}};
  812. const auto expectedFirstBorderVertexHandles =
  813. Api::VertexHandles{Api::VertexHandle{0}, Api::VertexHandle{1}, Api::VertexHandle{2}, Api::VertexHandle{3}};
  814. const auto expectedSecondBorderVertexHandles =
  815. Api::VertexHandles{Api::VertexHandle{4}, Api::VertexHandle{5}, Api::VertexHandle{6}, Api::VertexHandle{7}};
  816. const auto loopPolygonHandle = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{11});
  817. const auto borderVertexHandlesCollection = Api::PolygonBorderVertexHandles(*m_whiteBox, loopPolygonHandle);
  818. EXPECT_THAT(borderVertexHandlesCollection.size(), Eq(2));
  819. EXPECT_THAT(
  820. loopPolygonHandle.m_faceHandles,
  821. UnorderedElementsAreArray(expectedLoopFaceHandles.cbegin(), expectedLoopFaceHandles.cend()));
  822. EXPECT_THAT(
  823. borderVertexHandlesCollection[0],
  824. UnorderedElementsAreArray(
  825. expectedFirstBorderVertexHandles.cbegin(), expectedFirstBorderVertexHandles.cend()));
  826. EXPECT_THAT(
  827. borderVertexHandlesCollection[1],
  828. UnorderedElementsAreArray(
  829. expectedSecondBorderVertexHandles.cbegin(), expectedSecondBorderVertexHandles.cend()));
  830. }
  831. TEST_F(WhiteBoxTestFixture, SingleEdgeCanBeRestored)
  832. {
  833. namespace Api = WhiteBox::Api;
  834. using ::testing::Eq;
  835. using ::testing::UnorderedElementsAre;
  836. Api::InitializeAsUnitCube(*m_whiteBox);
  837. Api::EdgeHandles restoringEdgeHandles;
  838. const AZStd::optional<AZStd::array<Api::PolygonHandle, 2>> splitPolygons =
  839. Api::RestoreEdge(*m_whiteBox, Api::EdgeHandle{2}, restoringEdgeHandles);
  840. // no left over restoring edge handles
  841. EXPECT_THAT(restoringEdgeHandles.size(), Eq(0));
  842. // split polygon was returned
  843. EXPECT_THAT(splitPolygons.has_value(), Eq(true));
  844. // each polygon has a single face
  845. EXPECT_THAT((*splitPolygons)[0].m_faceHandles.size(), Eq(1));
  846. EXPECT_THAT((*splitPolygons)[1].m_faceHandles.size(), Eq(1));
  847. // each polygon has 3 edges
  848. EXPECT_THAT(
  849. Api::PolygonBorderEdgeHandlesFlattened(*m_whiteBox, (*splitPolygons)[0]),
  850. UnorderedElementsAre(Api::EdgeHandle{0}, Api::EdgeHandle{1}, Api::EdgeHandle{2}));
  851. EXPECT_THAT(
  852. Api::PolygonBorderEdgeHandlesFlattened(*m_whiteBox, (*splitPolygons)[1]),
  853. UnorderedElementsAre(Api::EdgeHandle{2}, Api::EdgeHandle{3}, Api::EdgeHandle{4}));
  854. }
  855. TEST_F(WhiteBoxTestFixture, MultipleEdgesCanBeRestored)
  856. {
  857. namespace Api = WhiteBox::Api;
  858. using ::testing::Eq;
  859. Create3x3CubeGrid(*m_whiteBox);
  860. HideAllTopUserEdgesFor3x3Grid(*m_whiteBox);
  861. const auto edgeHandlesToRestore = {
  862. Api::EdgeHandle{48}, Api::EdgeHandle{47}, Api::EdgeHandle{27}, Api::EdgeHandle{59}, Api::EdgeHandle{41}};
  863. int restoreCount = 0;
  864. Api::EdgeHandles restoringEdgeHandles; // inout param
  865. AZStd::optional<AZStd::array<Api::PolygonHandle, 2>> splitPolygons;
  866. for (const Api::EdgeHandle& edgeHandleToRestore : edgeHandlesToRestore)
  867. {
  868. splitPolygons = Api::RestoreEdge(*m_whiteBox, edgeHandleToRestore, restoringEdgeHandles);
  869. restoreCount++;
  870. if (splitPolygons.has_value())
  871. {
  872. break;
  873. }
  874. }
  875. EXPECT_THAT((*splitPolygons)[0].m_faceHandles.size(), Eq(8));
  876. EXPECT_THAT((*splitPolygons)[1].m_faceHandles.size(), Eq(10));
  877. EXPECT_THAT(restoringEdgeHandles.size(), Eq(0));
  878. EXPECT_THAT(restoreCount, Eq(edgeHandlesToRestore.size()));
  879. }
  880. TEST_F(WhiteBoxTestFixture, RestoreExistingUserEdgeHasNoEffect)
  881. {
  882. namespace Api = WhiteBox::Api;
  883. using ::testing::Eq;
  884. Api::InitializeAsUnitCube(*m_whiteBox);
  885. Api::EdgeHandles restoringEdgeHandles;
  886. const AZStd::optional<AZStd::array<Api::PolygonHandle, 2>> splitPolygons =
  887. Api::RestoreEdge(*m_whiteBox, Api::EdgeHandle{12}, restoringEdgeHandles);
  888. // no left over restoring edge handles
  889. EXPECT_THAT(restoringEdgeHandles.size(), Eq(0));
  890. // split polygon was not returned
  891. EXPECT_THAT(splitPolygons.has_value(), Eq(false));
  892. }
  893. TEST_F(WhiteBoxTestFixture, RestoreInnerOuterBorderSplitsPolygonLoop)
  894. {
  895. namespace Api = WhiteBox::Api;
  896. using ::testing::Eq;
  897. Create3x3CubeGrid(*m_whiteBox);
  898. // hide edges to form a polygon with a hole (inner and outer edge list - two borders)
  899. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{41});
  900. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{12});
  901. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{20});
  902. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{0});
  903. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{4});
  904. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{48});
  905. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{85});
  906. Api::EdgeHandles restoringEdgeHandles;
  907. const AZStd::optional<AZStd::array<Api::PolygonHandle, 2>> firstSplitPolygonsAttempt =
  908. Api::RestoreEdge(*m_whiteBox, Api::EdgeHandle{88}, restoringEdgeHandles);
  909. // one left over restoring edge handles
  910. EXPECT_THAT(restoringEdgeHandles.size(), Eq(1));
  911. // split polygon was not returned
  912. EXPECT_THAT(firstSplitPolygonsAttempt.has_value(), Eq(false));
  913. const AZStd::optional<AZStd::array<Api::PolygonHandle, 2>> secondSplitPolygonsAttempt =
  914. Api::RestoreEdge(*m_whiteBox, Api::EdgeHandle{28}, restoringEdgeHandles);
  915. // no left over restoring edge handles
  916. EXPECT_THAT(restoringEdgeHandles.empty(), Eq(true));
  917. // split polygon was returned
  918. EXPECT_THAT(secondSplitPolygonsAttempt.has_value(), Eq(true));
  919. }
  920. TEST_F(WhiteBoxTestFixture, PolygonWithMultipleFacesHalfedgeHandles)
  921. {
  922. namespace Api = WhiteBox::Api;
  923. using ::testing::Eq;
  924. Create3x3CubeGrid(*m_whiteBox);
  925. HideAllTopUserEdgesFor3x3Grid(*m_whiteBox);
  926. const Api::PolygonHandle topPolygonHandle = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{24});
  927. const Api::HalfedgeHandles polygonHalfedgeHandles = Api::PolygonHalfedgeHandles(*m_whiteBox, topPolygonHandle);
  928. // halfedge handles
  929. EXPECT_THAT(polygonHalfedgeHandles.size(), Eq(54));
  930. }
  931. TEST_F(WhiteBoxTestFixture, PolygonWithTwoFacesHalfedgeHandles)
  932. {
  933. namespace Api = WhiteBox::Api;
  934. using ::testing::Eq;
  935. using ::testing::UnorderedElementsAre;
  936. Api::InitializeAsUnitCube(*m_whiteBox);
  937. const Api::PolygonHandle topPolygonHandle = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0});
  938. const Api::HalfedgeHandles polygonHalfedgeHandles = Api::PolygonHalfedgeHandles(*m_whiteBox, topPolygonHandle);
  939. // halfedge handles
  940. EXPECT_THAT(polygonHalfedgeHandles.size(), Eq(6));
  941. EXPECT_THAT(
  942. polygonHalfedgeHandles,
  943. UnorderedElementsAre(
  944. Api::HalfedgeHandle{2}, Api::HalfedgeHandle{0}, Api::HalfedgeHandle{8}, Api::HalfedgeHandle{6},
  945. Api::HalfedgeHandle{5}, Api::HalfedgeHandle{4}));
  946. }
  947. TEST_F(WhiteBoxTestFixture, PolygonMultipleFacesBorderVertexPositionsInOrder)
  948. {
  949. namespace Api = WhiteBox::Api;
  950. using ::testing::Eq;
  951. using ::testing::Pointwise;
  952. Create3x3CubeGrid(*m_whiteBox);
  953. HideAllTopUserEdgesFor3x3Grid(*m_whiteBox);
  954. const Api::PolygonHandle topPolygonHandle = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{24});
  955. Api::VertexPositionsCollection polygonBorderVertexPositionsCollection =
  956. Api::PolygonBorderVertexPositions(*m_whiteBox, topPolygonHandle);
  957. const AZStd::vector<AZ::Vector3> expectedBorderVertexPositions = {
  958. AZ::Vector3{0.5f, 0.5f, 0.5f}, AZ::Vector3{-0.5f, 0.5f, 0.5f}, AZ::Vector3{-1.5f, 0.5f, 0.5f},
  959. AZ::Vector3{-2.5f, 0.5f, 0.5f}, AZ::Vector3{-2.5f, -0.5f, 0.5f}, AZ::Vector3{-2.5f, -1.5f, 0.5f},
  960. AZ::Vector3{-2.5f, -2.5f, 0.5f}, AZ::Vector3{-1.5f, -2.5f, 0.5f}, AZ::Vector3{-0.5f, -2.5f, 0.5f},
  961. AZ::Vector3{0.5f, -2.5f, 0.5f}, AZ::Vector3{0.5f, -1.5f, 0.5f}, AZ::Vector3{0.5f, -0.5f, 0.5f}};
  962. // find the bottom corner to start from (used as the pivot position)
  963. auto vertexPositionIt = AZStd::find_if(
  964. polygonBorderVertexPositionsCollection.front().begin(),
  965. polygonBorderVertexPositionsCollection.front().end(),
  966. [](const AZ::Vector3& vertexPosition)
  967. {
  968. return vertexPosition.IsClose(AZ::Vector3{0.5f, 0.5f, 0.5f});
  969. });
  970. // rotate about the pivot to make the ordering of the vertices a little easier to understand
  971. AZStd::rotate(
  972. polygonBorderVertexPositionsCollection.front().begin(), vertexPositionIt,
  973. polygonBorderVertexPositionsCollection.front().end());
  974. // check the vertex positions are what we expect
  975. EXPECT_THAT(polygonBorderVertexPositionsCollection.size(), Eq(1));
  976. EXPECT_THAT(polygonBorderVertexPositionsCollection.front().size(), Eq(12));
  977. EXPECT_THAT(
  978. expectedBorderVertexPositions,
  979. Pointwise(ContainerIsClose(), polygonBorderVertexPositionsCollection.front()));
  980. }
  981. TEST_F(WhiteBoxTestFixture, PolygonFacePositionsForMultiFacePolygon)
  982. {
  983. namespace Api = WhiteBox::Api;
  984. using ::testing::Eq;
  985. using ::testing::Pointwise;
  986. Create2x2CubeGrid(*m_whiteBox);
  987. HideAllTopUserEdgesFor2x2Grid(*m_whiteBox);
  988. AZStd::vector<AZ::Vector3> polygonTriangles =
  989. Api::PolygonFacesPositions(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{29}));
  990. const AZStd::vector<AZ::Vector3> expectedPolygonFacePositions = {
  991. AZ::Vector3{-0.5, -0.5, 0.5}, AZ::Vector3{0.5, -0.5, 0.5}, AZ::Vector3{0.5, 0.5, 0.5},
  992. AZ::Vector3{-0.5, -0.5, 0.5}, AZ::Vector3{0.5, 0.5, 0.5}, AZ::Vector3{-0.5, 0.5, 0.5},
  993. AZ::Vector3{0.5, -0.5, 0.5}, AZ::Vector3{-0.5, -0.5, 0.5}, AZ::Vector3{-0.5, -1.5, 0.5},
  994. AZ::Vector3{0.5, -0.5, 0.5}, AZ::Vector3{-0.5, -1.5, 0.5}, AZ::Vector3{0.5, -1.5, 0.5},
  995. AZ::Vector3{-0.5, -1.5, 0.5}, AZ::Vector3{-0.5, -0.5, 0.5}, AZ::Vector3{-1.5, -0.5, 0.5},
  996. AZ::Vector3{-0.5, -1.5, 0.5}, AZ::Vector3{-1.5, -0.5, 0.5}, AZ::Vector3{-1.5, -1.5, 0.5},
  997. AZ::Vector3{-0.5, -0.5, 0.5}, AZ::Vector3{-0.5, 0.5, 0.5}, AZ::Vector3{-1.5, 0.5, 0.5},
  998. AZ::Vector3{-0.5, -0.5, 0.5}, AZ::Vector3{-1.5, 0.5, 0.5}, AZ::Vector3{-1.5, -0.5, 0.5}};
  999. EXPECT_THAT(polygonTriangles.size(), Eq(24));
  1000. EXPECT_THAT(expectedPolygonFacePositions, Pointwise(ContainerIsClose(), polygonTriangles));
  1001. }
  1002. TEST_F(WhiteBoxTestFixture, HalfedgeHandleNext)
  1003. {
  1004. namespace Api = WhiteBox::Api;
  1005. using ::testing::Eq;
  1006. Api::InitializeAsUnitCube(*m_whiteBox);
  1007. // next - CCW order
  1008. EXPECT_THAT(Api::HalfedgeHandleNext(*m_whiteBox, Api::HalfedgeHandle{5}), Eq(Api::HalfedgeHandle{6}));
  1009. EXPECT_THAT(Api::HalfedgeHandleNext(*m_whiteBox, Api::HalfedgeHandle{34}), Eq(Api::HalfedgeHandle{19}));
  1010. EXPECT_THAT(Api::HalfedgeHandleNext(*m_whiteBox, Api::HalfedgeHandle{30}), Eq(Api::HalfedgeHandle{32}));
  1011. }
  1012. TEST_F(WhiteBoxTestFixture, HalfedgeHandlePrevious)
  1013. {
  1014. namespace Api = WhiteBox::Api;
  1015. using ::testing::Eq;
  1016. Api::InitializeAsUnitCube(*m_whiteBox);
  1017. // previous - CW order
  1018. EXPECT_THAT(Api::HalfedgeHandlePrevious(*m_whiteBox, Api::HalfedgeHandle{6}), Eq(Api::HalfedgeHandle{5}));
  1019. EXPECT_THAT(Api::HalfedgeHandlePrevious(*m_whiteBox, Api::HalfedgeHandle{19}), Eq(Api::HalfedgeHandle{34}));
  1020. EXPECT_THAT(Api::HalfedgeHandlePrevious(*m_whiteBox, Api::HalfedgeHandle{32}), Eq(Api::HalfedgeHandle{30}));
  1021. }
  1022. TEST_F(WhiteBoxTestFixture, CloneOperationProducesIdenticalResults)
  1023. {
  1024. namespace Api = WhiteBox::Api;
  1025. using ::testing::Eq;
  1026. using ::testing::NotNull;
  1027. Create3x3CubeGrid(*m_whiteBox);
  1028. Api::WhiteBoxMeshPtr whiteBoxClone = Api::CloneMesh(*m_whiteBox);
  1029. // ensure all important data is identical
  1030. EXPECT_THAT(whiteBoxClone, NotNull());
  1031. EXPECT_THAT(Api::MeshVertexCount(*m_whiteBox), Eq(Api::MeshVertexCount(*whiteBoxClone)));
  1032. EXPECT_THAT(Api::MeshVertexHandles(*m_whiteBox), Eq(Api::MeshVertexHandles(*whiteBoxClone)));
  1033. EXPECT_THAT(Api::MeshFaceHandles(*m_whiteBox), Eq(Api::MeshFaceHandles(*whiteBoxClone)));
  1034. EXPECT_THAT(Api::MeshEdgeHandles(*m_whiteBox), Eq(Api::MeshEdgeHandles(*whiteBoxClone)));
  1035. EXPECT_THAT(Api::MeshHalfedgeCount(*m_whiteBox), Eq(Api::MeshHalfedgeCount(*whiteBoxClone)));
  1036. EXPECT_THAT(Api::MeshFaces(*m_whiteBox), Eq(Api::MeshFaces(*whiteBoxClone)));
  1037. }
  1038. TEST_F(WhiteBoxTestFixture, EdgeVertexHandlesTailTipAreExpected)
  1039. {
  1040. namespace Api = WhiteBox::Api;
  1041. using ::testing::UnorderedElementsAreArray;
  1042. Api::InitializeAsUnitQuad(*m_whiteBox);
  1043. const AZStd::array<Api::VertexHandle, 2> edge0VertexHandles = {Api::VertexHandle{0}, Api::VertexHandle{1}};
  1044. const AZStd::array<Api::VertexHandle, 2> edge1VertexHandles = {Api::VertexHandle{1}, Api::VertexHandle{2}};
  1045. const AZStd::array<Api::VertexHandle, 2> edge2VertexHandles = {Api::VertexHandle{2}, Api::VertexHandle{0}};
  1046. const AZStd::array<Api::VertexHandle, 2> edge3VertexHandles = {Api::VertexHandle{2}, Api::VertexHandle{3}};
  1047. const AZStd::array<Api::VertexHandle, 2> edge4VertexHandles = {Api::VertexHandle{3}, Api::VertexHandle{0}};
  1048. // note: currently 'first' halfedge handle is always returned internally as
  1049. // it will be the CCW direction of the halfedge
  1050. EXPECT_THAT(
  1051. Api::EdgeVertexHandles(*m_whiteBox, Api::EdgeHandle{0}), UnorderedElementsAreArray(edge0VertexHandles));
  1052. EXPECT_THAT(
  1053. Api::EdgeVertexHandles(*m_whiteBox, Api::EdgeHandle{1}), UnorderedElementsAreArray(edge1VertexHandles));
  1054. EXPECT_THAT(
  1055. Api::EdgeVertexHandles(*m_whiteBox, Api::EdgeHandle{2}), UnorderedElementsAreArray(edge2VertexHandles));
  1056. EXPECT_THAT(
  1057. Api::EdgeVertexHandles(*m_whiteBox, Api::EdgeHandle{3}), UnorderedElementsAreArray(edge3VertexHandles));
  1058. EXPECT_THAT(
  1059. Api::EdgeVertexHandles(*m_whiteBox, Api::EdgeHandle{4}), UnorderedElementsAreArray(edge4VertexHandles));
  1060. }
  1061. TEST_F(WhiteBoxTestFixture, MultipleLoops)
  1062. {
  1063. namespace Api = WhiteBox::Api;
  1064. using ::testing::ElementsAreArray;
  1065. using ::testing::Eq;
  1066. const auto polygonHandle = Api::InitializeAsUnitQuad(*m_whiteBox);
  1067. Api::ScalePolygonAppendRelative(*m_whiteBox, polygonHandle, -0.25f);
  1068. // hide edges to create a polygon loop (two vertex lists)
  1069. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{13});
  1070. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{10});
  1071. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{6});
  1072. // retrieve the halfedge and vertex handles for the polygon loop
  1073. const auto halfedgeHandlesCollection =
  1074. PolygonBorderHalfedgeHandles(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{2}));
  1075. const auto vertexHandlesCollection =
  1076. PolygonBorderVertexHandles(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{2}));
  1077. const AZStd::array<Api::VertexHandle, 4> vertexHandlesFirstBorder = {
  1078. Api::VertexHandle{6}, Api::VertexHandle{5}, Api::VertexHandle{4}, Api::VertexHandle{7}};
  1079. const AZStd::array<Api::VertexHandle, 4> vertexHandlesSecondBorder = {
  1080. Api::VertexHandle{3}, Api::VertexHandle{0}, Api::VertexHandle{1}, Api::VertexHandle{2}};
  1081. const AZStd::array<Api::HalfedgeHandle, 4> halfedgeHandlesFirstBorder = {
  1082. Api::HalfedgeHandle{7}, Api::HalfedgeHandle{3}, Api::HalfedgeHandle{1}, Api::HalfedgeHandle{9}};
  1083. const AZStd::array<Api::HalfedgeHandle, 4> halfedgeHandlesSecondBorder = {
  1084. Api::HalfedgeHandle{24}, Api::HalfedgeHandle{30}, Api::HalfedgeHandle{10}, Api::HalfedgeHandle{18}};
  1085. EXPECT_THAT(vertexHandlesCollection.size(), Eq(2));
  1086. EXPECT_THAT(vertexHandlesCollection[0], ElementsAreArray(vertexHandlesFirstBorder));
  1087. EXPECT_THAT(vertexHandlesCollection[1], ElementsAreArray(vertexHandlesSecondBorder));
  1088. EXPECT_THAT(halfedgeHandlesCollection.size(), Eq(2));
  1089. EXPECT_THAT(halfedgeHandlesCollection[0], ElementsAreArray(halfedgeHandlesFirstBorder));
  1090. EXPECT_THAT(halfedgeHandlesCollection[1], ElementsAreArray(halfedgeHandlesSecondBorder));
  1091. }
  1092. TEST_F(WhiteBoxTestFixture, ExtrusionFromQuadWithBoundaryEdges)
  1093. {
  1094. namespace Api = WhiteBox::Api;
  1095. using ::testing::Eq;
  1096. Api::InitializeAsUnitQuad(*m_whiteBox);
  1097. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0}), 1.0f);
  1098. EXPECT_THAT(Api::MeshVertexCount(*m_whiteBox), Eq(8));
  1099. // note: MeshFaceCount should be 12 when 2D extrusion case is correctly handled
  1100. EXPECT_THAT(Api::MeshFaceCount(*m_whiteBox), Eq(10));
  1101. }
  1102. TEST_F(WhiteBoxTestFixture, ImpressionOneConnectedEdge)
  1103. {
  1104. namespace Api = WhiteBox::Api;
  1105. using ::testing::Eq;
  1106. Api::InitializeAsUnitCube(*m_whiteBox);
  1107. // append another cube
  1108. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{9}), 1.0f);
  1109. // use impression to squash one of the cubes down
  1110. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{16}), -0.5f);
  1111. EXPECT_THAT(Api::MeshVertexCount(*m_whiteBox), Eq(14)); // 2 vertices added
  1112. EXPECT_THAT(Api::MeshFaceCount(*m_whiteBox), Eq(24)); // 4 faces added (2 for side polygon, 2 for linking)
  1113. EXPECT_THAT(Api::MeshPolygonHandles(*m_whiteBox).size(), Eq(13)); // 3 polygons added
  1114. }
  1115. TEST_F(WhiteBoxTestFixture, ImpressionTwoConnectedEdges)
  1116. {
  1117. namespace Api = WhiteBox::Api;
  1118. using ::testing::Eq;
  1119. Api::InitializeAsUnitCube(*m_whiteBox);
  1120. // append another cube
  1121. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{9}), 1.0f);
  1122. // append another cube
  1123. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{11}), 1.0f);
  1124. // use impression to squash center cube down
  1125. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{16}), -0.5f);
  1126. EXPECT_THAT(Api::MeshVertexCount(*m_whiteBox), Eq(20)); // 4 vertices added
  1127. EXPECT_THAT(Api::MeshFaceCount(*m_whiteBox), Eq(36)); // 8 faces added (4 for side polygons, 4 for linking)
  1128. EXPECT_THAT(Api::MeshPolygonHandles(*m_whiteBox).size(), Eq(20)); // 6 polygons added
  1129. }
  1130. TEST_F(WhiteBoxTestFixture, ImpressionFourConnectedEdges)
  1131. {
  1132. namespace Api = WhiteBox::Api;
  1133. using ::testing::Eq;
  1134. Create3x3CubeGrid(*m_whiteBox);
  1135. // use impression to squash center cube
  1136. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{27}), -0.5f);
  1137. EXPECT_THAT(Api::MeshVertexCount(*m_whiteBox), Eq(36)); // 4 vertices added
  1138. EXPECT_THAT(Api::MeshFaceCount(*m_whiteBox), Eq(68)); // 16 faces added (8 for side polygons, 8 for linking)
  1139. EXPECT_THAT(Api::MeshPolygonHandles(*m_whiteBox).size(), Eq(32)); // 12 polygons added
  1140. }
  1141. TEST_F(WhiteBoxTestFixture, ImpressionInsideLoop)
  1142. {
  1143. namespace Api = WhiteBox::Api;
  1144. using ::testing::Eq;
  1145. Api::InitializeAsUnitCube(*m_whiteBox);
  1146. // scale append polygon in
  1147. Api::ScalePolygonAppendRelative(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0}), -0.25f);
  1148. // hide connecting edges (make loop)
  1149. Api::HideEdge(*m_whiteBox, Api::HalfedgeEdgeHandle(*m_whiteBox, Api::HalfedgeHandle{45}));
  1150. Api::HideEdge(*m_whiteBox, Api::HalfedgeEdgeHandle(*m_whiteBox, Api::HalfedgeHandle{50}));
  1151. Api::HideEdge(*m_whiteBox, Api::HalfedgeEdgeHandle(*m_whiteBox, Api::HalfedgeHandle{54}));
  1152. // use impression to squash center polygon
  1153. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{11}), -0.5f);
  1154. EXPECT_THAT(Api::MeshVertexCount(*m_whiteBox), Eq(16)); // 4 vertices added
  1155. EXPECT_THAT(Api::MeshFaceCount(*m_whiteBox), Eq(28)); // 28 faces added
  1156. EXPECT_THAT(Api::MeshPolygonHandles(*m_whiteBox).size(), Eq(11)); // 11 polygons added (should be 7 before?)
  1157. }
  1158. TEST_F(WhiteBoxTestFixture, ImpressionOutsideLoop)
  1159. {
  1160. namespace Api = WhiteBox::Api;
  1161. using ::testing::Eq;
  1162. Api::InitializeAsUnitCube(*m_whiteBox);
  1163. // scale append polygon in
  1164. Api::ScalePolygonAppendRelative(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0}), -0.25f);
  1165. // hide connecting edges (make loop)
  1166. Api::HideEdge(*m_whiteBox, Api::HalfedgeEdgeHandle(*m_whiteBox, Api::HalfedgeHandle{45}));
  1167. Api::HideEdge(*m_whiteBox, Api::HalfedgeEdgeHandle(*m_whiteBox, Api::HalfedgeHandle{50}));
  1168. Api::HideEdge(*m_whiteBox, Api::HalfedgeEdgeHandle(*m_whiteBox, Api::HalfedgeHandle{54}));
  1169. // use impression to squash outer polygon loop
  1170. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{19}), -0.5f);
  1171. EXPECT_THAT(Api::MeshVertexCount(*m_whiteBox), Eq(16)); // 4 vertices added
  1172. EXPECT_THAT(Api::MeshFaceCount(*m_whiteBox), Eq(28)); // 28 faces added
  1173. EXPECT_THAT(Api::MeshPolygonHandles(*m_whiteBox).size(), Eq(11)); // 11 polygons added (should be 7 before?)
  1174. }
  1175. TEST_F(WhiteBoxTestFixture, AdvancedPolygonAppendReturnsExpectedRestoredPolygonHandles)
  1176. {
  1177. namespace Api = WhiteBox::Api;
  1178. using ::testing::UnorderedElementsAre;
  1179. using ::testing::UnorderedElementsAreArray;
  1180. using fh = Api::FaceHandle;
  1181. Create3x3CubeGrid(*m_whiteBox);
  1182. HideAllTopUserEdgesFor3x3Grid(*m_whiteBox);
  1183. const auto appendedPolygonHandles = Api::TranslatePolygonAppendAdvanced(
  1184. *m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{34}), -1.0f);
  1185. const fh expectedFirstRestoredBefore[] = {fh(54), fh(55), fh(16), fh(17), fh(56), fh(57),
  1186. fh(0), fh(1), fh(5), fh(4), fh(52), fh(53),
  1187. fh(36), fh(37), fh(27), fh(26), fh(25), fh(24)};
  1188. const fh expectedFirstRestoredAfter[] = {fh(38), fh(39), fh(40), fh(41), fh(42), fh(43),
  1189. fh(44), fh(45), fh(46), fh(47), fh(48), fh(49),
  1190. fh(50), fh(51), fh(52), fh(53), fh(54), fh(55)};
  1191. const fh expectedSecondRestoredBefore[] = {fh(32), fh(33)};
  1192. const fh expectedSecondRestoredAfter[] = {fh(56), fh(57)};
  1193. EXPECT_THAT(
  1194. appendedPolygonHandles.m_appendedPolygonHandle.m_faceHandles,
  1195. UnorderedElementsAre(Api::FaceHandle{62}, Api::FaceHandle{63}));
  1196. EXPECT_THAT(
  1197. appendedPolygonHandles.m_restoredPolygonHandles[0].m_before.m_faceHandles,
  1198. UnorderedElementsAreArray(expectedFirstRestoredBefore));
  1199. EXPECT_THAT(
  1200. appendedPolygonHandles.m_restoredPolygonHandles[0].m_after.m_faceHandles,
  1201. UnorderedElementsAreArray(expectedFirstRestoredAfter));
  1202. EXPECT_THAT(
  1203. appendedPolygonHandles.m_restoredPolygonHandles[1].m_before.m_faceHandles,
  1204. UnorderedElementsAreArray(expectedSecondRestoredBefore));
  1205. EXPECT_THAT(
  1206. appendedPolygonHandles.m_restoredPolygonHandles[1].m_after.m_faceHandles,
  1207. UnorderedElementsAreArray(expectedSecondRestoredAfter));
  1208. }
  1209. TEST_F(WhiteBoxTestFixture, EdgeHandlesConnectedToVertex)
  1210. {
  1211. namespace Api = WhiteBox::Api;
  1212. using ::testing::UnorderedElementsAre;
  1213. Api::InitializeAsUnitCube(*m_whiteBox);
  1214. const auto edgeHandles = Api::VertexEdgeHandles(*m_whiteBox, Api::VertexHandle{0});
  1215. EXPECT_THAT(
  1216. edgeHandles,
  1217. UnorderedElementsAre(
  1218. Api::EdgeHandle{4}, Api::EdgeHandle{0}, Api::EdgeHandle{12}, Api::EdgeHandle{17}, Api::EdgeHandle{2}));
  1219. }
  1220. TEST_F(WhiteBoxTestFixture, EdgeHandlesConnectedToVertexAfterPolygonAppend)
  1221. {
  1222. namespace Api = WhiteBox::Api;
  1223. using ::testing::UnorderedElementsAre;
  1224. Api::InitializeAsUnitCube(*m_whiteBox);
  1225. Api::TranslatePolygonAppend(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{10}), 1.0f);
  1226. const auto edgeHandles = Api::VertexEdgeHandles(*m_whiteBox, Api::VertexHandle{0});
  1227. EXPECT_THAT(
  1228. edgeHandles,
  1229. UnorderedElementsAre(
  1230. Api::EdgeHandle{4}, Api::EdgeHandle{0}, Api::EdgeHandle{12}, Api::EdgeHandle{25}, Api::EdgeHandle{2},
  1231. Api::EdgeHandle{28}));
  1232. }
  1233. TEST_F(WhiteBoxTestFixture, VertexCanBeHidden)
  1234. {
  1235. namespace Api = WhiteBox::Api;
  1236. Api::InitializeAsUnitCube(*m_whiteBox);
  1237. Api::HideVertex(*m_whiteBox, Api::VertexHandle{0});
  1238. EXPECT_TRUE(Api::VertexIsHidden(*m_whiteBox, Api::VertexHandle{0}));
  1239. }
  1240. TEST_F(WhiteBoxTestFixture, VertexCanBeRestored)
  1241. {
  1242. namespace Api = WhiteBox::Api;
  1243. Api::InitializeAsUnitCube(*m_whiteBox);
  1244. Api::HideVertex(*m_whiteBox, Api::VertexHandle{0});
  1245. // verify precondition
  1246. EXPECT_TRUE(Api::VertexIsHidden(*m_whiteBox, Api::VertexHandle{0}));
  1247. Api::RestoreVertex(*m_whiteBox, Api::VertexHandle{0});
  1248. EXPECT_TRUE(!Api::VertexIsHidden(*m_whiteBox, Api::VertexHandle{0}));
  1249. }
  1250. TEST_F(WhiteBoxTestFixture, EdgeCanBeHidden)
  1251. {
  1252. namespace Api = WhiteBox::Api;
  1253. Api::InitializeAsUnitCube(*m_whiteBox);
  1254. EXPECT_FALSE(Api::EdgeIsHidden(*m_whiteBox, Api::EdgeHandle{0}));
  1255. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{0});
  1256. EXPECT_TRUE(Api::EdgeIsHidden(*m_whiteBox, Api::EdgeHandle{0}));
  1257. }
  1258. TEST_F(WhiteBoxTestFixture, EdgeCanBeRestored)
  1259. {
  1260. namespace Api = WhiteBox::Api;
  1261. Api::InitializeAsUnitCube(*m_whiteBox);
  1262. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{0});
  1263. // verify precondition
  1264. EXPECT_TRUE(Api::EdgeIsHidden(*m_whiteBox, Api::EdgeHandle{0}));
  1265. {
  1266. Api::EdgeHandles restoringEdgeHandles;
  1267. Api::RestoreEdge(*m_whiteBox, Api::EdgeHandle{0}, restoringEdgeHandles);
  1268. }
  1269. EXPECT_FALSE(Api::EdgeIsHidden(*m_whiteBox, Api::EdgeHandle{0}));
  1270. }
  1271. // note: no boundaries implies the mesh is closed (like a cube) as opposed
  1272. // to having unconnected halfedges in the case of a quad
  1273. TEST_F(WhiteBoxTestFixture, HalfedgeHandlesOfEdgeHandleWithoutBoundaries)
  1274. {
  1275. namespace Api = WhiteBox::Api;
  1276. using ::testing::UnorderedElementsAre;
  1277. Api::InitializeAsUnitCube(*m_whiteBox);
  1278. {
  1279. const auto halfedgeHandles = Api::EdgeHalfedgeHandles(*m_whiteBox, Api::EdgeHandle{0});
  1280. EXPECT_THAT(halfedgeHandles, UnorderedElementsAre(Api::HalfedgeHandle{0}, Api::HalfedgeHandle{1}));
  1281. }
  1282. {
  1283. const auto halfedgeHandles = Api::EdgeHalfedgeHandles(*m_whiteBox, Api::EdgeHandle{17});
  1284. EXPECT_THAT(halfedgeHandles, UnorderedElementsAre(Api::HalfedgeHandle{35}, Api::HalfedgeHandle{34}));
  1285. }
  1286. }
  1287. TEST_F(WhiteBoxTestFixture, HalfedgeHandlesOfEdgeHandleWithBoundaries)
  1288. {
  1289. namespace Api = WhiteBox::Api;
  1290. using ::testing::UnorderedElementsAre;
  1291. Api::InitializeAsUnitQuad(*m_whiteBox);
  1292. {
  1293. // edge will only have a single halfedge (one connected face)
  1294. const auto halfedgeHandles = Api::EdgeHalfedgeHandles(*m_whiteBox, Api::EdgeHandle{1});
  1295. EXPECT_THAT(halfedgeHandles, UnorderedElementsAre(Api::HalfedgeHandle{2}));
  1296. }
  1297. {
  1298. const auto halfedgeHandles = Api::EdgeHalfedgeHandles(*m_whiteBox, Api::EdgeHandle{2});
  1299. EXPECT_THAT(halfedgeHandles, UnorderedElementsAre(Api::HalfedgeHandle{5}, Api::HalfedgeHandle{4}));
  1300. }
  1301. }
  1302. TEST_F(WhiteBoxTestFixture, MeshUserEdgeHandlesForDefaultQuad)
  1303. {
  1304. namespace Api = WhiteBox::Api;
  1305. using ::testing::UnorderedElementsAre;
  1306. Api::InitializeAsUnitQuad(*m_whiteBox);
  1307. const auto userMeshEdgeHandles = Api::MeshUserEdgeHandles(*m_whiteBox);
  1308. EXPECT_THAT(userMeshEdgeHandles.m_mesh, UnorderedElementsAre(Api::EdgeHandle{2}));
  1309. EXPECT_THAT(
  1310. userMeshEdgeHandles.m_user,
  1311. UnorderedElementsAre(Api::EdgeHandle{0}, Api::EdgeHandle{1}, Api::EdgeHandle{3}, Api::EdgeHandle{4}));
  1312. }
  1313. // no hidden vertices means only a single edge (the one passed in) will be returned
  1314. TEST_F(WhiteBoxTestFixture, EdgeGroupingOfUserEdgeWithoutHiddenVertex)
  1315. {
  1316. namespace Api = WhiteBox::Api;
  1317. using ::testing::ElementsAre;
  1318. Api::InitializeAsUnitCube(*m_whiteBox);
  1319. {
  1320. const Api::EdgeHandles edgeGrouping = Api::EdgeGrouping(*m_whiteBox, Api::EdgeHandle{0});
  1321. EXPECT_THAT(edgeGrouping, ElementsAre(Api::EdgeHandle{0}));
  1322. }
  1323. {
  1324. const Api::EdgeHandles edgeGrouping = Api::EdgeGrouping(*m_whiteBox, Api::EdgeHandle{15});
  1325. EXPECT_THAT(edgeGrouping, ElementsAre(Api::EdgeHandle{15}));
  1326. }
  1327. }
  1328. // requesting an edge grouping for a 'mesh' edge (not selectable by
  1329. // the user) will return an empty grouping
  1330. TEST_F(WhiteBoxTestFixture, EdgeGroupingOfMeshEdgeWithoutHiddenVertex)
  1331. {
  1332. namespace Api = WhiteBox::Api;
  1333. using ::testing::Eq;
  1334. Api::InitializeAsUnitCube(*m_whiteBox);
  1335. {
  1336. const Api::EdgeHandles edgeGrouping = Api::EdgeGrouping(*m_whiteBox, Api::EdgeHandle{2});
  1337. EXPECT_THAT(edgeGrouping.size(), Eq(0));
  1338. }
  1339. }
  1340. TEST_F(WhiteBoxTestFixture, EdgeGroupingOfUserEdgeWithHiddenVertex)
  1341. {
  1342. namespace Api = WhiteBox::Api;
  1343. using ::testing::UnorderedElementsAreArray;
  1344. Api::InitializeAsUnitCube(*m_whiteBox);
  1345. Api::HideVertex(*m_whiteBox, Api::VertexHandle{0});
  1346. const auto expectedEdgeGrouping = Api::EdgeHandles{Api::EdgeHandle{0}, Api::EdgeHandle{4}, Api::EdgeHandle{12}};
  1347. {
  1348. const Api::EdgeHandles edgeGrouping = Api::EdgeGrouping(*m_whiteBox, Api::EdgeHandle{0});
  1349. EXPECT_THAT(edgeGrouping, UnorderedElementsAreArray(expectedEdgeGrouping));
  1350. }
  1351. {
  1352. const Api::EdgeHandles edgeGrouping = Api::EdgeGrouping(*m_whiteBox, Api::EdgeHandle{12});
  1353. EXPECT_THAT(edgeGrouping, UnorderedElementsAreArray(expectedEdgeGrouping));
  1354. }
  1355. }
  1356. // here verify hidden connected edges will not be added to the grouping
  1357. TEST_F(WhiteBoxTestFixture, EdgeGroupingOfUserEdgeWithHiddenVertexAndConnectedHiddenEdge)
  1358. {
  1359. namespace Api = WhiteBox::Api;
  1360. using ::testing::UnorderedElementsAreArray;
  1361. Api::InitializeAsUnitCube(*m_whiteBox);
  1362. Api::HideVertex(*m_whiteBox, Api::VertexHandle{3});
  1363. Api::HideEdge(*m_whiteBox, Api::EdgeHandle{15});
  1364. const auto expectedEdgeGrouping = Api::EdgeHandles{Api::EdgeHandle{3}, Api::EdgeHandle{4}};
  1365. const Api::EdgeHandles edgeGrouping = Api::EdgeGrouping(*m_whiteBox, Api::EdgeHandle{4});
  1366. EXPECT_THAT(edgeGrouping, UnorderedElementsAreArray(expectedEdgeGrouping));
  1367. }
  1368. TEST_F(WhiteBoxTestFixture, EdgeGroupingForTopLoopOfCube)
  1369. {
  1370. namespace Api = WhiteBox::Api;
  1371. using ::testing::UnorderedElementsAreArray;
  1372. Api::InitializeAsUnitCube(*m_whiteBox);
  1373. // hide all top vertices
  1374. for (const auto& vertexHandle :
  1375. {Api::VertexHandle{0}, Api::VertexHandle{1}, Api::VertexHandle{2}, Api::VertexHandle{3}})
  1376. {
  1377. Api::HideVertex(*m_whiteBox, vertexHandle);
  1378. }
  1379. // hide all vertical edges
  1380. for (const auto& edgeHandle :
  1381. {Api::EdgeHandle{15}, Api::EdgeHandle{13}, Api::EdgeHandle{12}, Api::EdgeHandle{10}})
  1382. {
  1383. Api::HideEdge(*m_whiteBox, edgeHandle);
  1384. }
  1385. // edge grouping is the top loop
  1386. const auto expectedEdgeGrouping =
  1387. Api::EdgeHandles{Api::EdgeHandle{0}, Api::EdgeHandle{1}, Api::EdgeHandle{3}, Api::EdgeHandle{4}};
  1388. const Api::EdgeHandles edgeGrouping = Api::EdgeGrouping(*m_whiteBox, Api::EdgeHandle{3});
  1389. EXPECT_THAT(edgeGrouping, UnorderedElementsAreArray(expectedEdgeGrouping));
  1390. }
  1391. TEST_F(WhiteBoxTestFixture, TriPolygonCreated)
  1392. {
  1393. namespace Api = WhiteBox::Api;
  1394. using ::testing::Pointwise;
  1395. using ::testing::UnorderedElementsAreArray;
  1396. Api::InitializeAsUnitTriangle(*m_whiteBox);
  1397. const auto vertexHandles = Api::MeshVertexHandles(*m_whiteBox);
  1398. const auto vertexPositions = Api::MeshVertexPositions(*m_whiteBox);
  1399. const auto expectedVertexHandles =
  1400. Api::VertexHandles{Api::VertexHandle{0}, Api::VertexHandle{1}, Api::VertexHandle{2}};
  1401. const auto expectedVertexPositions = AZStd::vector<AZ::Vector3>{
  1402. AZ::Vector3{0.0f, 1.0f, 0.0f}, AZ::Vector3{-0.866f, -0.5f, 0.0f}, AZ::Vector3{0.866f, -0.5f, 0.0f}};
  1403. EXPECT_THAT(vertexHandles, UnorderedElementsAreArray(expectedVertexHandles));
  1404. EXPECT_THAT(vertexPositions, Pointwise(ContainerIsClose(), expectedVertexPositions));
  1405. }
  1406. TEST_F(WhiteBoxTestFixture, SplitUserEdgeCausesNewlyFormedFacesToBeAddedToCorrespondingPolygons)
  1407. {
  1408. namespace Api = WhiteBox::Api;
  1409. using ::testing::ElementsAre;
  1410. using ::testing::Eq;
  1411. using ::testing::UnorderedElementsAre;
  1412. Api::InitializeAsUnitCube(*m_whiteBox);
  1413. // verify preconditions
  1414. const auto edgeFaceHandlesBefore = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{0});
  1415. const auto edgeVertexHandlesBefore = Api::EdgeVertexHandles(*m_whiteBox, Api::EdgeHandle{0});
  1416. const auto firstConnectedPolygonHandle = Api::FacePolygonHandle(*m_whiteBox, edgeFaceHandlesBefore[0]);
  1417. const auto secondConnectedPolygonHandle = Api::FacePolygonHandle(*m_whiteBox, edgeFaceHandlesBefore[1]);
  1418. // given
  1419. EXPECT_THAT(edgeFaceHandlesBefore, UnorderedElementsAre(Api::FaceHandle{5}, Api::FaceHandle{0}));
  1420. EXPECT_THAT(
  1421. firstConnectedPolygonHandle.m_faceHandles, UnorderedElementsAre(Api::FaceHandle{0}, Api::FaceHandle{1}));
  1422. EXPECT_THAT(
  1423. secondConnectedPolygonHandle.m_faceHandles, UnorderedElementsAre(Api::FaceHandle{5}, Api::FaceHandle{4}));
  1424. EXPECT_THAT(edgeVertexHandlesBefore, UnorderedElementsAre(Api::VertexHandle{0}, Api::VertexHandle{1}));
  1425. // when
  1426. const Api::VertexHandle splitVertexHandle =
  1427. Api::SplitEdge(*m_whiteBox, Api::EdgeHandle{0}, Api::EdgeMidpoint(*m_whiteBox, Api::EdgeHandle{3}));
  1428. // then
  1429. const auto splitVertexEdgeHandles = Api::VertexEdgeHandles(*m_whiteBox, splitVertexHandle);
  1430. const auto faceHandlesForEdge20 = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{20});
  1431. const auto faceHandlesForEdge19 = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{19});
  1432. const auto faceHandlesForEdge18 = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{18});
  1433. const auto faceHandlesForEdge0 = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{0});
  1434. const auto polygonHandleForFace0 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0});
  1435. const auto polygonHandleForFace5 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{5});
  1436. const auto bordedEdgeHandlesPolygon0 =
  1437. Api::PolygonBorderEdgeHandlesFlattened(*m_whiteBox, polygonHandleForFace0);
  1438. const auto bordedEdgeHandlesPolygon5 =
  1439. Api::PolygonBorderEdgeHandlesFlattened(*m_whiteBox, polygonHandleForFace5);
  1440. EXPECT_THAT(splitVertexHandle, Eq(Api::VertexHandle{8}));
  1441. EXPECT_THAT(Api::VertexIsHidden(*m_whiteBox, splitVertexHandle), Eq(false));
  1442. EXPECT_THAT(
  1443. splitVertexEdgeHandles,
  1444. UnorderedElementsAre(Api::EdgeHandle{0}, Api::EdgeHandle{18}, Api::EdgeHandle{20}, Api::EdgeHandle{19}));
  1445. EXPECT_THAT(faceHandlesForEdge0, UnorderedElementsAre(Api::FaceHandle{0}, Api::FaceHandle{5}));
  1446. EXPECT_THAT(faceHandlesForEdge18, UnorderedElementsAre(Api::FaceHandle{12}, Api::FaceHandle{13}));
  1447. EXPECT_THAT(faceHandlesForEdge19, UnorderedElementsAre(Api::FaceHandle{12}, Api::FaceHandle{0}));
  1448. EXPECT_THAT(faceHandlesForEdge20, UnorderedElementsAre(Api::FaceHandle{5}, Api::FaceHandle{13}));
  1449. EXPECT_THAT(
  1450. polygonHandleForFace0.m_faceHandles, // top face
  1451. UnorderedElementsAre(Api::FaceHandle{0}, Api::FaceHandle{12}, Api::FaceHandle{1}));
  1452. EXPECT_THAT(
  1453. polygonHandleForFace5.m_faceHandles, // near (side) face
  1454. UnorderedElementsAre(Api::FaceHandle{5}, Api::FaceHandle{4}, Api::FaceHandle{13}));
  1455. EXPECT_THAT(
  1456. bordedEdgeHandlesPolygon0,
  1457. ElementsAre(
  1458. Api::EdgeHandle{4}, Api::EdgeHandle{18}, Api::EdgeHandle{0}, Api::EdgeHandle{1}, Api::EdgeHandle{3}));
  1459. EXPECT_THAT(
  1460. bordedEdgeHandlesPolygon5,
  1461. ElementsAre(
  1462. Api::EdgeHandle{12}, Api::EdgeHandle{8}, Api::EdgeHandle{10}, Api::EdgeHandle{0}, Api::EdgeHandle{18}));
  1463. }
  1464. TEST_F(WhiteBoxTestFixture, SplitMeshEdgeCausesNewlyFormedFacesToBeAddedToCorrespondingPolygons)
  1465. {
  1466. namespace Api = WhiteBox::Api;
  1467. using ::testing::ElementsAre;
  1468. using ::testing::Eq;
  1469. using ::testing::UnorderedElementsAre;
  1470. Api::InitializeAsUnitCube(*m_whiteBox);
  1471. // verify preconditions
  1472. const auto edgeFaceHandlesBefore = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{0});
  1473. const auto edgeVertexHandlesBefore = Api::EdgeVertexHandles(*m_whiteBox, Api::EdgeHandle{0});
  1474. const auto firstConnectedPolygonHandle = Api::FacePolygonHandle(*m_whiteBox, edgeFaceHandlesBefore[0]);
  1475. const auto secondConnectedPolygonHandle = Api::FacePolygonHandle(*m_whiteBox, edgeFaceHandlesBefore[1]);
  1476. // given
  1477. EXPECT_THAT(edgeFaceHandlesBefore, UnorderedElementsAre(Api::FaceHandle{5}, Api::FaceHandle{0}));
  1478. EXPECT_THAT(
  1479. firstConnectedPolygonHandle.m_faceHandles, UnorderedElementsAre(Api::FaceHandle{0}, Api::FaceHandle{1}));
  1480. EXPECT_THAT(
  1481. secondConnectedPolygonHandle.m_faceHandles, UnorderedElementsAre(Api::FaceHandle{5}, Api::FaceHandle{4}));
  1482. EXPECT_THAT(edgeVertexHandlesBefore, UnorderedElementsAre(Api::VertexHandle{0}, Api::VertexHandle{1}));
  1483. // when
  1484. const Api::VertexHandle splitVertexHandle =
  1485. Api::SplitEdge(*m_whiteBox, Api::EdgeHandle{11}, Api::EdgeMidpoint(*m_whiteBox, Api::EdgeHandle{11}));
  1486. // then
  1487. const auto splitVertexEdgeHandles = Api::VertexEdgeHandles(*m_whiteBox, splitVertexHandle);
  1488. const auto faceHandlesForEdge20 = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{20});
  1489. const auto faceHandlesForEdge19 = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{19});
  1490. const auto faceHandlesForEdge18 = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{18});
  1491. const auto faceHandlesForEdge11 = Api::EdgeFaceHandles(*m_whiteBox, Api::EdgeHandle{11});
  1492. const auto polygonHandleForFace5 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{5});
  1493. const auto bordedEdgeHandlesPolygon5 =
  1494. Api::PolygonBorderEdgeHandlesFlattened(*m_whiteBox, polygonHandleForFace5);
  1495. EXPECT_THAT(splitVertexHandle, Eq(Api::VertexHandle{8}));
  1496. EXPECT_THAT(Api::VertexIsHidden(*m_whiteBox, splitVertexHandle), Eq(true));
  1497. EXPECT_THAT(
  1498. splitVertexEdgeHandles,
  1499. UnorderedElementsAre(Api::EdgeHandle{20}, Api::EdgeHandle{18}, Api::EdgeHandle{11}, Api::EdgeHandle{19}));
  1500. EXPECT_THAT(faceHandlesForEdge11, UnorderedElementsAre(Api::FaceHandle{5}, Api::FaceHandle{4}));
  1501. EXPECT_THAT(faceHandlesForEdge18, UnorderedElementsAre(Api::FaceHandle{12}, Api::FaceHandle{13}));
  1502. EXPECT_THAT(faceHandlesForEdge19, UnorderedElementsAre(Api::FaceHandle{12}, Api::FaceHandle{4}));
  1503. EXPECT_THAT(faceHandlesForEdge20, UnorderedElementsAre(Api::FaceHandle{5}, Api::FaceHandle{13}));
  1504. EXPECT_THAT(
  1505. polygonHandleForFace5.m_faceHandles, // near (side) face
  1506. UnorderedElementsAre(Api::FaceHandle{5}, Api::FaceHandle{4}, Api::FaceHandle{13}, Api::FaceHandle{12}));
  1507. EXPECT_THAT(
  1508. bordedEdgeHandlesPolygon5,
  1509. ElementsAre(Api::EdgeHandle{0}, Api::EdgeHandle{12}, Api::EdgeHandle{8}, Api::EdgeHandle{10}));
  1510. }
  1511. TEST_F(WhiteBoxTestFixture, SplitFaceCausesNewlyFormedFacesToBeAddedToCorrespondingPolygons)
  1512. {
  1513. namespace Api = WhiteBox::Api;
  1514. using ::testing::Eq;
  1515. using ::testing::UnorderedElementsAre;
  1516. Api::InitializeAsUnitCube(*m_whiteBox);
  1517. const auto polygonHandleForFace0 = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0});
  1518. const auto faceVertexHandles = Api::FaceVertexHandles(*m_whiteBox, Api::FaceHandle{0});
  1519. EXPECT_THAT(
  1520. polygonHandleForFace0.m_faceHandles, // top face
  1521. UnorderedElementsAre(Api::FaceHandle{0}, Api::FaceHandle{1}));
  1522. EXPECT_THAT(
  1523. faceVertexHandles, UnorderedElementsAre(Api::VertexHandle{0}, Api::VertexHandle{1}, Api::VertexHandle{2}));
  1524. const auto splitVertexHandle =
  1525. Api::SplitFace(*m_whiteBox, Api::FaceHandle{0}, Api::FaceMidpoint(*m_whiteBox, Api::FaceHandle{0}));
  1526. const auto edgeHandles = Api::VertexEdgeHandles(*m_whiteBox, splitVertexHandle);
  1527. const auto polygonHandleForFace0After = Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{0});
  1528. const auto faceVertexHandlesAfter = Api::FaceVertexHandles(*m_whiteBox, Api::FaceHandle{0});
  1529. EXPECT_THAT(splitVertexHandle, Eq(Api::VertexHandle{8}));
  1530. EXPECT_THAT(Api::VertexIsHidden(*m_whiteBox, splitVertexHandle), Eq(true));
  1531. EXPECT_THAT(edgeHandles, UnorderedElementsAre(Api::EdgeHandle{18}, Api::EdgeHandle{19}, Api::EdgeHandle{20}));
  1532. EXPECT_THAT(
  1533. polygonHandleForFace0After.m_faceHandles, // top face
  1534. UnorderedElementsAre(Api::FaceHandle{0}, Api::FaceHandle{1}, Api::FaceHandle{12}, Api::FaceHandle{13}));
  1535. EXPECT_THAT(
  1536. faceVertexHandlesAfter,
  1537. UnorderedElementsAre(Api::VertexHandle{0}, Api::VertexHandle{8}, Api::VertexHandle{2}));
  1538. }
  1539. TEST_F(WhiteBoxTestFixture, UserEdgeHandlesReturnedForVertexHandle)
  1540. {
  1541. namespace Api = WhiteBox::Api;
  1542. using ::testing::UnorderedElementsAre;
  1543. Api::InitializeAsUnitCube(*m_whiteBox);
  1544. const auto vertexUserEdgeHandles = Api::VertexUserEdgeHandles(*m_whiteBox, Api::VertexHandle{2});
  1545. // in the unit cube, vertex handle 2 has 5 connected edge handles but only two of these
  1546. // are user edges (two are internal edges of a cube face)
  1547. EXPECT_THAT(
  1548. vertexUserEdgeHandles, UnorderedElementsAre(Api::EdgeHandle{1}, Api::EdgeHandle{3}, Api::EdgeHandle{13}));
  1549. }
  1550. TEST_F(WhiteBoxTestFixture, UserEdgeAxesReturnedForVertexHandle)
  1551. {
  1552. namespace Api = WhiteBox::Api;
  1553. using ::testing::Pointwise;
  1554. Api::InitializeAsUnitCube(*m_whiteBox);
  1555. const auto vertexUserEdgeVectors = Api::VertexUserEdgeVectors(*m_whiteBox, Api::VertexHandle{2});
  1556. const auto expectedUserEdgeVectors = AZStd::vector<AZ::Vector3>{
  1557. -AZ::Vector3::CreateAxisZ(), -AZ::Vector3::CreateAxisX(), -AZ::Vector3::CreateAxisY()};
  1558. EXPECT_THAT(vertexUserEdgeVectors, Pointwise(ContainerIsClose(), expectedUserEdgeVectors));
  1559. }
  1560. TEST_F(WhiteBoxTestFixture, EdgeAxisReturnedForEdgeHandle)
  1561. {
  1562. namespace Api = WhiteBox::Api;
  1563. Api::InitializeAsUnitCube(*m_whiteBox);
  1564. Api::TranslatePolygon(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{11}), 1.0f);
  1565. Api::TranslatePolygon(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{1}), 0.5f);
  1566. const auto edgeVector_4 = Api::EdgeVector(*m_whiteBox, Api::EdgeHandle{4});
  1567. const auto edgeVector_17 = Api::EdgeVector(*m_whiteBox, Api::EdgeHandle{17});
  1568. const auto edgeVector_12 = Api::EdgeVector(*m_whiteBox, Api::EdgeHandle{12});
  1569. const auto edgeVector_0 = Api::EdgeVector(*m_whiteBox, Api::EdgeHandle{0});
  1570. const auto edgeVector_2 = Api::EdgeVector(*m_whiteBox, Api::EdgeHandle{2});
  1571. EXPECT_THAT(edgeVector_4, IsClose(AZ::Vector3(0.0f, -1.0f, 0.0f)));
  1572. EXPECT_THAT(edgeVector_17, IsClose(AZ::Vector3(0.0f, 1.0f, -1.5f)));
  1573. EXPECT_THAT(edgeVector_12, IsClose(AZ::Vector3(0.0f, 0.0f, -1.5f)));
  1574. EXPECT_THAT(edgeVector_0, IsClose(AZ::Vector3(2.0f, 0.0f, 0.0f)));
  1575. EXPECT_THAT(edgeVector_2, IsClose(AZ::Vector3(-2.0f, -1.0f, 0.0f)));
  1576. }
  1577. TEST_F(WhiteBoxTestFixture, UserEdgesWithZeroLengthNotReturned)
  1578. {
  1579. namespace Api = WhiteBox::Api;
  1580. using ::testing::Pointwise;
  1581. Api::InitializeAsUnitCube(*m_whiteBox);
  1582. // squash a cube to be flat (where certain edges will have zero length)
  1583. Api::TranslatePolygon(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{1}), 1.0f);
  1584. Api::TranslatePolygon(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{5}), 2.0f);
  1585. Api::TranslatePolygon(*m_whiteBox, Api::FacePolygonHandle(*m_whiteBox, Api::FaceHandle{11}), -1.0f);
  1586. const auto vertexUserEdgeVectors = Api::VertexUserEdgeVectors(*m_whiteBox, Api::VertexHandle{2});
  1587. const auto vertexUserEdgeAxes = Api::VertexUserEdgeAxes(*m_whiteBox, Api::VertexHandle{2});
  1588. const auto expectedUserEdgeVectors =
  1589. AZStd::vector<AZ::Vector3>{-AZ::Vector3::CreateAxisZ(2.0f), -AZ::Vector3::CreateAxisY(3.0f)};
  1590. const auto expectedUserEdgeAxes =
  1591. AZStd::vector<AZ::Vector3>{-AZ::Vector3::CreateAxisZ(), -AZ::Vector3::CreateAxisY()};
  1592. EXPECT_THAT(vertexUserEdgeVectors, Pointwise(ContainerIsClose(), expectedUserEdgeVectors));
  1593. EXPECT_THAT(vertexUserEdgeAxes, Pointwise(ContainerIsClose(), expectedUserEdgeAxes));
  1594. }
  1595. TEST_F(WhiteBoxTestFixture, IsolatedVerticesAreHiddenWhenCreatingNewPolygons)
  1596. {
  1597. namespace Api = WhiteBox::Api;
  1598. using ::testing::Each;
  1599. using ::testing::Eq;
  1600. using vh = Api::VertexHandle;
  1601. Create3x3CubeGrid(*m_whiteBox);
  1602. HideAllTopUserEdgesFor3x3Grid(*m_whiteBox);
  1603. const Api::VertexHandle internalVertexHandles[] = {vh{0}, vh{11}, vh{20}, vh{16}};
  1604. bool internalVertexHandlesHidden[std::size(internalVertexHandles)];
  1605. bool internalVertexHandlesIsolated[std::size(internalVertexHandles)];
  1606. AZStd::transform(
  1607. AZStd::cbegin(internalVertexHandles), AZStd::cend(internalVertexHandles),
  1608. AZStd::begin(internalVertexHandlesHidden),
  1609. [&whiteBox = m_whiteBox](const vh vertexHandle)
  1610. {
  1611. return Api::VertexIsHidden(*whiteBox, vertexHandle);
  1612. });
  1613. AZStd::transform(
  1614. AZStd::cbegin(internalVertexHandles), AZStd::cend(internalVertexHandles),
  1615. AZStd::begin(internalVertexHandlesIsolated),
  1616. [&whiteBox = m_whiteBox](const vh vertexHandle)
  1617. {
  1618. return Api::VertexIsIsolated(*whiteBox, vertexHandle);
  1619. });
  1620. EXPECT_THAT(internalVertexHandlesHidden, Each(Eq(true)));
  1621. EXPECT_THAT(internalVertexHandlesIsolated, Each(Eq(true)));
  1622. }
  1623. TEST_F(WhiteBoxTestFixture, HiddenVerticesConnectedToRestoredEdgesAreRestored)
  1624. {
  1625. namespace Api = WhiteBox::Api;
  1626. using ::testing::Each;
  1627. using ::testing::Eq;
  1628. using vh = Api::VertexHandle;
  1629. Create3x3CubeGrid(*m_whiteBox);
  1630. HideAllTopUserEdgesFor3x3Grid(*m_whiteBox);
  1631. const Api::VertexHandle reconnectedInternalVertexHandles[] = {vh{11}, vh{20}, vh{16}};
  1632. const auto edgeHandlesToRestore = {
  1633. Api::EdgeHandle{85}, Api::EdgeHandle{45}, Api::EdgeHandle{59}, Api::EdgeHandle{12}};
  1634. bool edgeRestored = false;
  1635. Api::EdgeHandles restoringEdgeHandles; // inout param
  1636. for (const Api::EdgeHandle& edgeHandleToRestore : edgeHandlesToRestore)
  1637. {
  1638. if (Api::RestoreEdge(*m_whiteBox, edgeHandleToRestore, restoringEdgeHandles))
  1639. {
  1640. edgeRestored = true;
  1641. break;
  1642. }
  1643. }
  1644. bool internalVertexHandlesHidden[std::size(reconnectedInternalVertexHandles)];
  1645. bool internalVertexHandlesIsolated[std::size(reconnectedInternalVertexHandles)];
  1646. AZStd::transform(
  1647. AZStd::cbegin(reconnectedInternalVertexHandles), AZStd::cend(reconnectedInternalVertexHandles),
  1648. AZStd::begin(internalVertexHandlesHidden),
  1649. [&whiteBox = m_whiteBox](const vh vertexHandle)
  1650. {
  1651. return Api::VertexIsHidden(*whiteBox, vertexHandle);
  1652. });
  1653. AZStd::transform(
  1654. AZStd::cbegin(reconnectedInternalVertexHandles), AZStd::cend(reconnectedInternalVertexHandles),
  1655. AZStd::begin(internalVertexHandlesIsolated),
  1656. [&whiteBox = m_whiteBox](const vh vertexHandle)
  1657. {
  1658. return Api::VertexIsIsolated(*whiteBox, vertexHandle);
  1659. });
  1660. // ensure the edge was correctly restored
  1661. EXPECT_THAT(edgeRestored, Eq(true));
  1662. // vertex handles connected to restored edges will be no longer be hidden or isolated
  1663. EXPECT_THAT(internalVertexHandlesHidden, Each(Eq(false)));
  1664. EXPECT_THAT(internalVertexHandlesIsolated, Each(Eq(false)));
  1665. // unaffected vertex will remain hidden and isolated
  1666. EXPECT_THAT(Api::VertexIsIsolated(*m_whiteBox, vh{0}), Eq(true));
  1667. EXPECT_THAT(Api::VertexIsHidden(*m_whiteBox, vh{0}), Eq(true));
  1668. }
  1669. TEST_F(WhiteBoxTestFixture, TryingToRestoreIsolatedHidddenVerticesFails)
  1670. {
  1671. namespace Api = WhiteBox::Api;
  1672. using ::testing::Each;
  1673. using ::testing::Eq;
  1674. using vh = Api::VertexHandle;
  1675. Create3x3CubeGrid(*m_whiteBox);
  1676. HideAllTopUserEdgesFor3x3Grid(*m_whiteBox);
  1677. // precondition check
  1678. EXPECT_THAT(Api::VertexIsIsolated(*m_whiteBox, vh{0}), Eq(true));
  1679. EXPECT_THAT(Api::VertexIsHidden(*m_whiteBox, vh{0}), Eq(true));
  1680. const bool vertexRestored = Api::TryRestoreVertex(*m_whiteBox, vh{0});
  1681. // postcondition check - values remain the same
  1682. EXPECT_THAT(vertexRestored, Eq(false));
  1683. EXPECT_THAT(Api::VertexIsIsolated(*m_whiteBox, vh{0}), Eq(true));
  1684. EXPECT_THAT(Api::VertexIsHidden(*m_whiteBox, vh{0}), Eq(true));
  1685. }
  1686. TEST_F(WhiteBoxTestFixture, TryingToRestoreConnectedHidddenVerticesSucceeds)
  1687. {
  1688. namespace Api = WhiteBox::Api;
  1689. using ::testing::Each;
  1690. using ::testing::Eq;
  1691. using vh = Api::VertexHandle;
  1692. Create3x3CubeGrid(*m_whiteBox);
  1693. // precondition check
  1694. Api::HideVertex(*m_whiteBox, vh{0});
  1695. EXPECT_THAT(Api::VertexIsIsolated(*m_whiteBox, vh{0}), Eq(false));
  1696. EXPECT_THAT(Api::VertexIsHidden(*m_whiteBox, vh{0}), Eq(true));
  1697. const bool vertexRestored = Api::TryRestoreVertex(*m_whiteBox, vh{0});
  1698. // postcondition check - values have changed
  1699. EXPECT_THAT(vertexRestored, Eq(true));
  1700. EXPECT_THAT(Api::VertexIsIsolated(*m_whiteBox, vh{0}), Eq(false));
  1701. EXPECT_THAT(Api::VertexIsHidden(*m_whiteBox, vh{0}), Eq(false));
  1702. }
  1703. } // namespace UnitTest
  1704. // Required to support running integration tests with Qt
  1705. AZTEST_EXPORT int AZ_UNIT_TEST_HOOK_NAME(int argc, char** argv)
  1706. {
  1707. ::testing::InitGoogleMock(&argc, argv);
  1708. AzQtComponents::PrepareQtPaths();
  1709. QApplication app(argc, argv);
  1710. AZ::Test::printUnusedParametersWarning(argc, argv);
  1711. int result = RUN_ALL_TESTS();
  1712. return result;
  1713. }
  1714. IMPLEMENT_TEST_EXECUTABLE_MAIN();