3
0

WhiteBoxTest.cpp 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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();