FBXExportProperty.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 "FBXExportProperty.h"
  36. #include <assimp/StreamWriter.h> // StreamWriterLE
  37. #include <assimp/Exceptional.h> // DeadlyExportError
  38. #include <string>
  39. #include <vector>
  40. #include <ostream>
  41. #include <locale>
  42. #include <sstream> // ostringstream
  43. namespace Assimp {
  44. namespace FBX {
  45. // constructors for single element properties
  46. FBXExportProperty::FBXExportProperty(bool v)
  47. : type('C')
  48. , data(1) {
  49. data = {
  50. uint8_t(v)
  51. };
  52. }
  53. FBXExportProperty::FBXExportProperty(int16_t v)
  54. : type('Y')
  55. , data(2) {
  56. uint8_t* d = data.data();
  57. (reinterpret_cast<int16_t*>(d))[0] = v;
  58. }
  59. FBXExportProperty::FBXExportProperty(int32_t v)
  60. : type('I')
  61. , data(4) {
  62. uint8_t* d = data.data();
  63. (reinterpret_cast<int32_t*>(d))[0] = v;
  64. }
  65. FBXExportProperty::FBXExportProperty(float v)
  66. : type('F')
  67. , data(4) {
  68. uint8_t* d = data.data();
  69. (reinterpret_cast<float*>(d))[0] = v;
  70. }
  71. FBXExportProperty::FBXExportProperty(double v)
  72. : type('D')
  73. , data(8) {
  74. uint8_t* d = data.data();
  75. (reinterpret_cast<double*>(d))[0] = v;
  76. }
  77. FBXExportProperty::FBXExportProperty(int64_t v)
  78. : type('L')
  79. , data(8) {
  80. uint8_t* d = data.data();
  81. (reinterpret_cast<int64_t*>(d))[0] = v;
  82. }
  83. // constructors for array-type properties
  84. FBXExportProperty::FBXExportProperty(const char* c, bool raw)
  85. : FBXExportProperty(std::string(c), raw) {
  86. // empty
  87. }
  88. // strings can either be saved as "raw" (R) data, or "string" (S) data
  89. FBXExportProperty::FBXExportProperty(const std::string& s, bool raw)
  90. : type(raw ? 'R' : 'S')
  91. , data(s.size()) {
  92. for (size_t i = 0; i < s.size(); ++i) {
  93. data[i] = uint8_t(s[i]);
  94. }
  95. }
  96. FBXExportProperty::FBXExportProperty(const std::vector<uint8_t>& r)
  97. : type('R')
  98. , data(r) {
  99. // empty
  100. }
  101. FBXExportProperty::FBXExportProperty(const std::vector<int32_t>& va)
  102. : type('i')
  103. , data(4 * va.size() ) {
  104. int32_t* d = reinterpret_cast<int32_t*>(data.data());
  105. for (size_t i = 0; i < va.size(); ++i) {
  106. d[i] = va[i];
  107. }
  108. }
  109. FBXExportProperty::FBXExportProperty(const std::vector<int64_t>& va)
  110. : type('l')
  111. , data(8 * va.size()) {
  112. int64_t* d = reinterpret_cast<int64_t*>(data.data());
  113. for (size_t i = 0; i < va.size(); ++i) {
  114. d[i] = va[i];
  115. }
  116. }
  117. FBXExportProperty::FBXExportProperty(const std::vector<float>& va)
  118. : type('f')
  119. , data(4 * va.size()) {
  120. float* d = reinterpret_cast<float*>(data.data());
  121. for (size_t i = 0; i < va.size(); ++i) {
  122. d[i] = va[i];
  123. }
  124. }
  125. FBXExportProperty::FBXExportProperty(const std::vector<double>& va)
  126. : type('d')
  127. , data(8 * va.size()) {
  128. double* d = reinterpret_cast<double*>(data.data());
  129. for (size_t i = 0; i < va.size(); ++i) {
  130. d[i] = va[i];
  131. }
  132. }
  133. FBXExportProperty::FBXExportProperty(const aiMatrix4x4& vm)
  134. : type('d')
  135. , data(8 * 16) {
  136. double* d = reinterpret_cast<double*>(data.data());
  137. for (unsigned int c = 0; c < 4; ++c) {
  138. for (unsigned int r = 0; r < 4; ++r) {
  139. d[4 * c + r] = vm[r][c];
  140. }
  141. }
  142. }
  143. // public member functions
  144. size_t FBXExportProperty::size() {
  145. switch (type) {
  146. case 'C':
  147. case 'Y':
  148. case 'I':
  149. case 'F':
  150. case 'D':
  151. case 'L':
  152. return data.size() + 1;
  153. case 'S':
  154. case 'R':
  155. return data.size() + 5;
  156. case 'i':
  157. case 'd':
  158. return data.size() + 13;
  159. default:
  160. throw DeadlyExportError("Requested size on property of unknown type");
  161. }
  162. }
  163. void FBXExportProperty::DumpBinary(Assimp::StreamWriterLE& s) {
  164. s.PutU1(type);
  165. uint8_t* d = data.data();
  166. size_t N;
  167. switch (type) {
  168. case 'C': s.PutU1(*(reinterpret_cast<uint8_t*>(d))); return;
  169. case 'Y': s.PutI2(*(reinterpret_cast<int16_t*>(d))); return;
  170. case 'I': s.PutI4(*(reinterpret_cast<int32_t*>(d))); return;
  171. case 'F': s.PutF4(*(reinterpret_cast<float*>(d))); return;
  172. case 'D': s.PutF8(*(reinterpret_cast<double*>(d))); return;
  173. case 'L': s.PutI8(*(reinterpret_cast<int64_t*>(d))); return;
  174. case 'S':
  175. case 'R':
  176. s.PutU4(uint32_t(data.size()));
  177. for (size_t i = 0; i < data.size(); ++i) { s.PutU1(data[i]); }
  178. return;
  179. case 'i':
  180. N = data.size() / 4;
  181. s.PutU4(uint32_t(N)); // number of elements
  182. s.PutU4(0); // no encoding (1 would be zip-compressed)
  183. // TODO: compress if large?
  184. s.PutU4(uint32_t(data.size())); // data size
  185. for (size_t i = 0; i < N; ++i) {
  186. s.PutI4((reinterpret_cast<int32_t*>(d))[i]);
  187. }
  188. return;
  189. case 'l':
  190. N = data.size() / 8;
  191. s.PutU4(uint32_t(N)); // number of elements
  192. s.PutU4(0); // no encoding (1 would be zip-compressed)
  193. // TODO: compress if large?
  194. s.PutU4(uint32_t(data.size())); // data size
  195. for (size_t i = 0; i < N; ++i) {
  196. s.PutI8((reinterpret_cast<int64_t*>(d))[i]);
  197. }
  198. return;
  199. case 'f':
  200. N = data.size() / 4;
  201. s.PutU4(uint32_t(N)); // number of elements
  202. s.PutU4(0); // no encoding (1 would be zip-compressed)
  203. // TODO: compress if large?
  204. s.PutU4(uint32_t(data.size())); // data size
  205. for (size_t i = 0; i < N; ++i) {
  206. s.PutF4((reinterpret_cast<float*>(d))[i]);
  207. }
  208. return;
  209. case 'd':
  210. N = data.size() / 8;
  211. s.PutU4(uint32_t(N)); // number of elements
  212. s.PutU4(0); // no encoding (1 would be zip-compressed)
  213. // TODO: compress if large?
  214. s.PutU4(uint32_t(data.size())); // data size
  215. for (size_t i = 0; i < N; ++i) {
  216. s.PutF8((reinterpret_cast<double*>(d))[i]);
  217. }
  218. return;
  219. default:
  220. std::ostringstream err;
  221. err << "Tried to dump property with invalid type '";
  222. err << type << "'!";
  223. throw DeadlyExportError(err.str());
  224. }
  225. }
  226. void FBXExportProperty::DumpAscii(Assimp::StreamWriterLE& outstream, int indent) {
  227. std::ostringstream ss;
  228. ss.imbue(std::locale::classic());
  229. ss.precision(15); // this seems to match official FBX SDK exports
  230. DumpAscii(ss, indent);
  231. outstream.PutString(ss.str());
  232. }
  233. void FBXExportProperty::DumpAscii(std::ostream& s, int indent) {
  234. // no writing type... or anything. just shove it into the stream.
  235. uint8_t* d = data.data();
  236. size_t N;
  237. size_t swap = data.size();
  238. size_t count = 0;
  239. switch (type) {
  240. case 'C':
  241. if (*(reinterpret_cast<uint8_t*>(d))) { s << 'T'; }
  242. else { s << 'F'; }
  243. return;
  244. case 'Y': s << *(reinterpret_cast<int16_t*>(d)); return;
  245. case 'I': s << *(reinterpret_cast<int32_t*>(d)); return;
  246. case 'F': s << *(reinterpret_cast<float*>(d)); return;
  247. case 'D': s << *(reinterpret_cast<double*>(d)); return;
  248. case 'L': s << *(reinterpret_cast<int64_t*>(d)); return;
  249. case 'S':
  250. // first search to see if it has "\x00\x01" in it -
  251. // which separates fields which are reversed in the ascii version.
  252. // yeah.
  253. // FBX, yeah.
  254. for (size_t i = 0; i < data.size(); ++i) {
  255. if (data[i] == '\0') {
  256. swap = i;
  257. break;
  258. }
  259. }
  260. case 'R':
  261. s << '"';
  262. // we might as well check this now,
  263. // probably it will never happen
  264. for (size_t i = 0; i < data.size(); ++i) {
  265. char c = data[i];
  266. if (c == '"') {
  267. throw runtime_error("can't handle quotes in property string");
  268. }
  269. }
  270. // first write the SWAPPED member (if any)
  271. for (size_t i = swap + 2; i < data.size(); ++i) {
  272. char c = data[i];
  273. s << c;
  274. }
  275. // then a separator
  276. if (swap != data.size()) {
  277. s << "::";
  278. }
  279. // then the initial member
  280. for (size_t i = 0; i < swap; ++i) {
  281. char c = data[i];
  282. s << c;
  283. }
  284. s << '"';
  285. return;
  286. case 'i':
  287. N = data.size() / 4; // number of elements
  288. s << '*' << N << " {\n";
  289. for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
  290. s << "a: ";
  291. for (size_t i = 0; i < N; ++i) {
  292. if (i > 0) { s << ','; }
  293. if (count++ > 120) { s << '\n'; count = 0; }
  294. s << (reinterpret_cast<int32_t*>(d))[i];
  295. }
  296. s << '\n';
  297. for (int i = 0; i < indent; ++i) { s << '\t'; }
  298. s << "} ";
  299. return;
  300. case 'l':
  301. N = data.size() / 8;
  302. s << '*' << N << " {\n";
  303. for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
  304. s << "a: ";
  305. for (size_t i = 0; i < N; ++i) {
  306. if (i > 0) { s << ','; }
  307. if (count++ > 120) { s << '\n'; count = 0; }
  308. s << (reinterpret_cast<int64_t*>(d))[i];
  309. }
  310. s << '\n';
  311. for (int i = 0; i < indent; ++i) { s << '\t'; }
  312. s << "} ";
  313. return;
  314. case 'f':
  315. N = data.size() / 4;
  316. s << '*' << N << " {\n";
  317. for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
  318. s << "a: ";
  319. for (size_t i = 0; i < N; ++i) {
  320. if (i > 0) { s << ','; }
  321. if (count++ > 120) { s << '\n'; count = 0; }
  322. s << (reinterpret_cast<float*>(d))[i];
  323. }
  324. s << '\n';
  325. for (int i = 0; i < indent; ++i) { s << '\t'; }
  326. s << "} ";
  327. return;
  328. case 'd':
  329. N = data.size() / 8;
  330. s << '*' << N << " {\n";
  331. for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
  332. s << "a: ";
  333. // set precision to something that can handle doubles
  334. s.precision(15);
  335. for (size_t i = 0; i < N; ++i) {
  336. if (i > 0) { s << ','; }
  337. if (count++ > 120) { s << '\n'; count = 0; }
  338. s << (reinterpret_cast<double*>(d))[i];
  339. }
  340. s << '\n';
  341. for (int i = 0; i < indent; ++i) { s << '\t'; }
  342. s << "} ";
  343. return;
  344. default:
  345. std::ostringstream err;
  346. err << "Tried to dump property with invalid type '";
  347. err << type << "'!";
  348. throw runtime_error(err.str());
  349. }
  350. }
  351. } // Namespace FBX
  352. } // Namespace Assimp
  353. #endif // ASSIMP_BUILD_NO_FBX_EXPORTER
  354. #endif // ASSIMP_BUILD_NO_EXPORT