BlenderDNA.h 29 KB

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