Info.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, 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 Info.cpp
  35. * @brief Implementation of the 'assimp info' utility */
  36. #include "Main.h"
  37. #include <cstdio>
  38. #include <iostream>
  39. #include <string>
  40. constexpr char AICMD_MSG_INFO_HELP_E[] =
  41. "assimp info <file> [-r] [-v]\n"
  42. "\tPrint basic structure of a 3D model\n"
  43. "\t-r,--raw: No postprocessing, do a raw import\n"
  44. "\t-v,--verbose: Print verbose info such as node transform data\n"
  45. "\t-s, --silent: Print only minimal info\n";
  46. // note: by default this is using utf-8 text.
  47. // this is well supported on pretty much any linux terminal.
  48. // if this causes problems on some platform,
  49. // put an #ifdef to use the ascii version for that platform.
  50. #ifdef _WIN32
  51. constexpr char TREE_BRANCH_ASCII[] = "|-";
  52. constexpr char TREE_STOP_ASCII[] = "'-";
  53. constexpr char TREE_CONTINUE_ASCII[] = "| ";
  54. const char *TREE_BRANCH = TREE_BRANCH_ASCII;
  55. const char *TREE_STOP = TREE_STOP_ASCII;
  56. const char *TREE_CONTINUE = TREE_CONTINUE_ASCII;
  57. #else // _WIN32
  58. constexpr char TREE_BRANCH_UTF8[] = "\xe2\x94\x9c\xe2\x95\xb4";
  59. constexpr char TREE_STOP_UTF8[] = "\xe2\x94\x94\xe2\x95\xb4";
  60. constexpr char TREE_CONTINUE_UTF8[] = "\xe2\x94\x82 ";
  61. const char *TREE_BRANCH = TREE_BRANCH_UTF8;
  62. const char *TREE_STOP = TREE_STOP_UTF8;
  63. const char *TREE_CONTINUE = TREE_CONTINUE_UTF8;
  64. #endif // _WIN32
  65. // -----------------------------------------------------------------------------------
  66. unsigned int CountNodes(const aiNode *root) {
  67. unsigned int i = 0;
  68. for (unsigned int a = 0; a < root->mNumChildren; ++a) {
  69. i += CountNodes(root->mChildren[a]);
  70. }
  71. return 1 + i;
  72. }
  73. // -----------------------------------------------------------------------------------
  74. unsigned int GetMaxDepth(const aiNode *root) {
  75. unsigned int cnt = 0;
  76. for (unsigned int i = 0; i < root->mNumChildren; ++i) {
  77. cnt = std::max(cnt, GetMaxDepth(root->mChildren[i]));
  78. }
  79. return cnt + 1;
  80. }
  81. // -----------------------------------------------------------------------------------
  82. unsigned int CountVertices(const aiScene *scene) {
  83. unsigned int cnt = 0;
  84. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  85. cnt += scene->mMeshes[i]->mNumVertices;
  86. }
  87. return cnt;
  88. }
  89. // -----------------------------------------------------------------------------------
  90. unsigned int CountFaces(const aiScene *scene) {
  91. unsigned int cnt = 0;
  92. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  93. cnt += scene->mMeshes[i]->mNumFaces;
  94. }
  95. return cnt;
  96. }
  97. // -----------------------------------------------------------------------------------
  98. unsigned int CountBones(const aiScene *scene) {
  99. unsigned int cnt = 0;
  100. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  101. cnt += scene->mMeshes[i]->mNumBones;
  102. }
  103. return cnt;
  104. }
  105. // -----------------------------------------------------------------------------------
  106. unsigned int CountAnimChannels(const aiScene *scene) {
  107. unsigned int cnt = 0;
  108. for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
  109. cnt += scene->mAnimations[i]->mNumChannels;
  110. }
  111. return cnt;
  112. }
  113. // -----------------------------------------------------------------------------------
  114. unsigned int GetAvgFacePerMesh(const aiScene *scene) {
  115. return (scene->mNumMeshes != 0) ? static_cast<unsigned int>(CountFaces(scene) / scene->mNumMeshes) : 0;
  116. }
  117. // -----------------------------------------------------------------------------------
  118. unsigned int GetAvgVertsPerMesh(const aiScene *scene) {
  119. return (scene->mNumMeshes != 0) ? static_cast<unsigned int>(CountVertices(scene) / scene->mNumMeshes) : 0;
  120. }
  121. // -----------------------------------------------------------------------------------
  122. void FindSpecialPoints(const aiScene *scene, const aiNode *root, aiVector3D special_points[3], const aiMatrix4x4 &mat = aiMatrix4x4()) {
  123. // XXX that could be greatly simplified by using code from code/ProcessHelper.h
  124. // XXX I just don't want to include it here.
  125. const aiMatrix4x4 trafo = root->mTransformation * mat;
  126. for (unsigned int i = 0; i < root->mNumMeshes; ++i) {
  127. const aiMesh *mesh = scene->mMeshes[root->mMeshes[i]];
  128. for (unsigned int a = 0; a < mesh->mNumVertices; ++a) {
  129. aiVector3D v = trafo * mesh->mVertices[a];
  130. special_points[0].x = std::min(special_points[0].x, v.x);
  131. special_points[0].y = std::min(special_points[0].y, v.y);
  132. special_points[0].z = std::min(special_points[0].z, v.z);
  133. special_points[1].x = std::max(special_points[1].x, v.x);
  134. special_points[1].y = std::max(special_points[1].y, v.y);
  135. special_points[1].z = std::max(special_points[1].z, v.z);
  136. }
  137. }
  138. for (unsigned int i = 0; i < root->mNumChildren; ++i) {
  139. FindSpecialPoints(scene, root->mChildren[i], special_points, trafo);
  140. }
  141. }
  142. // -----------------------------------------------------------------------------------
  143. void FindSpecialPoints(const aiScene *scene, aiVector3D special_points[3]) {
  144. special_points[0] = aiVector3D(1e10, 1e10, 1e10);
  145. special_points[1] = aiVector3D(-1e10, -1e10, -1e10);
  146. FindSpecialPoints(scene, scene->mRootNode, special_points);
  147. special_points[2] = (special_points[0] + special_points[1]) * (ai_real)0.5;
  148. }
  149. // -----------------------------------------------------------------------------------
  150. std::string FindPTypes(const aiScene *scene) {
  151. bool haveit[4] = { false };
  152. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  153. const unsigned int pt = scene->mMeshes[i]->mPrimitiveTypes;
  154. if (pt & aiPrimitiveType_POINT) {
  155. haveit[0] = true;
  156. }
  157. if (pt & aiPrimitiveType_LINE) {
  158. haveit[1] = true;
  159. }
  160. if (pt & aiPrimitiveType_TRIANGLE) {
  161. haveit[2] = true;
  162. }
  163. if (pt & aiPrimitiveType_POLYGON) {
  164. haveit[3] = true;
  165. }
  166. }
  167. return (haveit[0] ? std::string("points") : "") + (haveit[1] ? "lines" : "") +
  168. (haveit[2] ? "triangles" : "") + (haveit[3] ? "n-polygons" : "");
  169. }
  170. // -----------------------------------------------------------------------------------
  171. // Prettily print the node graph to stdout
  172. void PrintHierarchy(
  173. const aiNode *node,
  174. const std::string &indent,
  175. bool verbose,
  176. bool last = false,
  177. bool first = true) {
  178. // tree visualization
  179. std::string branchchar;
  180. if (first) {
  181. branchchar = "";
  182. } else if (last) {
  183. branchchar = TREE_STOP;
  184. } // "'-"
  185. else {
  186. branchchar = TREE_BRANCH;
  187. } // "|-"
  188. // print the indent and the branch character and the name
  189. std::cout << indent << branchchar << node->mName.C_Str();
  190. // if there are meshes attached, indicate this
  191. if (node->mNumMeshes) {
  192. std::cout << " (mesh ";
  193. bool sep = false;
  194. for (size_t i = 0; i < node->mNumMeshes; ++i) {
  195. unsigned int mesh_index = node->mMeshes[i];
  196. if (sep) {
  197. std::cout << ", ";
  198. }
  199. std::cout << mesh_index;
  200. sep = true;
  201. }
  202. std::cout << ")";
  203. }
  204. // finish the line
  205. std::cout << std::endl;
  206. // in verbose mode, print the transform data as well
  207. if (verbose) {
  208. // indent to use
  209. std::string indentadd;
  210. if (last) {
  211. indentadd += " ";
  212. } else {
  213. indentadd += TREE_CONTINUE;
  214. } // "| "..
  215. if (node->mNumChildren == 0) {
  216. indentadd += " ";
  217. } else {
  218. indentadd += TREE_CONTINUE;
  219. } // .."| "
  220. aiVector3D s, r, t;
  221. node->mTransformation.Decompose(s, r, t);
  222. if (s.x != 1.0 || s.y != 1.0 || s.z != 1.0) {
  223. std::cout << indent << indentadd;
  224. printf(" S:[%f %f %f]\n", s.x, s.y, s.z);
  225. }
  226. if (r.x || r.y || r.z) {
  227. std::cout << indent << indentadd;
  228. printf(" R:[%f %f %f]\n", r.x, r.y, r.z);
  229. }
  230. if (t.x || t.y || t.z) {
  231. std::cout << indent << indentadd;
  232. printf(" T:[%f %f %f]\n", t.x, t.y, t.z);
  233. }
  234. }
  235. // and recurse
  236. std::string nextIndent;
  237. if (first) {
  238. nextIndent = indent;
  239. } else if (last) {
  240. nextIndent = indent + " ";
  241. } else {
  242. nextIndent = indent + TREE_CONTINUE;
  243. } // "| "
  244. for (size_t i = 0; i < node->mNumChildren; ++i) {
  245. bool lastone = (i == node->mNumChildren - 1);
  246. PrintHierarchy(
  247. node->mChildren[i],
  248. nextIndent,
  249. verbose,
  250. lastone,
  251. false);
  252. }
  253. }
  254. // -----------------------------------------------------------------------------------
  255. // Implementation of the assimp info utility to print basic file info
  256. int Assimp_Info(const char *const *params, unsigned int num) {
  257. // asssimp info <file> [-r]
  258. if (num < 1) {
  259. printf("assimp info: Invalid number of arguments. "
  260. "See \'assimp info --help\'\n");
  261. return AssimpCmdError::InvalidNumberOfArguments;
  262. }
  263. // --help
  264. if (!strcmp(params[0], "-h") || !strcmp(params[0], "--help") || !strcmp(params[0], "-?")) {
  265. printf("%s", AICMD_MSG_INFO_HELP_E);
  266. return AssimpCmdError::Success;
  267. }
  268. const std::string in = std::string(params[0]);
  269. // get -r and -v arguments
  270. bool raw = false;
  271. bool verbose = false;
  272. bool silent = false;
  273. for (unsigned int i = 1; i < num; ++i) {
  274. if (!strcmp(params[i], "--raw") || !strcmp(params[i], "-r")) {
  275. raw = true;
  276. }
  277. if (!strcmp(params[i], "--verbose") || !strcmp(params[i], "-v")) {
  278. verbose = true;
  279. }
  280. if (!strcmp(params[i], "--silent") || !strcmp(params[i], "-s")) {
  281. silent = true;
  282. }
  283. }
  284. // Verbose and silent at the same time are not allowed
  285. if (verbose && silent) {
  286. printf("assimp info: Invalid arguments, verbose and silent at the same time are forbidden. ");
  287. return AssimpCmdInfoError::InvalidCombinaisonOfArguments;
  288. }
  289. // Parse post-processing flags unless -r was specified
  290. ImportData import;
  291. if (!raw) {
  292. // get import flags
  293. ProcessStandardArguments(import, params + 1, num - 1);
  294. //No custom post process flags defined, we set all the post process flags active
  295. if (import.ppFlags == 0)
  296. import.ppFlags |= aiProcessPreset_TargetRealtime_MaxQuality;
  297. }
  298. // import the main model
  299. const aiScene *scene = ImportModel(import, in);
  300. if (!scene) {
  301. printf("assimp info: Unable to load input file %s\n",
  302. in.c_str());
  303. return AssimpCmdError::FailedToLoadInputFile;
  304. }
  305. aiMemoryInfo mem;
  306. globalImporter->GetMemoryRequirements(mem);
  307. static const char *format_string =
  308. "Memory consumption: %i B\n"
  309. "Nodes: %i\n"
  310. "Maximum depth %i\n"
  311. "Meshes: %i\n"
  312. "Animations: %i\n"
  313. "Textures (embed.): %i\n"
  314. "Materials: %i\n"
  315. "Cameras: %i\n"
  316. "Lights: %i\n"
  317. "Vertices: %i\n"
  318. "Faces: %i\n"
  319. "Bones: %i\n"
  320. "Animation Channels: %i\n"
  321. "Primitive Types: %s\n"
  322. "Average faces/mesh %i\n"
  323. "Average verts/mesh %i\n"
  324. "Minimum point (%f %f %f)\n"
  325. "Maximum point (%f %f %f)\n"
  326. "Center point (%f %f %f)\n"
  327. ;
  328. aiVector3D special_points[3];
  329. FindSpecialPoints(scene, special_points);
  330. printf(format_string,
  331. mem.total,
  332. CountNodes(scene->mRootNode),
  333. GetMaxDepth(scene->mRootNode),
  334. scene->mNumMeshes,
  335. scene->mNumAnimations,
  336. scene->mNumTextures,
  337. scene->mNumMaterials,
  338. scene->mNumCameras,
  339. scene->mNumLights,
  340. CountVertices(scene),
  341. CountFaces(scene),
  342. CountBones(scene),
  343. CountAnimChannels(scene),
  344. FindPTypes(scene).c_str(),
  345. GetAvgFacePerMesh(scene),
  346. GetAvgVertsPerMesh(scene),
  347. special_points[0][0], special_points[0][1], special_points[0][2],
  348. special_points[1][0], special_points[1][1], special_points[1][2],
  349. special_points[2][0], special_points[2][1], special_points[2][2]);
  350. if (silent) {
  351. printf("\n");
  352. return AssimpCmdError::Success;
  353. }
  354. // meshes
  355. if (scene->mNumMeshes) {
  356. printf("\nMeshes: (name) [vertices / bones / faces | primitive_types]\n");
  357. }
  358. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  359. const aiMesh *mesh = scene->mMeshes[i];
  360. printf(" %d (%s)", i, mesh->mName.C_Str());
  361. printf(
  362. ": [%d / %d / %d |",
  363. mesh->mNumVertices,
  364. mesh->mNumBones,
  365. mesh->mNumFaces);
  366. const unsigned int ptypes = mesh->mPrimitiveTypes;
  367. if (ptypes & aiPrimitiveType_POINT) {
  368. printf(" point");
  369. }
  370. if (ptypes & aiPrimitiveType_LINE) {
  371. printf(" line");
  372. }
  373. if (ptypes & aiPrimitiveType_TRIANGLE) {
  374. printf(" triangle");
  375. }
  376. if (ptypes & aiPrimitiveType_POLYGON) {
  377. printf(" polygon");
  378. }
  379. printf("]\n");
  380. }
  381. // materials
  382. if (scene->mNumMaterials)
  383. printf("\nNamed Materials:");
  384. for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
  385. const aiMaterial *mat = scene->mMaterials[i];
  386. aiString name = mat->GetName();
  387. printf("\n \'%s\'", name.data);
  388. if (mat->mNumProperties)
  389. printf(" (prop) [index / bytes | texture semantic]");
  390. for (unsigned p = 0; p < mat->mNumProperties; p++) {
  391. const aiMaterialProperty *prop = mat->mProperties[p];
  392. const aiTextureType textype = static_cast<aiTextureType>(prop->mSemantic);
  393. printf("\n %d (%s): [%d / %d | %s]",
  394. p,
  395. prop->mKey.data,
  396. prop->mIndex,
  397. prop->mDataLength,
  398. aiTextureTypeToString(textype));
  399. }
  400. }
  401. if (scene->mNumMaterials) {
  402. printf("\n");
  403. }
  404. // textures
  405. unsigned int total = 0;
  406. for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
  407. aiString name;
  408. static const aiTextureType types[] = {
  409. aiTextureType_NONE,
  410. aiTextureType_DIFFUSE,
  411. aiTextureType_SPECULAR,
  412. aiTextureType_AMBIENT,
  413. aiTextureType_EMISSIVE,
  414. aiTextureType_HEIGHT,
  415. aiTextureType_NORMALS,
  416. aiTextureType_SHININESS,
  417. aiTextureType_OPACITY,
  418. aiTextureType_DISPLACEMENT,
  419. aiTextureType_LIGHTMAP,
  420. aiTextureType_REFLECTION,
  421. aiTextureType_BASE_COLOR,
  422. aiTextureType_NORMAL_CAMERA,
  423. aiTextureType_EMISSION_COLOR,
  424. aiTextureType_METALNESS,
  425. aiTextureType_DIFFUSE_ROUGHNESS,
  426. aiTextureType_AMBIENT_OCCLUSION,
  427. aiTextureType_UNKNOWN
  428. };
  429. for (unsigned int type = 0; type < sizeof(types) / sizeof(types[0]); ++type) {
  430. for (unsigned int idx = 0; AI_SUCCESS == aiGetMaterialString(scene->mMaterials[i],
  431. AI_MATKEY_TEXTURE(types[type], idx), &name);
  432. ++idx) {
  433. printf("%s\n \'%s\'", (total++ ? "" : "\nTexture Refs:"), name.data);
  434. }
  435. }
  436. }
  437. if (total) {
  438. printf("\n");
  439. }
  440. // animations
  441. total = 0;
  442. for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
  443. if (scene->mAnimations[i]->mName.length) {
  444. printf("%s\n \'%s\'", (total++ ? "" : "\nNamed Animations:"), scene->mAnimations[i]->mName.data);
  445. }
  446. }
  447. if (total) {
  448. printf("\n");
  449. }
  450. // node hierarchy
  451. printf("\nNode hierarchy:\n");
  452. PrintHierarchy(scene->mRootNode, "", verbose);
  453. printf("\n");
  454. return AssimpCmdError::Success;
  455. }