FBXDocument.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2019, 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. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  37. #include "FBXDocument.h"
  38. #include "FBXMeshGeometry.h"
  39. #include "FBXParser.h"
  40. #include "FBXUtil.h"
  41. #include "FBXImporter.h"
  42. #include "FBXImportSettings.h"
  43. #include "FBXDocumentUtil.h"
  44. #include "FBXProperties.h"
  45. #include <memory>
  46. #include <functional>
  47. #include <map>
  48. namespace Assimp {
  49. namespace FBX {
  50. using namespace Util;
  51. // ------------------------------------------------------------------------------------------------
  52. LazyObject::LazyObject(uint64_t id, const Element& element, const Document& doc)
  53. : doc(doc)
  54. , element(element)
  55. , id(id)
  56. , flags() {
  57. // empty
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. LazyObject::~LazyObject()
  61. {
  62. // empty
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. const Object* LazyObject::Get(bool dieOnError)
  66. {
  67. if(IsBeingConstructed() || FailedToConstruct()) {
  68. return nullptr;
  69. }
  70. if (object.get()) {
  71. return object.get();
  72. }
  73. const Token& key = element.KeyToken();
  74. const TokenList& tokens = element.Tokens();
  75. if(tokens.size() < 3) {
  76. DOMError("expected at least 3 tokens: id, name and class tag",&element);
  77. }
  78. const char* err;
  79. std::string name = ParseTokenAsString(*tokens[1],err);
  80. if (err) {
  81. DOMError(err,&element);
  82. }
  83. // small fix for binary reading: binary fbx files don't use
  84. // prefixes such as Model:: in front of their names. The
  85. // loading code expects this at many places, though!
  86. // so convert the binary representation (a 0x0001) to the
  87. // double colon notation.
  88. if(tokens[1]->IsBinary()) {
  89. for (size_t i = 0; i < name.length(); ++i) {
  90. if (name[i] == 0x0 && name[i+1] == 0x1) {
  91. name = name.substr(i+2) + "::" + name.substr(0,i);
  92. }
  93. }
  94. }
  95. const std::string classtag = ParseTokenAsString(*tokens[2],err);
  96. if (err) {
  97. DOMError(err,&element);
  98. }
  99. // prevent recursive calls
  100. flags |= BEING_CONSTRUCTED;
  101. try {
  102. // this needs to be relatively fast since it happens a lot,
  103. // so avoid constructing strings all the time.
  104. const char* obtype = key.begin();
  105. const size_t length = static_cast<size_t>(key.end()-key.begin());
  106. // For debugging
  107. //dumpObjectClassInfo( objtype, classtag );
  108. if (!strncmp(obtype,"Geometry",length)) {
  109. if (!strcmp(classtag.c_str(),"Mesh")) {
  110. object.reset(new MeshGeometry(id,element,name,doc));
  111. }
  112. if (!strcmp(classtag.c_str(), "Shape")) {
  113. object.reset(new ShapeGeometry(id, element, name, doc));
  114. }
  115. if (!strcmp(classtag.c_str(), "Line")) {
  116. object.reset(new LineGeometry(id, element, name, doc));
  117. }
  118. }
  119. else if (!strncmp(obtype,"NodeAttribute",length)) {
  120. if (!strcmp(classtag.c_str(),"Camera")) {
  121. object.reset(new Camera(id,element,doc,name));
  122. }
  123. else if (!strcmp(classtag.c_str(),"CameraSwitcher")) {
  124. object.reset(new CameraSwitcher(id,element,doc,name));
  125. }
  126. else if (!strcmp(classtag.c_str(),"Light")) {
  127. object.reset(new Light(id,element,doc,name));
  128. }
  129. else if (!strcmp(classtag.c_str(),"Null")) {
  130. object.reset(new Null(id,element,doc,name));
  131. }
  132. else if (!strcmp(classtag.c_str(),"LimbNode")) {
  133. object.reset(new LimbNode(id,element,doc,name));
  134. }
  135. }
  136. else if (!strncmp(obtype,"Deformer",length)) {
  137. if (!strcmp(classtag.c_str(),"Cluster")) {
  138. object.reset(new Cluster(id,element,doc,name));
  139. }
  140. else if (!strcmp(classtag.c_str(),"Skin")) {
  141. object.reset(new Skin(id,element,doc,name));
  142. }
  143. else if (!strcmp(classtag.c_str(), "BlendShape")) {
  144. object.reset(new BlendShape(id, element, doc, name));
  145. }
  146. else if (!strcmp(classtag.c_str(), "BlendShapeChannel")) {
  147. object.reset(new BlendShapeChannel(id, element, doc, name));
  148. }
  149. }
  150. else if ( !strncmp( obtype, "Model", length ) ) {
  151. // FK and IK effectors are not supported
  152. if ( strcmp( classtag.c_str(), "IKEffector" ) && strcmp( classtag.c_str(), "FKEffector" ) ) {
  153. object.reset( new Model( id, element, doc, name ) );
  154. }
  155. }
  156. else if (!strncmp(obtype,"Material",length)) {
  157. object.reset(new Material(id,element,doc,name));
  158. }
  159. else if (!strncmp(obtype,"Texture",length)) {
  160. object.reset(new Texture(id,element,doc,name));
  161. }
  162. else if (!strncmp(obtype,"LayeredTexture",length)) {
  163. object.reset(new LayeredTexture(id,element,doc,name));
  164. }
  165. else if (!strncmp(obtype,"Video",length)) {
  166. object.reset(new Video(id,element,doc,name));
  167. }
  168. else if (!strncmp(obtype,"AnimationStack",length)) {
  169. object.reset(new AnimationStack(id,element,name,doc));
  170. }
  171. else if (!strncmp(obtype,"AnimationLayer",length)) {
  172. object.reset(new AnimationLayer(id,element,name,doc));
  173. }
  174. // note: order matters for these two
  175. else if (!strncmp(obtype,"AnimationCurve",length)) {
  176. object.reset(new AnimationCurve(id,element,name,doc));
  177. }
  178. else if (!strncmp(obtype,"AnimationCurveNode",length)) {
  179. object.reset(new AnimationCurveNode(id,element,name,doc));
  180. }
  181. }
  182. catch(std::exception& ex) {
  183. flags &= ~BEING_CONSTRUCTED;
  184. flags |= FAILED_TO_CONSTRUCT;
  185. if(dieOnError || doc.Settings().strictMode) {
  186. throw;
  187. }
  188. // note: the error message is already formatted, so raw logging is ok
  189. if(!DefaultLogger::isNullLogger()) {
  190. ASSIMP_LOG_ERROR(ex.what());
  191. }
  192. return NULL;
  193. }
  194. if (!object.get()) {
  195. //DOMError("failed to convert element to DOM object, class: " + classtag + ", name: " + name,&element);
  196. }
  197. flags &= ~BEING_CONSTRUCTED;
  198. return object.get();
  199. }
  200. // ------------------------------------------------------------------------------------------------
  201. Object::Object(uint64_t id, const Element& element, const std::string& name)
  202. : element(element)
  203. , name(name)
  204. , id(id)
  205. {
  206. // empty
  207. }
  208. // ------------------------------------------------------------------------------------------------
  209. Object::~Object()
  210. {
  211. // empty
  212. }
  213. // ------------------------------------------------------------------------------------------------
  214. FileGlobalSettings::FileGlobalSettings(const Document& doc, std::shared_ptr<const PropertyTable> props)
  215. : props(props)
  216. , doc(doc)
  217. {
  218. // empty
  219. }
  220. // ------------------------------------------------------------------------------------------------
  221. FileGlobalSettings::~FileGlobalSettings()
  222. {
  223. // empty
  224. }
  225. // ------------------------------------------------------------------------------------------------
  226. Document::Document(const Parser& parser, const ImportSettings& settings)
  227. : settings(settings)
  228. , parser(parser)
  229. {
  230. // Cannot use array default initialization syntax because vc8 fails on it
  231. for (auto &timeStamp : creationTimeStamp) {
  232. timeStamp = 0;
  233. }
  234. ReadHeader();
  235. ReadPropertyTemplates();
  236. ReadGlobalSettings();
  237. // This order is important, connections need parsed objects to check
  238. // whether connections are ok or not. Objects may not be evaluated yet,
  239. // though, since this may require valid connections.
  240. ReadObjects();
  241. ReadConnections();
  242. }
  243. // ------------------------------------------------------------------------------------------------
  244. Document::~Document()
  245. {
  246. for(ObjectMap::value_type& v : objects) {
  247. delete v.second;
  248. }
  249. for(ConnectionMap::value_type& v : src_connections) {
  250. delete v.second;
  251. }
  252. // |dest_connections| contain the same Connection objects as the |src_connections|
  253. }
  254. // ------------------------------------------------------------------------------------------------
  255. static const unsigned int LowerSupportedVersion = 7100;
  256. static const unsigned int UpperSupportedVersion = 7400;
  257. void Document::ReadHeader() {
  258. // Read ID objects from "Objects" section
  259. const Scope& sc = parser.GetRootScope();
  260. const Element* const ehead = sc["FBXHeaderExtension"];
  261. if(!ehead || !ehead->Compound()) {
  262. DOMError("no FBXHeaderExtension dictionary found");
  263. }
  264. const Scope& shead = *ehead->Compound();
  265. fbxVersion = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(shead,"FBXVersion",ehead),0));
  266. // While we may have some success with newer files, we don't support
  267. // the older 6.n fbx format
  268. if(fbxVersion < LowerSupportedVersion ) {
  269. DOMError("unsupported, old format version, supported are only FBX 2011, FBX 2012 and FBX 2013");
  270. }
  271. if(fbxVersion > UpperSupportedVersion ) {
  272. if(Settings().strictMode) {
  273. DOMError("unsupported, newer format version, supported are only FBX 2011, FBX 2012 and FBX 2013"
  274. " (turn off strict mode to try anyhow) ");
  275. }
  276. else {
  277. DOMWarning("unsupported, newer format version, supported are only FBX 2011, FBX 2012 and FBX 2013,"
  278. " trying to read it nevertheless");
  279. }
  280. }
  281. const Element* const ecreator = shead["Creator"];
  282. if(ecreator) {
  283. creator = ParseTokenAsString(GetRequiredToken(*ecreator,0));
  284. }
  285. const Element* const etimestamp = shead["CreationTimeStamp"];
  286. if(etimestamp && etimestamp->Compound()) {
  287. const Scope& stimestamp = *etimestamp->Compound();
  288. creationTimeStamp[0] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Year"),0));
  289. creationTimeStamp[1] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Month"),0));
  290. creationTimeStamp[2] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Day"),0));
  291. creationTimeStamp[3] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Hour"),0));
  292. creationTimeStamp[4] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Minute"),0));
  293. creationTimeStamp[5] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Second"),0));
  294. creationTimeStamp[6] = ParseTokenAsInt(GetRequiredToken(GetRequiredElement(stimestamp,"Millisecond"),0));
  295. }
  296. }
  297. // ------------------------------------------------------------------------------------------------
  298. void Document::ReadGlobalSettings()
  299. {
  300. const Scope& sc = parser.GetRootScope();
  301. const Element* const ehead = sc["GlobalSettings"];
  302. if ( nullptr == ehead || !ehead->Compound() ) {
  303. DOMWarning( "no GlobalSettings dictionary found" );
  304. globals.reset(new FileGlobalSettings(*this, std::make_shared<const PropertyTable>()));
  305. return;
  306. }
  307. std::shared_ptr<const PropertyTable> props = GetPropertyTable( *this, "", *ehead, *ehead->Compound(), true );
  308. //double v = PropertyGet<float>( *props.get(), std::string("UnitScaleFactor"), 1.0 );
  309. if(!props) {
  310. DOMError("GlobalSettings dictionary contains no property table");
  311. }
  312. globals.reset(new FileGlobalSettings(*this, props));
  313. }
  314. // ------------------------------------------------------------------------------------------------
  315. void Document::ReadObjects()
  316. {
  317. // read ID objects from "Objects" section
  318. const Scope& sc = parser.GetRootScope();
  319. const Element* const eobjects = sc["Objects"];
  320. if(!eobjects || !eobjects->Compound()) {
  321. DOMError("no Objects dictionary found");
  322. }
  323. // add a dummy entry to represent the Model::RootNode object (id 0),
  324. // which is only indirectly defined in the input file
  325. objects[0] = new LazyObject(0L, *eobjects, *this);
  326. const Scope& sobjects = *eobjects->Compound();
  327. for(const ElementMap::value_type& el : sobjects.Elements()) {
  328. // extract ID
  329. const TokenList& tok = el.second->Tokens();
  330. if (tok.empty()) {
  331. DOMError("expected ID after object key",el.second);
  332. }
  333. const char* err;
  334. const uint64_t id = ParseTokenAsID(*tok[0], err);
  335. if(err) {
  336. DOMError(err,el.second);
  337. }
  338. // id=0 is normally implicit
  339. if(id == 0L) {
  340. DOMError("encountered object with implicitly defined id 0",el.second);
  341. }
  342. if(objects.find(id) != objects.end()) {
  343. DOMWarning("encountered duplicate object id, ignoring first occurrence",el.second);
  344. }
  345. objects[id] = new LazyObject(id, *el.second, *this);
  346. // grab all animation stacks upfront since there is no listing of them
  347. if(!strcmp(el.first.c_str(),"AnimationStack")) {
  348. animationStacks.push_back(id);
  349. }
  350. }
  351. }
  352. // ------------------------------------------------------------------------------------------------
  353. void Document::ReadPropertyTemplates()
  354. {
  355. const Scope& sc = parser.GetRootScope();
  356. // read property templates from "Definitions" section
  357. const Element* const edefs = sc["Definitions"];
  358. if(!edefs || !edefs->Compound()) {
  359. DOMWarning("no Definitions dictionary found");
  360. return;
  361. }
  362. const Scope& sdefs = *edefs->Compound();
  363. const ElementCollection otypes = sdefs.GetCollection("ObjectType");
  364. for(ElementMap::const_iterator it = otypes.first; it != otypes.second; ++it) {
  365. const Element& el = *(*it).second;
  366. const Scope* sc = el.Compound();
  367. if(!sc) {
  368. DOMWarning("expected nested scope in ObjectType, ignoring",&el);
  369. continue;
  370. }
  371. const TokenList& tok = el.Tokens();
  372. if(tok.empty()) {
  373. DOMWarning("expected name for ObjectType element, ignoring",&el);
  374. continue;
  375. }
  376. const std::string& oname = ParseTokenAsString(*tok[0]);
  377. const ElementCollection templs = sc->GetCollection("PropertyTemplate");
  378. for(ElementMap::const_iterator it = templs.first; it != templs.second; ++it) {
  379. const Element& el = *(*it).second;
  380. const Scope* sc = el.Compound();
  381. if(!sc) {
  382. DOMWarning("expected nested scope in PropertyTemplate, ignoring",&el);
  383. continue;
  384. }
  385. const TokenList& tok = el.Tokens();
  386. if(tok.empty()) {
  387. DOMWarning("expected name for PropertyTemplate element, ignoring",&el);
  388. continue;
  389. }
  390. const std::string& pname = ParseTokenAsString(*tok[0]);
  391. const Element* Properties70 = (*sc)["Properties70"];
  392. if(Properties70) {
  393. std::shared_ptr<const PropertyTable> props = std::make_shared<const PropertyTable>(
  394. *Properties70,std::shared_ptr<const PropertyTable>(static_cast<const PropertyTable*>(NULL))
  395. );
  396. templates[oname+"."+pname] = props;
  397. }
  398. }
  399. }
  400. }
  401. // ------------------------------------------------------------------------------------------------
  402. void Document::ReadConnections()
  403. {
  404. const Scope& sc = parser.GetRootScope();
  405. // read property templates from "Definitions" section
  406. const Element* const econns = sc["Connections"];
  407. if(!econns || !econns->Compound()) {
  408. DOMError("no Connections dictionary found");
  409. }
  410. uint64_t insertionOrder = 0l;
  411. const Scope& sconns = *econns->Compound();
  412. const ElementCollection conns = sconns.GetCollection("C");
  413. for(ElementMap::const_iterator it = conns.first; it != conns.second; ++it) {
  414. const Element& el = *(*it).second;
  415. const std::string& type = ParseTokenAsString(GetRequiredToken(el,0));
  416. // PP = property-property connection, ignored for now
  417. // (tokens: "PP", ID1, "Property1", ID2, "Property2")
  418. if ( type == "PP" ) {
  419. continue;
  420. }
  421. const uint64_t src = ParseTokenAsID(GetRequiredToken(el,1));
  422. const uint64_t dest = ParseTokenAsID(GetRequiredToken(el,2));
  423. // OO = object-object connection
  424. // OP = object-property connection, in which case the destination property follows the object ID
  425. const std::string& prop = (type == "OP" ? ParseTokenAsString(GetRequiredToken(el,3)) : "");
  426. if(objects.find(src) == objects.end()) {
  427. DOMWarning("source object for connection does not exist",&el);
  428. continue;
  429. }
  430. // dest may be 0 (root node) but we added a dummy object before
  431. if(objects.find(dest) == objects.end()) {
  432. DOMWarning("destination object for connection does not exist",&el);
  433. continue;
  434. }
  435. // add new connection
  436. const Connection* const c = new Connection(insertionOrder++,src,dest,prop,*this);
  437. src_connections.insert(ConnectionMap::value_type(src,c));
  438. dest_connections.insert(ConnectionMap::value_type(dest,c));
  439. }
  440. }
  441. // ------------------------------------------------------------------------------------------------
  442. const std::vector<const AnimationStack*>& Document::AnimationStacks() const
  443. {
  444. if (!animationStacksResolved.empty() || animationStacks.empty()) {
  445. return animationStacksResolved;
  446. }
  447. animationStacksResolved.reserve(animationStacks.size());
  448. for(uint64_t id : animationStacks) {
  449. LazyObject* const lazy = GetObject(id);
  450. const AnimationStack* stack;
  451. if(!lazy || !(stack = lazy->Get<AnimationStack>())) {
  452. DOMWarning("failed to read AnimationStack object");
  453. continue;
  454. }
  455. animationStacksResolved.push_back(stack);
  456. }
  457. return animationStacksResolved;
  458. }
  459. // ------------------------------------------------------------------------------------------------
  460. LazyObject* Document::GetObject(uint64_t id) const
  461. {
  462. ObjectMap::const_iterator it = objects.find(id);
  463. return it == objects.end() ? nullptr : (*it).second;
  464. }
  465. #define MAX_CLASSNAMES 6
  466. // ------------------------------------------------------------------------------------------------
  467. std::vector<const Connection*> Document::GetConnectionsSequenced(uint64_t id, const ConnectionMap& conns) const
  468. {
  469. std::vector<const Connection*> temp;
  470. const std::pair<ConnectionMap::const_iterator,ConnectionMap::const_iterator> range =
  471. conns.equal_range(id);
  472. temp.reserve(std::distance(range.first,range.second));
  473. for (ConnectionMap::const_iterator it = range.first; it != range.second; ++it) {
  474. temp.push_back((*it).second);
  475. }
  476. std::sort(temp.begin(), temp.end(), std::mem_fn(&Connection::Compare));
  477. return temp; // NRVO should handle this
  478. }
  479. // ------------------------------------------------------------------------------------------------
  480. std::vector<const Connection*> Document::GetConnectionsSequenced(uint64_t id, bool is_src,
  481. const ConnectionMap& conns,
  482. const char* const* classnames,
  483. size_t count) const
  484. {
  485. ai_assert(classnames);
  486. ai_assert( count != 0 );
  487. ai_assert( count <= MAX_CLASSNAMES);
  488. size_t lengths[MAX_CLASSNAMES];
  489. const size_t c = count;
  490. for (size_t i = 0; i < c; ++i) {
  491. lengths[ i ] = strlen(classnames[i]);
  492. }
  493. std::vector<const Connection*> temp;
  494. const std::pair<ConnectionMap::const_iterator,ConnectionMap::const_iterator> range =
  495. conns.equal_range(id);
  496. temp.reserve(std::distance(range.first,range.second));
  497. for (ConnectionMap::const_iterator it = range.first; it != range.second; ++it) {
  498. const Token& key = (is_src
  499. ? (*it).second->LazyDestinationObject()
  500. : (*it).second->LazySourceObject()
  501. ).GetElement().KeyToken();
  502. const char* obtype = key.begin();
  503. for (size_t i = 0; i < c; ++i) {
  504. ai_assert(classnames[i]);
  505. if(static_cast<size_t>(std::distance(key.begin(),key.end())) == lengths[i] && !strncmp(classnames[i],obtype,lengths[i])) {
  506. obtype = nullptr;
  507. break;
  508. }
  509. }
  510. if(obtype) {
  511. continue;
  512. }
  513. temp.push_back((*it).second);
  514. }
  515. std::sort(temp.begin(), temp.end(), std::mem_fn(&Connection::Compare));
  516. return temp; // NRVO should handle this
  517. }
  518. // ------------------------------------------------------------------------------------------------
  519. std::vector<const Connection*> Document::GetConnectionsBySourceSequenced(uint64_t source) const
  520. {
  521. return GetConnectionsSequenced(source, ConnectionsBySource());
  522. }
  523. // ------------------------------------------------------------------------------------------------
  524. std::vector<const Connection*> Document::GetConnectionsBySourceSequenced(uint64_t src, const char* classname) const
  525. {
  526. const char* arr[] = {classname};
  527. return GetConnectionsBySourceSequenced(src, arr,1);
  528. }
  529. // ------------------------------------------------------------------------------------------------
  530. std::vector<const Connection*> Document::GetConnectionsBySourceSequenced(uint64_t source,
  531. const char* const* classnames, size_t count) const
  532. {
  533. return GetConnectionsSequenced(source, true, ConnectionsBySource(),classnames, count);
  534. }
  535. // ------------------------------------------------------------------------------------------------
  536. std::vector<const Connection*> Document::GetConnectionsByDestinationSequenced(uint64_t dest,
  537. const char* classname) const
  538. {
  539. const char* arr[] = {classname};
  540. return GetConnectionsByDestinationSequenced(dest, arr,1);
  541. }
  542. // ------------------------------------------------------------------------------------------------
  543. std::vector<const Connection*> Document::GetConnectionsByDestinationSequenced(uint64_t dest) const
  544. {
  545. return GetConnectionsSequenced(dest, ConnectionsByDestination());
  546. }
  547. // ------------------------------------------------------------------------------------------------
  548. std::vector<const Connection*> Document::GetConnectionsByDestinationSequenced(uint64_t dest,
  549. const char* const* classnames, size_t count) const
  550. {
  551. return GetConnectionsSequenced(dest, false, ConnectionsByDestination(),classnames, count);
  552. }
  553. // ------------------------------------------------------------------------------------------------
  554. Connection::Connection(uint64_t insertionOrder, uint64_t src, uint64_t dest, const std::string& prop,
  555. const Document& doc)
  556. : insertionOrder(insertionOrder)
  557. , prop(prop)
  558. , src(src)
  559. , dest(dest)
  560. , doc(doc)
  561. {
  562. ai_assert(doc.Objects().find(src) != doc.Objects().end());
  563. // dest may be 0 (root node)
  564. ai_assert(!dest || doc.Objects().find(dest) != doc.Objects().end());
  565. }
  566. // ------------------------------------------------------------------------------------------------
  567. Connection::~Connection()
  568. {
  569. // empty
  570. }
  571. // ------------------------------------------------------------------------------------------------
  572. LazyObject& Connection::LazySourceObject() const
  573. {
  574. LazyObject* const lazy = doc.GetObject(src);
  575. ai_assert(lazy);
  576. return *lazy;
  577. }
  578. // ------------------------------------------------------------------------------------------------
  579. LazyObject& Connection::LazyDestinationObject() const
  580. {
  581. LazyObject* const lazy = doc.GetObject(dest);
  582. ai_assert(lazy);
  583. return *lazy;
  584. }
  585. // ------------------------------------------------------------------------------------------------
  586. const Object* Connection::SourceObject() const
  587. {
  588. LazyObject* const lazy = doc.GetObject(src);
  589. ai_assert(lazy);
  590. return lazy->Get();
  591. }
  592. // ------------------------------------------------------------------------------------------------
  593. const Object* Connection::DestinationObject() const
  594. {
  595. LazyObject* const lazy = doc.GetObject(dest);
  596. ai_assert(lazy);
  597. return lazy->Get();
  598. }
  599. } // !FBX
  600. } // !Assimp
  601. #endif