XFileParser.cpp 45 KB

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