XFileParser.cpp 44 KB

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