FBXExportNode.cpp 15 KB

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