BlenderDNA.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. /** @file BlenderDNA.h
  34. * @brief Blender `DNA` (file format specification embedded in
  35. * blend file itself) loader.
  36. */
  37. #ifndef INCLUDED_AI_BLEND_DNA_H
  38. #define INCLUDED_AI_BLEND_DNA_H
  39. #include <assimp/BaseImporter.h>
  40. #include <assimp/StreamReader.h>
  41. #include <assimp/DefaultLogger.hpp>
  42. #include <stdint.h>
  43. #include <memory>
  44. #include <map>
  45. // enable verbose log output. really verbose, so be careful.
  46. #ifdef ASSIMP_BUILD_DEBUG
  47. # define ASSIMP_BUILD_BLENDER_DEBUG
  48. #endif
  49. // #define ASSIMP_BUILD_BLENDER_NO_STATS
  50. namespace Assimp {
  51. template <bool,bool> class StreamReader;
  52. typedef StreamReader<true,true> StreamReaderAny;
  53. namespace Blender {
  54. class FileDatabase;
  55. struct FileBlockHead;
  56. template <template <typename> class TOUT>
  57. class ObjectCache;
  58. // -------------------------------------------------------------------------------
  59. /** Exception class used by the blender loader to selectively catch exceptions
  60. * thrown in its own code (DeadlyImportErrors thrown in general utility
  61. * functions are untouched then). If such an exception is not caught by
  62. * the loader itself, it will still be caught by Assimp due to its
  63. * ancestry. */
  64. // -------------------------------------------------------------------------------
  65. struct Error : DeadlyImportError {
  66. Error (const std::string& s)
  67. : DeadlyImportError(s) {
  68. // empty
  69. }
  70. };
  71. // -------------------------------------------------------------------------------
  72. /** The only purpose of this structure is to feed a virtual dtor into its
  73. * descendents. It serves as base class for all data structure fields. */
  74. // -------------------------------------------------------------------------------
  75. struct ElemBase {
  76. ElemBase()
  77. : dna_type(nullptr)
  78. {
  79. // empty
  80. }
  81. virtual ~ElemBase() {
  82. // empty
  83. }
  84. /** Type name of the element. The type
  85. * string points is the `c_str` of the `name` attribute of the
  86. * corresponding `Structure`, that is, it is only valid as long
  87. * as the DNA is not modified. The dna_type is only set if the
  88. * data type is not static, i.e. a std::shared_ptr<ElemBase>
  89. * in the scene description would have its type resolved
  90. * at runtime, so this member is always set. */
  91. const char* dna_type;
  92. };
  93. // -------------------------------------------------------------------------------
  94. /** Represents a generic pointer to a memory location, which can be either 32
  95. * or 64 bits. These pointers are loaded from the BLEND file and finally
  96. * fixed to point to the real, converted representation of the objects
  97. * they used to point to.*/
  98. // -------------------------------------------------------------------------------
  99. struct Pointer {
  100. Pointer()
  101. : val() {
  102. // empty
  103. }
  104. uint64_t val;
  105. };
  106. // -------------------------------------------------------------------------------
  107. /** Represents a generic offset within a BLEND file */
  108. // -------------------------------------------------------------------------------
  109. struct FileOffset {
  110. FileOffset()
  111. : val() {
  112. // empty
  113. }
  114. uint64_t val;
  115. };
  116. // -------------------------------------------------------------------------------
  117. /** Dummy derivate of std::vector to be able to use it in templates simultaenously
  118. * with std::shared_ptr, which takes only one template argument
  119. * while std::vector takes three. Also we need to provide some special member
  120. * functions of shared_ptr */
  121. // -------------------------------------------------------------------------------
  122. template <typename T>
  123. class vector : public std::vector<T> {
  124. public:
  125. using std::vector<T>::resize;
  126. using std::vector<T>::empty;
  127. void reset() {
  128. resize(0);
  129. }
  130. operator bool () const {
  131. return !empty();
  132. }
  133. };
  134. // -------------------------------------------------------------------------------
  135. /** Mixed flags for use in #Field */
  136. // -------------------------------------------------------------------------------
  137. enum FieldFlags {
  138. FieldFlag_Pointer = 0x1,
  139. FieldFlag_Array = 0x2
  140. };
  141. // -------------------------------------------------------------------------------
  142. /** Represents a single member of a data structure in a BLEND file */
  143. // -------------------------------------------------------------------------------
  144. struct Field {
  145. std::string name;
  146. std::string type;
  147. size_t size;
  148. size_t offset;
  149. /** Size of each array dimension. For flat arrays,
  150. * the second dimension is set to 1. */
  151. size_t array_sizes[2];
  152. /** Any of the #FieldFlags enumerated values */
  153. unsigned int flags;
  154. };
  155. // -------------------------------------------------------------------------------
  156. /** Range of possible behaviours for fields absend in the input file. Some are
  157. * mission critical so we need them, while others can silently be default
  158. * initialized and no animations are harmed. */
  159. // -------------------------------------------------------------------------------
  160. enum ErrorPolicy {
  161. /** Substitute default value and ignore */
  162. ErrorPolicy_Igno,
  163. /** Substitute default value and write to log */
  164. ErrorPolicy_Warn,
  165. /** Substitute a massive error message and crash the whole matrix. Its time for another zion */
  166. ErrorPolicy_Fail
  167. };
  168. #ifdef ASSIMP_BUILD_BLENDER_DEBUG
  169. # define ErrorPolicy_Igno ErrorPolicy_Warn
  170. #endif
  171. // -------------------------------------------------------------------------------
  172. /** Represents a data structure in a BLEND file. A Structure defines n fields
  173. * and their locations and encodings the input stream. Usually, every
  174. * Structure instance pertains to one equally-named data structure in the
  175. * BlenderScene.h header. This class defines various utilities to map a
  176. * binary `blob` read from the file to such a structure instance with
  177. * meaningful contents. */
  178. // -------------------------------------------------------------------------------
  179. class Structure {
  180. template <template <typename> class> friend class ObjectCache;
  181. public:
  182. Structure()
  183. : cache_idx(static_cast<size_t>(-1) ){
  184. // empty
  185. }
  186. public:
  187. // publicly accessible members
  188. std::string name;
  189. vector< Field > fields;
  190. std::map<std::string, size_t> indices;
  191. size_t size;
  192. public:
  193. // --------------------------------------------------------
  194. /** Access a field of the structure by its canonical name. The pointer version
  195. * returns NULL on failure while the reference version raises an import error. */
  196. inline const Field& operator [] (const std::string& ss) const;
  197. inline const Field* Get (const std::string& ss) const;
  198. // --------------------------------------------------------
  199. /** Access a field of the structure by its index */
  200. inline const Field& operator [] (const size_t i) const;
  201. // --------------------------------------------------------
  202. inline bool operator== (const Structure& other) const {
  203. return name == other.name; // name is meant to be an unique identifier
  204. }
  205. // --------------------------------------------------------
  206. inline bool operator!= (const Structure& other) const {
  207. return name != other.name;
  208. }
  209. public:
  210. // --------------------------------------------------------
  211. /** Try to read an instance of the structure from the stream
  212. * and attempt to convert to `T`. This is done by
  213. * an appropriate specialization. If none is available,
  214. * a compiler complain is the result.
  215. * @param dest Destination value to be written
  216. * @param db File database, including input stream. */
  217. template <typename T> void Convert (T& dest, const FileDatabase& db) const;
  218. // --------------------------------------------------------
  219. // generic converter
  220. template <typename T>
  221. void Convert(std::shared_ptr<ElemBase> in,const FileDatabase& db) const;
  222. // --------------------------------------------------------
  223. // generic allocator
  224. template <typename T> std::shared_ptr<ElemBase> Allocate() const;
  225. // --------------------------------------------------------
  226. // field parsing for 1d arrays
  227. template <int error_policy, typename T, size_t M>
  228. void ReadFieldArray(T (& out)[M], const char* name,
  229. const FileDatabase& db) const;
  230. // --------------------------------------------------------
  231. // field parsing for 2d arrays
  232. template <int error_policy, typename T, size_t M, size_t N>
  233. void ReadFieldArray2(T (& out)[M][N], const char* name,
  234. const FileDatabase& db) const;
  235. // --------------------------------------------------------
  236. // field parsing for pointer or dynamic array types
  237. // (std::shared_ptr)
  238. // The return value indicates whether the data was already cached.
  239. template <int error_policy, template <typename> class TOUT, typename T>
  240. bool ReadFieldPtr(TOUT<T>& out, const char* name,
  241. const FileDatabase& db,
  242. bool non_recursive = false) const;
  243. // --------------------------------------------------------
  244. // field parsing for static arrays of pointer or dynamic
  245. // array types (std::shared_ptr[])
  246. // The return value indicates whether the data was already cached.
  247. template <int error_policy, template <typename> class TOUT, typename T, size_t N>
  248. bool ReadFieldPtr(TOUT<T> (&out)[N], const char* name,
  249. const FileDatabase& db) const;
  250. // --------------------------------------------------------
  251. // field parsing for `normal` values
  252. // The return value indicates whether the data was already cached.
  253. template <int error_policy, typename T>
  254. void ReadField(T& out, const char* name,
  255. const FileDatabase& db) const;
  256. private:
  257. // --------------------------------------------------------
  258. template <template <typename> class TOUT, typename T>
  259. bool ResolvePointer(TOUT<T>& out, const Pointer & ptrval,
  260. const FileDatabase& db, const Field& f,
  261. bool non_recursive = false) const;
  262. // --------------------------------------------------------
  263. template <template <typename> class TOUT, typename T>
  264. bool ResolvePointer(vector< TOUT<T> >& out, const Pointer & ptrval,
  265. const FileDatabase& db, const Field& f, bool) const;
  266. // --------------------------------------------------------
  267. bool ResolvePointer( std::shared_ptr< FileOffset >& out, const Pointer & ptrval,
  268. const FileDatabase& db, const Field& f, bool) const;
  269. // --------------------------------------------------------
  270. inline const FileBlockHead* LocateFileBlockForAddress(
  271. const Pointer & ptrval,
  272. const FileDatabase& db) const;
  273. private:
  274. // ------------------------------------------------------------------------------
  275. template <typename T> T* _allocate(std::shared_ptr<T>& out, size_t& s) const {
  276. out = std::shared_ptr<T>(new T());
  277. s = 1;
  278. return out.get();
  279. }
  280. template <typename T> T* _allocate(vector<T>& out, size_t& s) const {
  281. out.resize(s);
  282. return s ? &out.front() : NULL;
  283. }
  284. // --------------------------------------------------------
  285. template <int error_policy>
  286. struct _defaultInitializer {
  287. template <typename T, unsigned int N>
  288. void operator ()(T (& out)[N], const char* = NULL) {
  289. for (unsigned int i = 0; i < N; ++i) {
  290. out[i] = T();
  291. }
  292. }
  293. template <typename T, unsigned int N, unsigned int M>
  294. void operator ()(T (& out)[N][M], const char* = NULL) {
  295. for (unsigned int i = 0; i < N; ++i) {
  296. for (unsigned int j = 0; j < M; ++j) {
  297. out[i][j] = T();
  298. }
  299. }
  300. }
  301. template <typename T>
  302. void operator ()(T& out, const char* = NULL) {
  303. out = T();
  304. }
  305. };
  306. private:
  307. mutable size_t cache_idx;
  308. };
  309. // --------------------------------------------------------
  310. template <> struct Structure :: _defaultInitializer<ErrorPolicy_Warn> {
  311. template <typename T>
  312. void operator ()(T& out, const char* reason = "<add reason>") {
  313. ASSIMP_LOG_WARN(reason);
  314. // ... and let the show go on
  315. _defaultInitializer<0 /*ErrorPolicy_Igno*/>()(out);
  316. }
  317. };
  318. template <> struct Structure :: _defaultInitializer<ErrorPolicy_Fail> {
  319. template <typename T>
  320. void operator ()(T& /*out*/,const char* = "") {
  321. // obviously, it is crucial that _DefaultInitializer is used
  322. // only from within a catch clause.
  323. throw;
  324. }
  325. };
  326. // -------------------------------------------------------------------------------------------------------
  327. template <> inline bool Structure :: ResolvePointer<std::shared_ptr,ElemBase>(std::shared_ptr<ElemBase>& out,
  328. const Pointer & ptrval,
  329. const FileDatabase& db,
  330. const Field& f,
  331. bool
  332. ) const;
  333. // -------------------------------------------------------------------------------
  334. /** Represents the full data structure information for a single BLEND file.
  335. * This data is extracted from the DNA1 chunk in the file.
  336. * #DNAParser does the reading and represents currently the only place where
  337. * DNA is altered.*/
  338. // -------------------------------------------------------------------------------
  339. class DNA
  340. {
  341. public:
  342. typedef void (Structure::*ConvertProcPtr) (
  343. std::shared_ptr<ElemBase> in,
  344. const FileDatabase&
  345. ) const;
  346. typedef std::shared_ptr<ElemBase> (
  347. Structure::*AllocProcPtr) () const;
  348. typedef std::pair< AllocProcPtr, ConvertProcPtr > FactoryPair;
  349. public:
  350. std::map<std::string, FactoryPair > converters;
  351. vector<Structure > structures;
  352. std::map<std::string, size_t> indices;
  353. public:
  354. // --------------------------------------------------------
  355. /** Access a structure by its canonical name, the pointer version returns NULL on failure
  356. * while the reference version raises an error. */
  357. inline const Structure& operator [] (const std::string& ss) const;
  358. inline const Structure* Get (const std::string& ss) const;
  359. // --------------------------------------------------------
  360. /** Access a structure by its index */
  361. inline const Structure& operator [] (const size_t i) const;
  362. public:
  363. // --------------------------------------------------------
  364. /** Add structure definitions for all the primitive types,
  365. * i.e. integer, short, char, float */
  366. void AddPrimitiveStructures();
  367. // --------------------------------------------------------
  368. /** Fill the @c converters member with converters for all
  369. * known data types. The implementation of this method is
  370. * in BlenderScene.cpp and is machine-generated.
  371. * Converters are used to quickly handle objects whose
  372. * exact data type is a runtime-property and not yet
  373. * known at compile time (consier Object::data).*/
  374. void RegisterConverters();
  375. // --------------------------------------------------------
  376. /** Take an input blob from the stream, interpret it according to
  377. * a its structure name and convert it to the intermediate
  378. * representation.
  379. * @param structure Destination structure definition
  380. * @param db File database.
  381. * @return A null pointer if no appropriate converter is available.*/
  382. std::shared_ptr< ElemBase > ConvertBlobToStructure(
  383. const Structure& structure,
  384. const FileDatabase& db
  385. ) const;
  386. // --------------------------------------------------------
  387. /** Find a suitable conversion function for a given Structure.
  388. * Such a converter function takes a blob from the input
  389. * stream, reads as much as it needs, and builds up a
  390. * complete object in intermediate representation.
  391. * @param structure Destination structure definition
  392. * @param db File database.
  393. * @return A null pointer in .first if no appropriate converter is available.*/
  394. FactoryPair GetBlobToStructureConverter(
  395. const Structure& structure,
  396. const FileDatabase& db
  397. ) const;
  398. #ifdef ASSIMP_BUILD_BLENDER_DEBUG
  399. // --------------------------------------------------------
  400. /** Dump the DNA to a text file. This is for debugging purposes.
  401. * The output file is `dna.txt` in the current working folder*/
  402. void DumpToFile();
  403. #endif
  404. // --------------------------------------------------------
  405. /** Extract array dimensions from a C array declaration, such
  406. * as `...[4][6]`. Returned string would be `...[][]`.
  407. * @param out
  408. * @param array_sizes Receive maximally two array dimensions,
  409. * the second element is set to 1 if the array is flat.
  410. * Both are set to 1 if the input is not an array.
  411. * @throw DeadlyImportError if more than 2 dimensions are
  412. * encountered. */
  413. static void ExtractArraySize(
  414. const std::string& out,
  415. size_t array_sizes[2]
  416. );
  417. };
  418. // special converters for primitive types
  419. template <> inline void Structure :: Convert<int> (int& dest,const FileDatabase& db) const;
  420. template <> inline void Structure :: Convert<short> (short& dest,const FileDatabase& db) const;
  421. template <> inline void Structure :: Convert<char> (char& dest,const FileDatabase& db) const;
  422. template <> inline void Structure :: Convert<float> (float& dest,const FileDatabase& db) const;
  423. template <> inline void Structure :: Convert<double> (double& dest,const FileDatabase& db) const;
  424. template <> inline void Structure :: Convert<Pointer> (Pointer& dest,const FileDatabase& db) const;
  425. // -------------------------------------------------------------------------------
  426. /** Describes a master file block header. Each master file sections holds n
  427. * elements of a certain SDNA structure (or otherwise unspecified data). */
  428. // -------------------------------------------------------------------------------
  429. struct FileBlockHead
  430. {
  431. // points right after the header of the file block
  432. StreamReaderAny::pos start;
  433. std::string id;
  434. size_t size;
  435. // original memory address of the data
  436. Pointer address;
  437. // index into DNA
  438. unsigned int dna_index;
  439. // number of structure instances to follow
  440. size_t num;
  441. // file blocks are sorted by address to quickly locate specific memory addresses
  442. bool operator < (const FileBlockHead& o) const {
  443. return address.val < o.address.val;
  444. }
  445. // for std::upper_bound
  446. operator const Pointer& () const {
  447. return address;
  448. }
  449. };
  450. // for std::upper_bound
  451. inline bool operator< (const Pointer& a, const Pointer& b) {
  452. return a.val < b.val;
  453. }
  454. // -------------------------------------------------------------------------------
  455. /** Utility to read all master file blocks in turn. */
  456. // -------------------------------------------------------------------------------
  457. class SectionParser
  458. {
  459. public:
  460. // --------------------------------------------------------
  461. /** @param stream Inout stream, must point to the
  462. * first section in the file. Call Next() once
  463. * to have it read.
  464. * @param ptr64 Pointer size in file is 64 bits? */
  465. SectionParser(StreamReaderAny& stream,bool ptr64)
  466. : stream(stream)
  467. , ptr64(ptr64)
  468. {
  469. current.size = current.start = 0;
  470. }
  471. public:
  472. // --------------------------------------------------------
  473. const FileBlockHead& GetCurrent() const {
  474. return current;
  475. }
  476. public:
  477. // --------------------------------------------------------
  478. /** Advance to the next section.
  479. * @throw DeadlyImportError if the last chunk was passed. */
  480. void Next();
  481. public:
  482. FileBlockHead current;
  483. StreamReaderAny& stream;
  484. bool ptr64;
  485. };
  486. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  487. // -------------------------------------------------------------------------------
  488. /** Import statistics, i.e. number of file blocks read*/
  489. // -------------------------------------------------------------------------------
  490. class Statistics {
  491. public:
  492. Statistics ()
  493. : fields_read ()
  494. , pointers_resolved ()
  495. , cache_hits ()
  496. // , blocks_read ()
  497. , cached_objects ()
  498. {}
  499. public:
  500. /** total number of fields we read */
  501. unsigned int fields_read;
  502. /** total number of resolved pointers */
  503. unsigned int pointers_resolved;
  504. /** number of pointers resolved from the cache */
  505. unsigned int cache_hits;
  506. /** number of blocks (from FileDatabase::entries)
  507. we did actually read from. */
  508. // unsigned int blocks_read;
  509. /** objects in FileData::cache */
  510. unsigned int cached_objects;
  511. };
  512. #endif
  513. // -------------------------------------------------------------------------------
  514. /** The object cache - all objects addressed by pointers are added here. This
  515. * avoids circular references and avoids object duplication. */
  516. // -------------------------------------------------------------------------------
  517. template <template <typename> class TOUT>
  518. class ObjectCache
  519. {
  520. public:
  521. typedef std::map< Pointer, TOUT<ElemBase> > StructureCache;
  522. public:
  523. ObjectCache(const FileDatabase& db)
  524. : db(db)
  525. {
  526. // currently there are only ~400 structure records per blend file.
  527. // we read only a small part of them and don't cache objects
  528. // which we don't need, so this should suffice.
  529. caches.reserve(64);
  530. }
  531. public:
  532. // --------------------------------------------------------
  533. /** Check whether a specific item is in the cache.
  534. * @param s Data type of the item
  535. * @param out Output pointer. Unchanged if the
  536. * cache doens't know the item yet.
  537. * @param ptr Item address to look for. */
  538. template <typename T> void get (
  539. const Structure& s,
  540. TOUT<T>& out,
  541. const Pointer& ptr) const;
  542. // --------------------------------------------------------
  543. /** Add an item to the cache after the item has
  544. * been fully read. Do not insert anything that
  545. * may be faulty or might cause the loading
  546. * to abort.
  547. * @param s Data type of the item
  548. * @param out Item to insert into the cache
  549. * @param ptr address (cache key) of the item. */
  550. template <typename T> void set
  551. (const Structure& s,
  552. const TOUT<T>& out,
  553. const Pointer& ptr);
  554. private:
  555. mutable vector<StructureCache> caches;
  556. const FileDatabase& db;
  557. };
  558. // -------------------------------------------------------------------------------
  559. // -------------------------------------------------------------------------------
  560. template <> class ObjectCache<Blender::vector>
  561. {
  562. public:
  563. ObjectCache(const FileDatabase&) {}
  564. template <typename T> void get(const Structure&, vector<T>&, const Pointer&) {}
  565. template <typename T> void set(const Structure&, const vector<T>&, const Pointer&) {}
  566. };
  567. #ifdef _MSC_VER
  568. # pragma warning(disable:4355)
  569. #endif
  570. // -------------------------------------------------------------------------------
  571. /** Memory representation of a full BLEND file and all its dependencies. The
  572. * output aiScene is constructed from an instance of this data structure. */
  573. // -------------------------------------------------------------------------------
  574. class FileDatabase
  575. {
  576. template <template <typename> class TOUT> friend class ObjectCache;
  577. public:
  578. FileDatabase()
  579. : _cacheArrays(*this)
  580. , _cache(*this)
  581. , next_cache_idx()
  582. {}
  583. public:
  584. // publicly accessible fields
  585. bool i64bit;
  586. bool little;
  587. DNA dna;
  588. std::shared_ptr< StreamReaderAny > reader;
  589. vector< FileBlockHead > entries;
  590. public:
  591. Statistics& stats() const {
  592. return _stats;
  593. }
  594. // For all our templates to work on both shared_ptr's and vector's
  595. // using the same code, a dummy cache for arrays is provided. Actually,
  596. // arrays of objects are never cached because we can't easily
  597. // ensure their proper destruction.
  598. template <typename T>
  599. ObjectCache<std::shared_ptr>& cache(std::shared_ptr<T>& /*in*/) const {
  600. return _cache;
  601. }
  602. template <typename T>
  603. ObjectCache<vector>& cache(vector<T>& /*in*/) const {
  604. return _cacheArrays;
  605. }
  606. private:
  607. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  608. mutable Statistics _stats;
  609. #endif
  610. mutable ObjectCache<vector> _cacheArrays;
  611. mutable ObjectCache<std::shared_ptr> _cache;
  612. mutable size_t next_cache_idx;
  613. };
  614. #ifdef _MSC_VER
  615. # pragma warning(default:4355)
  616. #endif
  617. // -------------------------------------------------------------------------------
  618. /** Factory to extract a #DNA from the DNA1 file block in a BLEND file. */
  619. // -------------------------------------------------------------------------------
  620. class DNAParser
  621. {
  622. public:
  623. /** Bind the parser to a empty DNA and an input stream */
  624. DNAParser(FileDatabase& db)
  625. : db(db)
  626. {}
  627. public:
  628. // --------------------------------------------------------
  629. /** Locate the DNA in the file and parse it. The input
  630. * stream is expected to point to the beginning of the DN1
  631. * chunk at the time this method is called and is
  632. * undefined afterwards.
  633. * @throw DeadlyImportError if the DNA cannot be read.
  634. * @note The position of the stream pointer is undefined
  635. * afterwards.*/
  636. void Parse ();
  637. public:
  638. /** Obtain a reference to the extracted DNA information */
  639. const Blender::DNA& GetDNA() const {
  640. return db.dna;
  641. }
  642. private:
  643. FileDatabase& db;
  644. };
  645. } // end Blend
  646. } // end Assimp
  647. #include "BlenderDNA.inl"
  648. #endif