FBXExportProperty.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "FBXExportProperty.h"
  36. #include <assimp/StreamWriter.h> // StreamWriterLE
  37. #include <assimp/Exceptional.h> // DeadlyExportError
  38. #include <string>
  39. #include <vector>
  40. #include <sstream> // stringstream
  41. // constructors for single element properties
  42. FBX::Property::Property(bool v)
  43. : type('C'), data(1)
  44. {
  45. data = {uint8_t(v)};
  46. }
  47. FBX::Property::Property(int16_t v) : type('Y'), data(2)
  48. {
  49. uint8_t* d = data.data();
  50. (reinterpret_cast<int16_t*>(d))[0] = v;
  51. }
  52. FBX::Property::Property(int32_t v) : type('I'), data(4)
  53. {
  54. uint8_t* d = data.data();
  55. (reinterpret_cast<int32_t*>(d))[0] = v;
  56. }
  57. FBX::Property::Property(float v) : type('F'), data(4)
  58. {
  59. uint8_t* d = data.data();
  60. (reinterpret_cast<float*>(d))[0] = v;
  61. }
  62. FBX::Property::Property(double v) : type('D'), data(8)
  63. {
  64. uint8_t* d = data.data();
  65. (reinterpret_cast<double*>(d))[0] = v;
  66. }
  67. FBX::Property::Property(int64_t v) : type('L'), data(8)
  68. {
  69. uint8_t* d = data.data();
  70. (reinterpret_cast<int64_t*>(d))[0] = v;
  71. }
  72. // constructors for array-type properties
  73. FBX::Property::Property(const char* c, bool raw)
  74. : Property(std::string(c), raw)
  75. {}
  76. // strings can either be saved as "raw" (R) data, or "string" (S) data
  77. FBX::Property::Property(const std::string& s, bool raw)
  78. : type(raw ? 'R' : 'S'), data(s.size())
  79. {
  80. for (size_t i = 0; i < s.size(); ++i) {
  81. data[i] = uint8_t(s[i]);
  82. }
  83. }
  84. FBX::Property::Property(const std::vector<uint8_t>& r)
  85. : type('R'), data(r)
  86. {}
  87. FBX::Property::Property(const std::vector<int32_t>& va)
  88. : type('i'), data(4*va.size())
  89. {
  90. int32_t* d = reinterpret_cast<int32_t*>(data.data());
  91. for (size_t i = 0; i < va.size(); ++i) { d[i] = va[i]; }
  92. }
  93. FBX::Property::Property(const std::vector<double>& va)
  94. : type('d'), data(8*va.size())
  95. {
  96. double* d = reinterpret_cast<double*>(data.data());
  97. for (size_t i = 0; i < va.size(); ++i) { d[i] = va[i]; }
  98. }
  99. FBX::Property::Property(const aiMatrix4x4& vm)
  100. : type('d'), data(8*16)
  101. {
  102. double* d = reinterpret_cast<double*>(data.data());
  103. for (size_t c = 0; c < 4; ++c) {
  104. for (size_t r = 0; r < 4; ++r) {
  105. d[4*c+r] = vm[r][c];
  106. }
  107. }
  108. }
  109. // public member functions
  110. size_t FBX::Property::size()
  111. {
  112. switch (type) {
  113. case 'C': case 'Y': case 'I': case 'F': case 'D': case 'L':
  114. return data.size() + 1;
  115. case 'S': case 'R':
  116. return data.size() + 5;
  117. case 'i': case 'd':
  118. return data.size() + 13;
  119. default:
  120. throw DeadlyExportError("Requested size on property of unknown type");
  121. }
  122. }
  123. void FBX::Property::Dump(Assimp::StreamWriterLE &s)
  124. {
  125. s.PutU1(type);
  126. uint8_t* d;
  127. size_t N;
  128. switch (type) {
  129. case 'C': s.PutU1(*(reinterpret_cast<uint8_t*>(data.data()))); return;
  130. case 'Y': s.PutI2(*(reinterpret_cast<int16_t*>(data.data()))); return;
  131. case 'I': s.PutI4(*(reinterpret_cast<int32_t*>(data.data()))); return;
  132. case 'F': s.PutF4(*(reinterpret_cast<float*>(data.data()))); return;
  133. case 'D': s.PutF8(*(reinterpret_cast<double*>(data.data()))); return;
  134. case 'L': s.PutI8(*(reinterpret_cast<int64_t*>(data.data()))); return;
  135. case 'S':
  136. case 'R':
  137. s.PutU4(data.size());
  138. for (size_t i = 0; i < data.size(); ++i) { s.PutU1(data[i]); }
  139. return;
  140. case 'i':
  141. N = data.size() / 4;
  142. s.PutU4(N); // number of elements
  143. s.PutU4(0); // no encoding (1 would be zip-compressed)
  144. // TODO: compress if large?
  145. s.PutU4(data.size()); // data size
  146. d = data.data();
  147. for (size_t i = 0; i < N; ++i) {
  148. s.PutI4((reinterpret_cast<int32_t*>(d))[i]);
  149. }
  150. return;
  151. case 'd':
  152. N = data.size() / 8;
  153. s.PutU4(N); // number of elements
  154. s.PutU4(0); // no encoding (1 would be zip-compressed)
  155. // TODO: compress if large?
  156. s.PutU4(data.size()); // data size
  157. d = data.data();
  158. for (size_t i = 0; i < N; ++i) {
  159. s.PutF8((reinterpret_cast<double*>(d))[i]);
  160. }
  161. return;
  162. default:
  163. std::stringstream err;
  164. err << "Tried to dump property with invalid type '";
  165. err << type << "'!";
  166. throw DeadlyExportError(err.str());
  167. }
  168. }
  169. #endif // ASSIMP_BUILD_NO_FBX_EXPORTER
  170. #endif // ASSIMP_BUILD_NO_EXPORT