FBXExportProperty.cpp 12 KB

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