FBXExportNode.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2018, 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 FBX {
  44. class Node;
  45. }
  46. class FBX::Node
  47. {
  48. public: // public data members
  49. // TODO: accessors
  50. std::string name; // node name
  51. std::vector<FBX::Property> properties; // node properties
  52. std::vector<FBX::Node> children; // child nodes
  53. public: // constructors
  54. Node() = default;
  55. Node(const std::string& n) : name(n) {}
  56. Node(const std::string& n, const FBX::Property &p)
  57. : name(n)
  58. { properties.push_back(p); }
  59. Node(const std::string& n, const std::vector<FBX::Property> &pv)
  60. : name(n), properties(pv) {}
  61. public: // functions to add properties or children
  62. // add a single property to the node
  63. template <typename T>
  64. void AddProperty(T value) {
  65. properties.emplace_back(value);
  66. }
  67. // convenience function to add multiple properties at once
  68. template <typename T, typename... More>
  69. void AddProperties(T value, More... more) {
  70. properties.emplace_back(value);
  71. AddProperties(more...);
  72. }
  73. void AddProperties() {}
  74. // add a child node directly
  75. void AddChild(const Node& node) { children.push_back(node); }
  76. // convenience function to add a child node with a single property
  77. template <typename... More>
  78. void AddChild(
  79. const std::string& name,
  80. More... more
  81. ) {
  82. FBX::Node c(name);
  83. c.AddProperties(more...);
  84. children.push_back(c);
  85. }
  86. public: // support specifically for dealing with Properties70 nodes
  87. // it really is simpler to make these all separate functions.
  88. // the versions with 'A' suffixes are for animatable properties.
  89. // those often follow a completely different format internally in FBX.
  90. void AddP70int(const std::string& name, int32_t value);
  91. void AddP70bool(const std::string& name, bool value);
  92. void AddP70double(const std::string& name, double value);
  93. void AddP70numberA(const std::string& name, double value);
  94. void AddP70color(const std::string& name, double r, double g, double b);
  95. void AddP70colorA(const std::string& name, double r, double g, double b);
  96. void AddP70vector(const std::string& name, double x, double y, double z);
  97. void AddP70vectorA(const std::string& name, double x, double y, double z);
  98. void AddP70string(const std::string& name, const std::string& value);
  99. void AddP70enum(const std::string& name, int32_t value);
  100. void AddP70time(const std::string& name, int64_t value);
  101. // template for custom P70 nodes.
  102. // anything that doesn't fit in the above can be created manually.
  103. template <typename... More>
  104. void AddP70(
  105. const std::string& name,
  106. const std::string& type,
  107. const std::string& type2,
  108. const std::string& flags,
  109. More... more
  110. ) {
  111. Node n("P");
  112. n.AddProperties(name, type, type2, flags, more...);
  113. AddChild(n);
  114. }
  115. public: // member functions for writing data to a file or stream
  116. // write the full node as binary data to the given file or stream
  117. void Dump(std::shared_ptr<Assimp::IOStream> outfile);
  118. void Dump(Assimp::StreamWriterLE &s);
  119. // these other functions are for writing data piece by piece.
  120. // they must be used carefully.
  121. // for usage examples see FBXExporter.cpp.
  122. void Begin(Assimp::StreamWriterLE &s);
  123. void DumpProperties(Assimp::StreamWriterLE& s);
  124. void EndProperties(Assimp::StreamWriterLE &s);
  125. void EndProperties(Assimp::StreamWriterLE &s, size_t num_properties);
  126. void DumpChildren(Assimp::StreamWriterLE& s);
  127. void End(Assimp::StreamWriterLE &s, bool has_children);
  128. private: // data used for binary dumps
  129. size_t start_pos; // starting position in stream
  130. size_t end_pos; // ending position in stream
  131. size_t property_start; // starting position of property section
  132. public: // static member functions
  133. // convenience function to create a node with a single property,
  134. // and write it to the stream.
  135. template <typename T>
  136. static void WritePropertyNode(
  137. const std::string& name,
  138. const T value,
  139. Assimp::StreamWriterLE& s
  140. ) {
  141. FBX::Property p(value);
  142. FBX::Node node(name, p);
  143. node.Dump(s);
  144. }
  145. // convenience function to create and write a property node,
  146. // holding a single property which is an array of values.
  147. // does not copy the data, so is efficient for large arrays.
  148. static void WritePropertyNode(
  149. const std::string& name,
  150. const std::vector<double>& v,
  151. Assimp::StreamWriterLE& s
  152. );
  153. // convenience function to create and write a property node,
  154. // holding a single property which is an array of values.
  155. // does not copy the data, so is efficient for large arrays.
  156. static void WritePropertyNode(
  157. const std::string& name,
  158. const std::vector<int32_t>& v,
  159. Assimp::StreamWriterLE& s
  160. );
  161. };
  162. #endif // ASSIMP_BUILD_NO_FBX_EXPORTER
  163. #endif // AI_FBXEXPORTNODE_H_INC