FBXDocument.cpp 25 KB

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