FBXDocument.cpp 25 KB

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