FBXDocument.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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.h
  34. * @brief FBX DOM
  35. */
  36. #ifndef INCLUDED_AI_FBX_DOCUMENT_H
  37. #define INCLUDED_AI_FBX_DOCUMENT_H
  38. #include <vector>
  39. #include <map>
  40. #include <string>
  41. namespace Assimp {
  42. namespace FBX {
  43. class Parser;
  44. class Object;
  45. struct ImportSettings;
  46. class PropertyTable;
  47. class Document;
  48. class Material;
  49. class Geometry;
  50. /** Represents a delay-parsed FBX objects. Many objects in the scene
  51. * are not needed by assimp, so it makes no sense to parse them
  52. * upfront. */
  53. class LazyObject
  54. {
  55. public:
  56. LazyObject(uint64_t id, const Element& element, const Document& doc);
  57. ~LazyObject();
  58. public:
  59. const Object* Get(bool dieOnError = false);
  60. template <typename T>
  61. const T* Get(bool dieOnError = false) {
  62. const Object* const ob = Get(dieOnError);
  63. return ob ? dynamic_cast<const T*>(ob) : NULL;
  64. }
  65. uint64_t ID() const {
  66. return id;
  67. }
  68. bool IsBeingConstructed() const {
  69. return (flags & BEING_CONSTRUCTED) != 0;
  70. }
  71. bool FailedToConstruct() const {
  72. return (flags & FAILED_TO_CONSTRUCT) != 0;
  73. }
  74. const Element& GetElement() const {
  75. return element;
  76. }
  77. const Document& GetDocument() const {
  78. return doc;
  79. }
  80. private:
  81. const Document& doc;
  82. const Element& element;
  83. boost::scoped_ptr<const Object> object;
  84. const uint64_t id;
  85. enum Flags {
  86. BEING_CONSTRUCTED = 0x1,
  87. FAILED_TO_CONSTRUCT = 0x2
  88. };
  89. unsigned int flags;
  90. };
  91. /** Base class for in-memory (DOM) representations of FBX objects */
  92. class Object
  93. {
  94. public:
  95. Object(uint64_t id, const Element& element, const std::string& name);
  96. virtual ~Object();
  97. public:
  98. const Element& SourceElement() const {
  99. return element;
  100. }
  101. const std::string& Name() const {
  102. return name;
  103. }
  104. uint64_t ID() const {
  105. return id;
  106. }
  107. protected:
  108. const Element& element;
  109. const std::string name;
  110. const uint64_t id;
  111. };
  112. /** DOM class for generic FBX NoteAttribute blocks. NoteAttribute's just hold a property table,
  113. * fixed members are added by deriving classes. */
  114. class NodeAttribute : public Object
  115. {
  116. public:
  117. NodeAttribute(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  118. ~NodeAttribute();
  119. public:
  120. const PropertyTable& Props() const {
  121. ai_assert(props.get());
  122. return *props.get();
  123. }
  124. private:
  125. boost::shared_ptr<const PropertyTable> props;
  126. };
  127. /** DOM base class for FBX camera settings attached to a node */
  128. class CameraSwitcher : public NodeAttribute
  129. {
  130. public:
  131. CameraSwitcher(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  132. ~CameraSwitcher();
  133. public:
  134. int CameraID() const {
  135. return cameraId;
  136. }
  137. const std::string& CameraName() const {
  138. return cameraName;
  139. }
  140. const std::string& CameraIndexName() const {
  141. return cameraIndexName;
  142. }
  143. private:
  144. int cameraId;
  145. std::string cameraName;
  146. std::string cameraIndexName;
  147. };
  148. /** DOM base class for FBX models (even though its semantics are more "node" than "model" */
  149. class Model : public Object
  150. {
  151. public:
  152. Model(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  153. ~Model();
  154. public:
  155. const std::string& Shading() const {
  156. return shading;
  157. }
  158. const std::string& Culling() const {
  159. return culling;
  160. }
  161. const PropertyTable& Props() const {
  162. ai_assert(props.get());
  163. return *props.get();
  164. }
  165. /** Get material links */
  166. const std::vector<const Material*>& GetMaterials() const {
  167. return materials;
  168. }
  169. /** Get geometry links */
  170. const std::vector<const Geometry*>& GetGeometry() const {
  171. return geometry;
  172. }
  173. /** Get node attachments */
  174. const std::vector<const NodeAttribute*>& GetAttributes() const {
  175. return attributes;
  176. }
  177. private:
  178. void ResolveLinks(const Element& element, const Document& doc);
  179. private:
  180. std::vector<const Material*> materials;
  181. std::vector<const Geometry*> geometry;
  182. std::vector<const NodeAttribute*> attributes;
  183. std::string shading;
  184. std::string culling;
  185. boost::shared_ptr<const PropertyTable> props;
  186. };
  187. /** DOM class for generic FBX textures */
  188. class Texture : public Object
  189. {
  190. public:
  191. Texture(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  192. ~Texture();
  193. public:
  194. const std::string& Type() const {
  195. return type;
  196. }
  197. const std::string& FileName() const {
  198. return fileName;
  199. }
  200. const std::string& RelativeFilename() const {
  201. return relativeFileName;
  202. }
  203. const std::string& AlphaSource() const {
  204. return alphaSource;
  205. }
  206. const aiVector2D& UVTranslation() const {
  207. return uvTrans;
  208. }
  209. const aiVector2D& UVScaling() const {
  210. return uvScaling;
  211. }
  212. const PropertyTable& Props() const {
  213. ai_assert(props.get());
  214. return *props.get();
  215. }
  216. // return a 4-tuple
  217. const unsigned int* Crop() const {
  218. return crop;
  219. }
  220. private:
  221. aiVector2D uvTrans;
  222. aiVector2D uvScaling;
  223. std::string type;
  224. std::string relativeFileName;
  225. std::string fileName;
  226. std::string alphaSource;
  227. boost::shared_ptr<const PropertyTable> props;
  228. unsigned int crop[4];
  229. };
  230. typedef std::fbx_unordered_map<std::string, const Texture*> TextureMap;
  231. /** DOM class for generic FBX materials */
  232. class Material : public Object
  233. {
  234. public:
  235. Material(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  236. ~Material();
  237. public:
  238. const std::string& GetShadingModel() const {
  239. return shading;
  240. }
  241. bool IsMultilayer() const {
  242. return multilayer;
  243. }
  244. const PropertyTable& Props() const {
  245. ai_assert(props.get());
  246. return *props.get();
  247. }
  248. const TextureMap& Textures() const {
  249. return textures;
  250. }
  251. private:
  252. std::string shading;
  253. bool multilayer;
  254. boost::shared_ptr<const PropertyTable> props;
  255. TextureMap textures;
  256. };
  257. /** DOM base class for all kinds of FBX geometry */
  258. class Geometry : public Object
  259. {
  260. public:
  261. Geometry(uint64_t id, const Element& element, const std::string& name);
  262. ~Geometry();
  263. };
  264. /** DOM class for FBX geometry of type "Mesh"*/
  265. class MeshGeometry : public Geometry
  266. {
  267. public:
  268. MeshGeometry(uint64_t id, const Element& element, const std::string& name, const Document& doc);
  269. ~MeshGeometry();
  270. public:
  271. /** Get a list of all vertex points, non-unique*/
  272. const std::vector<aiVector3D>& GetVertices() const {
  273. return vertices;
  274. }
  275. /** Get a list of all vertex normals or an empty array if
  276. * no normals are specified. */
  277. const std::vector<aiVector3D>& GetNormals() const {
  278. return normals;
  279. }
  280. /** Get a list of all vertex tangents or an empty array
  281. * if no tangents are specified */
  282. const std::vector<aiVector3D>& GetTangents() const {
  283. return tangents;
  284. }
  285. /** Get a list of all vertex binormals or an empty array
  286. * if no binormals are specified */
  287. const std::vector<aiVector3D>& GetBinormals() const {
  288. return binormals;
  289. }
  290. /** Return list of faces - each entry denotes a face and specifies
  291. * how many vertices it has. Vertices are taken from the
  292. * vertex data arrays in sequential order. */
  293. const std::vector<unsigned int>& GetFaceIndexCounts() const {
  294. return faces;
  295. }
  296. /** Get a UV coordinate slot, returns an empty array if
  297. * the requested slot does not exist. */
  298. const std::vector<aiVector2D>& GetTextureCoords(unsigned int index) const {
  299. static const std::vector<aiVector2D> empty;
  300. return index >= AI_MAX_NUMBER_OF_TEXTURECOORDS ? empty : uvs[index];
  301. }
  302. /** Get a UV coordinate slot, returns an empty array if
  303. * the requested slot does not exist. */
  304. std::string GetTextureCoordChannelName(unsigned int index) const {
  305. return index >= AI_MAX_NUMBER_OF_TEXTURECOORDS ? "" : uvNames[index];
  306. }
  307. /** Get a vertex color coordinate slot, returns an empty array if
  308. * the requested slot does not exist. */
  309. const std::vector<aiColor4D>& GetVertexColors(unsigned int index) const {
  310. static const std::vector<aiColor4D> empty;
  311. return index >= AI_MAX_NUMBER_OF_COLOR_SETS ? empty : colors[index];
  312. }
  313. /** Get per-face-vertex material assignments */
  314. const std::vector<unsigned int>& GetMaterialIndices() const {
  315. return materials;
  316. }
  317. public:
  318. private:
  319. void ReadLayer(const Scope& layer);
  320. void ReadLayerElement(const Scope& layerElement);
  321. void ReadVertexData(const std::string& type, int index, const Scope& source);
  322. void ReadVertexDataUV(std::vector<aiVector2D>& uv_out, const Scope& source,
  323. const std::string& MappingInformationType,
  324. const std::string& ReferenceInformationType);
  325. void ReadVertexDataNormals(std::vector<aiVector3D>& normals_out, const Scope& source,
  326. const std::string& MappingInformationType,
  327. const std::string& ReferenceInformationType);
  328. void ReadVertexDataColors(std::vector<aiColor4D>& colors_out, const Scope& source,
  329. const std::string& MappingInformationType,
  330. const std::string& ReferenceInformationType);
  331. void ReadVertexDataTangents(std::vector<aiVector3D>& tangents_out, const Scope& source,
  332. const std::string& MappingInformationType,
  333. const std::string& ReferenceInformationType);
  334. void ReadVertexDataBinormals(std::vector<aiVector3D>& binormals_out, const Scope& source,
  335. const std::string& MappingInformationType,
  336. const std::string& ReferenceInformationType);
  337. void ReadVertexDataMaterials(std::vector<unsigned int>& materials_out, const Scope& source,
  338. const std::string& MappingInformationType,
  339. const std::string& ReferenceInformationType);
  340. private:
  341. // cached data arrays
  342. std::vector<unsigned int> materials;
  343. std::vector<aiVector3D> vertices;
  344. std::vector<unsigned int> faces;
  345. std::vector<aiVector3D> tangents;
  346. std::vector<aiVector3D> binormals;
  347. std::vector<aiVector3D> normals;
  348. std::string uvNames[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  349. std::vector<aiVector2D> uvs[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  350. std::vector<aiColor4D> colors[AI_MAX_NUMBER_OF_COLOR_SETS];
  351. std::vector<unsigned int> mapping_counts;
  352. std::vector<unsigned int> mapping_offsets;
  353. std::vector<unsigned int> mappings;
  354. };
  355. typedef std::vector<uint64_t> KeyTimeList;
  356. typedef std::vector<float> KeyValueList;
  357. /** Represents a FBX animation curve (i.e. a 1-dimensional set of keyframes and values therefor) */
  358. class AnimationCurve : public Object
  359. {
  360. public:
  361. AnimationCurve(uint64_t id, const Element& element, const std::string& name, const Document& doc);
  362. ~AnimationCurve();
  363. public:
  364. /** get list of keyframe positions (time).
  365. * Invariant: |GetKeys()| > 0 */
  366. const KeyTimeList& GetKeys() const {
  367. return keys;
  368. }
  369. /** get list of keyframe values.
  370. * Invariant: |GetKeys()| == |GetValues()| && |GetKeys()| > 0*/
  371. const KeyValueList& GetValues() const {
  372. return values;
  373. }
  374. const std::vector<float>& GetAttributes() const {
  375. return attributes;
  376. }
  377. const std::vector<unsigned int>& GetFlags() const {
  378. return flags;
  379. }
  380. private:
  381. KeyTimeList keys;
  382. KeyValueList values;
  383. std::vector<float> attributes;
  384. std::vector<unsigned int> flags;
  385. };
  386. // property-name -> animation curve
  387. typedef std::map<std::string, const AnimationCurve*> AnimationCurveMap;
  388. /** Represents a FBX animation curve (i.e. a mapping from single animation curves to nodes) */
  389. class AnimationCurveNode : public Object
  390. {
  391. public:
  392. AnimationCurveNode(uint64_t id, const Element& element, const std::string& name, const Document& doc);
  393. ~AnimationCurveNode();
  394. public:
  395. const PropertyTable& Props() const {
  396. ai_assert(props.get());
  397. return *props.get();
  398. }
  399. const AnimationCurveMap Curves() const {
  400. return curves;
  401. }
  402. /** Model or NodeAttribute the curve is assigned to, this is always non-NULL
  403. * and never an objects not deriving one of the two aforementioned classes.*/
  404. const Object* Target() const {
  405. return target;
  406. }
  407. const Model* TargetAsModel() const {
  408. return dynamic_cast<const Model*>(target);
  409. }
  410. const NodeAttribute* TargetAsNodeAttribute() const {
  411. return dynamic_cast<const NodeAttribute*>(target);
  412. }
  413. /** Property of Target() that is being animated*/
  414. const std::string& TargetProperty() const {
  415. return prop;
  416. }
  417. private:
  418. const Object* target;
  419. boost::shared_ptr<const PropertyTable> props;
  420. AnimationCurveMap curves;
  421. std::string prop;
  422. };
  423. typedef std::vector<const AnimationCurveNode*> AnimationCurveNodeList;
  424. /** Represents a FBX animation layer (i.e. a list of node animations) */
  425. class AnimationLayer : public Object
  426. {
  427. public:
  428. AnimationLayer(uint64_t id, const Element& element, const std::string& name, const Document& doc);
  429. ~AnimationLayer();
  430. public:
  431. const PropertyTable& Props() const {
  432. ai_assert(props.get());
  433. return *props.get();
  434. }
  435. const AnimationCurveNodeList& Nodes() const {
  436. return nodes;
  437. }
  438. private:
  439. boost::shared_ptr<const PropertyTable> props;
  440. AnimationCurveNodeList nodes;
  441. };
  442. typedef std::vector<const AnimationLayer*> AnimationLayerList;
  443. /** Represents a FBX animation stack (i.e. a list of animation layers) */
  444. class AnimationStack : public Object
  445. {
  446. public:
  447. AnimationStack(uint64_t id, const Element& element, const std::string& name, const Document& doc);
  448. ~AnimationStack();
  449. public:
  450. const PropertyTable& Props() const {
  451. ai_assert(props.get());
  452. return *props.get();
  453. }
  454. const AnimationLayerList& Layers() const {
  455. return layers;
  456. }
  457. private:
  458. boost::shared_ptr<const PropertyTable> props;
  459. AnimationLayerList layers;
  460. };
  461. /** Represents a link between two FBX objects. */
  462. class Connection
  463. {
  464. public:
  465. Connection(uint64_t insertionOrder, uint64_t src, uint64_t dest, const std::string& prop, const Document& doc);
  466. ~Connection();
  467. // note: a connection ensures that the source and dest objects exist, but
  468. // not that they have DOM representations, so the return value of one of
  469. // these functions can still be NULL.
  470. const Object* SourceObject() const;
  471. const Object* DestinationObject() const;
  472. // these, however, are always guaranteed to be valid
  473. LazyObject& LazySourceObject() const;
  474. LazyObject& LazyDestinationObject() const;
  475. /** return the name of the property the connection is attached to.
  476. * this is an empty string for object to object (OO) connections. */
  477. const std::string& PropertyName() const {
  478. return prop;
  479. }
  480. uint64_t InsertionOrder() const {
  481. return insertionOrder;
  482. }
  483. int CompareTo(const Connection* c) const {
  484. // note: can't subtract because this would overflow uint64_t
  485. if(InsertionOrder() > c->InsertionOrder()) {
  486. return 1;
  487. }
  488. else if(InsertionOrder() < c->InsertionOrder()) {
  489. return -1;
  490. }
  491. return 0;
  492. }
  493. bool Compare(const Connection* c) const {
  494. return InsertionOrder() < c->InsertionOrder();
  495. }
  496. public:
  497. uint64_t insertionOrder;
  498. const std::string prop;
  499. uint64_t src, dest;
  500. const Document& doc;
  501. };
  502. // XXX again, unique_ptr would be useful. shared_ptr is too
  503. // bloated since the objects have a well-defined single owner
  504. // during their entire lifetime (Document). FBX files have
  505. // up to many thousands of objects (most of which we never use),
  506. // so the memory overhead for them should be kept at a minimum.
  507. typedef std::map<uint64_t, LazyObject*> ObjectMap;
  508. typedef std::fbx_unordered_map<std::string, boost::shared_ptr<const PropertyTable> > PropertyTemplateMap;
  509. typedef std::multimap<uint64_t, const Connection*> ConnectionMap;
  510. /** DOM root for a FBX file */
  511. class Document
  512. {
  513. public:
  514. Document(const Parser& parser, const ImportSettings& settings);
  515. ~Document();
  516. public:
  517. LazyObject* GetObject(uint64_t id) const;
  518. unsigned int FBXVersion() const {
  519. return fbxVersion;
  520. }
  521. const std::string& Creator() const {
  522. return creator;
  523. }
  524. // elements (in this order): Uear, Month, Day, Hour, Second, Millisecond
  525. const unsigned int* CreationTimeStamp() const {
  526. return creationTimeStamp;
  527. }
  528. const PropertyTemplateMap& Templates() const {
  529. return templates;
  530. }
  531. const ObjectMap& Objects() const {
  532. return objects;
  533. }
  534. const ImportSettings& Settings() const {
  535. return settings;
  536. }
  537. const ConnectionMap& ConnectionsBySource() const {
  538. return src_connections;
  539. }
  540. const ConnectionMap& ConnectionsByDestination() const {
  541. return dest_connections;
  542. }
  543. // note: the implicit rule in all DOM classes is to always resolve
  544. // from destination to source (since the FBX object hierarchy is,
  545. // with very few exceptions, a DAG, this avoids cycles). In all
  546. // cases that may involve back-facing edges in the object graph,
  547. // use LazyObject::IsBeingConstructed() to check.
  548. std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source) const;
  549. std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest) const;
  550. std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source, const char* classname) const;
  551. std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest, const char* classname) const;
  552. std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source, const char* const* classnames, size_t count) const;
  553. std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest, const char* const* classnames, size_t count) const;
  554. const std::vector<const AnimationStack*>& AnimationStacks() const;
  555. private:
  556. std::vector<const Connection*> GetConnectionsSequenced(uint64_t id, const ConnectionMap&) const;
  557. std::vector<const Connection*> GetConnectionsSequenced(uint64_t id, bool is_src, const ConnectionMap&, const char* const* classnames, size_t count) const;
  558. private:
  559. void ReadHeader();
  560. void ReadObjects();
  561. void ReadPropertyTemplates();
  562. void ReadConnections();
  563. private:
  564. const ImportSettings& settings;
  565. ObjectMap objects;
  566. const Parser& parser;
  567. PropertyTemplateMap templates;
  568. ConnectionMap src_connections;
  569. ConnectionMap dest_connections;
  570. unsigned int fbxVersion;
  571. std::string creator;
  572. unsigned int creationTimeStamp[7];
  573. std::vector<uint64_t> animationStacks;
  574. mutable std::vector<const AnimationStack*> animationStacksResolved;
  575. };
  576. }
  577. }
  578. #endif