BlenderDNA.inl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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 BlenderDNA.inl
  34. * @brief Blender `DNA` (file format specification embedded in
  35. * blend file itself) loader.
  36. */
  37. #ifndef INCLUDED_AI_BLEND_DNA_INL
  38. #define INCLUDED_AI_BLEND_DNA_INL
  39. #include <memory>
  40. namespace Assimp {
  41. namespace Blender {
  42. //--------------------------------------------------------------------------------
  43. const Field& Structure :: operator [] (const std::string& ss) const
  44. {
  45. std::map<std::string, size_t>::const_iterator it = indices.find(ss);
  46. if (it == indices.end()) {
  47. throw Error((Formatter::format(),
  48. "BlendDNA: Did not find a field named `",ss,"` in structure `",name,"`"
  49. ));
  50. }
  51. return fields[(*it).second];
  52. }
  53. //--------------------------------------------------------------------------------
  54. const Field* Structure :: Get (const std::string& ss) const
  55. {
  56. std::map<std::string, size_t>::const_iterator it = indices.find(ss);
  57. return it == indices.end() ? NULL : &fields[(*it).second];
  58. }
  59. //--------------------------------------------------------------------------------
  60. const Field& Structure :: operator [] (const size_t i) const
  61. {
  62. if (i >= fields.size()) {
  63. throw Error((Formatter::format(),
  64. "BlendDNA: There is no field with index `",i,"` in structure `",name,"`"
  65. ));
  66. }
  67. return fields[i];
  68. }
  69. //--------------------------------------------------------------------------------
  70. template <typename T> std::shared_ptr<ElemBase> Structure :: Allocate() const
  71. {
  72. return std::shared_ptr<T>(new T());
  73. }
  74. //--------------------------------------------------------------------------------
  75. template <typename T> void Structure :: Convert(
  76. std::shared_ptr<ElemBase> in,
  77. const FileDatabase& db) const
  78. {
  79. Convert<T> (*static_cast<T*> ( in.get() ),db);
  80. }
  81. //--------------------------------------------------------------------------------
  82. template <int error_policy, typename T, size_t M>
  83. void Structure :: ReadFieldArray(T (& out)[M], const char* name, const FileDatabase& db) const
  84. {
  85. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  86. try {
  87. const Field& f = (*this)[name];
  88. const Structure& s = db.dna[f.type];
  89. // is the input actually an array?
  90. if (!(f.flags & FieldFlag_Array)) {
  91. throw Error((Formatter::format(),"Field `",name,"` of structure `",
  92. this->name,"` ought to be an array of size ",M
  93. ));
  94. }
  95. db.reader->IncPtr(f.offset);
  96. // size conversions are always allowed, regardless of error_policy
  97. unsigned int i = 0;
  98. for(; i < std::min(f.array_sizes[0],M); ++i) {
  99. s.Convert(out[i],db);
  100. }
  101. for(; i < M; ++i) {
  102. _defaultInitializer<ErrorPolicy_Igno>()(out[i]);
  103. }
  104. }
  105. catch (const Error& e) {
  106. _defaultInitializer<error_policy>()(out,e.what());
  107. }
  108. // and recover the previous stream position
  109. db.reader->SetCurrentPos(old);
  110. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  111. ++db.stats().fields_read;
  112. #endif
  113. }
  114. //--------------------------------------------------------------------------------
  115. template <int error_policy, typename T, size_t M, size_t N>
  116. void Structure :: ReadFieldArray2(T (& out)[M][N], const char* name, const FileDatabase& db) const
  117. {
  118. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  119. try {
  120. const Field& f = (*this)[name];
  121. const Structure& s = db.dna[f.type];
  122. // is the input actually an array?
  123. if (!(f.flags & FieldFlag_Array)) {
  124. throw Error((Formatter::format(),"Field `",name,"` of structure `",
  125. this->name,"` ought to be an array of size ",M,"*",N
  126. ));
  127. }
  128. db.reader->IncPtr(f.offset);
  129. // size conversions are always allowed, regardless of error_policy
  130. unsigned int i = 0;
  131. for(; i < std::min(f.array_sizes[0],M); ++i) {
  132. unsigned int j = 0;
  133. for(; j < std::min(f.array_sizes[1],N); ++j) {
  134. s.Convert(out[i][j],db);
  135. }
  136. for(; j < N; ++j) {
  137. _defaultInitializer<ErrorPolicy_Igno>()(out[i][j]);
  138. }
  139. }
  140. for(; i < M; ++i) {
  141. _defaultInitializer<ErrorPolicy_Igno>()(out[i]);
  142. }
  143. }
  144. catch (const Error& e) {
  145. _defaultInitializer<error_policy>()(out,e.what());
  146. }
  147. // and recover the previous stream position
  148. db.reader->SetCurrentPos(old);
  149. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  150. ++db.stats().fields_read;
  151. #endif
  152. }
  153. //--------------------------------------------------------------------------------
  154. template <int error_policy, template <typename> class TOUT, typename T>
  155. bool Structure :: ReadFieldPtr(TOUT<T>& out, const char* name, const FileDatabase& db,
  156. bool non_recursive /*= false*/) const
  157. {
  158. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  159. Pointer ptrval;
  160. const Field* f;
  161. try {
  162. f = &(*this)[name];
  163. // sanity check, should never happen if the genblenddna script is right
  164. if (!(f->flags & FieldFlag_Pointer)) {
  165. throw Error((Formatter::format(),"Field `",name,"` of structure `",
  166. this->name,"` ought to be a pointer"));
  167. }
  168. db.reader->IncPtr(f->offset);
  169. Convert(ptrval,db);
  170. // actually it is meaningless on which Structure the Convert is called
  171. // because the `Pointer` argument triggers a special implementation.
  172. }
  173. catch (const Error& e) {
  174. _defaultInitializer<error_policy>()(out,e.what());
  175. out.reset();
  176. return false;
  177. }
  178. // resolve the pointer and load the corresponding structure
  179. const bool res = ResolvePointer(out,ptrval,db,*f, non_recursive);
  180. if(!non_recursive) {
  181. // and recover the previous stream position
  182. db.reader->SetCurrentPos(old);
  183. }
  184. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  185. ++db.stats().fields_read;
  186. #endif
  187. return res;
  188. }
  189. //--------------------------------------------------------------------------------
  190. template <int error_policy, template <typename> class TOUT, typename T, size_t N>
  191. bool Structure :: ReadFieldPtr(TOUT<T> (&out)[N], const char* name,
  192. const FileDatabase& db) const
  193. {
  194. // XXX see if we can reduce this to call to the 'normal' ReadFieldPtr
  195. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  196. Pointer ptrval[N];
  197. const Field* f;
  198. try {
  199. f = &(*this)[name];
  200. // sanity check, should never happen if the genblenddna script is right
  201. if ((FieldFlag_Pointer|FieldFlag_Pointer) != (f->flags & (FieldFlag_Pointer|FieldFlag_Pointer))) {
  202. throw Error((Formatter::format(),"Field `",name,"` of structure `",
  203. this->name,"` ought to be a pointer AND an array"));
  204. }
  205. db.reader->IncPtr(f->offset);
  206. size_t i = 0;
  207. for(; i < std::min(f->array_sizes[0],N); ++i) {
  208. Convert(ptrval[i],db);
  209. }
  210. for(; i < N; ++i) {
  211. _defaultInitializer<ErrorPolicy_Igno>()(ptrval[i]);
  212. }
  213. // actually it is meaningless on which Structure the Convert is called
  214. // because the `Pointer` argument triggers a special implementation.
  215. }
  216. catch (const Error& e) {
  217. _defaultInitializer<error_policy>()(out,e.what());
  218. for(size_t i = 0; i < N; ++i) {
  219. out[i].reset();
  220. }
  221. return false;
  222. }
  223. bool res = true;
  224. for(size_t i = 0; i < N; ++i) {
  225. // resolve the pointer and load the corresponding structure
  226. res = ResolvePointer(out[i],ptrval[i],db,*f) && res;
  227. }
  228. // and recover the previous stream position
  229. db.reader->SetCurrentPos(old);
  230. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  231. ++db.stats().fields_read;
  232. #endif
  233. return res;
  234. }
  235. //--------------------------------------------------------------------------------
  236. template <int error_policy, typename T>
  237. void Structure :: ReadField(T& out, const char* name, const FileDatabase& db) const
  238. {
  239. const StreamReaderAny::pos old = db.reader->GetCurrentPos();
  240. try {
  241. const Field& f = (*this)[name];
  242. // find the structure definition pertaining to this field
  243. const Structure& s = db.dna[f.type];
  244. db.reader->IncPtr(f.offset);
  245. s.Convert(out,db);
  246. }
  247. catch (const Error& e) {
  248. _defaultInitializer<error_policy>()(out,e.what());
  249. }
  250. // and recover the previous stream position
  251. db.reader->SetCurrentPos(old);
  252. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  253. ++db.stats().fields_read;
  254. #endif
  255. }
  256. //--------------------------------------------------------------------------------
  257. template <template <typename> class TOUT, typename T>
  258. bool Structure :: ResolvePointer(TOUT<T>& out, const Pointer & ptrval, const FileDatabase& db,
  259. const Field& f,
  260. bool non_recursive /*= false*/) const
  261. {
  262. out.reset(); // ensure null pointers work
  263. if (!ptrval.val) {
  264. return false;
  265. }
  266. const Structure& s = db.dna[f.type];
  267. // find the file block the pointer is pointing to
  268. const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
  269. // also determine the target type from the block header
  270. // and check if it matches the type which we expect.
  271. const Structure& ss = db.dna[block->dna_index];
  272. if (ss != s) {
  273. throw Error((Formatter::format(),"Expected target to be of type `",s.name,
  274. "` but seemingly it is a `",ss.name,"` instead"
  275. ));
  276. }
  277. // try to retrieve the object from the cache
  278. db.cache(out).get(s,out,ptrval);
  279. if (out) {
  280. return true;
  281. }
  282. // seek to this location, but save the previous stream pointer.
  283. const StreamReaderAny::pos pold = db.reader->GetCurrentPos();
  284. db.reader->SetCurrentPos(block->start+ static_cast<size_t>((ptrval.val - block->address.val) ));
  285. // FIXME: basically, this could cause problems with 64 bit pointers on 32 bit systems.
  286. // I really ought to improve StreamReader to work with 64 bit indices exclusively.
  287. // continue conversion after allocating the required storage
  288. size_t num = block->size / ss.size;
  289. T* o = _allocate(out,num);
  290. // cache the object before we convert it to avoid cyclic recursion.
  291. db.cache(out).set(s,out,ptrval);
  292. // if the non_recursive flag is set, we don't do anything but leave
  293. // the cursor at the correct position to resolve the object.
  294. if (!non_recursive) {
  295. for (size_t i = 0; i < num; ++i,++o) {
  296. s.Convert(*o,db);
  297. }
  298. db.reader->SetCurrentPos(pold);
  299. }
  300. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  301. if(out) {
  302. ++db.stats().pointers_resolved;
  303. }
  304. #endif
  305. return false;
  306. }
  307. //--------------------------------------------------------------------------------
  308. inline bool Structure :: ResolvePointer( std::shared_ptr< FileOffset >& out, const Pointer & ptrval,
  309. const FileDatabase& db,
  310. const Field&,
  311. bool) const
  312. {
  313. // Currently used exclusively by PackedFile::data to represent
  314. // a simple offset into the mapped BLEND file.
  315. out.reset();
  316. if (!ptrval.val) {
  317. return false;
  318. }
  319. // find the file block the pointer is pointing to
  320. const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
  321. out = std::shared_ptr< FileOffset > (new FileOffset());
  322. out->val = block->start+ static_cast<size_t>((ptrval.val - block->address.val) );
  323. return false;
  324. }
  325. //--------------------------------------------------------------------------------
  326. template <template <typename> class TOUT, typename T>
  327. bool Structure :: ResolvePointer(vector< TOUT<T> >& out, const Pointer & ptrval,
  328. const FileDatabase& db,
  329. const Field& f,
  330. bool) const
  331. {
  332. // This is a function overload, not a template specialization. According to
  333. // the partial ordering rules, it should be selected by the compiler
  334. // for array-of-pointer inputs, i.e. Object::mats.
  335. out.reset();
  336. if (!ptrval.val) {
  337. return false;
  338. }
  339. // find the file block the pointer is pointing to
  340. const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
  341. const size_t num = block->size / (db.i64bit?8:4);
  342. // keep the old stream position
  343. const StreamReaderAny::pos pold = db.reader->GetCurrentPos();
  344. db.reader->SetCurrentPos(block->start+ static_cast<size_t>((ptrval.val - block->address.val) ));
  345. bool res = false;
  346. // allocate raw storage for the array
  347. out.resize(num);
  348. for (size_t i = 0; i< num; ++i) {
  349. Pointer val;
  350. Convert(val,db);
  351. // and resolve the pointees
  352. res = ResolvePointer(out[i],val,db,f) && res;
  353. }
  354. db.reader->SetCurrentPos(pold);
  355. return res;
  356. }
  357. //--------------------------------------------------------------------------------
  358. template <> bool Structure :: ResolvePointer<std::shared_ptr,ElemBase>(std::shared_ptr<ElemBase>& out,
  359. const Pointer & ptrval,
  360. const FileDatabase& db,
  361. const Field&,
  362. bool
  363. ) const
  364. {
  365. // Special case when the data type needs to be determined at runtime.
  366. // Less secure than in the `strongly-typed` case.
  367. out.reset();
  368. if (!ptrval.val) {
  369. return false;
  370. }
  371. // find the file block the pointer is pointing to
  372. const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
  373. // determine the target type from the block header
  374. const Structure& s = db.dna[block->dna_index];
  375. // try to retrieve the object from the cache
  376. db.cache(out).get(s,out,ptrval);
  377. if (out) {
  378. return true;
  379. }
  380. // seek to this location, but save the previous stream pointer.
  381. const StreamReaderAny::pos pold = db.reader->GetCurrentPos();
  382. db.reader->SetCurrentPos(block->start+ static_cast<size_t>((ptrval.val - block->address.val) ));
  383. // FIXME: basically, this could cause problems with 64 bit pointers on 32 bit systems.
  384. // I really ought to improve StreamReader to work with 64 bit indices exclusively.
  385. // continue conversion after allocating the required storage
  386. DNA::FactoryPair builders = db.dna.GetBlobToStructureConverter(s,db);
  387. if (!builders.first) {
  388. // this might happen if DNA::RegisterConverters hasn't been called so far
  389. // or if the target type is not contained in `our` DNA.
  390. out.reset();
  391. DefaultLogger::get()->warn((Formatter::format(),
  392. "Failed to find a converter for the `",s.name,"` structure"
  393. ));
  394. return false;
  395. }
  396. // allocate the object hull
  397. out = (s.*builders.first)();
  398. // cache the object immediately to prevent infinite recursion in a
  399. // circular list with a single element (i.e. a self-referencing element).
  400. db.cache(out).set(s,out,ptrval);
  401. // and do the actual conversion
  402. (s.*builders.second)(out,db);
  403. db.reader->SetCurrentPos(pold);
  404. // store a pointer to the name string of the actual type
  405. // in the object itself. This allows the conversion code
  406. // to perform additional type checking.
  407. out->dna_type = s.name.c_str();
  408. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  409. ++db.stats().pointers_resolved;
  410. #endif
  411. return false;
  412. }
  413. //--------------------------------------------------------------------------------
  414. const FileBlockHead* Structure :: LocateFileBlockForAddress(const Pointer & ptrval, const FileDatabase& db) const
  415. {
  416. // the file blocks appear in list sorted by
  417. // with ascending base addresses so we can run a
  418. // binary search to locate the pointee quickly.
  419. // NOTE: Blender seems to distinguish between side-by-side
  420. // data (stored in the same data block) and far pointers,
  421. // which are only used for structures starting with an ID.
  422. // We don't need to make this distinction, our algorithm
  423. // works regardless where the data is stored.
  424. vector<FileBlockHead>::const_iterator it = std::lower_bound(db.entries.begin(),db.entries.end(),ptrval);
  425. if (it == db.entries.end()) {
  426. // this is crucial, pointers may not be invalid.
  427. // this is either a corrupted file or an attempted attack.
  428. throw DeadlyImportError((Formatter::format(),"Failure resolving pointer 0x",
  429. std::hex,ptrval.val,", no file block falls into this address range"
  430. ));
  431. }
  432. if (ptrval.val >= (*it).address.val + (*it).size) {
  433. throw DeadlyImportError((Formatter::format(),"Failure resolving pointer 0x",
  434. std::hex,ptrval.val,", nearest file block starting at 0x",
  435. (*it).address.val," ends at 0x",
  436. (*it).address.val + (*it).size
  437. ));
  438. }
  439. return &*it;
  440. }
  441. // ------------------------------------------------------------------------------------------------
  442. // NOTE: The MSVC debugger keeps showing up this annoying `a cast to a smaller data type has
  443. // caused a loss of data`-warning. Avoid this warning by a masking with an appropriate bitmask.
  444. template <typename T> struct signless;
  445. template <> struct signless<char> {typedef unsigned char type;};
  446. template <> struct signless<short> {typedef unsigned short type;};
  447. template <> struct signless<int> {typedef unsigned int type;};
  448. template <> struct signless<unsigned char> { typedef unsigned char type; };
  449. template <typename T>
  450. struct static_cast_silent {
  451. template <typename V>
  452. T operator()(V in) {
  453. return static_cast<T>(in & static_cast<typename signless<T>::type>(-1));
  454. }
  455. };
  456. template <> struct static_cast_silent<float> {
  457. template <typename V> float operator()(V in) {
  458. return static_cast<float> (in);
  459. }
  460. };
  461. template <> struct static_cast_silent<double> {
  462. template <typename V> double operator()(V in) {
  463. return static_cast<double>(in);
  464. }
  465. };
  466. // ------------------------------------------------------------------------------------------------
  467. template <typename T> inline void ConvertDispatcher(T& out, const Structure& in,const FileDatabase& db)
  468. {
  469. if (in.name == "int") {
  470. out = static_cast_silent<T>()(db.reader->GetU4());
  471. }
  472. else if (in.name == "short") {
  473. out = static_cast_silent<T>()(db.reader->GetU2());
  474. }
  475. else if (in.name == "char") {
  476. out = static_cast_silent<T>()(db.reader->GetU1());
  477. }
  478. else if (in.name == "float") {
  479. out = static_cast<T>(db.reader->GetF4());
  480. }
  481. else if (in.name == "double") {
  482. out = static_cast<T>(db.reader->GetF8());
  483. }
  484. else {
  485. throw DeadlyImportError("Unknown source for conversion to primitive data type: "+in.name);
  486. }
  487. }
  488. // ------------------------------------------------------------------------------------------------
  489. template <> inline void Structure :: Convert<int> (int& dest,const FileDatabase& db) const
  490. {
  491. ConvertDispatcher(dest,*this,db);
  492. }
  493. // ------------------------------------------------------------------------------------------------
  494. template <> inline void Structure :: Convert<short> (short& dest,const FileDatabase& db) const
  495. {
  496. // automatic rescaling from short to float and vice versa (seems to be used by normals)
  497. if (name == "float") {
  498. dest = static_cast<short>(db.reader->GetF4() * 32767.f);
  499. //db.reader->IncPtr(-4);
  500. return;
  501. }
  502. else if (name == "double") {
  503. dest = static_cast<short>(db.reader->GetF8() * 32767.);
  504. //db.reader->IncPtr(-8);
  505. return;
  506. }
  507. ConvertDispatcher(dest,*this,db);
  508. }
  509. // ------------------------------------------------------------------------------------------------
  510. template <> inline void Structure :: Convert<char> (char& dest,const FileDatabase& db) const
  511. {
  512. // automatic rescaling from char to float and vice versa (seems useful for RGB colors)
  513. if (name == "float") {
  514. dest = static_cast<char>(db.reader->GetF4() * 255.f);
  515. return;
  516. }
  517. else if (name == "double") {
  518. dest = static_cast<char>(db.reader->GetF8() * 255.f);
  519. return;
  520. }
  521. ConvertDispatcher(dest,*this,db);
  522. }
  523. // ------------------------------------------------------------------------------------------------
  524. template <> inline void Structure::Convert<unsigned char>(unsigned char& dest, const FileDatabase& db) const
  525. {
  526. // automatic rescaling from char to float and vice versa (seems useful for RGB colors)
  527. if (name == "float") {
  528. dest = static_cast<unsigned char>(db.reader->GetF4() * 255.f);
  529. return;
  530. }
  531. else if (name == "double") {
  532. dest = static_cast<unsigned char>(db.reader->GetF8() * 255.f);
  533. return;
  534. }
  535. ConvertDispatcher(dest, *this, db);
  536. }
  537. // ------------------------------------------------------------------------------------------------
  538. template <> inline void Structure :: Convert<float> (float& dest,const FileDatabase& db) const
  539. {
  540. // automatic rescaling from char to float and vice versa (seems useful for RGB colors)
  541. if (name == "char") {
  542. dest = db.reader->GetI1() / 255.f;
  543. return;
  544. }
  545. // automatic rescaling from short to float and vice versa (used by normals)
  546. else if (name == "short") {
  547. dest = db.reader->GetI2() / 32767.f;
  548. return;
  549. }
  550. ConvertDispatcher(dest,*this,db);
  551. }
  552. // ------------------------------------------------------------------------------------------------
  553. template <> inline void Structure :: Convert<double> (double& dest,const FileDatabase& db) const
  554. {
  555. if (name == "char") {
  556. dest = db.reader->GetI1() / 255.;
  557. return;
  558. }
  559. else if (name == "short") {
  560. dest = db.reader->GetI2() / 32767.;
  561. return;
  562. }
  563. ConvertDispatcher(dest,*this,db);
  564. }
  565. // ------------------------------------------------------------------------------------------------
  566. template <> inline void Structure :: Convert<Pointer> (Pointer& dest,const FileDatabase& db) const
  567. {
  568. if (db.i64bit) {
  569. dest.val = db.reader->GetU8();
  570. //db.reader->IncPtr(-8);
  571. return;
  572. }
  573. dest.val = db.reader->GetU4();
  574. //db.reader->IncPtr(-4);
  575. }
  576. //--------------------------------------------------------------------------------
  577. const Structure& DNA :: operator [] (const std::string& ss) const
  578. {
  579. std::map<std::string, size_t>::const_iterator it = indices.find(ss);
  580. if (it == indices.end()) {
  581. throw Error((Formatter::format(),
  582. "BlendDNA: Did not find a structure named `",ss,"`"
  583. ));
  584. }
  585. return structures[(*it).second];
  586. }
  587. //--------------------------------------------------------------------------------
  588. const Structure* DNA :: Get (const std::string& ss) const
  589. {
  590. std::map<std::string, size_t>::const_iterator it = indices.find(ss);
  591. return it == indices.end() ? NULL : &structures[(*it).second];
  592. }
  593. //--------------------------------------------------------------------------------
  594. const Structure& DNA :: operator [] (const size_t i) const
  595. {
  596. if (i >= structures.size()) {
  597. throw Error((Formatter::format(),
  598. "BlendDNA: There is no structure with index `",i,"`"
  599. ));
  600. }
  601. return structures[i];
  602. }
  603. //--------------------------------------------------------------------------------
  604. template <template <typename> class TOUT> template <typename T> void ObjectCache<TOUT> :: get (
  605. const Structure& s,
  606. TOUT<T>& out,
  607. const Pointer& ptr
  608. ) const {
  609. if(s.cache_idx == static_cast<size_t>(-1)) {
  610. s.cache_idx = db.next_cache_idx++;
  611. caches.resize(db.next_cache_idx);
  612. return;
  613. }
  614. typename StructureCache::const_iterator it = caches[s.cache_idx].find(ptr);
  615. if (it != caches[s.cache_idx].end()) {
  616. out = std::static_pointer_cast<T>( (*it).second );
  617. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  618. ++db.stats().cache_hits;
  619. #endif
  620. }
  621. // otherwise, out remains untouched
  622. }
  623. //--------------------------------------------------------------------------------
  624. template <template <typename> class TOUT> template <typename T> void ObjectCache<TOUT> :: set (
  625. const Structure& s,
  626. const TOUT<T>& out,
  627. const Pointer& ptr
  628. ) {
  629. if(s.cache_idx == static_cast<size_t>(-1)) {
  630. s.cache_idx = db.next_cache_idx++;
  631. caches.resize(db.next_cache_idx);
  632. }
  633. caches[s.cache_idx][ptr] = std::static_pointer_cast<ElemBase>( out );
  634. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  635. ++db.stats().cached_objects;
  636. #endif
  637. }
  638. }}
  639. #endif