FBXExportNode.cpp 15 KB

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