XFileParser.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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 Implementation of the XFile parser helper class */
  35. #ifndef ASSIMP_BUILD_NO_X_IMPORTER
  36. #include "XFileParser.h"
  37. #include "XFileHelper.h"
  38. #include <assimp/ByteSwapper.h>
  39. #include <assimp/Exceptional.h>
  40. #include <assimp/StringUtils.h>
  41. #include <assimp/TinyFormatter.h>
  42. #include <assimp/fast_atof.h>
  43. #include <assimp/DefaultLogger.hpp>
  44. using namespace Assimp;
  45. using namespace Assimp::XFile;
  46. using namespace Assimp::Formatter;
  47. #ifndef ASSIMP_BUILD_NO_COMPRESSED_X
  48. #include "Common/Compression.h"
  49. // Magic identifier for MSZIP compressed data
  50. constexpr unsigned int MSZIP_MAGIC = 0x4B43;
  51. constexpr size_t MSZIP_BLOCK = 32786l;
  52. #endif // !! ASSIMP_BUILD_NO_COMPRESSED_X
  53. // ------------------------------------------------------------------------------------------------
  54. // Throws an exception with a line number and the given text.
  55. template<typename... T>
  56. AI_WONT_RETURN void XFileParser::ThrowException(T&&... args) {
  57. if (mIsBinaryFormat) {
  58. throw DeadlyImportError(args...);
  59. } else {
  60. throw DeadlyImportError("Line ", mLineNumber, ": ", args...);
  61. }
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // Constructor. Creates a data structure out of the XFile given in the memory block.
  65. XFileParser::XFileParser(const std::vector<char> &pBuffer) :
  66. mMajorVersion(0), mMinorVersion(0), mIsBinaryFormat(false), mBinaryNumCount(0), mP(nullptr), mEnd(nullptr), mLineNumber(0), mScene(nullptr) {
  67. // vector to store uncompressed file for INFLATE'd X files
  68. std::vector<char> uncompressed;
  69. // set up memory pointers
  70. mP = &pBuffer.front();
  71. mEnd = mP + pBuffer.size() - 1;
  72. // check header
  73. if (0 != strncmp(mP, "xof ", 4)) {
  74. throw DeadlyImportError("Header mismatch, file is not an XFile.");
  75. }
  76. // read version. It comes in a four byte format such as "0302"
  77. mMajorVersion = (unsigned int)(mP[4] - 48) * 10 + (unsigned int)(mP[5] - 48);
  78. mMinorVersion = (unsigned int)(mP[6] - 48) * 10 + (unsigned int)(mP[7] - 48);
  79. bool compressed = false;
  80. // txt - pure ASCII text format
  81. if (strncmp(mP + 8, "txt ", 4) == 0)
  82. mIsBinaryFormat = false;
  83. // bin - Binary format
  84. else if (strncmp(mP + 8, "bin ", 4) == 0)
  85. mIsBinaryFormat = true;
  86. // tzip - Inflate compressed text format
  87. else if (strncmp(mP + 8, "tzip", 4) == 0) {
  88. mIsBinaryFormat = false;
  89. compressed = true;
  90. }
  91. // bzip - Inflate compressed binary format
  92. else if (strncmp(mP + 8, "bzip", 4) == 0) {
  93. mIsBinaryFormat = true;
  94. compressed = true;
  95. } else
  96. ThrowException("Unsupported xfile format '", mP[8], mP[9], mP[10], mP[11], "'");
  97. // float size
  98. mBinaryFloatSize = (unsigned int)(mP[12] - 48) * 1000 + (unsigned int)(mP[13] - 48) * 100 + (unsigned int)(mP[14] - 48) * 10 + (unsigned int)(mP[15] - 48);
  99. if (mBinaryFloatSize != 32 && mBinaryFloatSize != 64)
  100. ThrowException("Unknown float size ", mBinaryFloatSize, " specified in xfile header.");
  101. // The x format specifies size in bits, but we work in bytes
  102. mBinaryFloatSize /= 8;
  103. mP += 16;
  104. // If this is a compressed X file, apply the inflate algorithm to it
  105. if (compressed) {
  106. #ifdef ASSIMP_BUILD_NO_COMPRESSED_X
  107. throw DeadlyImportError("Assimp was built without compressed X support");
  108. #else
  109. /* ///////////////////////////////////////////////////////////////////////
  110. * COMPRESSED X FILE FORMAT
  111. * ///////////////////////////////////////////////////////////////////////
  112. * [xhead]
  113. * 2 major
  114. * 2 minor
  115. * 4 type // bzip,tzip
  116. * [mszip_master_head]
  117. * 4 unkn // checksum?
  118. * 2 unkn // flags? (seems to be constant)
  119. * [mszip_head]
  120. * 2 ofs // offset to next section
  121. * 2 magic // 'CK'
  122. * ... ofs bytes of data
  123. * ... next mszip_head
  124. *
  125. * http://www.kdedevelopers.org/node/3181 has been very helpful.
  126. * ///////////////////////////////////////////////////////////////////////
  127. */
  128. // skip unknown data (checksum, flags?)
  129. mP += 6;
  130. // First find out how much storage we'll need. Count sections.
  131. const char *P1 = mP;
  132. unsigned int est_out = 0;
  133. while (P1 + 3 < mEnd) {
  134. // read next offset
  135. uint16_t ofs = *((uint16_t *)P1);
  136. AI_SWAP2(ofs);
  137. P1 += 2;
  138. if (ofs >= MSZIP_BLOCK)
  139. throw DeadlyImportError("X: Invalid offset to next MSZIP compressed block");
  140. // check magic word
  141. uint16_t magic = *((uint16_t *)P1);
  142. AI_SWAP2(magic);
  143. P1 += 2;
  144. if (magic != MSZIP_MAGIC)
  145. throw DeadlyImportError("X: Unsupported compressed format, expected MSZIP header");
  146. // and advance to the next offset
  147. P1 += ofs;
  148. est_out += MSZIP_BLOCK; // one decompressed block is 32786 in size
  149. }
  150. // Allocate storage and terminating zero and do the actual uncompressing
  151. Compression compression;
  152. uncompressed.resize(est_out + 1);
  153. char *out = &uncompressed.front();
  154. if (compression.open(mIsBinaryFormat ? Compression::Format::Binary : Compression::Format::ASCII, Compression::FlushMode::SyncFlush)) {
  155. while (mP + 3 < mEnd) {
  156. uint16_t ofs = *((uint16_t *)mP);
  157. AI_SWAP2(ofs);
  158. mP += 4;
  159. if (mP + ofs > mEnd + 2) {
  160. throw DeadlyImportError("X: Unexpected EOF in compressed chunk");
  161. }
  162. out += compression.decompressBlock(mP, ofs, out, MSZIP_BLOCK);
  163. mP += ofs;
  164. }
  165. compression.close();
  166. }
  167. // ok, update pointers to point to the uncompressed file data
  168. mP = &uncompressed[0];
  169. mEnd = out;
  170. // FIXME: we don't need the compressed data anymore, could release
  171. // it already for better memory usage. Consider breaking const-co.
  172. ASSIMP_LOG_INFO("Successfully decompressed MSZIP-compressed file");
  173. #endif // !! ASSIMP_BUILD_NO_COMPRESSED_X
  174. } else {
  175. // start reading here
  176. ReadUntilEndOfLine();
  177. }
  178. mScene = new Scene;
  179. ParseFile();
  180. // filter the imported hierarchy for some degenerated cases
  181. if (mScene->mRootNode) {
  182. FilterHierarchy(mScene->mRootNode);
  183. }
  184. }
  185. // ------------------------------------------------------------------------------------------------
  186. // Destructor. Destroys all imported data along with it
  187. XFileParser::~XFileParser() {
  188. // kill everything we created
  189. delete mScene;
  190. }
  191. // ------------------------------------------------------------------------------------------------
  192. void XFileParser::ParseFile() {
  193. bool running = true;
  194. while (running) {
  195. // read name of next object
  196. std::string objectName = GetNextToken();
  197. if (objectName.length() == 0) {
  198. break;
  199. }
  200. // parse specific object
  201. if (objectName == "template")
  202. ParseDataObjectTemplate();
  203. else if (objectName == "Frame")
  204. ParseDataObjectFrame(nullptr);
  205. else if (objectName == "Mesh") {
  206. // some meshes have no frames at all
  207. Mesh *mesh = new Mesh;
  208. ParseDataObjectMesh(mesh);
  209. mScene->mGlobalMeshes.push_back(mesh);
  210. } else if (objectName == "AnimTicksPerSecond")
  211. ParseDataObjectAnimTicksPerSecond();
  212. else if (objectName == "AnimationSet")
  213. ParseDataObjectAnimationSet();
  214. else if (objectName == "Material") {
  215. // Material outside of a mesh or node
  216. Material material;
  217. ParseDataObjectMaterial(&material);
  218. mScene->mGlobalMaterials.push_back(material);
  219. } else if (objectName == "}") {
  220. // whatever?
  221. ASSIMP_LOG_WARN("} found in dataObject");
  222. } else {
  223. // unknown format
  224. ASSIMP_LOG_WARN("Unknown data object in animation of .x file");
  225. ParseUnknownDataObject();
  226. }
  227. }
  228. }
  229. // ------------------------------------------------------------------------------------------------
  230. void XFileParser::ParseDataObjectTemplate() {
  231. // parse a template data object. Currently not stored.
  232. std::string name;
  233. readHeadOfDataObject(&name);
  234. // read GUID
  235. std::string guid = GetNextToken();
  236. // read and ignore data members
  237. bool running = true;
  238. while (running) {
  239. std::string s = GetNextToken();
  240. if (s == "}")
  241. break;
  242. if (s.length() == 0)
  243. ThrowException("Unexpected end of file reached while parsing template definition");
  244. }
  245. }
  246. // ------------------------------------------------------------------------------------------------
  247. void XFileParser::ParseDataObjectFrame(Node *pParent) {
  248. // A coordinate frame, or "frame of reference." The Frame template
  249. // is open and can contain any object. The Direct3D extensions (D3DX)
  250. // mesh-loading functions recognize Mesh, FrameTransformMatrix, and
  251. // Frame template instances as child objects when loading a Frame
  252. // instance.
  253. std::string name;
  254. readHeadOfDataObject(&name);
  255. // create a named node and place it at its parent, if given
  256. Node *node = new Node(pParent);
  257. node->mName = name;
  258. if (pParent) {
  259. pParent->mChildren.push_back(node);
  260. } else {
  261. // there might be multiple root nodes
  262. if (mScene->mRootNode != nullptr) {
  263. // place a dummy root if not there
  264. if (mScene->mRootNode->mName != "$dummy_root") {
  265. Node *exroot = mScene->mRootNode;
  266. mScene->mRootNode = new Node(nullptr);
  267. mScene->mRootNode->mName = "$dummy_root";
  268. mScene->mRootNode->mChildren.push_back(exroot);
  269. exroot->mParent = mScene->mRootNode;
  270. }
  271. // put the new node as its child instead
  272. mScene->mRootNode->mChildren.push_back(node);
  273. node->mParent = mScene->mRootNode;
  274. } else {
  275. // it's the first node imported. place it as root
  276. mScene->mRootNode = node;
  277. }
  278. }
  279. // Now inside a frame.
  280. // read tokens until closing brace is reached.
  281. bool running = true;
  282. while (running) {
  283. std::string objectName = GetNextToken();
  284. if (objectName.size() == 0)
  285. ThrowException("Unexpected end of file reached while parsing frame");
  286. if (objectName == "}")
  287. break; // frame finished
  288. else if (objectName == "Frame")
  289. ParseDataObjectFrame(node); // child frame
  290. else if (objectName == "FrameTransformMatrix")
  291. ParseDataObjectTransformationMatrix(node->mTrafoMatrix);
  292. else if (objectName == "Mesh") {
  293. Mesh *mesh = new Mesh(name);
  294. node->mMeshes.push_back(mesh);
  295. ParseDataObjectMesh(mesh);
  296. } else {
  297. ASSIMP_LOG_WARN("Unknown data object in frame in x file");
  298. ParseUnknownDataObject();
  299. }
  300. }
  301. }
  302. // ------------------------------------------------------------------------------------------------
  303. void XFileParser::ParseDataObjectTransformationMatrix(aiMatrix4x4 &pMatrix) {
  304. // read header, we're not interested if it has a name
  305. readHeadOfDataObject();
  306. // read its components
  307. pMatrix.a1 = ReadFloat();
  308. pMatrix.b1 = ReadFloat();
  309. pMatrix.c1 = ReadFloat();
  310. pMatrix.d1 = ReadFloat();
  311. pMatrix.a2 = ReadFloat();
  312. pMatrix.b2 = ReadFloat();
  313. pMatrix.c2 = ReadFloat();
  314. pMatrix.d2 = ReadFloat();
  315. pMatrix.a3 = ReadFloat();
  316. pMatrix.b3 = ReadFloat();
  317. pMatrix.c3 = ReadFloat();
  318. pMatrix.d3 = ReadFloat();
  319. pMatrix.a4 = ReadFloat();
  320. pMatrix.b4 = ReadFloat();
  321. pMatrix.c4 = ReadFloat();
  322. pMatrix.d4 = ReadFloat();
  323. // trailing symbols
  324. CheckForSemicolon();
  325. CheckForClosingBrace();
  326. }
  327. // ------------------------------------------------------------------------------------------------
  328. void XFileParser::ParseDataObjectMesh(Mesh *pMesh) {
  329. std::string name;
  330. readHeadOfDataObject(&name);
  331. // read vertex count
  332. unsigned int numVertices = ReadInt();
  333. pMesh->mPositions.resize(numVertices);
  334. // read vertices
  335. for (unsigned int a = 0; a < numVertices; a++)
  336. pMesh->mPositions[a] = ReadVector3();
  337. // read position faces
  338. unsigned int numPosFaces = ReadInt();
  339. pMesh->mPosFaces.resize(numPosFaces);
  340. for (unsigned int a = 0; a < numPosFaces; ++a) {
  341. // read indices
  342. unsigned int numIndices = ReadInt();
  343. Face &face = pMesh->mPosFaces[a];
  344. for (unsigned int b = 0; b < numIndices; ++b) {
  345. const int idx(ReadInt());
  346. if (static_cast<unsigned int>(idx) <= numVertices) {
  347. face.mIndices.push_back(idx);
  348. }
  349. }
  350. TestForSeparator();
  351. }
  352. // here, other data objects may follow
  353. bool running = true;
  354. while (running) {
  355. std::string objectName = GetNextToken();
  356. if (objectName.empty())
  357. ThrowException("Unexpected end of file while parsing mesh structure");
  358. else if (objectName == "}")
  359. break; // mesh finished
  360. else if (objectName == "MeshNormals")
  361. ParseDataObjectMeshNormals(pMesh);
  362. else if (objectName == "MeshTextureCoords")
  363. ParseDataObjectMeshTextureCoords(pMesh);
  364. else if (objectName == "MeshVertexColors")
  365. ParseDataObjectMeshVertexColors(pMesh);
  366. else if (objectName == "MeshMaterialList")
  367. ParseDataObjectMeshMaterialList(pMesh);
  368. else if (objectName == "VertexDuplicationIndices")
  369. ParseUnknownDataObject(); // we'll ignore vertex duplication indices
  370. else if (objectName == "XSkinMeshHeader")
  371. ParseDataObjectSkinMeshHeader(pMesh);
  372. else if (objectName == "SkinWeights")
  373. ParseDataObjectSkinWeights(pMesh);
  374. else {
  375. ASSIMP_LOG_WARN("Unknown data object in mesh in x file");
  376. ParseUnknownDataObject();
  377. }
  378. }
  379. }
  380. // ------------------------------------------------------------------------------------------------
  381. void XFileParser::ParseDataObjectSkinWeights(Mesh *pMesh) {
  382. if (nullptr == pMesh) {
  383. return;
  384. }
  385. readHeadOfDataObject();
  386. std::string transformNodeName;
  387. GetNextTokenAsString(transformNodeName);
  388. pMesh->mBones.push_back(Bone());
  389. Bone &bone = pMesh->mBones.back();
  390. bone.mName = transformNodeName;
  391. // read vertex weights
  392. unsigned int numWeights = ReadInt();
  393. bone.mWeights.reserve(numWeights);
  394. for (unsigned int a = 0; a < numWeights; a++) {
  395. BoneWeight weight = {};
  396. weight.mVertex = ReadInt();
  397. bone.mWeights.push_back(weight);
  398. }
  399. // read vertex weights
  400. for (unsigned int a = 0; a < numWeights; a++)
  401. bone.mWeights[a].mWeight = ReadFloat();
  402. // read matrix offset
  403. bone.mOffsetMatrix.a1 = ReadFloat();
  404. bone.mOffsetMatrix.b1 = ReadFloat();
  405. bone.mOffsetMatrix.c1 = ReadFloat();
  406. bone.mOffsetMatrix.d1 = ReadFloat();
  407. bone.mOffsetMatrix.a2 = ReadFloat();
  408. bone.mOffsetMatrix.b2 = ReadFloat();
  409. bone.mOffsetMatrix.c2 = ReadFloat();
  410. bone.mOffsetMatrix.d2 = ReadFloat();
  411. bone.mOffsetMatrix.a3 = ReadFloat();
  412. bone.mOffsetMatrix.b3 = ReadFloat();
  413. bone.mOffsetMatrix.c3 = ReadFloat();
  414. bone.mOffsetMatrix.d3 = ReadFloat();
  415. bone.mOffsetMatrix.a4 = ReadFloat();
  416. bone.mOffsetMatrix.b4 = ReadFloat();
  417. bone.mOffsetMatrix.c4 = ReadFloat();
  418. bone.mOffsetMatrix.d4 = ReadFloat();
  419. CheckForSemicolon();
  420. CheckForClosingBrace();
  421. }
  422. // ------------------------------------------------------------------------------------------------
  423. void XFileParser::ParseDataObjectSkinMeshHeader(Mesh * /*pMesh*/) {
  424. readHeadOfDataObject();
  425. /*unsigned int maxSkinWeightsPerVertex =*/ReadInt();
  426. /*unsigned int maxSkinWeightsPerFace =*/ReadInt();
  427. /*unsigned int numBonesInMesh = */ ReadInt();
  428. CheckForClosingBrace();
  429. }
  430. // ------------------------------------------------------------------------------------------------
  431. void XFileParser::ParseDataObjectMeshNormals(Mesh *pMesh) {
  432. readHeadOfDataObject();
  433. // read count
  434. unsigned int numNormals = ReadInt();
  435. pMesh->mNormals.resize(numNormals);
  436. // read normal vectors
  437. for (unsigned int a = 0; a < numNormals; ++a) {
  438. pMesh->mNormals[a] = ReadVector3();
  439. }
  440. // read normal indices
  441. unsigned int numFaces = ReadInt();
  442. if (numFaces != pMesh->mPosFaces.size()) {
  443. ThrowException("Normal face count does not match vertex face count.");
  444. }
  445. // do not crah when no face definitions are there
  446. if (numFaces > 0) {
  447. // normal face creation
  448. pMesh->mNormFaces.resize(numFaces);
  449. for (unsigned int a = 0; a < numFaces; ++a) {
  450. unsigned int numIndices = ReadInt();
  451. pMesh->mNormFaces[a] = Face();
  452. Face &face = pMesh->mNormFaces[a];
  453. for (unsigned int b = 0; b < numIndices; ++b) {
  454. face.mIndices.push_back(ReadInt());
  455. }
  456. TestForSeparator();
  457. }
  458. }
  459. CheckForClosingBrace();
  460. }
  461. // ------------------------------------------------------------------------------------------------
  462. void XFileParser::ParseDataObjectMeshTextureCoords(Mesh *pMesh) {
  463. readHeadOfDataObject();
  464. if (pMesh->mNumTextures + 1 > AI_MAX_NUMBER_OF_TEXTURECOORDS)
  465. ThrowException("Too many sets of texture coordinates");
  466. std::vector<aiVector2D> &coords = pMesh->mTexCoords[pMesh->mNumTextures++];
  467. unsigned int numCoords = ReadInt();
  468. if (numCoords != pMesh->mPositions.size())
  469. ThrowException("Texture coord count does not match vertex count");
  470. coords.resize(numCoords);
  471. for (unsigned int a = 0; a < numCoords; a++)
  472. coords[a] = ReadVector2();
  473. CheckForClosingBrace();
  474. }
  475. // ------------------------------------------------------------------------------------------------
  476. void XFileParser::ParseDataObjectMeshVertexColors(Mesh *pMesh) {
  477. readHeadOfDataObject();
  478. if (pMesh->mNumColorSets + 1 > AI_MAX_NUMBER_OF_COLOR_SETS)
  479. ThrowException("Too many colorsets");
  480. std::vector<aiColor4D> &colors = pMesh->mColors[pMesh->mNumColorSets++];
  481. unsigned int numColors = ReadInt();
  482. if (numColors != pMesh->mPositions.size())
  483. ThrowException("Vertex color count does not match vertex count");
  484. colors.resize(numColors, aiColor4D(0, 0, 0, 1));
  485. for (unsigned int a = 0; a < numColors; a++) {
  486. unsigned int index = ReadInt();
  487. if (index >= pMesh->mPositions.size())
  488. ThrowException("Vertex color index out of bounds");
  489. colors[index] = ReadRGBA();
  490. // HACK: (thom) Maxon Cinema XPort plugin puts a third separator here, kwxPort puts a comma.
  491. // Ignore gracefully.
  492. if (!mIsBinaryFormat) {
  493. FindNextNoneWhiteSpace();
  494. if (*mP == ';' || *mP == ',')
  495. mP++;
  496. }
  497. }
  498. CheckForClosingBrace();
  499. }
  500. // ------------------------------------------------------------------------------------------------
  501. void XFileParser::ParseDataObjectMeshMaterialList(Mesh *pMesh) {
  502. readHeadOfDataObject();
  503. // read material count
  504. /*unsigned int numMaterials =*/ReadInt();
  505. // read non triangulated face material index count
  506. unsigned int numMatIndices = ReadInt();
  507. // some models have a material index count of 1... to be able to read them we
  508. // replicate this single material index on every face
  509. if (numMatIndices != pMesh->mPosFaces.size() && numMatIndices != 1)
  510. ThrowException("Per-Face material index count does not match face count.");
  511. // read per-face material indices
  512. for (unsigned int a = 0; a < numMatIndices; a++)
  513. pMesh->mFaceMaterials.push_back(ReadInt());
  514. // in version 03.02, the face indices end with two semicolons.
  515. // commented out version check, as version 03.03 exported from blender also has 2 semicolons
  516. if (!mIsBinaryFormat) // && MajorVersion == 3 && MinorVersion <= 2)
  517. {
  518. if (mP < mEnd && *mP == ';')
  519. ++mP;
  520. }
  521. // if there was only a single material index, replicate it on all faces
  522. while (pMesh->mFaceMaterials.size() < pMesh->mPosFaces.size())
  523. pMesh->mFaceMaterials.push_back(pMesh->mFaceMaterials.front());
  524. // read following data objects
  525. bool running = true;
  526. while (running) {
  527. std::string objectName = GetNextToken();
  528. if (objectName.size() == 0)
  529. ThrowException("Unexpected end of file while parsing mesh material list.");
  530. else if (objectName == "}")
  531. break; // material list finished
  532. else if (objectName == "{") {
  533. // template materials
  534. std::string matName = GetNextToken();
  535. Material material;
  536. material.mIsReference = true;
  537. material.mName = matName;
  538. pMesh->mMaterials.push_back(material);
  539. CheckForClosingBrace(); // skip }
  540. } else if (objectName == "Material") {
  541. pMesh->mMaterials.push_back(Material());
  542. ParseDataObjectMaterial(&pMesh->mMaterials.back());
  543. } else if (objectName == ";") {
  544. // ignore
  545. } else {
  546. ASSIMP_LOG_WARN("Unknown data object in material list in x file");
  547. ParseUnknownDataObject();
  548. }
  549. }
  550. }
  551. // ------------------------------------------------------------------------------------------------
  552. void XFileParser::ParseDataObjectMaterial(Material *pMaterial) {
  553. std::string matName;
  554. readHeadOfDataObject(&matName);
  555. if (matName.empty())
  556. matName = std::string("material") + ai_to_string(mLineNumber);
  557. pMaterial->mName = matName;
  558. pMaterial->mIsReference = false;
  559. // read material values
  560. pMaterial->mDiffuse = ReadRGBA();
  561. pMaterial->mSpecularExponent = ReadFloat();
  562. pMaterial->mSpecular = ReadRGB();
  563. pMaterial->mEmissive = ReadRGB();
  564. // read other data objects
  565. bool running = true;
  566. while (running) {
  567. std::string objectName = GetNextToken();
  568. if (objectName.size() == 0)
  569. ThrowException("Unexpected end of file while parsing mesh material");
  570. else if (objectName == "}")
  571. break; // material finished
  572. else if (objectName == "TextureFilename" || objectName == "TextureFileName") {
  573. // some exporters write "TextureFileName" instead.
  574. std::string texname;
  575. ParseDataObjectTextureFilename(texname);
  576. pMaterial->mTextures.push_back(TexEntry(texname));
  577. } else if (objectName == "NormalmapFilename" || objectName == "NormalmapFileName") {
  578. // one exporter writes out the normal map in a separate filename tag
  579. std::string texname;
  580. ParseDataObjectTextureFilename(texname);
  581. pMaterial->mTextures.push_back(TexEntry(texname, true));
  582. } else {
  583. ASSIMP_LOG_WARN("Unknown data object in material in x file");
  584. ParseUnknownDataObject();
  585. }
  586. }
  587. }
  588. // ------------------------------------------------------------------------------------------------
  589. void XFileParser::ParseDataObjectAnimTicksPerSecond() {
  590. readHeadOfDataObject();
  591. mScene->mAnimTicksPerSecond = ReadInt();
  592. CheckForClosingBrace();
  593. }
  594. // ------------------------------------------------------------------------------------------------
  595. void XFileParser::ParseDataObjectAnimationSet() {
  596. std::string animName;
  597. readHeadOfDataObject(&animName);
  598. Animation *anim = new Animation;
  599. mScene->mAnims.push_back(anim);
  600. anim->mName = animName;
  601. bool running = true;
  602. while (running) {
  603. std::string objectName = GetNextToken();
  604. if (objectName.length() == 0)
  605. ThrowException("Unexpected end of file while parsing animation set.");
  606. else if (objectName == "}")
  607. break; // animation set finished
  608. else if (objectName == "Animation")
  609. ParseDataObjectAnimation(anim);
  610. else {
  611. ASSIMP_LOG_WARN("Unknown data object in animation set in x file");
  612. ParseUnknownDataObject();
  613. }
  614. }
  615. }
  616. // ------------------------------------------------------------------------------------------------
  617. void XFileParser::ParseDataObjectAnimation(Animation *pAnim) {
  618. readHeadOfDataObject();
  619. AnimBone *banim = new AnimBone;
  620. pAnim->mAnims.push_back(banim);
  621. bool running = true;
  622. while (running) {
  623. std::string objectName = GetNextToken();
  624. if (objectName.length() == 0)
  625. ThrowException("Unexpected end of file while parsing animation.");
  626. else if (objectName == "}")
  627. break; // animation finished
  628. else if (objectName == "AnimationKey")
  629. ParseDataObjectAnimationKey(banim);
  630. else if (objectName == "AnimationOptions")
  631. ParseUnknownDataObject(); // not interested
  632. else if (objectName == "{") {
  633. // read frame name
  634. banim->mBoneName = GetNextToken();
  635. CheckForClosingBrace();
  636. } else {
  637. ASSIMP_LOG_WARN("Unknown data object in animation in x file");
  638. ParseUnknownDataObject();
  639. }
  640. }
  641. }
  642. // ------------------------------------------------------------------------------------------------
  643. void XFileParser::ParseDataObjectAnimationKey(AnimBone *pAnimBone) {
  644. readHeadOfDataObject();
  645. // read key type
  646. unsigned int keyType = ReadInt();
  647. // read number of keys
  648. unsigned int numKeys = ReadInt();
  649. for (unsigned int a = 0; a < numKeys; a++) {
  650. // read time
  651. unsigned int time = ReadInt();
  652. // read keys
  653. switch (keyType) {
  654. case 0: // rotation quaternion
  655. {
  656. // read count
  657. if (ReadInt() != 4)
  658. ThrowException("Invalid number of arguments for quaternion key in animation");
  659. aiQuatKey key;
  660. key.mTime = double(time);
  661. key.mValue.w = ReadFloat();
  662. key.mValue.x = ReadFloat();
  663. key.mValue.y = ReadFloat();
  664. key.mValue.z = ReadFloat();
  665. pAnimBone->mRotKeys.push_back(key);
  666. CheckForSemicolon();
  667. break;
  668. }
  669. case 1: // scale vector
  670. case 2: // position vector
  671. {
  672. // read count
  673. if (ReadInt() != 3)
  674. ThrowException("Invalid number of arguments for vector key in animation");
  675. aiVectorKey key;
  676. key.mTime = double(time);
  677. key.mValue = ReadVector3();
  678. if (keyType == 2)
  679. pAnimBone->mPosKeys.push_back(key);
  680. else
  681. pAnimBone->mScaleKeys.push_back(key);
  682. break;
  683. }
  684. case 3: // combined transformation matrix
  685. case 4: // denoted both as 3 or as 4
  686. {
  687. // read count
  688. if (ReadInt() != 16)
  689. ThrowException("Invalid number of arguments for matrix key in animation");
  690. // read matrix
  691. MatrixKey key;
  692. key.mTime = double(time);
  693. key.mMatrix.a1 = ReadFloat();
  694. key.mMatrix.b1 = ReadFloat();
  695. key.mMatrix.c1 = ReadFloat();
  696. key.mMatrix.d1 = ReadFloat();
  697. key.mMatrix.a2 = ReadFloat();
  698. key.mMatrix.b2 = ReadFloat();
  699. key.mMatrix.c2 = ReadFloat();
  700. key.mMatrix.d2 = ReadFloat();
  701. key.mMatrix.a3 = ReadFloat();
  702. key.mMatrix.b3 = ReadFloat();
  703. key.mMatrix.c3 = ReadFloat();
  704. key.mMatrix.d3 = ReadFloat();
  705. key.mMatrix.a4 = ReadFloat();
  706. key.mMatrix.b4 = ReadFloat();
  707. key.mMatrix.c4 = ReadFloat();
  708. key.mMatrix.d4 = ReadFloat();
  709. pAnimBone->mTrafoKeys.push_back(key);
  710. CheckForSemicolon();
  711. break;
  712. }
  713. default:
  714. ThrowException("Unknown key type ", keyType, " in animation.");
  715. break;
  716. } // end switch
  717. // key separator
  718. CheckForSeparator();
  719. }
  720. CheckForClosingBrace();
  721. }
  722. // ------------------------------------------------------------------------------------------------
  723. void XFileParser::ParseDataObjectTextureFilename(std::string &pName) {
  724. readHeadOfDataObject();
  725. GetNextTokenAsString(pName);
  726. CheckForClosingBrace();
  727. // FIX: some files (e.g. AnimationTest.x) have "" as texture file name
  728. if (!pName.length()) {
  729. ASSIMP_LOG_WARN("Length of texture file name is zero. Skipping this texture.");
  730. }
  731. // some exporters write double backslash paths out. We simply replace them if we find them
  732. while (pName.find("\\\\") != std::string::npos)
  733. pName.replace(pName.find("\\\\"), 2, "\\");
  734. }
  735. // ------------------------------------------------------------------------------------------------
  736. void XFileParser::ParseUnknownDataObject() {
  737. // find opening delimiter
  738. bool running = true;
  739. while (running) {
  740. std::string t = GetNextToken();
  741. if (t.length() == 0)
  742. ThrowException("Unexpected end of file while parsing unknown segment.");
  743. if (t == "{")
  744. break;
  745. }
  746. unsigned int counter = 1;
  747. // parse until closing delimiter
  748. while (counter > 0) {
  749. std::string t = GetNextToken();
  750. if (t.length() == 0)
  751. ThrowException("Unexpected end of file while parsing unknown segment.");
  752. if (t == "{")
  753. ++counter;
  754. else if (t == "}")
  755. --counter;
  756. }
  757. }
  758. // ------------------------------------------------------------------------------------------------
  759. //! checks for closing curly brace
  760. void XFileParser::CheckForClosingBrace() {
  761. if (GetNextToken() != "}")
  762. ThrowException("Closing brace expected.");
  763. }
  764. // ------------------------------------------------------------------------------------------------
  765. //! checks for one following semicolon
  766. void XFileParser::CheckForSemicolon() {
  767. if (mIsBinaryFormat)
  768. return;
  769. if (GetNextToken() != ";")
  770. ThrowException("Semicolon expected.");
  771. }
  772. // ------------------------------------------------------------------------------------------------
  773. //! checks for a separator char, either a ',' or a ';'
  774. void XFileParser::CheckForSeparator() {
  775. if (mIsBinaryFormat)
  776. return;
  777. std::string token = GetNextToken();
  778. if (token != "," && token != ";")
  779. ThrowException("Separator character (';' or ',') expected.");
  780. }
  781. // ------------------------------------------------------------------------------------------------
  782. // tests and possibly consumes a separator char, but does nothing if there was no separator
  783. void XFileParser::TestForSeparator() {
  784. if (mIsBinaryFormat)
  785. return;
  786. FindNextNoneWhiteSpace();
  787. if (mP >= mEnd)
  788. return;
  789. // test and skip
  790. if (*mP == ';' || *mP == ',')
  791. mP++;
  792. }
  793. // ------------------------------------------------------------------------------------------------
  794. void XFileParser::readHeadOfDataObject(std::string *poName) {
  795. std::string nameOrBrace = GetNextToken();
  796. if (nameOrBrace != "{") {
  797. if (poName)
  798. *poName = nameOrBrace;
  799. if (GetNextToken() != "{") {
  800. delete mScene;
  801. ThrowException("Opening brace expected.");
  802. }
  803. }
  804. }
  805. // ------------------------------------------------------------------------------------------------
  806. std::string XFileParser::GetNextToken() {
  807. std::string s;
  808. // process binary-formatted file
  809. if (mIsBinaryFormat) {
  810. // in binary mode it will only return NAME and STRING token
  811. // and (correctly) skip over other tokens.
  812. if (mEnd - mP < 2) {
  813. return s;
  814. }
  815. unsigned int tok = ReadBinWord();
  816. unsigned int len;
  817. // standalone tokens
  818. switch (tok) {
  819. case 1: {
  820. // name token
  821. if (mEnd - mP < 4) {
  822. return s;
  823. }
  824. len = ReadBinDWord();
  825. const int bounds = int(mEnd - mP);
  826. const int iLen = int(len);
  827. if (iLen < 0) {
  828. return s;
  829. }
  830. if (bounds < iLen) {
  831. return s;
  832. }
  833. s = std::string(mP, len);
  834. mP += len;
  835. }
  836. return s;
  837. case 2:
  838. // string token
  839. if (mEnd - mP < 4) return s;
  840. len = ReadBinDWord();
  841. if (mEnd - mP < int(len)) return s;
  842. s = std::string(mP, len);
  843. mP += (len + 2);
  844. return s;
  845. case 3:
  846. // integer token
  847. mP += 4;
  848. return "<integer>";
  849. case 5:
  850. // GUID token
  851. mP += 16;
  852. return "<guid>";
  853. case 6:
  854. if (mEnd - mP < 4) return s;
  855. len = ReadBinDWord();
  856. mP += (len * 4);
  857. return "<int_list>";
  858. case 7:
  859. if (mEnd - mP < 4) return s;
  860. len = ReadBinDWord();
  861. mP += (len * mBinaryFloatSize);
  862. return "<flt_list>";
  863. case 0x0a:
  864. return "{";
  865. case 0x0b:
  866. return "}";
  867. case 0x0c:
  868. return "(";
  869. case 0x0d:
  870. return ")";
  871. case 0x0e:
  872. return "[";
  873. case 0x0f:
  874. return "]";
  875. case 0x10:
  876. return "<";
  877. case 0x11:
  878. return ">";
  879. case 0x12:
  880. return ".";
  881. case 0x13:
  882. return ",";
  883. case 0x14:
  884. return ";";
  885. case 0x1f:
  886. return "template";
  887. case 0x28:
  888. return "WORD";
  889. case 0x29:
  890. return "DWORD";
  891. case 0x2a:
  892. return "FLOAT";
  893. case 0x2b:
  894. return "DOUBLE";
  895. case 0x2c:
  896. return "CHAR";
  897. case 0x2d:
  898. return "UCHAR";
  899. case 0x2e:
  900. return "SWORD";
  901. case 0x2f:
  902. return "SDWORD";
  903. case 0x30:
  904. return "void";
  905. case 0x31:
  906. return "string";
  907. case 0x32:
  908. return "unicode";
  909. case 0x33:
  910. return "cstring";
  911. case 0x34:
  912. return "array";
  913. }
  914. }
  915. // process text-formatted file
  916. else {
  917. FindNextNoneWhiteSpace();
  918. if (mP >= mEnd)
  919. return s;
  920. while ((mP < mEnd) && !isspace((unsigned char)*mP)) {
  921. // either keep token delimiters when already holding a token, or return if first valid char
  922. if (*mP == ';' || *mP == '}' || *mP == '{' || *mP == ',') {
  923. if (!s.size())
  924. s.append(mP++, 1);
  925. break; // stop for delimiter
  926. }
  927. s.append(mP++, 1);
  928. }
  929. }
  930. return s;
  931. }
  932. // ------------------------------------------------------------------------------------------------
  933. void XFileParser::FindNextNoneWhiteSpace() {
  934. if (mIsBinaryFormat)
  935. return;
  936. bool running = true;
  937. while (running) {
  938. while (mP < mEnd && isspace((unsigned char)*mP)) {
  939. if (*mP == '\n')
  940. mLineNumber++;
  941. ++mP;
  942. }
  943. if (mP >= mEnd)
  944. return;
  945. // check if this is a comment
  946. if ((mP[0] == '/' && mP[1] == '/') || mP[0] == '#')
  947. ReadUntilEndOfLine();
  948. else
  949. break;
  950. }
  951. }
  952. // ------------------------------------------------------------------------------------------------
  953. void XFileParser::GetNextTokenAsString(std::string &poString) {
  954. if (mIsBinaryFormat) {
  955. poString = GetNextToken();
  956. return;
  957. }
  958. FindNextNoneWhiteSpace();
  959. if (mP >= mEnd) {
  960. delete mScene;
  961. ThrowException("Unexpected end of file while parsing string");
  962. }
  963. if (*mP != '"') {
  964. delete mScene;
  965. ThrowException("Expected quotation mark.");
  966. }
  967. ++mP;
  968. while (mP < mEnd && *mP != '"')
  969. poString.append(mP++, 1);
  970. if (mP >= mEnd - 1) {
  971. delete mScene;
  972. ThrowException("Unexpected end of file while parsing string");
  973. }
  974. if (mP[1] != ';' || mP[0] != '"') {
  975. delete mScene;
  976. ThrowException("Expected quotation mark and semicolon at the end of a string.");
  977. }
  978. mP += 2;
  979. }
  980. // ------------------------------------------------------------------------------------------------
  981. void XFileParser::ReadUntilEndOfLine() {
  982. if (mIsBinaryFormat)
  983. return;
  984. while (mP < mEnd) {
  985. if (*mP == '\n' || *mP == '\r') {
  986. ++mP;
  987. mLineNumber++;
  988. return;
  989. }
  990. ++mP;
  991. }
  992. }
  993. // ------------------------------------------------------------------------------------------------
  994. unsigned short XFileParser::ReadBinWord() {
  995. ai_assert(mEnd - mP >= 2);
  996. const unsigned char *q = (const unsigned char *)mP;
  997. unsigned short tmp = q[0] | (q[1] << 8);
  998. mP += 2;
  999. return tmp;
  1000. }
  1001. // ------------------------------------------------------------------------------------------------
  1002. unsigned int XFileParser::ReadBinDWord() {
  1003. ai_assert(mEnd - mP >= 4);
  1004. const unsigned char *q = (const unsigned char *)mP;
  1005. unsigned int tmp = q[0] | (q[1] << 8) | (q[2] << 16) | (q[3] << 24);
  1006. mP += 4;
  1007. return tmp;
  1008. }
  1009. // ------------------------------------------------------------------------------------------------
  1010. unsigned int XFileParser::ReadInt() {
  1011. if (mIsBinaryFormat) {
  1012. if (mBinaryNumCount == 0 && mEnd - mP >= 2) {
  1013. unsigned short tmp = ReadBinWord(); // 0x06 or 0x03
  1014. if (tmp == 0x06 && mEnd - mP >= 4) // array of ints follows
  1015. mBinaryNumCount = ReadBinDWord();
  1016. else // single int follows
  1017. mBinaryNumCount = 1;
  1018. }
  1019. --mBinaryNumCount;
  1020. const size_t len(mEnd - mP);
  1021. if (len >= 4) {
  1022. return ReadBinDWord();
  1023. } else {
  1024. mP = mEnd;
  1025. return 0;
  1026. }
  1027. } else {
  1028. FindNextNoneWhiteSpace();
  1029. // TODO: consider using strtol10 instead???
  1030. // check preceding minus sign
  1031. bool isNegative = false;
  1032. if (*mP == '-') {
  1033. isNegative = true;
  1034. mP++;
  1035. }
  1036. // at least one digit expected
  1037. if (!isdigit((unsigned char)*mP))
  1038. ThrowException("Number expected.");
  1039. // read digits
  1040. unsigned int number = 0;
  1041. while (mP < mEnd) {
  1042. if (!isdigit((unsigned char)*mP))
  1043. break;
  1044. number = number * 10 + (*mP - 48);
  1045. mP++;
  1046. }
  1047. CheckForSeparator();
  1048. return isNegative ? ((unsigned int)-int(number)) : number;
  1049. }
  1050. }
  1051. // ------------------------------------------------------------------------------------------------
  1052. ai_real XFileParser::ReadFloat() {
  1053. if (mIsBinaryFormat) {
  1054. if (mBinaryNumCount == 0 && mEnd - mP >= 2) {
  1055. unsigned short tmp = ReadBinWord(); // 0x07 or 0x42
  1056. if (tmp == 0x07 && mEnd - mP >= 4) // array of floats following
  1057. mBinaryNumCount = ReadBinDWord();
  1058. else // single float following
  1059. mBinaryNumCount = 1;
  1060. }
  1061. --mBinaryNumCount;
  1062. if (mBinaryFloatSize == 8) {
  1063. if (mEnd - mP >= 8) {
  1064. double res;
  1065. ::memcpy(&res, mP, 8);
  1066. mP += 8;
  1067. const ai_real result(static_cast<ai_real>(res));
  1068. return result;
  1069. } else {
  1070. mP = mEnd;
  1071. return 0;
  1072. }
  1073. } else {
  1074. if (mEnd - mP >= 4) {
  1075. ai_real result;
  1076. ::memcpy(&result, mP, 4);
  1077. mP += 4;
  1078. return result;
  1079. } else {
  1080. mP = mEnd;
  1081. return 0;
  1082. }
  1083. }
  1084. }
  1085. // text version
  1086. FindNextNoneWhiteSpace();
  1087. // check for various special strings to allow reading files from faulty exporters
  1088. // I mean you, Blender!
  1089. // Reading is safe because of the terminating zero
  1090. if (strncmp(mP, "-1.#IND00", 9) == 0 || strncmp(mP, "1.#IND00", 8) == 0) {
  1091. mP += 9;
  1092. CheckForSeparator();
  1093. return 0.0;
  1094. } else if (strncmp(mP, "1.#QNAN0", 8) == 0) {
  1095. mP += 8;
  1096. CheckForSeparator();
  1097. return 0.0;
  1098. }
  1099. ai_real result = 0.0;
  1100. mP = fast_atoreal_move<ai_real>(mP, result);
  1101. CheckForSeparator();
  1102. return result;
  1103. }
  1104. // ------------------------------------------------------------------------------------------------
  1105. aiVector2D XFileParser::ReadVector2() {
  1106. aiVector2D vector;
  1107. vector.x = ReadFloat();
  1108. vector.y = ReadFloat();
  1109. TestForSeparator();
  1110. return vector;
  1111. }
  1112. // ------------------------------------------------------------------------------------------------
  1113. aiVector3D XFileParser::ReadVector3() {
  1114. aiVector3D vector;
  1115. vector.x = ReadFloat();
  1116. vector.y = ReadFloat();
  1117. vector.z = ReadFloat();
  1118. TestForSeparator();
  1119. return vector;
  1120. }
  1121. // ------------------------------------------------------------------------------------------------
  1122. aiColor4D XFileParser::ReadRGBA() {
  1123. aiColor4D color;
  1124. color.r = ReadFloat();
  1125. color.g = ReadFloat();
  1126. color.b = ReadFloat();
  1127. color.a = ReadFloat();
  1128. TestForSeparator();
  1129. return color;
  1130. }
  1131. // ------------------------------------------------------------------------------------------------
  1132. aiColor3D XFileParser::ReadRGB() {
  1133. aiColor3D color;
  1134. color.r = ReadFloat();
  1135. color.g = ReadFloat();
  1136. color.b = ReadFloat();
  1137. TestForSeparator();
  1138. return color;
  1139. }
  1140. // ------------------------------------------------------------------------------------------------
  1141. // Filters the imported hierarchy for some degenerated cases that some exporters produce.
  1142. void XFileParser::FilterHierarchy(XFile::Node *pNode) {
  1143. // if the node has just a single unnamed child containing a mesh, remove
  1144. // the anonymous node between. The 3DSMax kwXport plugin seems to produce this
  1145. // mess in some cases
  1146. if (pNode->mChildren.size() == 1 && pNode->mMeshes.empty()) {
  1147. XFile::Node *child = pNode->mChildren.front();
  1148. if (child->mName.length() == 0 && child->mMeshes.size() > 0) {
  1149. // transfer its meshes to us
  1150. for (unsigned int a = 0; a < child->mMeshes.size(); a++)
  1151. pNode->mMeshes.push_back(child->mMeshes[a]);
  1152. child->mMeshes.clear();
  1153. // transfer the transform as well
  1154. pNode->mTrafoMatrix = pNode->mTrafoMatrix * child->mTrafoMatrix;
  1155. // then kill it
  1156. delete child;
  1157. pNode->mChildren.clear();
  1158. }
  1159. }
  1160. // recurse
  1161. for (unsigned int a = 0; a < pNode->mChildren.size(); a++)
  1162. FilterHierarchy(pNode->mChildren[a]);
  1163. }
  1164. #endif // !! ASSIMP_BUILD_NO_X_IMPORTER