FBXDocumentUtil.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. /** @file FBXDocumentUtil.cpp
  34. * @brief Implementation of the FBX DOM utility functions declared in FBXDocumentUtil.h
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include <functional>
  39. #include "FBXParser.h"
  40. #include "FBXDocument.h"
  41. #include "FBXUtil.h"
  42. #include "FBXDocumentUtil.h"
  43. #include "FBXProperties.h"
  44. namespace Assimp {
  45. namespace FBX {
  46. namespace Util {
  47. // ------------------------------------------------------------------------------------------------
  48. // signal DOM construction error, this is always unrecoverable. Throws DeadlyImportError.
  49. void DOMError(const std::string& message, const Token& token)
  50. {
  51. throw DeadlyImportError(Util::AddTokenText("FBX-DOM",message,&token));
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. void DOMError(const std::string& message, const Element* element /*= NULL*/)
  55. {
  56. if(element) {
  57. DOMError(message,element->KeyToken());
  58. }
  59. throw DeadlyImportError("FBX-DOM " + message);
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. // print warning, do return
  63. void DOMWarning(const std::string& message, const Token& token)
  64. {
  65. if(DefaultLogger::get()) {
  66. DefaultLogger::get()->warn(Util::AddTokenText("FBX-DOM",message,&token));
  67. }
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. void DOMWarning(const std::string& message, const Element* element /*= NULL*/)
  71. {
  72. if(element) {
  73. DOMWarning(message,element->KeyToken());
  74. return;
  75. }
  76. if(DefaultLogger::get()) {
  77. DefaultLogger::get()->warn("FBX-DOM: " + message);
  78. }
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // extract required compound scope
  82. const Scope& GetRequiredScope(const Element& el)
  83. {
  84. const Scope* const s = el.Compound();
  85. if(!s) {
  86. DOMError("expected compound scope",&el);
  87. }
  88. return *s;
  89. }
  90. // ------------------------------------------------------------------------------------------------
  91. // get token at a particular index
  92. const Token& GetRequiredToken(const Element& el, unsigned int index)
  93. {
  94. const TokenList& t = el.Tokens();
  95. if(index >= t.size()) {
  96. DOMError(Formatter::format( "missing token at index " ) << index,&el);
  97. }
  98. return *t[index];
  99. }
  100. // ------------------------------------------------------------------------------------------------
  101. // wrapper around ParseTokenAsID() with DOMError handling
  102. uint64_t ParseTokenAsID(const Token& t)
  103. {
  104. const char* err;
  105. const uint64_t i = ParseTokenAsID(t,err);
  106. if(err) {
  107. DOMError(err,t);
  108. }
  109. return i;
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. // wrapper around ParseTokenAsDim() with DOMError handling
  113. size_t ParseTokenAsDim(const Token& t)
  114. {
  115. const char* err;
  116. const size_t i = ParseTokenAsDim(t,err);
  117. if(err) {
  118. DOMError(err,t);
  119. }
  120. return i;
  121. }
  122. // ------------------------------------------------------------------------------------------------
  123. // wrapper around ParseTokenAsFloat() with DOMError handling
  124. float ParseTokenAsFloat(const Token& t)
  125. {
  126. const char* err;
  127. const float i = ParseTokenAsFloat(t,err);
  128. if(err) {
  129. DOMError(err,t);
  130. }
  131. return i;
  132. }
  133. // ------------------------------------------------------------------------------------------------
  134. // wrapper around ParseTokenAsInt() with DOMError handling
  135. int ParseTokenAsInt(const Token& t)
  136. {
  137. const char* err;
  138. const int i = ParseTokenAsInt(t,err);
  139. if(err) {
  140. DOMError(err,t);
  141. }
  142. return i;
  143. }
  144. // ------------------------------------------------------------------------------------------------
  145. // wrapper around ParseTokenAsString() with DOMError handling
  146. std::string ParseTokenAsString(const Token& t)
  147. {
  148. const char* err;
  149. const std::string& i = ParseTokenAsString(t,err);
  150. if(err) {
  151. DOMError(err,t);
  152. }
  153. return i;
  154. }
  155. // ------------------------------------------------------------------------------------------------
  156. // extract a required element from a scope, abort if the element cannot be found
  157. const Element& GetRequiredElement(const Scope& sc, const std::string& index, const Element* element /*= NULL*/)
  158. {
  159. const Element* el = sc[index];
  160. if(!el) {
  161. DOMError("did not find required element \"" + index + "\"",element);
  162. }
  163. return *el;
  164. }
  165. // XXX: tacke code duplication in the various ReadVectorDataArray() overloads below.
  166. // could use a type traits based solution.
  167. // ------------------------------------------------------------------------------------------------
  168. // read an array of float3 tuples
  169. void ReadVectorDataArray(std::vector<aiVector3D>& out, const Element& el)
  170. {
  171. out.clear();
  172. const TokenList& tok = el.Tokens();
  173. const size_t dim = ParseTokenAsDim(*tok[0]);
  174. // may throw bad_alloc if the input is rubbish, but this need
  175. // not to be prevented - importing would fail but we wouldn't
  176. // crash since assimp handles this case properly.
  177. out.reserve(dim);
  178. const Scope& scope = GetRequiredScope(el);
  179. const Element& a = GetRequiredElement(scope,"a",&el);
  180. if (a.Tokens().size() % 3 != 0) {
  181. DOMError("number of floats is not a multiple of three (3)",&el);
  182. }
  183. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  184. aiVector3D v;
  185. v.x = ParseTokenAsFloat(**it++);
  186. v.y = ParseTokenAsFloat(**it++);
  187. v.z = ParseTokenAsFloat(**it++);
  188. out.push_back(v);
  189. }
  190. }
  191. // ------------------------------------------------------------------------------------------------
  192. // read an array of color4 tuples
  193. void ReadVectorDataArray(std::vector<aiColor4D>& out, const Element& el)
  194. {
  195. out.clear();
  196. const TokenList& tok = el.Tokens();
  197. const size_t dim = ParseTokenAsDim(*tok[0]);
  198. // see notes in ReadVectorDataArray() above
  199. out.reserve(dim);
  200. const Scope& scope = GetRequiredScope(el);
  201. const Element& a = GetRequiredElement(scope,"a",&el);
  202. if (a.Tokens().size() % 4 != 0) {
  203. DOMError("number of floats is not a multiple of four (4)",&el);
  204. }
  205. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  206. aiColor4D v;
  207. v.r = ParseTokenAsFloat(**it++);
  208. v.g = ParseTokenAsFloat(**it++);
  209. v.b = ParseTokenAsFloat(**it++);
  210. v.a = ParseTokenAsFloat(**it++);
  211. out.push_back(v);
  212. }
  213. }
  214. // ------------------------------------------------------------------------------------------------
  215. // read an array of float2 tuples
  216. void ReadVectorDataArray(std::vector<aiVector2D>& out, const Element& el)
  217. {
  218. out.clear();
  219. const TokenList& tok = el.Tokens();
  220. const size_t dim = ParseTokenAsDim(*tok[0]);
  221. // see notes in ReadVectorDataArray() above
  222. out.reserve(dim);
  223. const Scope& scope = GetRequiredScope(el);
  224. const Element& a = GetRequiredElement(scope,"a",&el);
  225. if (a.Tokens().size() % 2 != 0) {
  226. DOMError("number of floats is not a multiple of two (2)",&el);
  227. }
  228. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  229. aiVector2D v;
  230. v.x = ParseTokenAsFloat(**it++);
  231. v.y = ParseTokenAsFloat(**it++);
  232. out.push_back(v);
  233. }
  234. }
  235. // ------------------------------------------------------------------------------------------------
  236. // read an array of ints
  237. void ReadVectorDataArray(std::vector<int>& out, const Element& el)
  238. {
  239. out.clear();
  240. const TokenList& tok = el.Tokens();
  241. const size_t dim = ParseTokenAsDim(*tok[0]);
  242. // see notes in ReadVectorDataArray()
  243. out.reserve(dim);
  244. const Scope& scope = GetRequiredScope(el);
  245. const Element& a = GetRequiredElement(scope,"a",&el);
  246. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  247. const int ival = ParseTokenAsInt(**it++);
  248. out.push_back(ival);
  249. }
  250. }
  251. // ------------------------------------------------------------------------------------------------
  252. // read an array of floats
  253. void ReadVectorDataArray(std::vector<float>& out, const Element& el)
  254. {
  255. out.clear();
  256. const TokenList& tok = el.Tokens();
  257. const size_t dim = ParseTokenAsDim(*tok[0]);
  258. // see notes in ReadVectorDataArray()
  259. out.reserve(dim);
  260. const Scope& scope = GetRequiredScope(el);
  261. const Element& a = GetRequiredElement(scope,"a",&el);
  262. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  263. const float ival = ParseTokenAsFloat(**it++);
  264. out.push_back(ival);
  265. }
  266. }
  267. // ------------------------------------------------------------------------------------------------
  268. // read an array of uints
  269. void ReadVectorDataArray(std::vector<unsigned int>& out, const Element& el)
  270. {
  271. out.clear();
  272. const TokenList& tok = el.Tokens();
  273. const size_t dim = ParseTokenAsDim(*tok[0]);
  274. // see notes in ReadVectorDataArray()
  275. out.reserve(dim);
  276. const Scope& scope = GetRequiredScope(el);
  277. const Element& a = GetRequiredElement(scope,"a",&el);
  278. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  279. const int ival = ParseTokenAsInt(**it++);
  280. if(ival < 0) {
  281. DOMError("encountered negative integer index");
  282. }
  283. out.push_back(static_cast<unsigned int>(ival));
  284. }
  285. }
  286. // ------------------------------------------------------------------------------------------------
  287. // read an array of uint64_ts
  288. void ReadVectorDataArray(std::vector<uint64_t>& out, const Element& el)
  289. {
  290. out.clear();
  291. const TokenList& tok = el.Tokens();
  292. const size_t dim = ParseTokenAsDim(*tok[0]);
  293. // see notes in ReadVectorDataArray()
  294. out.reserve(dim);
  295. const Scope& scope = GetRequiredScope(el);
  296. const Element& a = GetRequiredElement(scope,"a",&el);
  297. for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
  298. const uint64_t ival = ParseTokenAsID(**it++);
  299. out.push_back(ival);
  300. }
  301. }
  302. // ------------------------------------------------------------------------------------------------
  303. aiMatrix4x4 ReadMatrix(const Element& element)
  304. {
  305. std::vector<float> values;
  306. ReadVectorDataArray(values,element);
  307. if(values.size() != 16) {
  308. DOMError("expected 16 matrix elements");
  309. }
  310. aiMatrix4x4 result;
  311. result.a1 = values[0];
  312. result.a2 = values[1];
  313. result.a3 = values[2];
  314. result.a4 = values[3];
  315. result.b1 = values[4];
  316. result.b2 = values[5];
  317. result.b3 = values[6];
  318. result.b4 = values[7];
  319. result.c1 = values[8];
  320. result.c2 = values[9];
  321. result.c3 = values[10];
  322. result.c4 = values[11];
  323. result.d1 = values[12];
  324. result.d2 = values[13];
  325. result.d3 = values[14];
  326. result.d4 = values[15];
  327. result.Transpose();
  328. return result;
  329. }
  330. // ------------------------------------------------------------------------------------------------
  331. // fetch a property table and the corresponding property template
  332. boost::shared_ptr<const PropertyTable> GetPropertyTable(const Document& doc,
  333. const std::string& templateName,
  334. const Element &element,
  335. const Scope& sc)
  336. {
  337. const Element* const Properties70 = sc["Properties70"];
  338. boost::shared_ptr<const PropertyTable> templateProps = boost::shared_ptr<const PropertyTable>(
  339. static_cast<const PropertyTable*>(NULL));
  340. if(templateName.length()) {
  341. PropertyTemplateMap::const_iterator it = doc.Templates().find(templateName);
  342. if(it != doc.Templates().end()) {
  343. templateProps = (*it).second;
  344. }
  345. }
  346. if(!Properties70) {
  347. DOMWarning("material property table (Properties70) not found",&element);
  348. if(templateProps) {
  349. return templateProps;
  350. }
  351. else {
  352. return boost::make_shared<const PropertyTable>();
  353. }
  354. }
  355. return boost::make_shared<const PropertyTable>(*Properties70,templateProps);
  356. }
  357. } // !Util
  358. } // !FBX
  359. } // !Assimp
  360. #endif