FBXExportNode.h 8.9 KB

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