FBXDocument.cpp 25 KB

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