Info.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2018, 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. const 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. const std::string TREE_BRANCH_ASCII = "|-";
  46. const std::string TREE_BRANCH_UTF8 = "\xe2\x94\x9c\xe2\x95\xb4";
  47. const std::string TREE_STOP_ASCII = "'-";
  48. const std::string TREE_STOP_UTF8 = "\xe2\x94\x94\xe2\x95\xb4";
  49. const std::string TREE_CONTINUE_ASCII = "| ";
  50. const std::string TREE_CONTINUE_UTF8 = "\xe2\x94\x82 ";
  51. // note: by default this is outputing utf-8 text.
  52. // this is well supported on pretty much any linux terminal.
  53. // if this causes problems on some platform,
  54. // put an #ifdef to use the ascii version for that platform.
  55. const std::string TREE_BRANCH = TREE_BRANCH_UTF8;
  56. const std::string TREE_STOP = TREE_STOP_UTF8;
  57. const std::string TREE_CONTINUE = TREE_CONTINUE_UTF8;
  58. // -----------------------------------------------------------------------------------
  59. unsigned int CountNodes(const aiNode* root)
  60. {
  61. unsigned int i = 0;
  62. for (unsigned int a = 0; a < root->mNumChildren; ++a ) {
  63. i += CountNodes(root->mChildren[a]);
  64. }
  65. return 1+i;
  66. }
  67. // -----------------------------------------------------------------------------------
  68. unsigned int GetMaxDepth(const aiNode* root)
  69. {
  70. unsigned int cnt = 0;
  71. for (unsigned int i = 0; i < root->mNumChildren; ++i ) {
  72. cnt = std::max(cnt,GetMaxDepth(root->mChildren[i]));
  73. }
  74. return cnt+1;
  75. }
  76. // -----------------------------------------------------------------------------------
  77. unsigned int CountVertices(const aiScene* scene)
  78. {
  79. unsigned int cnt = 0;
  80. for(unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  81. cnt += scene->mMeshes[i]->mNumVertices;
  82. }
  83. return cnt;
  84. }
  85. // -----------------------------------------------------------------------------------
  86. unsigned int CountFaces(const aiScene* scene)
  87. {
  88. unsigned int cnt = 0;
  89. for(unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  90. cnt += scene->mMeshes[i]->mNumFaces;
  91. }
  92. return cnt;
  93. }
  94. // -----------------------------------------------------------------------------------
  95. unsigned int CountBones(const aiScene* scene)
  96. {
  97. unsigned int cnt = 0;
  98. for(unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  99. cnt += scene->mMeshes[i]->mNumBones;
  100. }
  101. return cnt;
  102. }
  103. // -----------------------------------------------------------------------------------
  104. unsigned int CountAnimChannels(const aiScene* scene)
  105. {
  106. unsigned int cnt = 0;
  107. for(unsigned int i = 0; i < scene->mNumAnimations; ++i) {
  108. cnt += scene->mAnimations[i]->mNumChannels;
  109. }
  110. return cnt;
  111. }
  112. // -----------------------------------------------------------------------------------
  113. unsigned int GetAvgFacePerMesh(const aiScene* scene) {
  114. return (scene->mNumMeshes != 0) ? static_cast<unsigned int>(CountFaces(scene)/scene->mNumMeshes) : 0;
  115. }
  116. // -----------------------------------------------------------------------------------
  117. unsigned int GetAvgVertsPerMesh(const aiScene* scene) {
  118. return (scene->mNumMeshes != 0) ? static_cast<unsigned int>(CountVertices(scene)/scene->mNumMeshes) : 0;
  119. }
  120. // -----------------------------------------------------------------------------------
  121. void FindSpecialPoints(const aiScene* scene,const aiNode* root,aiVector3D special_points[3],const aiMatrix4x4& mat=aiMatrix4x4())
  122. {
  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. {
  145. special_points[0] = aiVector3D(1e10,1e10,1e10);
  146. special_points[1] = aiVector3D(-1e10,-1e10,-1e10);
  147. FindSpecialPoints(scene,scene->mRootNode,special_points);
  148. special_points[2] = (special_points[0]+special_points[1])*(ai_real)0.5;
  149. }
  150. // -----------------------------------------------------------------------------------
  151. std::string FindPTypes(const aiScene* scene)
  152. {
  153. bool haveit[4] = {0};
  154. for(unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  155. const unsigned int pt = scene->mMeshes[i]->mPrimitiveTypes;
  156. if (pt & aiPrimitiveType_POINT) {
  157. haveit[0]=true;
  158. }
  159. if (pt & aiPrimitiveType_LINE) {
  160. haveit[1]=true;
  161. }
  162. if (pt & aiPrimitiveType_TRIANGLE) {
  163. haveit[2]=true;
  164. }
  165. if (pt & aiPrimitiveType_POLYGON) {
  166. haveit[3]=true;
  167. }
  168. }
  169. return (haveit[0]?std::string("points"):"")+(haveit[1]?"lines":"")+
  170. (haveit[2]?"triangles":"")+(haveit[3]?"n-polygons":"");
  171. }
  172. // -----------------------------------------------------------------------------------
  173. // Prettily print the node graph to stdout
  174. void PrintHierarchy(
  175. const aiNode* node,
  176. const std::string &indent,
  177. bool verbose,
  178. bool last = false,
  179. bool first = true
  180. ){
  181. // tree visualization
  182. std::string branchchar;
  183. if (first) { branchchar = ""; }
  184. else if (last) { branchchar = TREE_STOP; } // "'-"
  185. else { branchchar = TREE_BRANCH; } // "|-"
  186. // print the indent and the branch character and the name
  187. std::cout << indent << branchchar << node->mName.C_Str();
  188. // if there are meshes attached, indicate this
  189. if (node->mNumMeshes) {
  190. std::cout << " (mesh ";
  191. bool sep = false;
  192. for (size_t i=0; i < node->mNumMeshes; ++i) {
  193. unsigned int mesh_index = node->mMeshes[i];
  194. if (sep) { std::cout << ", "; }
  195. std::cout << mesh_index;
  196. sep = true;
  197. }
  198. std::cout << ")";
  199. }
  200. // finish the line
  201. std::cout << std::endl;
  202. // in verbose mode, print the transform data as well
  203. if (verbose) {
  204. // indent to use
  205. std::string indentadd;
  206. if (last) { indentadd += " "; }
  207. else { indentadd += TREE_CONTINUE; } // "| "..
  208. if (node->mNumChildren == 0) { indentadd += " "; }
  209. else { indentadd += TREE_CONTINUE; } // .."| "
  210. aiVector3D s, r, t;
  211. node->mTransformation.Decompose(s, r, t);
  212. if (s.x != 1.0 || s.y != 1.0 || s.z != 1.0) {
  213. std::cout << indent << indentadd;
  214. printf(" S:[%f %f %f]\n", s.x, s.y, s.z);
  215. }
  216. if (r.x || r.y || r.z) {
  217. std::cout << indent << indentadd;
  218. printf(" R:[%f %f %f]\n", r.x, r.y, r.z);
  219. }
  220. if (t.x || t.y || t.z) {
  221. std::cout << indent << indentadd;
  222. printf(" T:[%f %f %f]\n", t.x, t.y, t.z);
  223. }
  224. }
  225. // and recurse
  226. std::string nextIndent;
  227. if (first) { nextIndent = indent; }
  228. else if (last) { nextIndent = indent + " "; }
  229. else { nextIndent = indent + TREE_CONTINUE; } // "| "
  230. for (size_t i = 0; i < node->mNumChildren; ++i) {
  231. bool lastone = (i == node->mNumChildren - 1);
  232. PrintHierarchy(
  233. node->mChildren[i],
  234. nextIndent,
  235. verbose,
  236. lastone,
  237. false
  238. );
  239. }
  240. }
  241. // -----------------------------------------------------------------------------------
  242. // Implementation of the assimp info utility to print basic file info
  243. int Assimp_Info (const char* const* params, unsigned int num)
  244. {
  245. if (num < 1) {
  246. printf("assimp info: Invalid number of arguments. "
  247. "See \'assimp info --help\'\n");
  248. return 1;
  249. }
  250. // --help
  251. if (!strcmp( params[0],"-h")||!strcmp( params[0],"--help")||!strcmp( params[0],"-?") ) {
  252. printf("%s",AICMD_MSG_INFO_HELP_E);
  253. return 0;
  254. }
  255. // asssimp info <file> [-r]
  256. if (num < 1) {
  257. printf("assimp info: Invalid number of arguments. "
  258. "See \'assimp info --help\'\n");
  259. return 1;
  260. }
  261. const std::string in = std::string(params[0]);
  262. // get -r and -v arguments
  263. bool raw = false;
  264. bool verbose = false;
  265. for(unsigned int i = 1; i < num; ++i) {
  266. if (!strcmp(params[i],"--raw")||!strcmp(params[i],"-r")) {
  267. raw = true;
  268. }
  269. if (!strcmp(params[i],"--verbose")||!strcmp(params[i],"-v")) {
  270. verbose = true;
  271. }
  272. }
  273. // do maximum post-processing unless -r was specified
  274. ImportData import;
  275. if (!raw) {
  276. import.ppFlags = aiProcessPreset_TargetRealtime_MaxQuality;
  277. }
  278. // import the main model
  279. const aiScene* scene = ImportModel(import,in);
  280. if (!scene) {
  281. printf("assimp info: Unable to load input file %s\n",
  282. in.c_str());
  283. return 5;
  284. }
  285. aiMemoryInfo mem;
  286. globalImporter->GetMemoryRequirements(mem);
  287. static const char* format_string =
  288. "Memory consumption: %i B\n"
  289. "Nodes: %i\n"
  290. "Maximum depth %i\n"
  291. "Meshes: %i\n"
  292. "Animations: %i\n"
  293. "Textures (embed.): %i\n"
  294. "Materials: %i\n"
  295. "Cameras: %i\n"
  296. "Lights: %i\n"
  297. "Vertices: %i\n"
  298. "Faces: %i\n"
  299. "Bones: %i\n"
  300. "Animation Channels: %i\n"
  301. "Primitive Types: %s\n"
  302. "Average faces/mesh %i\n"
  303. "Average verts/mesh %i\n"
  304. "Minimum point (%f %f %f)\n"
  305. "Maximum point (%f %f %f)\n"
  306. "Center point (%f %f %f)\n"
  307. ;
  308. aiVector3D special_points[3];
  309. FindSpecialPoints(scene,special_points);
  310. printf(format_string,
  311. mem.total,
  312. CountNodes(scene->mRootNode),
  313. GetMaxDepth(scene->mRootNode),
  314. scene->mNumMeshes,
  315. scene->mNumAnimations,
  316. scene->mNumTextures,
  317. scene->mNumMaterials,
  318. scene->mNumCameras,
  319. scene->mNumLights,
  320. CountVertices(scene),
  321. CountFaces(scene),
  322. CountBones(scene),
  323. CountAnimChannels(scene),
  324. FindPTypes(scene).c_str(),
  325. GetAvgFacePerMesh(scene),
  326. GetAvgVertsPerMesh(scene),
  327. special_points[0][0],special_points[0][1],special_points[0][2],
  328. special_points[1][0],special_points[1][1],special_points[1][2],
  329. special_points[2][0],special_points[2][1],special_points[2][2]
  330. )
  331. ;
  332. // meshes
  333. if (scene->mNumMeshes) {
  334. printf("\nMeshes: (name) [vertices / bones / faces | primitive_types]\n");
  335. }
  336. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  337. const aiMesh* mesh = scene->mMeshes[i];
  338. printf(" %d (%s)", i, mesh->mName.C_Str());
  339. printf(
  340. ": [%d / %d / %d |",
  341. mesh->mNumVertices,
  342. mesh->mNumBones,
  343. mesh->mNumFaces
  344. );
  345. const unsigned int ptypes = mesh->mPrimitiveTypes;
  346. if (ptypes & aiPrimitiveType_POINT) { printf(" point"); }
  347. if (ptypes & aiPrimitiveType_LINE) { printf(" line"); }
  348. if (ptypes & aiPrimitiveType_TRIANGLE) { printf(" triangle"); }
  349. if (ptypes & aiPrimitiveType_POLYGON) { printf(" polygon"); }
  350. printf("]\n");
  351. }
  352. // materials
  353. unsigned int total=0;
  354. for(unsigned int i = 0;i < scene->mNumMaterials; ++i) {
  355. aiString name;
  356. if (AI_SUCCESS==aiGetMaterialString(scene->mMaterials[i],AI_MATKEY_NAME,&name)) {
  357. printf("%s\n \'%s\'",(total++?"":"\nNamed Materials:" ),name.data);
  358. }
  359. }
  360. if(total) {
  361. printf("\n");
  362. }
  363. // textures
  364. total=0;
  365. for(unsigned int i = 0;i < scene->mNumMaterials; ++i) {
  366. aiString name;
  367. static const aiTextureType types[] = {
  368. aiTextureType_NONE,
  369. aiTextureType_DIFFUSE,
  370. aiTextureType_SPECULAR,
  371. aiTextureType_AMBIENT,
  372. aiTextureType_EMISSIVE,
  373. aiTextureType_HEIGHT,
  374. aiTextureType_NORMALS,
  375. aiTextureType_SHININESS,
  376. aiTextureType_OPACITY,
  377. aiTextureType_DISPLACEMENT,
  378. aiTextureType_LIGHTMAP,
  379. aiTextureType_REFLECTION,
  380. aiTextureType_UNKNOWN
  381. };
  382. for(unsigned int type = 0; type < sizeof(types)/sizeof(types[0]); ++type) {
  383. for(unsigned int idx = 0;AI_SUCCESS==aiGetMaterialString(scene->mMaterials[i],
  384. AI_MATKEY_TEXTURE(types[type],idx),&name); ++idx) {
  385. printf("%s\n \'%s\'",(total++?"":"\nTexture Refs:" ),name.data);
  386. }
  387. }
  388. }
  389. if(total) {
  390. printf("\n");
  391. }
  392. // animations
  393. total=0;
  394. for(unsigned int i = 0;i < scene->mNumAnimations; ++i) {
  395. if (scene->mAnimations[i]->mName.length) {
  396. printf("%s\n \'%s\'",(total++?"":"\nNamed Animations:" ),scene->mAnimations[i]->mName.data);
  397. }
  398. }
  399. if(total) {
  400. printf("\n");
  401. }
  402. // node hierarchy
  403. printf("\nNode hierarchy:\n");
  404. PrintHierarchy(scene->mRootNode,"",verbose);
  405. printf("\n");
  406. return 0;
  407. }