FBXExportNode.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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 FBXExportNode.h
  34. * Declares the FBX::Node helper class for fbx export.
  35. */
  36. #ifndef AI_FBXEXPORTNODE_H_INC
  37. #define AI_FBXEXPORTNODE_H_INC
  38. #ifndef ASSIMP_BUILD_NO_FBX_EXPORTER
  39. #include "FBXExportProperty.h"
  40. #include <assimp/StreamWriter.h> // StreamWriterLE
  41. #include <string>
  42. #include <vector>
  43. namespace Assimp {
  44. namespace FBX {
  45. class Node;
  46. }
  47. class FBX::Node {
  48. public:
  49. // TODO: accessors
  50. std::string name; // node name
  51. std::vector<FBX::FBXExportProperty> properties; // node properties
  52. std::vector<FBX::Node> children; // child nodes
  53. // some nodes always pretend they have children...
  54. bool force_has_children = false;
  55. public: // constructors
  56. /// The default class constructor.
  57. Node() = default;
  58. /// The class constructor with the name.
  59. Node(const std::string& n)
  60. : name(n)
  61. , properties()
  62. , children()
  63. , force_has_children( false ) {
  64. // empty
  65. }
  66. // convenience template to construct with properties directly
  67. template <typename... More>
  68. Node(const std::string& n, const More... more)
  69. : name(n)
  70. , properties()
  71. , children()
  72. , force_has_children(false) {
  73. AddProperties(more...);
  74. }
  75. public: // functions to add properties or children
  76. // add a single property to the node
  77. template <typename T>
  78. void AddProperty(T value) {
  79. properties.emplace_back(value);
  80. }
  81. // convenience function to add multiple properties at once
  82. template <typename T, typename... More>
  83. void AddProperties(T value, More... more) {
  84. properties.emplace_back(value);
  85. AddProperties(more...);
  86. }
  87. void AddProperties() {}
  88. // add a child node directly
  89. void AddChild(const Node& node) { children.push_back(node); }
  90. // convenience function to add a child node with a single property
  91. template <typename... More>
  92. void AddChild(
  93. const std::string& name,
  94. More... more
  95. ) {
  96. FBX::Node c(name);
  97. c.AddProperties(more...);
  98. children.push_back(c);
  99. }
  100. public: // support specifically for dealing with Properties70 nodes
  101. // it really is simpler to make these all separate functions.
  102. // the versions with 'A' suffixes are for animatable properties.
  103. // those often follow a completely different format internally in FBX.
  104. void AddP70int(const std::string& name, int32_t value);
  105. void AddP70bool(const std::string& name, bool value);
  106. void AddP70double(const std::string& name, double value);
  107. void AddP70numberA(const std::string& name, double value);
  108. void AddP70color(const std::string& name, double r, double g, double b);
  109. void AddP70colorA(const std::string& name, double r, double g, double b);
  110. void AddP70vector(const std::string& name, double x, double y, double z);
  111. void AddP70vectorA(const std::string& name, double x, double y, double z);
  112. void AddP70string(const std::string& name, const std::string& value);
  113. void AddP70enum(const std::string& name, int32_t value);
  114. void AddP70time(const std::string& name, int64_t value);
  115. // template for custom P70 nodes.
  116. // anything that doesn't fit in the above can be created manually.
  117. template <typename... More>
  118. void AddP70(
  119. const std::string& name,
  120. const std::string& type,
  121. const std::string& type2,
  122. const std::string& flags,
  123. More... more
  124. ) {
  125. Node n("P");
  126. n.AddProperties(name, type, type2, flags, more...);
  127. AddChild(n);
  128. }
  129. public: // member functions for writing data to a file or stream
  130. // write the full node to the given file or stream
  131. void Dump(
  132. std::shared_ptr<Assimp::IOStream> outfile,
  133. bool binary, int indent
  134. );
  135. void Dump(Assimp::StreamWriterLE &s, bool binary, int indent);
  136. // these other functions are for writing data piece by piece.
  137. // they must be used carefully.
  138. // for usage examples see FBXExporter.cpp.
  139. void Begin(Assimp::StreamWriterLE &s, bool binary, int indent);
  140. void DumpProperties(Assimp::StreamWriterLE& s, bool binary, int indent);
  141. void EndProperties(Assimp::StreamWriterLE &s, bool binary, int indent);
  142. void EndProperties(
  143. Assimp::StreamWriterLE &s, bool binary, int indent,
  144. size_t num_properties
  145. );
  146. void BeginChildren(Assimp::StreamWriterLE &s, bool binary, int indent);
  147. void DumpChildren(Assimp::StreamWriterLE& s, bool binary, int indent);
  148. void End(
  149. Assimp::StreamWriterLE &s, bool binary, int indent,
  150. bool has_children
  151. );
  152. private: // internal functions used for writing
  153. void DumpBinary(Assimp::StreamWriterLE &s);
  154. void DumpAscii(Assimp::StreamWriterLE &s, int indent);
  155. void DumpAscii(std::ostream &s, int indent);
  156. void BeginBinary(Assimp::StreamWriterLE &s);
  157. void DumpPropertiesBinary(Assimp::StreamWriterLE& s);
  158. void EndPropertiesBinary(Assimp::StreamWriterLE &s);
  159. void EndPropertiesBinary(Assimp::StreamWriterLE &s, size_t num_properties);
  160. void DumpChildrenBinary(Assimp::StreamWriterLE& s);
  161. void EndBinary(Assimp::StreamWriterLE &s, bool has_children);
  162. void BeginAscii(std::ostream &s, int indent);
  163. void DumpPropertiesAscii(std::ostream &s, int indent);
  164. void BeginChildrenAscii(std::ostream &s, int indent);
  165. void DumpChildrenAscii(std::ostream &s, int indent);
  166. void EndAscii(std::ostream &s, int indent, bool has_children);
  167. private: // data used for binary dumps
  168. size_t start_pos; // starting position in stream
  169. size_t end_pos; // ending position in stream
  170. size_t property_start; // starting position of property section
  171. public: // static member functions
  172. // convenience function to create a node with a single property,
  173. // and write it to the stream.
  174. template <typename T>
  175. static void WritePropertyNode(
  176. const std::string& name,
  177. const T value,
  178. Assimp::StreamWriterLE& s,
  179. bool binary, int indent
  180. ) {
  181. FBX::FBXExportProperty p(value);
  182. FBX::Node node(name, p);
  183. node.Dump(s, binary, indent);
  184. }
  185. // convenience function to create and write a property node,
  186. // holding a single property which is an array of values.
  187. // does not copy the data, so is efficient for large arrays.
  188. static void WritePropertyNode(
  189. const std::string& name,
  190. const std::vector<double>& v,
  191. Assimp::StreamWriterLE& s,
  192. bool binary, int indent
  193. );
  194. // convenience function to create and write a property node,
  195. // holding a single property which is an array of values.
  196. // does not copy the data, so is efficient for large arrays.
  197. static void WritePropertyNode(
  198. const std::string& name,
  199. const std::vector<int32_t>& v,
  200. Assimp::StreamWriterLE& s,
  201. bool binary, int indent
  202. );
  203. private: // static helper functions
  204. static void WritePropertyNodeAscii(
  205. const std::string& name,
  206. const std::vector<double>& v,
  207. Assimp::StreamWriterLE& s,
  208. int indent
  209. );
  210. static void WritePropertyNodeAscii(
  211. const std::string& name,
  212. const std::vector<int32_t>& v,
  213. Assimp::StreamWriterLE& s,
  214. int indent
  215. );
  216. static void WritePropertyNodeBinary(
  217. const std::string& name,
  218. const std::vector<double>& v,
  219. Assimp::StreamWriterLE& s
  220. );
  221. static void WritePropertyNodeBinary(
  222. const std::string& name,
  223. const std::vector<int32_t>& v,
  224. Assimp::StreamWriterLE& s
  225. );
  226. };
  227. }
  228. #endif // ASSIMP_BUILD_NO_FBX_EXPORTER
  229. #endif // AI_FBXEXPORTNODE_H_INC