BlenderDNA.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2019, 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. // --------------------------------------------------------
  257. /**
  258. * @brief field parsing for dynamic vectors
  259. * @param[in] out vector of struct to be filled
  260. * @param[in] name of field
  261. * @param[in] db to access the file, dna, ...
  262. * @return true when read was successful
  263. */
  264. template <int error_policy, template <typename> class TOUT, typename T>
  265. bool ReadFieldPtrVector(vector<TOUT<T>>&out, const char* name, const FileDatabase& db) const;
  266. /**
  267. * @brief parses raw customdata
  268. * @param[in] out shared_ptr to be filled
  269. * @param[in] cdtype customdata type to read
  270. * @param[in] name of field ptr
  271. * @param[in] db to access the file, dna, ...
  272. * @return true when read was successful
  273. */
  274. template <int error_policy>
  275. bool ReadCustomDataPtr(std::shared_ptr<ElemBase>&out, int cdtype, const char* name, const FileDatabase& db) const;
  276. private:
  277. // --------------------------------------------------------
  278. template <template <typename> class TOUT, typename T>
  279. bool ResolvePointer(TOUT<T>& out, const Pointer & ptrval,
  280. const FileDatabase& db, const Field& f,
  281. bool non_recursive = false) const;
  282. // --------------------------------------------------------
  283. template <template <typename> class TOUT, typename T>
  284. bool ResolvePointer(vector< TOUT<T> >& out, const Pointer & ptrval,
  285. const FileDatabase& db, const Field& f, bool) const;
  286. // --------------------------------------------------------
  287. bool ResolvePointer( std::shared_ptr< FileOffset >& out, const Pointer & ptrval,
  288. const FileDatabase& db, const Field& f, bool) const;
  289. // --------------------------------------------------------
  290. inline const FileBlockHead* LocateFileBlockForAddress(
  291. const Pointer & ptrval,
  292. const FileDatabase& db) const;
  293. private:
  294. // ------------------------------------------------------------------------------
  295. template <typename T> T* _allocate(std::shared_ptr<T>& out, size_t& s) const {
  296. out = std::shared_ptr<T>(new T());
  297. s = 1;
  298. return out.get();
  299. }
  300. template <typename T> T* _allocate(vector<T>& out, size_t& s) const {
  301. out.resize(s);
  302. return s ? &out.front() : NULL;
  303. }
  304. // --------------------------------------------------------
  305. template <int error_policy>
  306. struct _defaultInitializer {
  307. template <typename T, unsigned int N>
  308. void operator ()(T (& out)[N], const char* = NULL) {
  309. for (unsigned int i = 0; i < N; ++i) {
  310. out[i] = T();
  311. }
  312. }
  313. template <typename T, unsigned int N, unsigned int M>
  314. void operator ()(T (& out)[N][M], const char* = NULL) {
  315. for (unsigned int i = 0; i < N; ++i) {
  316. for (unsigned int j = 0; j < M; ++j) {
  317. out[i][j] = T();
  318. }
  319. }
  320. }
  321. template <typename T>
  322. void operator ()(T& out, const char* = NULL) {
  323. out = T();
  324. }
  325. };
  326. private:
  327. mutable size_t cache_idx;
  328. };
  329. // --------------------------------------------------------
  330. template <> struct Structure :: _defaultInitializer<ErrorPolicy_Warn> {
  331. template <typename T>
  332. void operator ()(T& out, const char* reason = "<add reason>") {
  333. ASSIMP_LOG_WARN(reason);
  334. // ... and let the show go on
  335. _defaultInitializer<0 /*ErrorPolicy_Igno*/>()(out);
  336. }
  337. };
  338. template <> struct Structure :: _defaultInitializer<ErrorPolicy_Fail> {
  339. template <typename T>
  340. void operator ()(T& /*out*/,const char* = "") {
  341. // obviously, it is crucial that _DefaultInitializer is used
  342. // only from within a catch clause.
  343. throw;
  344. }
  345. };
  346. // -------------------------------------------------------------------------------------------------------
  347. template <> inline bool Structure :: ResolvePointer<std::shared_ptr,ElemBase>(std::shared_ptr<ElemBase>& out,
  348. const Pointer & ptrval,
  349. const FileDatabase& db,
  350. const Field& f,
  351. bool
  352. ) const;
  353. // -------------------------------------------------------------------------------
  354. /** Represents the full data structure information for a single BLEND file.
  355. * This data is extracted from the DNA1 chunk in the file.
  356. * #DNAParser does the reading and represents currently the only place where
  357. * DNA is altered.*/
  358. // -------------------------------------------------------------------------------
  359. class DNA
  360. {
  361. public:
  362. typedef void (Structure::*ConvertProcPtr) (
  363. std::shared_ptr<ElemBase> in,
  364. const FileDatabase&
  365. ) const;
  366. typedef std::shared_ptr<ElemBase> (
  367. Structure::*AllocProcPtr) () const;
  368. typedef std::pair< AllocProcPtr, ConvertProcPtr > FactoryPair;
  369. public:
  370. std::map<std::string, FactoryPair > converters;
  371. vector<Structure > structures;
  372. std::map<std::string, size_t> indices;
  373. public:
  374. // --------------------------------------------------------
  375. /** Access a structure by its canonical name, the pointer version returns NULL on failure
  376. * while the reference version raises an error. */
  377. inline const Structure& operator [] (const std::string& ss) const;
  378. inline const Structure* Get (const std::string& ss) const;
  379. // --------------------------------------------------------
  380. /** Access a structure by its index */
  381. inline const Structure& operator [] (const size_t i) const;
  382. public:
  383. // --------------------------------------------------------
  384. /** Add structure definitions for all the primitive types,
  385. * i.e. integer, short, char, float */
  386. void AddPrimitiveStructures();
  387. // --------------------------------------------------------
  388. /** Fill the @c converters member with converters for all
  389. * known data types. The implementation of this method is
  390. * in BlenderScene.cpp and is machine-generated.
  391. * Converters are used to quickly handle objects whose
  392. * exact data type is a runtime-property and not yet
  393. * known at compile time (consier Object::data).*/
  394. void RegisterConverters();
  395. // --------------------------------------------------------
  396. /** Take an input blob from the stream, interpret it according to
  397. * a its structure name and convert it to the intermediate
  398. * representation.
  399. * @param structure Destination structure definition
  400. * @param db File database.
  401. * @return A null pointer if no appropriate converter is available.*/
  402. std::shared_ptr< ElemBase > ConvertBlobToStructure(
  403. const Structure& structure,
  404. const FileDatabase& db
  405. ) const;
  406. // --------------------------------------------------------
  407. /** Find a suitable conversion function for a given Structure.
  408. * Such a converter function takes a blob from the input
  409. * stream, reads as much as it needs, and builds up a
  410. * complete object in intermediate representation.
  411. * @param structure Destination structure definition
  412. * @param db File database.
  413. * @return A null pointer in .first if no appropriate converter is available.*/
  414. FactoryPair GetBlobToStructureConverter(
  415. const Structure& structure,
  416. const FileDatabase& db
  417. ) const;
  418. #ifdef ASSIMP_BUILD_BLENDER_DEBUG
  419. // --------------------------------------------------------
  420. /** Dump the DNA to a text file. This is for debugging purposes.
  421. * The output file is `dna.txt` in the current working folder*/
  422. void DumpToFile();
  423. #endif
  424. // --------------------------------------------------------
  425. /** Extract array dimensions from a C array declaration, such
  426. * as `...[4][6]`. Returned string would be `...[][]`.
  427. * @param out
  428. * @param array_sizes Receive maximally two array dimensions,
  429. * the second element is set to 1 if the array is flat.
  430. * Both are set to 1 if the input is not an array.
  431. * @throw DeadlyImportError if more than 2 dimensions are
  432. * encountered. */
  433. static void ExtractArraySize(
  434. const std::string& out,
  435. size_t array_sizes[2]
  436. );
  437. };
  438. // special converters for primitive types
  439. template <> inline void Structure :: Convert<int> (int& dest,const FileDatabase& db) const;
  440. template <> inline void Structure :: Convert<short> (short& dest,const FileDatabase& db) const;
  441. template <> inline void Structure :: Convert<char> (char& dest,const FileDatabase& db) const;
  442. template <> inline void Structure :: Convert<float> (float& dest,const FileDatabase& db) const;
  443. template <> inline void Structure :: Convert<double> (double& dest,const FileDatabase& db) const;
  444. template <> inline void Structure :: Convert<Pointer> (Pointer& dest,const FileDatabase& db) const;
  445. // -------------------------------------------------------------------------------
  446. /** Describes a master file block header. Each master file sections holds n
  447. * elements of a certain SDNA structure (or otherwise unspecified data). */
  448. // -------------------------------------------------------------------------------
  449. struct FileBlockHead
  450. {
  451. // points right after the header of the file block
  452. StreamReaderAny::pos start;
  453. std::string id;
  454. size_t size;
  455. // original memory address of the data
  456. Pointer address;
  457. // index into DNA
  458. unsigned int dna_index;
  459. // number of structure instances to follow
  460. size_t num;
  461. // file blocks are sorted by address to quickly locate specific memory addresses
  462. bool operator < (const FileBlockHead& o) const {
  463. return address.val < o.address.val;
  464. }
  465. // for std::upper_bound
  466. operator const Pointer& () const {
  467. return address;
  468. }
  469. };
  470. // for std::upper_bound
  471. inline bool operator< (const Pointer& a, const Pointer& b) {
  472. return a.val < b.val;
  473. }
  474. // -------------------------------------------------------------------------------
  475. /** Utility to read all master file blocks in turn. */
  476. // -------------------------------------------------------------------------------
  477. class SectionParser
  478. {
  479. public:
  480. // --------------------------------------------------------
  481. /** @param stream Inout stream, must point to the
  482. * first section in the file. Call Next() once
  483. * to have it read.
  484. * @param ptr64 Pointer size in file is 64 bits? */
  485. SectionParser(StreamReaderAny& stream,bool ptr64)
  486. : stream(stream)
  487. , ptr64(ptr64)
  488. {
  489. current.size = current.start = 0;
  490. }
  491. public:
  492. // --------------------------------------------------------
  493. const FileBlockHead& GetCurrent() const {
  494. return current;
  495. }
  496. public:
  497. // --------------------------------------------------------
  498. /** Advance to the next section.
  499. * @throw DeadlyImportError if the last chunk was passed. */
  500. void Next();
  501. public:
  502. FileBlockHead current;
  503. StreamReaderAny& stream;
  504. bool ptr64;
  505. };
  506. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  507. // -------------------------------------------------------------------------------
  508. /** Import statistics, i.e. number of file blocks read*/
  509. // -------------------------------------------------------------------------------
  510. class Statistics {
  511. public:
  512. Statistics ()
  513. : fields_read ()
  514. , pointers_resolved ()
  515. , cache_hits ()
  516. // , blocks_read ()
  517. , cached_objects ()
  518. {}
  519. public:
  520. /** total number of fields we read */
  521. unsigned int fields_read;
  522. /** total number of resolved pointers */
  523. unsigned int pointers_resolved;
  524. /** number of pointers resolved from the cache */
  525. unsigned int cache_hits;
  526. /** number of blocks (from FileDatabase::entries)
  527. we did actually read from. */
  528. // unsigned int blocks_read;
  529. /** objects in FileData::cache */
  530. unsigned int cached_objects;
  531. };
  532. #endif
  533. // -------------------------------------------------------------------------------
  534. /** The object cache - all objects addressed by pointers are added here. This
  535. * avoids circular references and avoids object duplication. */
  536. // -------------------------------------------------------------------------------
  537. template <template <typename> class TOUT>
  538. class ObjectCache
  539. {
  540. public:
  541. typedef std::map< Pointer, TOUT<ElemBase> > StructureCache;
  542. public:
  543. ObjectCache(const FileDatabase& db)
  544. : db(db)
  545. {
  546. // currently there are only ~400 structure records per blend file.
  547. // we read only a small part of them and don't cache objects
  548. // which we don't need, so this should suffice.
  549. caches.reserve(64);
  550. }
  551. public:
  552. // --------------------------------------------------------
  553. /** Check whether a specific item is in the cache.
  554. * @param s Data type of the item
  555. * @param out Output pointer. Unchanged if the
  556. * cache doesn't know the item yet.
  557. * @param ptr Item address to look for. */
  558. template <typename T> void get (
  559. const Structure& s,
  560. TOUT<T>& out,
  561. const Pointer& ptr) const;
  562. // --------------------------------------------------------
  563. /** Add an item to the cache after the item has
  564. * been fully read. Do not insert anything that
  565. * may be faulty or might cause the loading
  566. * to abort.
  567. * @param s Data type of the item
  568. * @param out Item to insert into the cache
  569. * @param ptr address (cache key) of the item. */
  570. template <typename T> void set
  571. (const Structure& s,
  572. const TOUT<T>& out,
  573. const Pointer& ptr);
  574. private:
  575. mutable vector<StructureCache> caches;
  576. const FileDatabase& db;
  577. };
  578. // -------------------------------------------------------------------------------
  579. // -------------------------------------------------------------------------------
  580. template <> class ObjectCache<Blender::vector>
  581. {
  582. public:
  583. ObjectCache(const FileDatabase&) {}
  584. template <typename T> void get(const Structure&, vector<T>&, const Pointer&) {}
  585. template <typename T> void set(const Structure&, const vector<T>&, const Pointer&) {}
  586. };
  587. #ifdef _MSC_VER
  588. # pragma warning(disable:4355)
  589. #endif
  590. // -------------------------------------------------------------------------------
  591. /** Memory representation of a full BLEND file and all its dependencies. The
  592. * output aiScene is constructed from an instance of this data structure. */
  593. // -------------------------------------------------------------------------------
  594. class FileDatabase
  595. {
  596. template <template <typename> class TOUT> friend class ObjectCache;
  597. public:
  598. FileDatabase()
  599. : _cacheArrays(*this)
  600. , _cache(*this)
  601. , next_cache_idx()
  602. {}
  603. public:
  604. // publicly accessible fields
  605. bool i64bit;
  606. bool little;
  607. DNA dna;
  608. std::shared_ptr< StreamReaderAny > reader;
  609. vector< FileBlockHead > entries;
  610. public:
  611. Statistics& stats() const {
  612. return _stats;
  613. }
  614. // For all our templates to work on both shared_ptr's and vector's
  615. // using the same code, a dummy cache for arrays is provided. Actually,
  616. // arrays of objects are never cached because we can't easily
  617. // ensure their proper destruction.
  618. template <typename T>
  619. ObjectCache<std::shared_ptr>& cache(std::shared_ptr<T>& /*in*/) const {
  620. return _cache;
  621. }
  622. template <typename T>
  623. ObjectCache<vector>& cache(vector<T>& /*in*/) const {
  624. return _cacheArrays;
  625. }
  626. private:
  627. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  628. mutable Statistics _stats;
  629. #endif
  630. mutable ObjectCache<vector> _cacheArrays;
  631. mutable ObjectCache<std::shared_ptr> _cache;
  632. mutable size_t next_cache_idx;
  633. };
  634. #ifdef _MSC_VER
  635. # pragma warning(default:4355)
  636. #endif
  637. // -------------------------------------------------------------------------------
  638. /** Factory to extract a #DNA from the DNA1 file block in a BLEND file. */
  639. // -------------------------------------------------------------------------------
  640. class DNAParser
  641. {
  642. public:
  643. /** Bind the parser to a empty DNA and an input stream */
  644. DNAParser(FileDatabase& db)
  645. : db(db)
  646. {}
  647. public:
  648. // --------------------------------------------------------
  649. /** Locate the DNA in the file and parse it. The input
  650. * stream is expected to point to the beginning of the DN1
  651. * chunk at the time this method is called and is
  652. * undefined afterwards.
  653. * @throw DeadlyImportError if the DNA cannot be read.
  654. * @note The position of the stream pointer is undefined
  655. * afterwards.*/
  656. void Parse ();
  657. public:
  658. /** Obtain a reference to the extracted DNA information */
  659. const Blender::DNA& GetDNA() const {
  660. return db.dna;
  661. }
  662. private:
  663. FileDatabase& db;
  664. };
  665. /**
  666. * @brief read CustomData's data to ptr to mem
  667. * @param[out] out memory ptr to set
  668. * @param[in] cdtype to read
  669. * @param[in] cnt cnt of elements to read
  670. * @param[in] db to read elements from
  671. * @return true when ok
  672. */
  673. bool readCustomData(std::shared_ptr<ElemBase> &out, int cdtype, size_t cnt, const FileDatabase &db);
  674. } // end Blend
  675. } // end Assimp
  676. #include "BlenderDNA.inl"
  677. #endif