FBXDocument.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXDocument.cpp
  34. * @brief Implementation of the FBX DOM classes
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include <functional>
  39. #include "FBXParser.h"
  40. #include "FBXDocument.h"
  41. #include "FBXUtil.h"
  42. #include "FBXImporter.h"
  43. #include "FBXImportSettings.h"
  44. #include "FBXDocumentUtil.h"
  45. #include "FBXProperties.h"
  46. namespace Assimp {
  47. namespace FBX {
  48. namespace Util {
  49. // ------------------------------------------------------------------------------------------------
  50. // signal DOM construction error, this is always unrecoverable. Throws DeadlyImportError.
  51. void DOMError(const std::string& message, const Token& token)
  52. {
  53. throw DeadlyImportError(Util::AddTokenText("FBX-DOM",message,&token));
  54. }
  55. // ------------------------------------------------------------------------------------------------
  56. void DOMError(const std::string& message, const Element* element /*= NULL*/)
  57. {
  58. if(element) {
  59. DOMError(message,element->KeyToken());
  60. }
  61. throw DeadlyImportError("FBX-DOM " + message);
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // print warning, do return
  65. void DOMWarning(const std::string& message, const Token& token)
  66. {
  67. if(DefaultLogger::get()) {
  68. DefaultLogger::get()->warn(Util::AddTokenText("FBX-DOM",message,&token));
  69. }
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. void DOMWarning(const std::string& message, const Element* element /*= NULL*/)
  73. {
  74. if(element) {
  75. DOMWarning(message,element->KeyToken());
  76. return;
  77. }
  78. if(DefaultLogger::get()) {
  79. DefaultLogger::get()->warn("FBX-DOM: " + message);
  80. }
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. // extract required compound scope
  84. const Scope& GetRequiredScope(const Element& el)
  85. {
  86. const Scope* const s = el.Compound();
  87. if(!s) {
  88. DOMError("expected compound scope",&el);
  89. }
  90. return *s;
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. // get token at a particular index
  94. const Token& GetRequiredToken(const Element& el, unsigned int index)
  95. {
  96. const TokenList& t = el.Tokens();
  97. if(index >= t.size()) {
  98. DOMError(Formatter::format( "missing token at index " ) << index,&el);
  99. }
  100. return *t[index];
  101. }
  102. // ------------------------------------------------------------------------------------------------
  103. // wrapper around ParseTokenAsID() with DOMError handling
  104. uint64_t ParseTokenAsID(const Token& t)
  105. {
  106. const char* err;
  107. const uint64_t i = ParseTokenAsID(t,err);
  108. if(err) {
  109. DOMError(err,t);
  110. }
  111. return i;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // wrapper around ParseTokenAsDim() with DOMError handling
  115. size_t ParseTokenAsDim(const Token& t)
  116. {
  117. const char* err;
  118. const size_t i = ParseTokenAsDim(t,err);
  119. if(err) {
  120. DOMError(err,t);
  121. }
  122. return i;
  123. }
  124. // ------------------------------------------------------------------------------------------------
  125. // wrapper around ParseTokenAsFloat() with DOMError handling
  126. float ParseTokenAsFloat(const Token& t)
  127. {
  128. const char* err;
  129. const float i = ParseTokenAsFloat(t,err);
  130. if(err) {
  131. DOMError(err,t);
  132. }
  133. return i;
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. // wrapper around ParseTokenAsInt() with DOMError handling
  137. int ParseTokenAsInt(const Token& t)
  138. {
  139. const char* err;
  140. const int i = ParseTokenAsInt(t,err);
  141. if(err) {
  142. DOMError(err,t);
  143. }
  144. return i;
  145. }
  146. // ------------------------------------------------------------------------------------------------
  147. // wrapper around ParseTokenAsString() with DOMError handling
  148. std::string ParseTokenAsString(const Token& t)
  149. {
  150. const char* err;
  151. const std::string& i = ParseTokenAsString(t,err);
  152. if(err) {
  153. DOMError(err,t);
  154. }
  155. return i;
  156. }
  157. // ------------------------------------------------------------------------------------------------
  158. // extract a required element from a scope, abort if the element cannot be found
  159. const Element& GetRequiredElement(const Scope& sc, const std::string& index, const Element* element /*= NULL*/)
  160. {
  161. const Element* el = sc[index];
  162. if(!el) {
  163. DOMError("did not find required element \"" + index + "\"",element);
  164. }
  165. return *el;
  166. }
  167. // XXX: tacke code duplication in the various ReadVectorDataArray() overloads below.
  168. // could use a type traits based solution.
  169. // ------------------------------------------------------------------------------------------------
  170. // read an array of float3 tuples
  171. void ReadVectorDataArray(std::vector<aiVector3D>& out, const Element& el)
  172. {
  173. out.clear();
  174. const TokenList& tok = el.Tokens();
  175. const size_t dim = ParseTokenAsDim(*tok[0]);
  176. // may throw bad_alloc if the input is rubbish, but this need
  177. // not to be prevented - importing would fail but we wouldn't
  178. // crash since assimp handles this case properly.
  179. out.reserve(dim);
  180. const Scope& scope = GetRequiredScope(el);
  181. const Element& a = GetRequiredElement(scope,"a",&el);
  182. if (a.Tokens().size() % 3 != 0) {
  183. DOMError("number of floats is not a multiple of three (3)",&el);
  184. }
  185. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  186. aiVector3D v;
  187. v.x = ParseTokenAsFloat(**it++);
  188. v.y = ParseTokenAsFloat(**it++);
  189. v.z = ParseTokenAsFloat(**it++);
  190. out.push_back(v);
  191. }
  192. }
  193. // ------------------------------------------------------------------------------------------------
  194. // read an array of color4 tuples
  195. void ReadVectorDataArray(std::vector<aiColor4D>& out, const Element& el)
  196. {
  197. out.clear();
  198. const TokenList& tok = el.Tokens();
  199. const size_t dim = ParseTokenAsDim(*tok[0]);
  200. // see notes in ReadVectorDataArray() above
  201. out.reserve(dim);
  202. const Scope& scope = GetRequiredScope(el);
  203. const Element& a = GetRequiredElement(scope,"a",&el);
  204. if (a.Tokens().size() % 4 != 0) {
  205. DOMError("number of floats is not a multiple of four (4)",&el);
  206. }
  207. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  208. aiColor4D v;
  209. v.r = ParseTokenAsFloat(**it++);
  210. v.g = ParseTokenAsFloat(**it++);
  211. v.b = ParseTokenAsFloat(**it++);
  212. v.a = ParseTokenAsFloat(**it++);
  213. out.push_back(v);
  214. }
  215. }
  216. // ------------------------------------------------------------------------------------------------
  217. // read an array of float2 tuples
  218. void ReadVectorDataArray(std::vector<aiVector2D>& out, const Element& el)
  219. {
  220. out.clear();
  221. const TokenList& tok = el.Tokens();
  222. const size_t dim = ParseTokenAsDim(*tok[0]);
  223. // see notes in ReadVectorDataArray() above
  224. out.reserve(dim);
  225. const Scope& scope = GetRequiredScope(el);
  226. const Element& a = GetRequiredElement(scope,"a",&el);
  227. if (a.Tokens().size() % 2 != 0) {
  228. DOMError("number of floats is not a multiple of two (2)",&el);
  229. }
  230. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  231. aiVector2D v;
  232. v.x = ParseTokenAsFloat(**it++);
  233. v.y = ParseTokenAsFloat(**it++);
  234. out.push_back(v);
  235. }
  236. }
  237. // ------------------------------------------------------------------------------------------------
  238. // read an array of ints
  239. void ReadVectorDataArray(std::vector<int>& out, const Element& el)
  240. {
  241. out.clear();
  242. const TokenList& tok = el.Tokens();
  243. const size_t dim = ParseTokenAsDim(*tok[0]);
  244. // see notes in ReadVectorDataArray()
  245. out.reserve(dim);
  246. const Scope& scope = GetRequiredScope(el);
  247. const Element& a = GetRequiredElement(scope,"a",&el);
  248. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  249. const int ival = ParseTokenAsInt(**it++);
  250. out.push_back(ival);
  251. }
  252. }
  253. // ------------------------------------------------------------------------------------------------
  254. // read an array of floats
  255. void ReadVectorDataArray(std::vector<float>& out, const Element& el)
  256. {
  257. out.clear();
  258. const TokenList& tok = el.Tokens();
  259. const size_t dim = ParseTokenAsDim(*tok[0]);
  260. // see notes in ReadVectorDataArray()
  261. out.reserve(dim);
  262. const Scope& scope = GetRequiredScope(el);
  263. const Element& a = GetRequiredElement(scope,"a",&el);
  264. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  265. const float ival = ParseTokenAsFloat(**it++);
  266. out.push_back(ival);
  267. }
  268. }
  269. // ------------------------------------------------------------------------------------------------
  270. // read an array of uints
  271. void ReadVectorDataArray(std::vector<unsigned int>& out, const Element& el)
  272. {
  273. out.clear();
  274. const TokenList& tok = el.Tokens();
  275. const size_t dim = ParseTokenAsDim(*tok[0]);
  276. // see notes in ReadVectorDataArray()
  277. out.reserve(dim);
  278. const Scope& scope = GetRequiredScope(el);
  279. const Element& a = GetRequiredElement(scope,"a",&el);
  280. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  281. const int ival = ParseTokenAsInt(**it++);
  282. if(ival < 0) {
  283. DOMError("encountered negative integer index");
  284. }
  285. out.push_back(static_cast<unsigned int>(ival));
  286. }
  287. }
  288. // ------------------------------------------------------------------------------------------------
  289. // read an array of uint64_ts
  290. void ReadVectorDataArray(std::vector<uint64_t>& out, const Element& el)
  291. {
  292. out.clear();
  293. const TokenList& tok = el.Tokens();
  294. const size_t dim = ParseTokenAsDim(*tok[0]);
  295. // see notes in ReadVectorDataArray()
  296. out.reserve(dim);
  297. const Scope& scope = GetRequiredScope(el);
  298. const Element& a = GetRequiredElement(scope,"a",&el);
  299. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  300. const uint64_t ival = ParseTokenAsID(**it++);
  301. out.push_back(ival);
  302. }
  303. }
  304. // ------------------------------------------------------------------------------------------------
  305. aiMatrix4x4 ReadMatrix(const Element& element)
  306. {
  307. std::vector<float> values;
  308. ReadVectorDataArray(values,element);
  309. if(values.size() != 16) {
  310. DOMError("expected 16 matrix elements");
  311. }
  312. aiMatrix4x4 result;
  313. result.a1 = values[0];
  314. result.a2 = values[1];
  315. result.a3 = values[2];
  316. result.a4 = values[3];
  317. result.b1 = values[4];
  318. result.b2 = values[5];
  319. result.b3 = values[6];
  320. result.b4 = values[7];
  321. result.c1 = values[8];
  322. result.c2 = values[9];
  323. result.c3 = values[10];
  324. result.c4 = values[11];
  325. result.d1 = values[12];
  326. result.d2 = values[13];
  327. result.d3 = values[14];
  328. result.d4 = values[15];
  329. result.Transpose();
  330. return result;
  331. }
  332. // ------------------------------------------------------------------------------------------------
  333. // fetch a property table and the corresponding property template
  334. boost::shared_ptr<const PropertyTable> GetPropertyTable(const Document& doc,
  335. const std::string& templateName,
  336. const Element &element,
  337. const Scope& sc)
  338. {
  339. const Element* const Properties70 = sc["Properties70"];
  340. boost::shared_ptr<const PropertyTable> templateProps = boost::shared_ptr<const PropertyTable>(
  341. static_cast<const PropertyTable*>(NULL));
  342. if(templateName.length()) {
  343. PropertyTemplateMap::const_iterator it = doc.Templates().find(templateName);
  344. if(it != doc.Templates().end()) {
  345. templateProps = (*it).second;
  346. }
  347. }
  348. if(!Properties70) {
  349. DOMWarning("material property table (Properties70) not found",&element);
  350. if(templateProps) {
  351. return templateProps;
  352. }
  353. else {
  354. return boost::make_shared<const PropertyTable>();
  355. }
  356. }
  357. return boost::make_shared<const PropertyTable>(*Properties70,templateProps);
  358. }
  359. } // !Util
  360. using namespace Util;
  361. // ------------------------------------------------------------------------------------------------
  362. LazyObject::LazyObject(uint64_t id, const Element& element, const Document& doc)
  363. : doc(doc)
  364. , element(element)
  365. , id(id)
  366. , flags()
  367. {
  368. }
  369. // ------------------------------------------------------------------------------------------------
  370. LazyObject::~LazyObject()
  371. {
  372. }
  373. // ------------------------------------------------------------------------------------------------
  374. const Object* LazyObject::Get(bool dieOnError)
  375. {
  376. if(IsBeingConstructed() || FailedToConstruct()) {
  377. return NULL;
  378. }
  379. if (object.get()) {
  380. return object.get();
  381. }
  382. const Token& key = element.KeyToken();
  383. const TokenList& tokens = element.Tokens();
  384. if(tokens.size() < 3) {
  385. DOMError("expected at least 3 tokens: id, name and class tag",&element);
  386. }
  387. const char* err;
  388. const std::string name = ParseTokenAsString(*tokens[1],err);
  389. if (err) {
  390. DOMError(err,&element);
  391. }
  392. const std::string classtag = ParseTokenAsString(*tokens[2],err);
  393. if (err) {
  394. DOMError(err,&element);
  395. }
  396. // prevent recursive calls
  397. flags |= BEING_CONSTRUCTED;
  398. try {
  399. // this needs to be relatively fast since it happens a lot,
  400. // so avoid constructing strings all the time.
  401. const char* obtype = key.begin();
  402. const size_t length = static_cast<size_t>(key.end()-key.begin());
  403. if (!strncmp(obtype,"Geometry",length)) {
  404. if (!strcmp(classtag.c_str(),"Mesh")) {
  405. object.reset(new MeshGeometry(id,element,name,doc));
  406. }
  407. }
  408. else if (!strncmp(obtype,"NodeAttribute",length)) {
  409. if (!strcmp(classtag.c_str(),"CameraSwitcher")) {
  410. object.reset(new CameraSwitcher(id,element,doc,name));
  411. }
  412. }
  413. else if (!strncmp(obtype,"Deformer",length)) {
  414. if (!strcmp(classtag.c_str(),"Cluster")) {
  415. object.reset(new Cluster(id,element,doc,name));
  416. }
  417. else if (!strcmp(classtag.c_str(),"Skin")) {
  418. object.reset(new Skin(id,element,doc,name));
  419. }
  420. }
  421. else if (!strncmp(obtype,"Model",length)) {
  422. object.reset(new Model(id,element,doc,name));
  423. }
  424. else if (!strncmp(obtype,"Material",length)) {
  425. object.reset(new Material(id,element,doc,name));
  426. }
  427. else if (!strncmp(obtype,"Texture",length)) {
  428. object.reset(new Texture(id,element,doc,name));
  429. }
  430. else if (!strncmp(obtype,"AnimationStack",length)) {
  431. object.reset(new AnimationStack(id,element,name,doc));
  432. }
  433. else if (!strncmp(obtype,"AnimationLayer",length)) {
  434. object.reset(new AnimationLayer(id,element,name,doc));
  435. }
  436. // note: order matters for these two
  437. else if (!strncmp(obtype,"AnimationCurve",length)) {
  438. object.reset(new AnimationCurve(id,element,name,doc));
  439. }
  440. else if (!strncmp(obtype,"AnimationCurveNode",length)) {
  441. object.reset(new AnimationCurveNode(id,element,name,doc));
  442. }
  443. }
  444. catch(std::exception& ex) {
  445. flags &= ~BEING_CONSTRUCTED;
  446. flags |= FAILED_TO_CONSTRUCT;
  447. if(dieOnError || doc.Settings().strictMode) {
  448. throw;
  449. }
  450. // note: the error message is already formatted, so raw logging is ok
  451. if(!DefaultLogger::isNullLogger()) {
  452. DefaultLogger::get()->error(ex.what());
  453. }
  454. return NULL;
  455. }
  456. if (!object.get()) {
  457. //DOMError("failed to convert element to DOM object, class: " + classtag + ", name: " + name,&element);
  458. }
  459. flags &= ~BEING_CONSTRUCTED;
  460. return object.get();
  461. }
  462. // ------------------------------------------------------------------------------------------------
  463. Object::Object(uint64_t id, const Element& element, const std::string& name)
  464. : element(element)
  465. , name(name)
  466. , id(id)
  467. {
  468. }
  469. // ------------------------------------------------------------------------------------------------
  470. Object::~Object()
  471. {
  472. }
  473. // ------------------------------------------------------------------------------------------------
  474. Document::Document(const Parser& parser, const ImportSettings& settings)
  475. : settings(settings)
  476. , parser(parser)
  477. {
  478. // cannot use array default initialization syntax because vc8 fails on it
  479. for (unsigned int i = 0; i < 7; ++i) {
  480. creationTimeStamp[i] = 0;
  481. }
  482. ReadHeader();
  483. ReadPropertyTemplates();
  484. // this order is important, connections need parsed objects to check
  485. // whether connections are ok or not. Objects may not be evaluated yet,
  486. // though, since this may require valid connections.
  487. ReadObjects();
  488. ReadConnections();
  489. }
  490. // ------------------------------------------------------------------------------------------------
  491. Document::~Document()
  492. {
  493. BOOST_FOREACH(ObjectMap::value_type& v, objects) {
  494. delete v.second;
  495. }
  496. }
  497. // ------------------------------------------------------------------------------------------------
  498. void Document::ReadHeader()
  499. {
  500. // read ID objects from "Objects" section
  501. const Scope& sc = parser.GetRootScope();
  502. const Element* const ehead = sc["FBXHeaderExtension"];
  503. if(!ehead || !ehead->Compound()) {
  504. DOMError("no FBXHeaderExtension dictionary found");
  505. }
  506. const Scope& shead = *ehead->Compound();
  507. fbxVersion = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(shead,"FBXVersion",ehead),0));
  508. if(Settings().strictMode) {
  509. if(fbxVersion < 7200 || fbxVersion > 7300) {
  510. DOMError("unsupported format version, supported are only FBX 2012 and FBX 2013"\
  511. " in ASCII format (turn off strict mode to try anyhow) ");
  512. }
  513. }
  514. const Element* const ecreator = shead["Creator"];
  515. if(ecreator) {
  516. creator = ParseTokenAsString(GetRequiredToken(*ecreator,0));
  517. }
  518. const Element* const etimestamp = shead["CreationTimeStamp"];
  519. if(etimestamp && etimestamp->Compound()) {
  520. const Scope& stimestamp = *etimestamp->Compound();
  521. creationTimeStamp[0] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Year"),0));
  522. creationTimeStamp[1] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Month"),0));
  523. creationTimeStamp[2] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Day"),0));
  524. creationTimeStamp[3] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Hour"),0));
  525. creationTimeStamp[4] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Minute"),0));
  526. creationTimeStamp[5] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Second"),0));
  527. creationTimeStamp[6] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Millisecond"),0));
  528. }
  529. }
  530. // ------------------------------------------------------------------------------------------------
  531. void Document::ReadObjects()
  532. {
  533. // read ID objects from "Objects" section
  534. const Scope& sc = parser.GetRootScope();
  535. const Element* const eobjects = sc["Objects"];
  536. if(!eobjects || !eobjects->Compound()) {
  537. DOMError("no Objects dictionary found");
  538. }
  539. const Scope& sobjects = *eobjects->Compound();
  540. BOOST_FOREACH(const ElementMap::value_type& el, sobjects.Elements()) {
  541. // extract ID
  542. const TokenList& tok = el.second->Tokens();
  543. if (tok.empty()) {
  544. DOMError("expected ID after object key",el.second);
  545. }
  546. const char* err;
  547. const uint64_t id = ParseTokenAsID(*tok[0], err);
  548. if(err) {
  549. DOMError(err,el.second);
  550. }
  551. objects[id] = new LazyObject(id, *el.second, *this);
  552. // grab all animation stacks upfront since there is no listing of them
  553. if(el.first == "AnimationStack") {
  554. animationStacks.push_back(id);
  555. }
  556. }
  557. }
  558. // ------------------------------------------------------------------------------------------------
  559. void Document::ReadPropertyTemplates()
  560. {
  561. const Scope& sc = parser.GetRootScope();
  562. // read property templates from "Definitions" section
  563. const Element* const edefs = sc["Definitions"];
  564. if(!edefs || !edefs->Compound()) {
  565. DOMWarning("no Definitions dictionary found");
  566. return;
  567. }
  568. const Scope& sdefs = *edefs->Compound();
  569. const ElementCollection otypes = sdefs.GetCollection("ObjectType");
  570. for(ElementMap::const_iterator it = otypes.first; it != otypes.second; ++it) {
  571. const Element& el = *(*it).second;
  572. const Scope* sc = el.Compound();
  573. if(!sc) {
  574. DOMWarning("expected nested scope in ObjectType, ignoring",&el);
  575. continue;
  576. }
  577. const TokenList& tok = el.Tokens();
  578. if(tok.empty()) {
  579. DOMWarning("expected name for ObjectType element, ignoring",&el);
  580. continue;
  581. }
  582. const std::string& oname = ParseTokenAsString(*tok[0]);
  583. const ElementCollection templs = sc->GetCollection("PropertyTemplate");
  584. for(ElementMap::const_iterator it = templs.first; it != templs.second; ++it) {
  585. const Element& el = *(*it).second;
  586. const Scope* sc = el.Compound();
  587. if(!sc) {
  588. DOMWarning("expected nested scope in PropertyTemplate, ignoring",&el);
  589. continue;
  590. }
  591. const TokenList& tok = el.Tokens();
  592. if(tok.empty()) {
  593. DOMWarning("expected name for PropertyTemplate element, ignoring",&el);
  594. continue;
  595. }
  596. const std::string& pname = ParseTokenAsString(*tok[0]);
  597. const Element* Properties70 = (*sc)["Properties70"];
  598. if(Properties70) {
  599. boost::shared_ptr<const PropertyTable> props = boost::make_shared<const PropertyTable>(
  600. *Properties70,boost::shared_ptr<const PropertyTable>(static_cast<const PropertyTable*>(NULL))
  601. );
  602. templates[oname+"."+pname] = props;
  603. }
  604. }
  605. }
  606. }
  607. // ------------------------------------------------------------------------------------------------
  608. void Document::ReadConnections()
  609. {
  610. const Scope& sc = parser.GetRootScope();
  611. // read property templates from "Definitions" section
  612. const Element* const econns = sc["Connections"];
  613. if(!econns || !econns->Compound()) {
  614. DOMError("no Connections dictionary found");
  615. }
  616. uint64_t insertionOrder = 0l;
  617. const Scope& sconns = *econns->Compound();
  618. const ElementCollection conns = sconns.GetCollection("C");
  619. for(ElementMap::const_iterator it = conns.first; it != conns.second; ++it) {
  620. const Element& el = *(*it).second;
  621. const std::string& type = ParseTokenAsString(GetRequiredToken(el,0));
  622. const uint64_t src = ParseTokenAsID(GetRequiredToken(el,1));
  623. const uint64_t dest = ParseTokenAsID(GetRequiredToken(el,2));
  624. // OO = object-object connection
  625. // OP = object-property connection, in which case the destination property follows the object ID
  626. const std::string& prop = (type == "OP" ? ParseTokenAsString(GetRequiredToken(el,3)) : "");
  627. if(objects.find(src) == objects.end()) {
  628. DOMWarning("source object for connection does not exist",&el);
  629. continue;
  630. }
  631. // dest may be 0 (root node)
  632. if(dest && objects.find(dest) == objects.end()) {
  633. DOMWarning("destination object for connection does not exist",&el);
  634. continue;
  635. }
  636. // add new connection
  637. const Connection* const c = new Connection(insertionOrder++,src,dest,prop,*this);
  638. src_connections.insert(ConnectionMap::value_type(src,c));
  639. dest_connections.insert(ConnectionMap::value_type(dest,c));
  640. }
  641. }
  642. // ------------------------------------------------------------------------------------------------
  643. const std::vector<const AnimationStack*>& Document::AnimationStacks() const
  644. {
  645. if (!animationStacksResolved.empty() || !animationStacks.size()) {
  646. return animationStacksResolved;
  647. }
  648. animationStacksResolved.reserve(animationStacks.size());
  649. BOOST_FOREACH(uint64_t id, animationStacks) {
  650. LazyObject* const lazy = GetObject(id);
  651. const AnimationStack* stack;
  652. if(!lazy || !(stack = lazy->Get<AnimationStack>())) {
  653. DOMWarning("failed to read AnimationStack object");
  654. continue;
  655. }
  656. animationStacksResolved.push_back(stack);
  657. }
  658. return animationStacksResolved;
  659. }
  660. // ------------------------------------------------------------------------------------------------
  661. LazyObject* Document::GetObject(uint64_t id) const
  662. {
  663. ObjectMap::const_iterator it = objects.find(id);
  664. return it == objects.end() ? NULL : (*it).second;
  665. }
  666. #define MAX_CLASSNAMES 6
  667. // ------------------------------------------------------------------------------------------------
  668. std::vector<const Connection*> Document::GetConnectionsSequenced(uint64_t id, const ConnectionMap& conns) const
  669. {
  670. std::vector<const Connection*> temp;
  671. const std::pair<ConnectionMap::const_iterator,ConnectionMap::const_iterator> range =
  672. conns.equal_range(id);
  673. temp.reserve(std::distance(range.first,range.second));
  674. for (ConnectionMap::const_iterator it = range.first; it != range.second; ++it) {
  675. temp.push_back((*it).second);
  676. }
  677. std::sort(temp.begin(), temp.end(), std::mem_fun(&Connection::Compare));
  678. return temp; // NRVO should handle this
  679. }
  680. // ------------------------------------------------------------------------------------------------
  681. std::vector<const Connection*> Document::GetConnectionsSequenced(uint64_t id, bool is_src, const ConnectionMap& conns, const char* const* classnames, size_t count) const
  682. {
  683. ai_assert(classnames);
  684. ai_assert(count != 0 && count <= MAX_CLASSNAMES);
  685. size_t lenghts[MAX_CLASSNAMES];
  686. const size_t c = count;
  687. for (size_t i = 0; i < c; ++i) {
  688. lenghts[i] = strlen(classnames[i]);
  689. }
  690. std::vector<const Connection*> temp;
  691. const std::pair<ConnectionMap::const_iterator,ConnectionMap::const_iterator> range =
  692. conns.equal_range(id);
  693. temp.reserve(std::distance(range.first,range.second));
  694. for (ConnectionMap::const_iterator it = range.first; it != range.second; ++it) {
  695. const Token& key = (is_src
  696. ? (*it).second->LazyDestinationObject()
  697. : (*it).second->LazySourceObject()
  698. ).GetElement().KeyToken();
  699. const char* obtype = key.begin();
  700. for (size_t i = 0; i < c; ++i) {
  701. ai_assert(classnames[i]);
  702. if(std::distance(key.begin(),key.end()) == lenghts[i] && !strncmp(classnames[i],obtype,lenghts[i])) {
  703. obtype = NULL;
  704. break;
  705. }
  706. }
  707. if(obtype) {
  708. continue;
  709. }
  710. temp.push_back((*it).second);
  711. }
  712. std::sort(temp.begin(), temp.end(), std::mem_fun(&Connection::Compare));
  713. return temp; // NRVO should handle this
  714. }
  715. // ------------------------------------------------------------------------------------------------
  716. std::vector<const Connection*> Document::GetConnectionsBySourceSequenced(uint64_t source) const
  717. {
  718. return GetConnectionsSequenced(source, ConnectionsBySource());
  719. }
  720. // ------------------------------------------------------------------------------------------------
  721. std::vector<const Connection*> Document::GetConnectionsBySourceSequenced(uint64_t dest, const char* classname) const
  722. {
  723. const char* arr[] = {classname};
  724. return GetConnectionsBySourceSequenced(dest, arr,1);
  725. }
  726. // ------------------------------------------------------------------------------------------------
  727. std::vector<const Connection*> Document::GetConnectionsBySourceSequenced(uint64_t source, const char* const* classnames, size_t count) const
  728. {
  729. return GetConnectionsSequenced(source, true, ConnectionsBySource(),classnames, count);
  730. }
  731. // ------------------------------------------------------------------------------------------------
  732. std::vector<const Connection*> Document::GetConnectionsByDestinationSequenced(uint64_t dest, const char* classname) const
  733. {
  734. const char* arr[] = {classname};
  735. return GetConnectionsByDestinationSequenced(dest, arr,1);
  736. }
  737. // ------------------------------------------------------------------------------------------------
  738. std::vector<const Connection*> Document::GetConnectionsByDestinationSequenced(uint64_t dest) const
  739. {
  740. return GetConnectionsSequenced(dest, ConnectionsByDestination());
  741. }
  742. // ------------------------------------------------------------------------------------------------
  743. std::vector<const Connection*> Document::GetConnectionsByDestinationSequenced(uint64_t dest, const char* const* classnames, size_t count) const
  744. {
  745. return GetConnectionsSequenced(dest, false, ConnectionsByDestination(),classnames, count);
  746. }
  747. // ------------------------------------------------------------------------------------------------
  748. Connection::Connection(uint64_t insertionOrder, uint64_t src, uint64_t dest, const std::string& prop, const Document& doc)
  749. : insertionOrder(insertionOrder)
  750. , prop(prop)
  751. , src(src)
  752. , dest(dest)
  753. , doc(doc)
  754. {
  755. ai_assert(doc.Objects().find(src) != doc.Objects().end());
  756. // dest may be 0 (root node)
  757. ai_assert(!dest || doc.Objects().find(dest) != doc.Objects().end());
  758. }
  759. // ------------------------------------------------------------------------------------------------
  760. Connection::~Connection()
  761. {
  762. }
  763. // ------------------------------------------------------------------------------------------------
  764. LazyObject& Connection::LazySourceObject() const
  765. {
  766. LazyObject* const lazy = doc.GetObject(src);
  767. ai_assert(lazy);
  768. return *lazy;
  769. }
  770. // ------------------------------------------------------------------------------------------------
  771. LazyObject& Connection::LazyDestinationObject() const
  772. {
  773. LazyObject* const lazy = doc.GetObject(dest);
  774. ai_assert(lazy);
  775. return *lazy;
  776. }
  777. // ------------------------------------------------------------------------------------------------
  778. const Object* Connection::SourceObject() const
  779. {
  780. LazyObject* const lazy = doc.GetObject(src);
  781. ai_assert(lazy);
  782. return lazy->Get();
  783. }
  784. // ------------------------------------------------------------------------------------------------
  785. const Object* Connection::DestinationObject() const
  786. {
  787. LazyObject* const lazy = doc.GetObject(dest);
  788. ai_assert(lazy);
  789. return lazy->Get();
  790. }
  791. } // !FBX
  792. } // !Assimp
  793. #endif