FBXExportNode.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. #ifndef ASSIMP_BUILD_NO_EXPORT
  34. #ifndef ASSIMP_BUILD_NO_FBX_EXPORTER
  35. #include "FBXExportNode.h"
  36. #include "FBXCommon.h"
  37. #include <assimp/StreamWriter.h> // StreamWriterLE
  38. #include <assimp/Exceptional.h> // DeadlyExportError
  39. #include <assimp/ai_assert.h>
  40. #include <assimp/StringUtils.h> // ai_snprintf
  41. #include <string>
  42. #include <ostream>
  43. #include <sstream> // ostringstream
  44. #include <memory> // shared_ptr
  45. namespace Assimp {
  46. // AddP70<type> helpers... there's no usable pattern here,
  47. // so all are defined as separate functions.
  48. // Even "animatable" properties are often completely different
  49. // from the standard (nonanimated) property definition,
  50. // so they are specified with an 'A' suffix.
  51. void FBX::Node::AddP70int(
  52. const std::string& cur_name, int32_t value
  53. ) {
  54. FBX::Node n("P");
  55. n.AddProperties(cur_name, "int", "Integer", "", value);
  56. AddChild(n);
  57. }
  58. void FBX::Node::AddP70bool(
  59. const std::string& cur_name, bool value
  60. ) {
  61. FBX::Node n("P");
  62. n.AddProperties(cur_name, "bool", "", "", int32_t(value));
  63. AddChild(n);
  64. }
  65. void FBX::Node::AddP70double(
  66. const std::string &cur_name, double value) {
  67. FBX::Node n("P");
  68. n.AddProperties(cur_name, "double", "Number", "", value);
  69. AddChild(n);
  70. }
  71. void FBX::Node::AddP70numberA(
  72. const std::string &cur_name, double value) {
  73. FBX::Node n("P");
  74. n.AddProperties(cur_name, "Number", "", "A", value);
  75. AddChild(n);
  76. }
  77. void FBX::Node::AddP70color(
  78. const std::string &cur_name, double r, double g, double b) {
  79. FBX::Node n("P");
  80. n.AddProperties(cur_name, "ColorRGB", "Color", "", r, g, b);
  81. AddChild(n);
  82. }
  83. void FBX::Node::AddP70colorA(
  84. const std::string &cur_name, double r, double g, double b) {
  85. FBX::Node n("P");
  86. n.AddProperties(cur_name, "Color", "", "A", r, g, b);
  87. AddChild(n);
  88. }
  89. void FBX::Node::AddP70vector(
  90. const std::string &cur_name, double x, double y, double z) {
  91. FBX::Node n("P");
  92. n.AddProperties(cur_name, "Vector3D", "Vector", "", x, y, z);
  93. AddChild(n);
  94. }
  95. void FBX::Node::AddP70vectorA(
  96. const std::string &cur_name, double x, double y, double z) {
  97. FBX::Node n("P");
  98. n.AddProperties(cur_name, "Vector", "", "A", x, y, z);
  99. AddChild(n);
  100. }
  101. void FBX::Node::AddP70string(
  102. const std::string &cur_name, const std::string &value) {
  103. FBX::Node n("P");
  104. n.AddProperties(cur_name, "KString", "", "", value);
  105. AddChild(n);
  106. }
  107. void FBX::Node::AddP70enum(
  108. const std::string &cur_name, int32_t value) {
  109. FBX::Node n("P");
  110. n.AddProperties(cur_name, "enum", "", "", value);
  111. AddChild(n);
  112. }
  113. void FBX::Node::AddP70time(
  114. const std::string &cur_name, int64_t value) {
  115. FBX::Node n("P");
  116. n.AddProperties(cur_name, "KTime", "Time", "", value);
  117. AddChild(n);
  118. }
  119. // public member functions for writing nodes to stream
  120. void FBX::Node::Dump(
  121. std::shared_ptr<Assimp::IOStream> outfile,
  122. bool binary, int indent
  123. ) {
  124. if (binary) {
  125. Assimp::StreamWriterLE outstream(outfile);
  126. DumpBinary(outstream);
  127. } else {
  128. std::ostringstream ss;
  129. DumpAscii(ss, indent);
  130. std::string s = ss.str();
  131. outfile->Write(s.c_str(), s.size(), 1);
  132. }
  133. }
  134. void FBX::Node::Dump(
  135. Assimp::StreamWriterLE &outstream,
  136. bool binary, int indent
  137. ) {
  138. if (binary) {
  139. DumpBinary(outstream);
  140. } else {
  141. std::ostringstream ss;
  142. DumpAscii(ss, indent);
  143. outstream.PutString(ss.str());
  144. }
  145. }
  146. // public member functions for low-level writing
  147. void FBX::Node::Begin(
  148. Assimp::StreamWriterLE &s,
  149. bool binary, int indent
  150. ) {
  151. if (binary) {
  152. BeginBinary(s);
  153. } else {
  154. // assume we're at the correct place to start already
  155. (void)indent;
  156. std::ostringstream ss;
  157. BeginAscii(ss, indent);
  158. s.PutString(ss.str());
  159. }
  160. }
  161. void FBX::Node::DumpProperties(
  162. Assimp::StreamWriterLE& s,
  163. bool binary, int indent
  164. ) {
  165. if (binary) {
  166. DumpPropertiesBinary(s);
  167. } else {
  168. std::ostringstream ss;
  169. DumpPropertiesAscii(ss, indent);
  170. s.PutString(ss.str());
  171. }
  172. }
  173. void FBX::Node::EndProperties(
  174. Assimp::StreamWriterLE &s,
  175. bool binary, int indent
  176. ) {
  177. EndProperties(s, binary, indent, properties.size());
  178. }
  179. void FBX::Node::EndProperties(
  180. Assimp::StreamWriterLE &s,
  181. bool binary, int indent,
  182. size_t num_properties
  183. ) {
  184. if (binary) {
  185. EndPropertiesBinary(s, num_properties);
  186. } else {
  187. // nothing to do
  188. (void)indent;
  189. }
  190. }
  191. void FBX::Node::BeginChildren(
  192. Assimp::StreamWriterLE &s,
  193. bool binary, int indent
  194. ) {
  195. if (binary) {
  196. // nothing to do
  197. } else {
  198. std::ostringstream ss;
  199. BeginChildrenAscii(ss, indent);
  200. s.PutString(ss.str());
  201. }
  202. }
  203. void FBX::Node::DumpChildren(
  204. Assimp::StreamWriterLE& s,
  205. bool binary, int indent
  206. ) {
  207. if (binary) {
  208. DumpChildrenBinary(s);
  209. } else {
  210. std::ostringstream ss;
  211. DumpChildrenAscii(ss, indent);
  212. if (ss.tellp() > 0)
  213. s.PutString(ss.str());
  214. }
  215. }
  216. void FBX::Node::End(
  217. Assimp::StreamWriterLE &s,
  218. bool binary, int indent,
  219. bool has_children
  220. ) {
  221. if (binary) {
  222. EndBinary(s, has_children);
  223. } else {
  224. std::ostringstream ss;
  225. EndAscii(ss, indent, has_children);
  226. if (ss.tellp() > 0)
  227. s.PutString(ss.str());
  228. }
  229. }
  230. // public member functions for writing to binary fbx
  231. void FBX::Node::DumpBinary(Assimp::StreamWriterLE &s)
  232. {
  233. // write header section (with placeholders for some things)
  234. BeginBinary(s);
  235. // write properties
  236. DumpPropertiesBinary(s);
  237. // go back and fill in property related placeholders
  238. EndPropertiesBinary(s, properties.size());
  239. // write children
  240. DumpChildrenBinary(s);
  241. // finish, filling in end offset placeholder
  242. EndBinary(s, force_has_children || !children.empty());
  243. }
  244. // public member functions for writing to ascii fbx
  245. void FBX::Node::DumpAscii(std::ostream &s, int indent)
  246. {
  247. // write name
  248. BeginAscii(s, indent);
  249. // write properties
  250. DumpPropertiesAscii(s, indent);
  251. if (force_has_children || !children.empty()) {
  252. // begin children (with a '{')
  253. BeginChildrenAscii(s, indent + 1);
  254. // write children
  255. DumpChildrenAscii(s, indent + 1);
  256. }
  257. // finish (also closing the children bracket '}')
  258. EndAscii(s, indent, force_has_children || !children.empty());
  259. }
  260. // private member functions for low-level writing to fbx
  261. void FBX::Node::BeginBinary(Assimp::StreamWriterLE &s)
  262. {
  263. // remember start pos so we can come back and write the end pos
  264. this->start_pos = s.Tell();
  265. // placeholders for end pos and property section info
  266. s.PutU8(0); // end pos
  267. s.PutU8(0); // number of properties
  268. s.PutU8(0); // total property section length
  269. // node name
  270. s.PutU1(uint8_t(name.size())); // length of node name
  271. s.PutString(name); // node name as raw bytes
  272. // property data comes after here
  273. this->property_start = s.Tell();
  274. }
  275. void FBX::Node::DumpPropertiesBinary(Assimp::StreamWriterLE& s)
  276. {
  277. for (auto &p : properties) {
  278. p.DumpBinary(s);
  279. }
  280. }
  281. void FBX::Node::EndPropertiesBinary(
  282. Assimp::StreamWriterLE &s,
  283. size_t num_properties
  284. ) {
  285. if (num_properties == 0) { return; }
  286. size_t pos = s.Tell();
  287. ai_assert(pos > property_start);
  288. size_t property_section_size = pos - property_start;
  289. s.Seek(start_pos + 8); // 8 bytes of uint64_t of end_pos
  290. s.PutU8(num_properties);
  291. s.PutU8(property_section_size);
  292. s.Seek(pos);
  293. }
  294. void FBX::Node::DumpChildrenBinary(Assimp::StreamWriterLE& s)
  295. {
  296. for (FBX::Node& child : children) {
  297. child.DumpBinary(s);
  298. }
  299. }
  300. void FBX::Node::EndBinary(
  301. Assimp::StreamWriterLE &s,
  302. bool has_children
  303. ) {
  304. // if there were children, add a null record
  305. if (has_children) { s.PutString(Assimp::FBX::NULL_RECORD); }
  306. // now go back and write initial pos
  307. this->end_pos = s.Tell();
  308. s.Seek(start_pos);
  309. s.PutU8(end_pos);
  310. s.Seek(end_pos);
  311. }
  312. void FBX::Node::BeginAscii(std::ostream& s, int indent)
  313. {
  314. s << '\n';
  315. for (int i = 0; i < indent; ++i) { s << '\t'; }
  316. s << name << ": ";
  317. }
  318. void FBX::Node::DumpPropertiesAscii(std::ostream &s, int indent)
  319. {
  320. for (size_t i = 0; i < properties.size(); ++i) {
  321. if (i > 0) { s << ", "; }
  322. properties[i].DumpAscii(s, indent);
  323. }
  324. }
  325. void FBX::Node::BeginChildrenAscii(std::ostream& s, int indent)
  326. {
  327. // only call this if there are actually children
  328. s << " {";
  329. (void)indent;
  330. }
  331. void FBX::Node::DumpChildrenAscii(std::ostream& s, int indent)
  332. {
  333. // children will need a lot of padding and corralling
  334. if (children.size() || force_has_children) {
  335. for (size_t i = 0; i < children.size(); ++i) {
  336. // no compression in ascii files, so skip this node if it exists
  337. if (children[i].name == "EncryptionType") { continue; }
  338. // the child can dump itself
  339. children[i].DumpAscii(s, indent);
  340. }
  341. }
  342. }
  343. void FBX::Node::EndAscii(std::ostream& s, int indent, bool has_children)
  344. {
  345. if (!has_children) { return; } // nothing to do
  346. s << '\n';
  347. for (int i = 0; i < indent; ++i) { s << '\t'; }
  348. s << "}";
  349. }
  350. // private helpers for static member functions
  351. // ascii property node from vector of doubles
  352. void FBX::Node::WritePropertyNodeAscii(
  353. const std::string& name,
  354. const std::vector<double>& v,
  355. Assimp::StreamWriterLE& s,
  356. int indent
  357. ){
  358. char buffer[32];
  359. FBX::Node node(name);
  360. node.Begin(s, false, indent);
  361. std::string vsize = to_string(v.size());
  362. // *<size> {
  363. s.PutChar('*'); s.PutString(vsize); s.PutString(" {\n");
  364. // indent + 1
  365. for (int i = 0; i < indent + 1; ++i) { s.PutChar('\t'); }
  366. // a: value,value,value,...
  367. s.PutString("a: ");
  368. int count = 0;
  369. for (size_t i = 0; i < v.size(); ++i) {
  370. if (i > 0) { s.PutChar(','); }
  371. int len = ai_snprintf(buffer, sizeof(buffer), "%f", v[i]);
  372. count += len;
  373. if (count > 2048) { s.PutChar('\n'); count = 0; }
  374. if (len < 0 || len > 31) {
  375. // this should never happen
  376. throw DeadlyExportError("failed to convert double to string");
  377. }
  378. for (int j = 0; j < len; ++j) { s.PutChar(buffer[j]); }
  379. }
  380. // }
  381. s.PutChar('\n');
  382. for (int i = 0; i < indent; ++i) { s.PutChar('\t'); }
  383. s.PutChar('}'); s.PutChar(' ');
  384. node.End(s, false, indent, false);
  385. }
  386. // ascii property node from vector of int32_t
  387. void FBX::Node::WritePropertyNodeAscii(
  388. const std::string& name,
  389. const std::vector<int32_t>& v,
  390. Assimp::StreamWriterLE& s,
  391. int indent
  392. ){
  393. char buffer[32];
  394. FBX::Node node(name);
  395. node.Begin(s, false, indent);
  396. std::string vsize = to_string(v.size());
  397. // *<size> {
  398. s.PutChar('*'); s.PutString(vsize); s.PutString(" {\n");
  399. // indent + 1
  400. for (int i = 0; i < indent + 1; ++i) { s.PutChar('\t'); }
  401. // a: value,value,value,...
  402. s.PutString("a: ");
  403. int count = 0;
  404. for (size_t i = 0; i < v.size(); ++i) {
  405. if (i > 0) { s.PutChar(','); }
  406. int len = ai_snprintf(buffer, sizeof(buffer), "%d", v[i]);
  407. count += len;
  408. if (count > 2048) { s.PutChar('\n'); count = 0; }
  409. if (len < 0 || len > 31) {
  410. // this should never happen
  411. throw DeadlyExportError("failed to convert double to string");
  412. }
  413. for (int j = 0; j < len; ++j) { s.PutChar(buffer[j]); }
  414. }
  415. // }
  416. s.PutChar('\n');
  417. for (int i = 0; i < indent; ++i) { s.PutChar('\t'); }
  418. s.PutChar('}'); s.PutChar(' ');
  419. node.End(s, false, indent, false);
  420. }
  421. // binary property node from vector of doubles
  422. // TODO: optional zip compression!
  423. void FBX::Node::WritePropertyNodeBinary(
  424. const std::string& name,
  425. const std::vector<double>& v,
  426. Assimp::StreamWriterLE& s
  427. ){
  428. FBX::Node node(name);
  429. node.BeginBinary(s);
  430. s.PutU1('d');
  431. s.PutU4(uint32_t(v.size())); // number of elements
  432. s.PutU4(0); // no encoding (1 would be zip-compressed)
  433. s.PutU4(uint32_t(v.size()) * 8); // data size
  434. for (auto it = v.begin(); it != v.end(); ++it) { s.PutF8(*it); }
  435. node.EndPropertiesBinary(s, 1);
  436. node.EndBinary(s, false);
  437. }
  438. // binary property node from vector of int32_t
  439. // TODO: optional zip compression!
  440. void FBX::Node::WritePropertyNodeBinary(
  441. const std::string& name,
  442. const std::vector<int32_t>& v,
  443. Assimp::StreamWriterLE& s
  444. ){
  445. FBX::Node node(name);
  446. node.BeginBinary(s);
  447. s.PutU1('i');
  448. s.PutU4(uint32_t(v.size())); // number of elements
  449. s.PutU4(0); // no encoding (1 would be zip-compressed)
  450. s.PutU4(uint32_t(v.size()) * 4); // data size
  451. for (auto it = v.begin(); it != v.end(); ++it) { s.PutI4(*it); }
  452. node.EndPropertiesBinary(s, 1);
  453. node.EndBinary(s, false);
  454. }
  455. // public static member functions
  456. // convenience function to create and write a property node,
  457. // holding a single property which is an array of values.
  458. // does not copy the data, so is efficient for large arrays.
  459. void FBX::Node::WritePropertyNode(
  460. const std::string& name,
  461. const std::vector<double>& v,
  462. Assimp::StreamWriterLE& s,
  463. bool binary, int indent
  464. ){
  465. if (binary) {
  466. FBX::Node::WritePropertyNodeBinary(name, v, s);
  467. } else {
  468. FBX::Node::WritePropertyNodeAscii(name, v, s, indent);
  469. }
  470. }
  471. // convenience function to create and write a property node,
  472. // holding a single property which is an array of values.
  473. // does not copy the data, so is efficient for large arrays.
  474. void FBX::Node::WritePropertyNode(
  475. const std::string& name,
  476. const std::vector<int32_t>& v,
  477. Assimp::StreamWriterLE& s,
  478. bool binary, int indent
  479. ){
  480. if (binary) {
  481. FBX::Node::WritePropertyNodeBinary(name, v, s);
  482. } else {
  483. FBX::Node::WritePropertyNodeAscii(name, v, s, indent);
  484. }
  485. }
  486. }
  487. #endif // ASSIMP_BUILD_NO_FBX_EXPORTER
  488. #endif // ASSIMP_BUILD_NO_EXPORT