You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

733 line
23 KiB

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