AnimGraphSyncTrackTests.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 "SystemComponentFixture.h"
  9. #include "TestAssetCode/MotionEvent.h"
  10. #include <EMotionFX/Source/EventManager.h>
  11. #include <EMotionFX/Source/MotionEventTable.h>
  12. #include <EMotionFX/Source/MotionManager.h>
  13. #include <EMotionFX/Source/Motion.h>
  14. #include <EMotionFX/Source/TwoStringEventData.h>
  15. #include <EMotionFX/Source/MotionData/NonUniformMotionData.h>
  16. namespace EMotionFX
  17. {
  18. struct FindEventIndicesParams
  19. {
  20. void (*m_eventFactory)(MotionEventTrack* track);
  21. float m_timeValue;
  22. size_t m_expectedLeft;
  23. size_t m_expectedRight;
  24. };
  25. void PrintTo(FindEventIndicesParams const object, ::std::ostream* os)
  26. {
  27. if (object.m_eventFactory == &MakeNoEvents)
  28. {
  29. *os << "Events: 0";
  30. }
  31. else if (object.m_eventFactory == &MakeOneEvent)
  32. {
  33. *os << "Events: 1";
  34. }
  35. else if (object.m_eventFactory == &MakeTwoEvents)
  36. {
  37. *os << "Events: 2";
  38. }
  39. else if (object.m_eventFactory == &MakeThreeEvents)
  40. {
  41. *os << "Events: 3";
  42. }
  43. else
  44. {
  45. *os << "Events: Unknown";
  46. }
  47. *os << " Time value: " << object.m_timeValue
  48. << " Expected left: " << object.m_expectedLeft
  49. << " Expected right: " << object.m_expectedRight
  50. ;
  51. }
  52. class TestFindEventIndicesFixture : public SystemComponentFixture,
  53. public ::testing::WithParamInterface<FindEventIndicesParams>
  54. {
  55. void SetUp() override
  56. {
  57. SystemComponentFixture::SetUp();
  58. m_motion = aznew Motion("TestFindEventIndicesMotion");
  59. m_motion->SetMotionData(aznew NonUniformMotionData());
  60. m_motion->GetMotionData()->SetDuration(2.0f);
  61. m_motion->GetEventTable()->AutoCreateSyncTrack(m_motion);
  62. m_syncTrack = m_motion->GetEventTable()->GetSyncTrack();
  63. const FindEventIndicesParams& params = GetParam();
  64. params.m_eventFactory(m_syncTrack);
  65. }
  66. void TearDown() override
  67. {
  68. m_motion->Destroy();
  69. SystemComponentFixture::TearDown();
  70. }
  71. protected:
  72. Motion* m_motion = nullptr;
  73. AnimGraphSyncTrack* m_syncTrack = nullptr;
  74. };
  75. TEST_P(TestFindEventIndicesFixture, TestFindEventIndices)
  76. {
  77. const FindEventIndicesParams& params = GetParam();
  78. size_t indexLeft, indexRight;
  79. m_syncTrack->FindEventIndices(params.m_timeValue, &indexLeft, &indexRight);
  80. EXPECT_EQ(indexLeft, params.m_expectedLeft);
  81. EXPECT_EQ(indexRight, params.m_expectedRight);
  82. }
  83. INSTANTIATE_TEST_SUITE_P(TestFindEventIndices, TestFindEventIndicesFixture,
  84. ::testing::ValuesIn(std::vector<FindEventIndicesParams> {
  85. {
  86. MakeNoEvents,
  87. 0.5f,
  88. InvalidIndex,
  89. InvalidIndex
  90. },
  91. {
  92. MakeOneEvent,
  93. 0.0f,
  94. 0,
  95. 0
  96. },
  97. {
  98. MakeOneEvent,
  99. 0.5f,
  100. 0,
  101. 0
  102. },
  103. {
  104. MakeTwoEvents,
  105. 0.0f,
  106. 1,
  107. 0
  108. },
  109. {
  110. MakeTwoEvents,
  111. 0.5f,
  112. 0,
  113. 1
  114. },
  115. {
  116. MakeTwoEvents,
  117. 1.0f,
  118. 1,
  119. 0
  120. },
  121. {
  122. MakeThreeEvents,
  123. 0.0f,
  124. 2,
  125. 0
  126. },
  127. {
  128. MakeThreeEvents,
  129. 0.5f,
  130. 0,
  131. 1
  132. },
  133. {
  134. MakeThreeEvents,
  135. 1.0f,
  136. 1,
  137. 2
  138. },
  139. {
  140. MakeThreeEvents,
  141. 1.5f,
  142. 2,
  143. 0
  144. },
  145. {
  146. [](MotionEventTrack* track)
  147. {
  148. MakeTwoEvents(track);
  149. MakeTwoEvents(track);
  150. },
  151. 0.25,
  152. 1,
  153. 2
  154. },
  155. })
  156. );
  157. struct FindMatchingEventsParams
  158. {
  159. void (*m_eventFactory)(MotionEventTrack* track);
  160. size_t m_startingIndex;
  161. size_t m_inEventAIndex;
  162. size_t m_inEventBIndex;
  163. size_t m_expectedEventA;
  164. size_t m_expectedEventB;
  165. bool m_mirrorInput;
  166. bool m_mirrorOutput;
  167. bool m_forward;
  168. };
  169. void PrintTo(FindMatchingEventsParams const object, ::std::ostream* os)
  170. {
  171. if (object.m_eventFactory == &MakeNoEvents)
  172. {
  173. *os << "Events: 0";
  174. }
  175. else if (object.m_eventFactory == &MakeOneEvent)
  176. {
  177. *os << "Events: 1";
  178. }
  179. else if (object.m_eventFactory == &MakeTwoLeftRightEvents)
  180. {
  181. *os << "Events: LRLR";
  182. }
  183. else
  184. {
  185. *os << "Events: Unknown";
  186. }
  187. *os << " Start index: " << object.m_startingIndex
  188. << " In Event A: " << object.m_inEventAIndex
  189. << " In Event B: " << object.m_inEventBIndex
  190. << " Expected Event A: " << object.m_expectedEventA
  191. << " Expected Event B: " << object.m_expectedEventB
  192. << " Mirror Input: " << object.m_mirrorInput
  193. << " Mirror Output: " << object.m_mirrorOutput
  194. << " Play direction: " << (object.m_forward ? "Forward" : "Backward")
  195. ;
  196. }
  197. class TestFindMatchingEventsFixture : public SystemComponentFixture,
  198. public ::testing::WithParamInterface<FindMatchingEventsParams>
  199. {
  200. void SetUp() override
  201. {
  202. SystemComponentFixture::SetUp();
  203. m_motion = aznew Motion("TestFindMatchingEventsMotion");
  204. m_motion->SetMotionData(aznew NonUniformMotionData());
  205. m_motion->GetMotionData()->SetDuration(4.0f);
  206. m_motion->GetEventTable()->AutoCreateSyncTrack(m_motion);
  207. m_syncTrack = m_motion->GetEventTable()->GetSyncTrack();
  208. const FindMatchingEventsParams& params = GetParam();
  209. params.m_eventFactory(m_syncTrack);
  210. }
  211. void TearDown() override
  212. {
  213. m_motion->Destroy();
  214. SystemComponentFixture::TearDown();
  215. }
  216. protected:
  217. Motion* m_motion = nullptr;
  218. AnimGraphSyncTrack* m_syncTrack = nullptr;
  219. };
  220. TEST_P(TestFindMatchingEventsFixture, TestFindMatchingEvents)
  221. {
  222. const FindMatchingEventsParams& params = GetParam();
  223. // Make sure we have an event to get the id of
  224. const size_t eventCount = m_syncTrack->GetNumEvents();
  225. const size_t eventAID = eventCount ? m_syncTrack->GetEvent(params.m_inEventAIndex).HashForSyncing(params.m_mirrorInput) : 0;
  226. const size_t eventBID = eventCount ? m_syncTrack->GetEvent(params.m_inEventBIndex).HashForSyncing(params.m_mirrorInput) : 0;
  227. size_t outLeft, outRight;
  228. m_syncTrack->FindMatchingEvents(
  229. params.m_startingIndex,
  230. eventAID,
  231. eventBID,
  232. &outLeft,
  233. &outRight,
  234. params.m_forward,
  235. params.m_mirrorOutput
  236. );
  237. EXPECT_EQ(outLeft, params.m_expectedEventA);
  238. EXPECT_EQ(outRight, params.m_expectedEventB);
  239. }
  240. INSTANTIATE_TEST_SUITE_P(TestFindMatchingEvents, TestFindMatchingEventsFixture,
  241. ::testing::ValuesIn(std::vector<FindMatchingEventsParams> {
  242. // With no events, it shouldn't matter what we put in, we'll get
  243. // back invalid indices
  244. {
  245. MakeNoEvents,
  246. 0, // startingIndex
  247. 0, // inEventAIndex
  248. 1, // inEventBIndex
  249. InvalidIndex, // expectedEventA
  250. InvalidIndex, // expectedEventB
  251. false, // mirrorInput
  252. false, // mirrorOutput
  253. true // forward
  254. },
  255. // With just one event, we'll always get back indices (0,0)
  256. {
  257. MakeOneEvent,
  258. 0, // startingIndex
  259. 0, // inEventAIndex
  260. 0, // inEventBIndex
  261. 0, // expectedEventA
  262. 0, // expectedEventB
  263. false, // mirrorInput
  264. false, // mirrorOutput
  265. true // forward
  266. },
  267. // When forward is true
  268. // Look for L->R events. The L->R event pairs are (0,1) and (2,3)
  269. // (expectedEventA will be 0 or 2 and expectedEventB will be 1 or 3)
  270. {
  271. // Starting at event 0[L], looking for events L->R, should find events 0 and 1
  272. MakeTwoLeftRightEvents,
  273. 0, // startingIndex
  274. 0, // inEventAIndex
  275. 1, // inEventBIndex
  276. 0, // expectedEventA
  277. 1, // expectedEventB
  278. false, // mirrorInput
  279. false, // mirrorOutput
  280. true // forward
  281. },
  282. {
  283. // Starting at event 1[R], looking for events L->R, should find events 2 and 3
  284. MakeTwoLeftRightEvents,
  285. 1, // startingIndex
  286. 0, // inEventAIndex
  287. 1, // inEventBIndex
  288. 2, // expectedEventA
  289. 3, // expectedEventB
  290. false, // mirrorInput
  291. false, // mirrorOutput
  292. true // forward
  293. },
  294. {
  295. // Starting at event 2[L], looking for events L->R, should find events 2 and 3
  296. MakeTwoLeftRightEvents,
  297. 2, // startingIndex
  298. 0, // inEventAIndex
  299. 1, // inEventBIndex
  300. 2, // expectedEventA
  301. 3, // expectedEventB
  302. false, // mirrorInput
  303. false, // mirrorOutput
  304. true // forward
  305. },
  306. {
  307. // Starting at event 3[R], looking for events L->R, should find events 0 and 1
  308. MakeTwoLeftRightEvents,
  309. 3, // startingIndex
  310. 0, // inEventAIndex
  311. 1, // inEventBIndex
  312. 0, // expectedEventA
  313. 1, // expectedEventB
  314. false, // mirrorInput
  315. false, // mirrorOutput
  316. true // forward
  317. },
  318. // Look for R->L events. The R->R event pairs are (1,2) and (3,0)
  319. // (expectedEventA will be 1 or 3 and expectedEventB will be 2 or 0)
  320. {
  321. // Starting at event 0[L], looking for events R->L, should find events 1 and 2
  322. MakeTwoLeftRightEvents,
  323. 0, // startingIndex
  324. 1, // inEventAIndex
  325. 2, // inEventBIndex
  326. 1, // expectedEventA
  327. 2, // expectedEventB
  328. false, // mirrorInput
  329. false, // mirrorOutput
  330. true // forward
  331. },
  332. {
  333. // Starting at event 1[R], looking for events R->L, should find events 1 and 2
  334. MakeTwoLeftRightEvents,
  335. 1, // startingIndex
  336. 1, // inEventAIndex
  337. 2, // inEventBIndex
  338. 1, // expectedEventA
  339. 2, // expectedEventB
  340. false, // mirrorInput
  341. false, // mirrorOutput
  342. true // forward
  343. },
  344. {
  345. // Starting at event 2[L], looking for events R->L, should find events 3 and 0
  346. MakeTwoLeftRightEvents,
  347. 2, // startingIndex
  348. 1, // inEventAIndex
  349. 2, // inEventBIndex
  350. 3, // expectedEventA
  351. 0, // expectedEventB
  352. false, // mirrorInput
  353. false, // mirrorOutput
  354. true // forward
  355. },
  356. {
  357. // Starting at event 3[R], looking for events R->L, should find events 3 and 0
  358. MakeTwoLeftRightEvents,
  359. 3, // startingIndex
  360. 1, // inEventAIndex
  361. 2, // inEventBIndex
  362. 3, // expectedEventA
  363. 0, // expectedEventB
  364. false, // mirrorInput
  365. false, // mirrorOutput
  366. true // forward
  367. },
  368. // When forward is false
  369. // Look for L->R events. The L->R event pairs are (0,1) and (2,3)
  370. // (expectedEventA will be 0 or 2 and expectedEventB will be 1 or 3)
  371. {
  372. // Starting at event 0[L], looking for events L->R, going backward, should find events 2 and 3
  373. MakeTwoLeftRightEvents,
  374. 0, // startingIndex
  375. 0, // inEventAIndex
  376. 1, // inEventBIndex
  377. 2, // expectedEventA
  378. 3, // expectedEventB
  379. false, // mirrorInput
  380. false, // mirrorOutput
  381. false // forward
  382. },
  383. {
  384. // Starting at event 1[R], looking for events L->R, going backward, should find events 0 and 1
  385. MakeTwoLeftRightEvents,
  386. 1, // startingIndex
  387. 0, // inEventAIndex
  388. 1, // inEventBIndex
  389. 0, // expectedEventA
  390. 1, // expectedEventB
  391. false, // mirrorInput
  392. false, // mirrorOutput
  393. false // forward
  394. },
  395. {
  396. // Starting at event 2[L], looking for events L->R, going backward, should find events 0 and 1
  397. MakeTwoLeftRightEvents,
  398. 2, // startingIndex
  399. 0, // inEventAIndex
  400. 1, // inEventBIndex
  401. 0, // expectedEventA
  402. 1, // expectedEventB
  403. false, // mirrorInput
  404. false, // mirrorOutput
  405. false // forward
  406. },
  407. {
  408. // Starting at event 3[R], looking for events L->R, going backward, should find events 2 and 3
  409. MakeTwoLeftRightEvents,
  410. 3, // startingIndex
  411. 0, // inEventAIndex
  412. 1, // inEventBIndex
  413. 2, // expectedEventA
  414. 3, // expectedEventB
  415. false, // mirrorInput
  416. false, // mirrorOutput
  417. false // forward
  418. },
  419. // Look for R->L events. The R->R event pairs are (1,2) and (3,0)
  420. // (expectedEventA will be 1 or 3 and expectedEventB will be 2 or 0)
  421. {
  422. // Starting at event 0[L], looking for events R->L, going backward, should find events 3 and 0
  423. MakeTwoLeftRightEvents,
  424. 0, // startingIndex
  425. 1, // inEventAIndex
  426. 2, // inEventBIndex
  427. 3, // expectedEventA
  428. 0, // expectedEventB
  429. false, // mirrorInput
  430. false, // mirrorOutput
  431. false // forward
  432. },
  433. {
  434. // Starting at event 1[R], looking for events R->L, going backward, should find events 3 and 0
  435. MakeTwoLeftRightEvents,
  436. 1, // startingIndex
  437. 1, // inEventAIndex
  438. 2, // inEventBIndex
  439. 3, // expectedEventA
  440. 0, // expectedEventB
  441. false, // mirrorInput
  442. false, // mirrorOutput
  443. false // forward
  444. },
  445. {
  446. // Starting at event 2[L], looking for events R->L, going backward, should find events 1 and 2
  447. MakeTwoLeftRightEvents,
  448. 2, // startingIndex
  449. 1, // inEventAIndex
  450. 2, // inEventBIndex
  451. 1, // expectedEventA
  452. 2, // expectedEventB
  453. false, // mirrorInput
  454. false, // mirrorOutput
  455. false // forward
  456. },
  457. {
  458. // Starting at event 3[R], looking for events R->L, going backward, should find events 1 and 2
  459. MakeTwoLeftRightEvents,
  460. 3, // startingIndex
  461. 1, // inEventAIndex
  462. 2, // inEventBIndex
  463. 1, // expectedEventA
  464. 2, // expectedEventB
  465. false, // mirrorInput
  466. false, // mirrorOutput
  467. false // forward
  468. }
  469. })
  470. );
  471. } // end namespace EMotionFX