utMDLImporter_HL1_Nodes.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file utMDLImporter_HL1_Nodes.cpp
  35. * @brief Half-Life 1 MDL loader nodes tests.
  36. */
  37. #include "AbstractImportExportBase.h"
  38. #include "AssetLib/MDL/HalfLife/HL1ImportDefinitions.h"
  39. #include "MDLHL1TestFiles.h"
  40. #include "UnitTestPCH.h"
  41. #include <assimp/postprocess.h>
  42. #include <assimp/scene.h>
  43. #include <assimp/Importer.hpp>
  44. using namespace Assimp;
  45. class utMDLImporter_HL1_Nodes : public ::testing::Test {
  46. /**
  47. * @note Represents a flattened node hierarchy where each item is a pair
  48. * containing the node level and it's name.
  49. */
  50. using Hierarchy = std::vector<std::pair<unsigned int, std::string>>;
  51. /**
  52. * @note A vector of strings. Used for symplifying syntax.
  53. */
  54. using StringVector = std::vector<std::string>;
  55. public:
  56. /**
  57. * @note The following tests require a basic understanding
  58. * of the SMD format. For more information about SMD format,
  59. * please refer to the SMD importer or go to VDC
  60. * (Valve Developer Community).
  61. */
  62. // Given a model, verify that the bones nodes hierarchy is correctly formed.
  63. void checkBoneHierarchy() {
  64. Assimp::Importer importer;
  65. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MDL_HL1_MODELS_DIR "multiple_roots.mdl", aiProcess_ValidateDataStructure);
  66. ASSERT_NE(nullptr, scene);
  67. ASSERT_NE(nullptr, scene->mRootNode);
  68. // First, check that "<MDL_root>" and "<MDL_bones>" are linked.
  69. const aiNode* node_MDL_root = scene->mRootNode->FindNode(AI_MDL_HL1_NODE_ROOT);
  70. ASSERT_NE(nullptr, node_MDL_root);
  71. const aiNode *node_MDL_bones = scene->mRootNode->FindNode(AI_MDL_HL1_NODE_BONES);
  72. ASSERT_NE(nullptr, node_MDL_bones);
  73. ASSERT_NE(nullptr, node_MDL_bones->mParent);
  74. ASSERT_EQ(node_MDL_root, node_MDL_bones->mParent);
  75. // Second, verify "<MDL_bones>" hierarchy.
  76. const Hierarchy expected_hierarchy = {
  77. { 0, AI_MDL_HL1_NODE_BONES },
  78. { 1, "root1_bone1" },
  79. { 2, "root1_bone2" },
  80. { 3, "root1_bone4" },
  81. { 3, "root1_bone5" },
  82. { 2, "root1_bone3" },
  83. { 3, "root1_bone6" },
  84. { 1, "root2_bone1" },
  85. { 2, "root2_bone2" },
  86. { 2, "root2_bone3" },
  87. { 3, "root2_bone5" },
  88. { 2, "root2_bone4" },
  89. { 3, "root2_bone6" },
  90. { 1, "root3_bone1" },
  91. { 2, "root3_bone2" },
  92. { 2, "root3_bone3" },
  93. { 2, "root3_bone4" },
  94. { 3, "root3_bone5" },
  95. { 4, "root3_bone6" },
  96. { 4, "root3_bone7" },
  97. };
  98. Hierarchy actual_hierarchy;
  99. flatten_hierarchy(node_MDL_bones, actual_hierarchy);
  100. ASSERT_EQ(expected_hierarchy, actual_hierarchy);
  101. }
  102. /* Given a model with bones that have empty names,
  103. verify that all the bones of the imported model
  104. have unique and no empty names.
  105. "" <----+---- empty names
  106. "" <----+
  107. "" <----+
  108. "Bone_3" |
  109. "" <----+
  110. "Bone_2" |
  111. "Bone_5" |
  112. "" <----+
  113. "" <----+
  114. */
  115. void emptyBonesNames() {
  116. Assimp::Importer importer;
  117. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MDL_HL1_MODELS_DIR "unnamed_bones.mdl", aiProcess_ValidateDataStructure);
  118. ASSERT_NE(nullptr, scene);
  119. const StringVector expected_bones_names = {
  120. "Bone",
  121. "Bone_0",
  122. "Bone_1",
  123. "Bone_3",
  124. "Bone_4",
  125. "Bone_2",
  126. "Bone_5",
  127. "Bone_6",
  128. "Bone_7"
  129. };
  130. StringVector actual_bones_names;
  131. get_node_children_names(scene->mRootNode->FindNode(AI_MDL_HL1_NODE_BONES), actual_bones_names);
  132. ASSERT_EQ(expected_bones_names, actual_bones_names);
  133. }
  134. /* Given a model with bodyparts that have empty names,
  135. verify that the imported model contains bodyparts with
  136. unique and no empty names.
  137. $body "" <----+---- empty names
  138. $body "Bodypart_1" |
  139. $body "Bodypart_5" |
  140. $body "Bodypart_6" |
  141. $body "" <----+
  142. $body "Bodypart_2" |
  143. $body "" <----+
  144. $body "Bodypart_3" |
  145. $body "" <----+
  146. */
  147. void emptyBodypartsNames() {
  148. Assimp::Importer importer;
  149. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MDL_HL1_MODELS_DIR "unnamed_bodyparts.mdl", aiProcess_ValidateDataStructure);
  150. ASSERT_NE(nullptr, scene);
  151. const StringVector expected_bodyparts_names = {
  152. "Bodypart",
  153. "Bodypart_1",
  154. "Bodypart_5",
  155. "Bodypart_6",
  156. "Bodypart_0",
  157. "Bodypart_2",
  158. "Bodypart_4",
  159. "Bodypart_3",
  160. "Bodypart_7"
  161. };
  162. StringVector actual_bodyparts_names;
  163. // Get the bodyparts names "without" the submodels.
  164. get_node_children_names(scene->mRootNode->FindNode(AI_MDL_HL1_NODE_BODYPARTS), actual_bodyparts_names, 0);
  165. ASSERT_EQ(expected_bodyparts_names, actual_bodyparts_names);
  166. }
  167. /* Given a model with bodyparts that have duplicate names,
  168. verify that the imported model contains bodyparts with
  169. unique and no duplicate names.
  170. $body "Bodypart" <-----+
  171. $body "Bodypart_1" <--+ |
  172. $body "Bodypart_2" | |
  173. $body "Bodypart1" | |
  174. $body "Bodypart" ---|--+
  175. $body "Bodypart_1" ---+ |
  176. $body "Bodypart2" |
  177. $body "Bodypart" ------+
  178. $body "Bodypart_4"
  179. */
  180. void duplicateBodypartsNames() {
  181. Assimp::Importer importer;
  182. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MDL_HL1_MODELS_DIR "duplicate_bodyparts.mdl", aiProcess_ValidateDataStructure);
  183. ASSERT_NE(nullptr, scene);
  184. const StringVector expected_bodyparts_names = {
  185. "Bodypart",
  186. "Bodypart_1",
  187. "Bodypart_2",
  188. "Bodypart1",
  189. "Bodypart_0",
  190. "Bodypart_1_0",
  191. "Bodypart2",
  192. "Bodypart_3",
  193. "Bodypart_4"
  194. };
  195. StringVector actual_bodyparts_names;
  196. // Get the bodyparts names "without" the submodels.
  197. get_node_children_names(scene->mRootNode->FindNode(AI_MDL_HL1_NODE_BODYPARTS), actual_bodyparts_names, 0);
  198. ASSERT_EQ(expected_bodyparts_names, actual_bodyparts_names);
  199. }
  200. /* Given a model with several bodyparts that contains multiple
  201. sub models with the same file name, verify for each bodypart
  202. sub model of the imported model that they have a unique name.
  203. $bodygroup "first_bodypart"
  204. {
  205. studio "triangle" <------+ duplicate file names.
  206. studio "triangle" -------+
  207. } |
  208. |
  209. $bodygroup "second_bodypart" |
  210. { |
  211. studio "triangle" -------+ same as first bodypart, but with same file.
  212. studio "triangle" -------+
  213. }
  214. $bodygroup "last_bodypart"
  215. {
  216. studio "triangle2" <------+ duplicate names.
  217. studio "triangle2" -------+
  218. }
  219. */
  220. void duplicateSubModelsNames() {
  221. Assimp::Importer importer;
  222. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MDL_HL1_MODELS_DIR "duplicate_submodels.mdl", aiProcess_ValidateDataStructure);
  223. ASSERT_NE(nullptr, scene);
  224. const std::vector<StringVector> expected_bodypart_sub_models_names = {
  225. {
  226. "triangle",
  227. "triangle_0",
  228. },
  229. {
  230. "triangle_1",
  231. "triangle_2",
  232. },
  233. {
  234. "triangle2",
  235. "triangle2_0",
  236. }
  237. };
  238. const aiNode *bodyparts_node = scene->mRootNode->FindNode(AI_MDL_HL1_NODE_BODYPARTS);
  239. ASSERT_NE(nullptr, bodyparts_node);
  240. EXPECT_EQ(3u, bodyparts_node->mNumChildren);
  241. StringVector actual_submodels_names;
  242. for (unsigned int i = 0; i < bodyparts_node->mNumChildren; ++i)
  243. {
  244. actual_submodels_names.clear();
  245. get_node_children_names(bodyparts_node->mChildren[i], actual_submodels_names);
  246. ASSERT_EQ(expected_bodypart_sub_models_names[i], actual_submodels_names);
  247. }
  248. }
  249. /* Given a model with sequences that have duplicate names, verify
  250. that each sequence from the imported model has a unique
  251. name.
  252. $sequence "idle_1" <-------+
  253. $sequence "idle" <----+ |
  254. $sequence "idle_2" | |
  255. $sequence "idle" -----+ |
  256. $sequence "idle_0" | |
  257. $sequence "idle_1" -----|--+
  258. $sequence "idle_3" |
  259. $sequence "idle" -----+
  260. $sequence "idle_7"
  261. */
  262. void duplicateSequenceNames() {
  263. Assimp::Importer importer;
  264. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MDL_HL1_MODELS_DIR "duplicate_sequences.mdl", aiProcess_ValidateDataStructure);
  265. ASSERT_NE(nullptr, scene);
  266. const StringVector expected_sequence_names = {
  267. "idle_1",
  268. "idle",
  269. "idle_2",
  270. "idle_4",
  271. "idle_0",
  272. "idle_1_0",
  273. "idle_3",
  274. "idle_5",
  275. "idle_7"
  276. };
  277. StringVector actual_sequence_names;
  278. get_node_children_names(scene->mRootNode->FindNode(AI_MDL_HL1_NODE_SEQUENCE_INFOS), actual_sequence_names);
  279. ASSERT_EQ(expected_sequence_names, actual_sequence_names);
  280. }
  281. /* Given a model with sequences that have empty names, verify
  282. that each sequence from the imported model has a unique
  283. name.
  284. $sequence "" <----+---- empty names
  285. $sequence "Sequence_1" |
  286. $sequence "" <----+
  287. $sequence "Sequence_4" |
  288. $sequence "" <----+
  289. $sequence "Sequence_8" |
  290. $sequence "" <----+
  291. $sequence "Sequence_2" |
  292. $sequence "" <----+
  293. */
  294. void emptySequenceNames() {
  295. Assimp::Importer importer;
  296. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MDL_HL1_MODELS_DIR "unnamed_sequences.mdl", aiProcess_ValidateDataStructure);
  297. ASSERT_NE(nullptr, scene);
  298. const StringVector expected_sequence_names = {
  299. "Sequence",
  300. "Sequence_1",
  301. "Sequence_0",
  302. "Sequence_4",
  303. "Sequence_3",
  304. "Sequence_8",
  305. "Sequence_5",
  306. "Sequence_2",
  307. "Sequence_6"
  308. };
  309. StringVector actual_sequence_names;
  310. get_node_children_names(scene->mRootNode->FindNode(AI_MDL_HL1_NODE_SEQUENCE_INFOS), actual_sequence_names);
  311. ASSERT_EQ(expected_sequence_names, actual_sequence_names);
  312. }
  313. /* Given a model with sequence groups that have duplicate names,
  314. verify that each sequence group from the imported model has
  315. a unique name.
  316. "default"
  317. $sequencegroup "SequenceGroup" <----+
  318. $sequencegroup "SequenceGroup_1" |
  319. $sequencegroup "SequenceGroup_5" <----|--+
  320. $sequencegroup "SequenceGroup" -----+ |
  321. $sequencegroup "SequenceGroup_0" | |
  322. $sequencegroup "SequenceGroup" -----+ |
  323. $sequencegroup "SequenceGroup_5" --------+
  324. $sequencegroup "SequenceGroup_6"
  325. $sequencegroup "SequenceGroup_2"
  326. */
  327. void duplicateSequenceGroupNames() {
  328. Assimp::Importer importer;
  329. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MDL_HL1_MODELS_DIR "duplicate_sequence_groups/duplicate_sequence_groups.mdl", aiProcess_ValidateDataStructure);
  330. ASSERT_NE(nullptr, scene);
  331. const StringVector expected_sequence_names = {
  332. "default",
  333. "SequenceGroup",
  334. "SequenceGroup_1",
  335. "SequenceGroup_5",
  336. "SequenceGroup_3",
  337. "SequenceGroup_0",
  338. "SequenceGroup_4",
  339. "SequenceGroup_5_0",
  340. "SequenceGroup_6",
  341. "SequenceGroup_2"
  342. };
  343. StringVector actual_sequence_names;
  344. get_node_children_names(scene->mRootNode->FindNode(AI_MDL_HL1_NODE_SEQUENCE_GROUPS), actual_sequence_names);
  345. ASSERT_EQ(expected_sequence_names, actual_sequence_names);
  346. }
  347. /* Given a model with sequence groups that have empty names,
  348. verify that each sequence group from the imported model has
  349. a unique name.
  350. "default"
  351. $sequencegroup "" <----+---- empty names
  352. $sequencegroup "SequenceGroup_2" |
  353. $sequencegroup "SequenceGroup_6" |
  354. $sequencegroup "" <----+
  355. $sequencegroup "" <----+
  356. $sequencegroup "SequenceGroup_1" |
  357. $sequencegroup "SequenceGroup_5" |
  358. $sequencegroup "" <----+
  359. $sequencegroup "SequenceGroup_4"
  360. */
  361. void emptySequenceGroupNames() {
  362. Assimp::Importer importer;
  363. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MDL_HL1_MODELS_DIR "unnamed_sequence_groups/unnamed_sequence_groups.mdl", aiProcess_ValidateDataStructure);
  364. ASSERT_NE(nullptr, scene);
  365. const StringVector expected_sequence_names = {
  366. "default",
  367. "SequenceGroup",
  368. "SequenceGroup_2",
  369. "SequenceGroup_6",
  370. "SequenceGroup_0",
  371. "SequenceGroup_3",
  372. "SequenceGroup_1",
  373. "SequenceGroup_5",
  374. "SequenceGroup_7",
  375. "SequenceGroup_4"
  376. };
  377. StringVector actual_sequence_names;
  378. get_node_children_names(scene->mRootNode->FindNode(AI_MDL_HL1_NODE_SEQUENCE_GROUPS), actual_sequence_names);
  379. ASSERT_EQ(expected_sequence_names, actual_sequence_names);
  380. }
  381. /* Verify that mOffsetMatrix applies the correct
  382. inverse bind pose transform. */
  383. void offsetMatrixUnappliesTransformations() {
  384. const float TOLERANCE = 0.01f;
  385. Assimp::Importer importer;
  386. const aiScene *scene = importer.ReadFile(MDL_HL1_FILE_MAN, aiProcess_ValidateDataStructure);
  387. ASSERT_NE(nullptr, scene);
  388. aiNode *scene_bones_node = scene->mRootNode->FindNode(AI_MDL_HL1_NODE_BONES);
  389. const aiMatrix4x4 identity_matrix;
  390. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  391. aiMesh *scene_mesh = scene->mMeshes[i];
  392. for (unsigned int j = 0; j < scene_mesh->mNumBones; ++j) {
  393. aiBone *scene_mesh_bone = scene_mesh->mBones[j];
  394. // Store local node transforms.
  395. aiNode *n = scene_bones_node->FindNode(scene_mesh_bone->mName);
  396. std::vector<aiMatrix4x4> bone_matrices = { n->mTransformation };
  397. while (n->mParent != scene->mRootNode) {
  398. n = n->mParent;
  399. bone_matrices.push_back(n->mTransformation);
  400. }
  401. // Compute absolute node transform.
  402. aiMatrix4x4 transform;
  403. for (auto it = bone_matrices.rbegin(); it != bone_matrices.rend(); ++it)
  404. transform *= *it;
  405. // Unapply the transformation using the offset matrix.
  406. aiMatrix4x4 unapplied_transform = scene_mesh_bone->mOffsetMatrix * transform;
  407. // Ensure that we have, approximately, the identity matrix.
  408. expect_equal_matrices(identity_matrix, unapplied_transform, TOLERANCE);
  409. }
  410. }
  411. }
  412. private:
  413. void expect_equal_matrices(const aiMatrix4x4 &expected, const aiMatrix4x4 &actual, float abs_error) {
  414. for (int i = 0; i < 4; ++i) {
  415. for (int j = 0; j < 4; ++j)
  416. EXPECT_NEAR(expected[i][j], actual[i][j], abs_error);
  417. }
  418. }
  419. /** Get a flattened representation of a node's hierarchy.
  420. * \param[in] node The node.
  421. * \param[out] hierarchy The flattened node's hierarchy.
  422. */
  423. void flatten_hierarchy(const aiNode *node, Hierarchy &hierarchy)
  424. {
  425. flatten_hierarchy_impl(node, hierarchy, 0);
  426. }
  427. void flatten_hierarchy_impl(const aiNode *node, Hierarchy &hierarchy, unsigned int level)
  428. {
  429. hierarchy.push_back({ level, node->mName.C_Str() });
  430. for (size_t i = 0; i < node->mNumChildren; ++i)
  431. {
  432. flatten_hierarchy_impl(node->mChildren[i], hierarchy, level + 1);
  433. }
  434. }
  435. /** Get all node's children names beneath max_level.
  436. * \param[in] node The parent node from which to get all children names.
  437. * \param[out] names The list of children names.
  438. * \param[in] max_level If set to -1, all children names will be collected.
  439. */
  440. void get_node_children_names(const aiNode *node, StringVector &names, const int max_level = -1)
  441. {
  442. get_node_children_names_impl(node, names, 0, max_level);
  443. }
  444. void get_node_children_names_impl(const aiNode *node, StringVector &names, int level, const int max_level = -1)
  445. {
  446. for (size_t i = 0; i < node->mNumChildren; ++i)
  447. {
  448. names.push_back(node->mChildren[i]->mName.C_Str());
  449. if (max_level == -1 || level < max_level)
  450. {
  451. get_node_children_names_impl(node->mChildren[i], names, level + 1, max_level);
  452. }
  453. }
  454. }
  455. };
  456. TEST_F(utMDLImporter_HL1_Nodes, checkBoneHierarchy) {
  457. checkBoneHierarchy();
  458. }
  459. TEST_F(utMDLImporter_HL1_Nodes, emptyBonesNames) {
  460. emptyBonesNames();
  461. }
  462. TEST_F(utMDLImporter_HL1_Nodes, emptyBodypartsNames) {
  463. emptyBodypartsNames();
  464. }
  465. TEST_F(utMDLImporter_HL1_Nodes, duplicateBodypartsNames) {
  466. duplicateBodypartsNames();
  467. }
  468. TEST_F(utMDLImporter_HL1_Nodes, duplicateSubModelsNames) {
  469. duplicateSubModelsNames();
  470. }
  471. TEST_F(utMDLImporter_HL1_Nodes, emptySequenceNames) {
  472. emptySequenceNames();
  473. }
  474. TEST_F(utMDLImporter_HL1_Nodes, duplicateSequenceNames) {
  475. duplicateSequenceNames();
  476. }
  477. TEST_F(utMDLImporter_HL1_Nodes, emptySequenceGroupNames) {
  478. emptySequenceGroupNames();
  479. }
  480. TEST_F(utMDLImporter_HL1_Nodes, duplicateSequenceGroupNames) {
  481. duplicateSequenceGroupNames();
  482. }
  483. TEST_F(utMDLImporter_HL1_Nodes, offsetMatrixUnappliesTransformations) {
  484. offsetMatrixUnappliesTransformations();
  485. }