FBXDocument.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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();
  60. template <typename T>
  61. T* Get() {
  62. const Object* const ob = Get();
  63. return ob ? dynamic_cast<T*>(ob) : NULL;
  64. }
  65. uint64_t ID() const {
  66. return id;
  67. }
  68. private:
  69. const Document& doc;
  70. const Element& element;
  71. boost::scoped_ptr<const Object> object;
  72. const uint64_t id;
  73. };
  74. /** Base class for in-memory (DOM) representations of FBX objects */
  75. class Object
  76. {
  77. public:
  78. Object(uint64_t id, const Element& element, const std::string& name);
  79. virtual ~Object();
  80. public:
  81. const Element& SourceElement() const {
  82. return element;
  83. }
  84. const std::string& Name() const {
  85. return name;
  86. }
  87. uint64_t ID() const {
  88. return id;
  89. }
  90. protected:
  91. const Element& element;
  92. const std::string name;
  93. const uint64_t id;
  94. };
  95. /** DOM base class for FBX models */
  96. class Model : public Object
  97. {
  98. public:
  99. Model(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  100. ~Model();
  101. public:
  102. const std::string& Shading() const {
  103. return shading;
  104. }
  105. const std::string& Culling() const {
  106. return culling;
  107. }
  108. const PropertyTable& Props() const {
  109. ai_assert(props.get());
  110. return *props.get();
  111. }
  112. /** Get material links */
  113. const std::vector<const Material*>& GetMaterials() const {
  114. return materials;
  115. }
  116. /** Get geometry links */
  117. const std::vector<const Geometry*>& GetGeometry() const {
  118. return geometry;
  119. }
  120. private:
  121. void ResolveLinks(const Element& element, const Document& doc);
  122. private:
  123. std::vector<const Material*> materials;
  124. std::vector<const Geometry*> geometry;
  125. std::string shading;
  126. std::string culling;
  127. boost::shared_ptr<const PropertyTable> props;
  128. };
  129. /** DOM class for generic FBX textures */
  130. class Texture : public Object
  131. {
  132. public:
  133. Texture(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  134. ~Texture();
  135. public:
  136. const std::string& Type() const {
  137. return type;
  138. }
  139. const std::string& FileName() const {
  140. return fileName;
  141. }
  142. const std::string& RelativeFilename() const {
  143. return relativeFileName;
  144. }
  145. const std::string& AlphaSource() const {
  146. return alphaSource;
  147. }
  148. const aiVector2D& UVTranslation() const {
  149. return uvTrans;
  150. }
  151. const aiVector2D& UVScaling() const {
  152. return uvScaling;
  153. }
  154. const PropertyTable& Props() const {
  155. ai_assert(props.get());
  156. return *props.get();
  157. }
  158. // return a 4-tuple
  159. const unsigned int* Crop() const {
  160. return crop;
  161. }
  162. private:
  163. aiVector2D uvTrans;
  164. aiVector2D uvScaling;
  165. std::string type;
  166. std::string relativeFileName;
  167. std::string fileName;
  168. std::string alphaSource;
  169. boost::shared_ptr<const PropertyTable> props;
  170. unsigned int crop[4];
  171. };
  172. typedef std::fbx_unordered_map<std::string, const Texture*> TextureMap;
  173. /** DOM class for generic FBX materials */
  174. class Material : public Object
  175. {
  176. public:
  177. Material(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  178. ~Material();
  179. public:
  180. const std::string& GetShadingModel() const {
  181. return shading;
  182. }
  183. bool IsMultilayer() const {
  184. return multilayer;
  185. }
  186. const PropertyTable& Props() const {
  187. ai_assert(props.get());
  188. return *props.get();
  189. }
  190. const TextureMap& Textures() const {
  191. return textures;
  192. }
  193. private:
  194. std::string shading;
  195. bool multilayer;
  196. boost::shared_ptr<const PropertyTable> props;
  197. TextureMap textures;
  198. };
  199. /** DOM base class for all kinds of FBX geometry */
  200. class Geometry : public Object
  201. {
  202. public:
  203. Geometry(uint64_t id, const Element& element, const std::string& name);
  204. ~Geometry();
  205. };
  206. /** DOM class for FBX geometry of type "Mesh"*/
  207. class MeshGeometry : public Geometry
  208. {
  209. public:
  210. MeshGeometry(uint64_t id, const Element& element, const std::string& name, const Document& doc);
  211. ~MeshGeometry();
  212. public:
  213. /** Get a list of all vertex points, non-unique*/
  214. const std::vector<aiVector3D>& GetVertices() const {
  215. return vertices;
  216. }
  217. /** Get a list of all vertex normals or an empty array if
  218. * no normals are specified. */
  219. const std::vector<aiVector3D>& GetNormals() const {
  220. return normals;
  221. }
  222. /** Get a list of all vertex tangents or an empty array
  223. * if no tangents are specified */
  224. const std::vector<aiVector3D>& GetTangents() const {
  225. return tangents;
  226. }
  227. /** Get a list of all vertex binormals or an empty array
  228. * if no binormals are specified */
  229. const std::vector<aiVector3D>& GetBinormals() const {
  230. return binormals;
  231. }
  232. /** Return list of faces - each entry denotes a face and specifies
  233. * how many vertices it has. Vertices are taken from the
  234. * vertex data arrays in sequential order. */
  235. const std::vector<unsigned int>& GetFaceIndexCounts() const {
  236. return faces;
  237. }
  238. /** Get a UV coordinate slot, returns an empty array if
  239. * the requested slot does not exist. */
  240. const std::vector<aiVector2D>& GetTextureCoords(unsigned int index) const {
  241. static const std::vector<aiVector2D> empty;
  242. return index >= AI_MAX_NUMBER_OF_TEXTURECOORDS ? empty : uvs[index];
  243. }
  244. /** Get a UV coordinate slot, returns an empty array if
  245. * the requested slot does not exist. */
  246. std::string GetTextureCoordChannelName(unsigned int index) const {
  247. return index >= AI_MAX_NUMBER_OF_TEXTURECOORDS ? "" : uvNames[index];
  248. }
  249. /** Get a vertex color coordinate slot, returns an empty array if
  250. * the requested slot does not exist. */
  251. const std::vector<aiColor4D>& GetVertexColors(unsigned int index) const {
  252. static const std::vector<aiColor4D> empty;
  253. return index >= AI_MAX_NUMBER_OF_COLOR_SETS ? empty : colors[index];
  254. }
  255. /** Get per-face-vertex material assignments */
  256. const std::vector<unsigned int>& GetMaterialIndices() const {
  257. return materials;
  258. }
  259. public:
  260. private:
  261. void ReadLayer(const Scope& layer);
  262. void ReadLayerElement(const Scope& layerElement);
  263. void ReadVertexData(const std::string& type, int index, const Scope& source);
  264. void ReadVertexDataUV(std::vector<aiVector2D>& uv_out, const Scope& source,
  265. const std::string& MappingInformationType,
  266. const std::string& ReferenceInformationType);
  267. void ReadVertexDataNormals(std::vector<aiVector3D>& normals_out, const Scope& source,
  268. const std::string& MappingInformationType,
  269. const std::string& ReferenceInformationType);
  270. void ReadVertexDataColors(std::vector<aiColor4D>& colors_out, const Scope& source,
  271. const std::string& MappingInformationType,
  272. const std::string& ReferenceInformationType);
  273. void ReadVertexDataTangents(std::vector<aiVector3D>& tangents_out, const Scope& source,
  274. const std::string& MappingInformationType,
  275. const std::string& ReferenceInformationType);
  276. void ReadVertexDataBinormals(std::vector<aiVector3D>& binormals_out, const Scope& source,
  277. const std::string& MappingInformationType,
  278. const std::string& ReferenceInformationType);
  279. void ReadVertexDataMaterials(std::vector<unsigned int>& materials_out, const Scope& source,
  280. const std::string& MappingInformationType,
  281. const std::string& ReferenceInformationType);
  282. private:
  283. // cached data arrays
  284. std::vector<unsigned int> materials;
  285. std::vector<aiVector3D> vertices;
  286. std::vector<unsigned int> faces;
  287. std::vector<aiVector3D> tangents;
  288. std::vector<aiVector3D> binormals;
  289. std::vector<aiVector3D> normals;
  290. std::string uvNames[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  291. std::vector<aiVector2D> uvs[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  292. std::vector<aiColor4D> colors[AI_MAX_NUMBER_OF_COLOR_SETS];
  293. std::vector<unsigned int> mapping_counts;
  294. std::vector<unsigned int> mapping_offsets;
  295. std::vector<unsigned int> mappings;
  296. };
  297. /** Represents a link between two FBX objects. */
  298. class Connection
  299. {
  300. public:
  301. Connection(uint64_t insertionOrder, uint64_t src, uint64_t dest, const std::string& prop, const Document& doc);
  302. ~Connection();
  303. // note: a connection ensures that the source and dest objects exist, but
  304. // not that they have DOM representations, so the return value of one of
  305. // these functions can still be NULL.
  306. const Object* SourceObject() const;
  307. const Object* DestinationObject() const;
  308. // return the name of the property the connection is attached to.
  309. // this is an empty string for object to object (OO) connections.
  310. const std::string& PropertyName() const {
  311. return prop;
  312. }
  313. uint64_t InsertionOrder() const {
  314. return insertionOrder;
  315. }
  316. int CompareTo(const Connection* c) const {
  317. // note: can't subtract because this would overflow uint64_t
  318. if(InsertionOrder() > c->InsertionOrder()) {
  319. return 1;
  320. }
  321. else if(InsertionOrder() < c->InsertionOrder()) {
  322. return -1;
  323. }
  324. return 0;
  325. }
  326. public:
  327. uint64_t insertionOrder;
  328. const std::string prop;
  329. uint64_t src, dest;
  330. const Document& doc;
  331. };
  332. // XXX again, unique_ptr would be useful. shared_ptr is too
  333. // bloated since the objects have a well-defined single owner
  334. // during their entire lifetime (Document). FBX files have
  335. // up to many thousands of objects (most of which we never use),
  336. // so the memory overhead for them should be kept at a minimum.
  337. typedef std::map<uint64_t, LazyObject*> ObjectMap;
  338. typedef std::fbx_unordered_map<std::string, boost::shared_ptr<const PropertyTable> > PropertyTemplateMap;
  339. typedef std::multimap<uint64_t, const Connection*> ConnectionMap;
  340. /** DOM root for a FBX file */
  341. class Document
  342. {
  343. public:
  344. Document(const Parser& parser, const ImportSettings& settings);
  345. ~Document();
  346. public:
  347. LazyObject* GetObject(uint64_t id) const;
  348. const PropertyTemplateMap& Templates() const {
  349. return templates;
  350. }
  351. const ObjectMap& Objects() const {
  352. return objects;
  353. }
  354. const ImportSettings& Settings() const {
  355. return settings;
  356. }
  357. const ConnectionMap& ConnectionsBySource() const {
  358. return src_connections;
  359. }
  360. const ConnectionMap& ConnectionsByDestination() const {
  361. return dest_connections;
  362. }
  363. std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source) const;
  364. std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest) const;
  365. private:
  366. void ReadObjects();
  367. void ReadPropertyTemplates();
  368. void ReadConnections();
  369. private:
  370. const ImportSettings& settings;
  371. ObjectMap objects;
  372. const Parser& parser;
  373. PropertyTemplateMap templates;
  374. ConnectionMap src_connections;
  375. ConnectionMap dest_connections;
  376. };
  377. }
  378. }
  379. #endif