FBXDocument.cpp 25 KB

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