2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2016-01-01 20:07:24 +00:00
|
|
|
Copyright (c) 2006-2016, assimp team
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-05-19 03:48:29 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file BlenderDNA.h
|
2015-05-19 03:52:10 +00:00
|
|
|
* @brief Blender `DNA` (file format specification embedded in
|
2015-05-19 03:48:29 +00:00
|
|
|
* blend file itself) loader.
|
|
|
|
*/
|
|
|
|
#ifndef INCLUDED_AI_BLEND_DNA_H
|
|
|
|
#define INCLUDED_AI_BLEND_DNA_H
|
|
|
|
|
|
|
|
#include "BaseImporter.h"
|
|
|
|
#include "TinyFormatter.h"
|
|
|
|
#include "StreamReader.h"
|
|
|
|
#include "../include/assimp/DefaultLogger.hpp"
|
|
|
|
#include <stdint.h>
|
2016-04-05 21:23:53 +00:00
|
|
|
#include <memory>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
// enable verbose log output. really verbose, so be careful.
|
|
|
|
#ifdef ASSIMP_BUILD_DEBUG
|
2015-05-19 03:57:13 +00:00
|
|
|
# define ASSIMP_BUILD_BLENDER_DEBUG
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// #define ASSIMP_BUILD_BLENDER_NO_STATS
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
namespace Assimp {
|
|
|
|
template <bool,bool> class StreamReader;
|
|
|
|
typedef StreamReader<true,true> StreamReaderAny;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
namespace Blender {
|
|
|
|
class FileDatabase;
|
|
|
|
struct FileBlockHead;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
template <template <typename> class TOUT>
|
|
|
|
class ObjectCache;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Exception class used by the blender loader to selectively catch exceptions
|
|
|
|
* thrown in its own code (DeadlyImportErrors thrown in general utility
|
|
|
|
* functions are untouched then). If such an exception is not caught by
|
|
|
|
* the loader itself, it will still be caught by Assimp due to its
|
|
|
|
* ancestry. */
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
struct Error : DeadlyImportError
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
Error (const std::string& s)
|
|
|
|
: DeadlyImportError(s)
|
|
|
|
{}
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** The only purpose of this structure is to feed a virtual dtor into its
|
|
|
|
* descendents. It serves as base class for all data structure fields. */
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
struct ElemBase
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
virtual ~ElemBase() {}
|
|
|
|
|
|
|
|
/** Type name of the element. The type
|
|
|
|
* string points is the `c_str` of the `name` attribute of the
|
|
|
|
* corresponding `Structure`, that is, it is only valid as long
|
|
|
|
* as the DNA is not modified. The dna_type is only set if the
|
2016-04-05 21:23:53 +00:00
|
|
|
* data type is not static, i.e. a std::shared_ptr<ElemBase>
|
2015-05-19 03:57:13 +00:00
|
|
|
* in the scene description would have its type resolved
|
|
|
|
* at runtime, so this member is always set. */
|
|
|
|
const char* dna_type;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Represents a generic pointer to a memory location, which can be either 32
|
|
|
|
* or 64 bits. These pointers are loaded from the BLEND file and finally
|
2015-05-19 03:52:10 +00:00
|
|
|
* fixed to point to the real, converted representation of the objects
|
2015-05-19 03:48:29 +00:00
|
|
|
* they used to point to.*/
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
struct Pointer
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
Pointer() : val() {}
|
|
|
|
uint64_t val;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Represents a generic offset within a BLEND file */
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
struct FileOffset
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
FileOffset() : val() {}
|
|
|
|
uint64_t val;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Dummy derivate of std::vector to be able to use it in templates simultaenously
|
2016-04-05 21:23:53 +00:00
|
|
|
* with std::shared_ptr, which takes only one template argument
|
2015-05-19 03:48:29 +00:00
|
|
|
* while std::vector takes three. Also we need to provide some special member
|
|
|
|
* functions of shared_ptr */
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
class vector : public std::vector<T>
|
|
|
|
{
|
|
|
|
public:
|
2015-05-19 03:57:13 +00:00
|
|
|
using std::vector<T>::resize;
|
|
|
|
using std::vector<T>::empty;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
void reset() {
|
|
|
|
resize(0);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
operator bool () const {
|
|
|
|
return !empty();
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Mixed flags for use in #Field */
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
enum FieldFlags
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
FieldFlag_Pointer = 0x1,
|
|
|
|
FieldFlag_Array = 0x2
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Represents a single member of a data structure in a BLEND file */
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
struct Field
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string name;
|
|
|
|
std::string type;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t size;
|
|
|
|
size_t offset;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
/** Size of each array dimension. For flat arrays,
|
|
|
|
* the second dimension is set to 1. */
|
|
|
|
size_t array_sizes[2];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
/** Any of the #FieldFlags enumerated values */
|
|
|
|
unsigned int flags;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Range of possible behaviours for fields absend in the input file. Some are
|
|
|
|
* mission critical so we need them, while others can silently be default
|
|
|
|
* initialized and no animations are harmed. */
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
enum ErrorPolicy
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
/** Substitute default value and ignore */
|
|
|
|
ErrorPolicy_Igno,
|
|
|
|
/** Substitute default value and write to log */
|
|
|
|
ErrorPolicy_Warn,
|
|
|
|
/** Substitute a massive error message and crash the whole matrix. Its time for another zion */
|
|
|
|
ErrorPolicy_Fail
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef ASSIMP_BUILD_BLENDER_DEBUG
|
2015-05-19 03:57:13 +00:00
|
|
|
# define ErrorPolicy_Igno ErrorPolicy_Warn
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Represents a data structure in a BLEND file. A Structure defines n fields
|
|
|
|
* and their locatios and encodings the input stream. Usually, every
|
|
|
|
* Structure instance pertains to one equally-named data structure in the
|
|
|
|
* BlenderScene.h header. This class defines various utilities to map a
|
|
|
|
* binary `blob` read from the file to such a structure instance with
|
|
|
|
* meaningful contents. */
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
class Structure
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
template <template <typename> class> friend class ObjectCache;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
Structure()
|
|
|
|
: cache_idx(-1)
|
|
|
|
{}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// publicly accessible members
|
|
|
|
std::string name;
|
|
|
|
vector< Field > fields;
|
|
|
|
std::map<std::string, size_t> indices;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t size;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Access a field of the structure by its canonical name. The pointer version
|
|
|
|
* returns NULL on failure while the reference version raises an import error. */
|
|
|
|
inline const Field& operator [] (const std::string& ss) const;
|
|
|
|
inline const Field* Get (const std::string& ss) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Access a field of the structure by its index */
|
|
|
|
inline const Field& operator [] (const size_t i) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
inline bool operator== (const Structure& other) const {
|
|
|
|
return name == other.name; // name is meant to be an unique identifier
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
inline bool operator!= (const Structure& other) const {
|
|
|
|
return name != other.name;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Try to read an instance of the structure from the stream
|
|
|
|
* and attempt to convert to `T`. This is done by
|
|
|
|
* an appropriate specialization. If none is available,
|
|
|
|
* a compiler complain is the result.
|
|
|
|
* @param dest Destination value to be written
|
|
|
|
* @param db File database, including input stream. */
|
|
|
|
template <typename T> inline void Convert (T& dest,
|
|
|
|
const FileDatabase& db) const;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// generic converter
|
|
|
|
template <typename T>
|
2016-04-05 21:23:53 +00:00
|
|
|
void Convert(std::shared_ptr<ElemBase> in,const FileDatabase& db) const;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// generic allocator
|
2016-04-05 21:23:53 +00:00
|
|
|
template <typename T> std::shared_ptr<ElemBase> Allocate() const;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// field parsing for 1d arrays
|
|
|
|
template <int error_policy, typename T, size_t M>
|
|
|
|
void ReadFieldArray(T (& out)[M], const char* name,
|
|
|
|
const FileDatabase& db) const;
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// field parsing for 2d arrays
|
|
|
|
template <int error_policy, typename T, size_t M, size_t N>
|
|
|
|
void ReadFieldArray2(T (& out)[M][N], const char* name,
|
|
|
|
const FileDatabase& db) const;
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// field parsing for pointer or dynamic array types
|
2016-04-05 21:23:53 +00:00
|
|
|
// (std::shared_ptr)
|
2015-05-19 03:57:13 +00:00
|
|
|
// The return value indicates whether the data was already cached.
|
|
|
|
template <int error_policy, template <typename> class TOUT, typename T>
|
|
|
|
bool ReadFieldPtr(TOUT<T>& out, const char* name,
|
|
|
|
const FileDatabase& db,
|
|
|
|
bool non_recursive = false) const;
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// field parsing for static arrays of pointer or dynamic
|
2016-04-05 21:23:53 +00:00
|
|
|
// array types (std::shared_ptr[])
|
2015-05-19 03:57:13 +00:00
|
|
|
// The return value indicates whether the data was already cached.
|
|
|
|
template <int error_policy, template <typename> class TOUT, typename T, size_t N>
|
|
|
|
bool ReadFieldPtr(TOUT<T> (&out)[N], const char* name,
|
|
|
|
const FileDatabase& db) const;
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// field parsing for `normal` values
|
|
|
|
// The return value indicates whether the data was already cached.
|
|
|
|
template <int error_policy, typename T>
|
|
|
|
void ReadField(T& out, const char* name,
|
|
|
|
const FileDatabase& db) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
template <template <typename> class TOUT, typename T>
|
|
|
|
bool ResolvePointer(TOUT<T>& out, const Pointer & ptrval,
|
|
|
|
const FileDatabase& db, const Field& f,
|
|
|
|
bool non_recursive = false) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
template <template <typename> class TOUT, typename T>
|
|
|
|
bool ResolvePointer(vector< TOUT<T> >& out, const Pointer & ptrval,
|
|
|
|
const FileDatabase& db, const Field& f, bool) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
bool ResolvePointer( std::shared_ptr< FileOffset >& out, const Pointer & ptrval,
|
2015-05-19 03:57:13 +00:00
|
|
|
const FileDatabase& db, const Field& f, bool) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
inline const FileBlockHead* LocateFileBlockForAddress(
|
|
|
|
const Pointer & ptrval,
|
|
|
|
const FileDatabase& db) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// ------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
template <typename T> T* _allocate(std::shared_ptr<T>& out, size_t& s) const {
|
|
|
|
out = std::shared_ptr<T>(new T());
|
2015-05-19 03:57:13 +00:00
|
|
|
s = 1;
|
|
|
|
return out.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> T* _allocate(vector<T>& out, size_t& s) const {
|
|
|
|
out.resize(s);
|
|
|
|
return s ? &out.front() : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
template <int error_policy>
|
|
|
|
struct _defaultInitializer {
|
|
|
|
|
|
|
|
template <typename T, unsigned int N>
|
|
|
|
void operator ()(T (& out)[N], const char* = NULL) {
|
|
|
|
for (unsigned int i = 0; i < N; ++i) {
|
|
|
|
out[i] = T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, unsigned int N, unsigned int M>
|
|
|
|
void operator ()(T (& out)[N][M], const char* = NULL) {
|
|
|
|
for (unsigned int i = 0; i < N; ++i) {
|
|
|
|
for (unsigned int j = 0; j < M; ++j) {
|
|
|
|
out[i][j] = T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void operator ()(T& out, const char* = NULL) {
|
|
|
|
out = T();
|
|
|
|
}
|
|
|
|
};
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
mutable size_t cache_idx;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
template <> struct Structure :: _defaultInitializer<ErrorPolicy_Warn> {
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
template <typename T>
|
|
|
|
void operator ()(T& out, const char* reason = "<add reason>") {
|
|
|
|
DefaultLogger::get()->warn(reason);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// ... and let the show go on
|
|
|
|
_defaultInitializer<0 /*ErrorPolicy_Igno*/>()(out);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct Structure :: _defaultInitializer<ErrorPolicy_Fail> {
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
template <typename T>
|
|
|
|
void operator ()(T& /*out*/,const char* = "") {
|
|
|
|
// obviously, it is crucial that _DefaultInitializer is used
|
|
|
|
// only from within a catch clause.
|
|
|
|
throw;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
template <> inline bool Structure :: ResolvePointer<std::shared_ptr,ElemBase>(std::shared_ptr<ElemBase>& out,
|
2015-05-19 03:57:13 +00:00
|
|
|
const Pointer & ptrval,
|
|
|
|
const FileDatabase& db,
|
|
|
|
const Field& f,
|
|
|
|
bool
|
|
|
|
) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Represents the full data structure information for a single BLEND file.
|
|
|
|
* This data is extracted from the DNA1 chunk in the file.
|
2015-05-19 03:52:10 +00:00
|
|
|
* #DNAParser does the reading and represents currently the only place where
|
2015-05-19 03:48:29 +00:00
|
|
|
* DNA is altered.*/
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
class DNA
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
typedef void (Structure::*ConvertProcPtr) (
|
2016-04-05 21:23:53 +00:00
|
|
|
std::shared_ptr<ElemBase> in,
|
2015-05-19 03:57:13 +00:00
|
|
|
const FileDatabase&
|
|
|
|
) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
typedef std::shared_ptr<ElemBase> (
|
2015-05-19 03:57:13 +00:00
|
|
|
Structure::*AllocProcPtr) () const;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
typedef std::pair< AllocProcPtr, ConvertProcPtr > FactoryPair;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
std::map<std::string, FactoryPair > converters;
|
|
|
|
vector<Structure > structures;
|
|
|
|
std::map<std::string, size_t> indices;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Access a structure by its canonical name, the pointer version returns NULL on failure
|
|
|
|
* while the reference version raises an error. */
|
|
|
|
inline const Structure& operator [] (const std::string& ss) const;
|
|
|
|
inline const Structure* Get (const std::string& ss) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Access a structure by its index */
|
|
|
|
inline const Structure& operator [] (const size_t i) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Add structure definitions for all the primitive types,
|
|
|
|
* i.e. integer, short, char, float */
|
|
|
|
void AddPrimitiveStructures();
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Fill the @c converters member with converters for all
|
|
|
|
* known data types. The implementation of this method is
|
|
|
|
* in BlenderScene.cpp and is machine-generated.
|
|
|
|
* Converters are used to quickly handle objects whose
|
|
|
|
* exact data type is a runtime-property and not yet
|
|
|
|
* known at compile time (consier Object::data).*/
|
|
|
|
void RegisterConverters();
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Take an input blob from the stream, interpret it according to
|
|
|
|
* a its structure name and convert it to the intermediate
|
|
|
|
* representation.
|
|
|
|
* @param structure Destination structure definition
|
|
|
|
* @param db File database.
|
|
|
|
* @return A null pointer if no appropriate converter is available.*/
|
2016-04-05 21:23:53 +00:00
|
|
|
std::shared_ptr< ElemBase > ConvertBlobToStructure(
|
2015-05-19 03:57:13 +00:00
|
|
|
const Structure& structure,
|
|
|
|
const FileDatabase& db
|
|
|
|
) const;
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Find a suitable conversion function for a given Structure.
|
|
|
|
* Such a converter function takes a blob from the input
|
|
|
|
* stream, reads as much as it needs, and builds up a
|
|
|
|
* complete object in intermediate representation.
|
|
|
|
* @param structure Destination structure definition
|
|
|
|
* @param db File database.
|
|
|
|
* @return A null pointer in .first if no appropriate converter is available.*/
|
|
|
|
FactoryPair GetBlobToStructureConverter(
|
|
|
|
const Structure& structure,
|
|
|
|
const FileDatabase& db
|
|
|
|
) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef ASSIMP_BUILD_BLENDER_DEBUG
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Dump the DNA to a text file. This is for debugging purposes.
|
|
|
|
* The output file is `dna.txt` in the current working folder*/
|
|
|
|
void DumpToFile();
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Extract array dimensions from a C array declaration, such
|
|
|
|
* as `...[4][6]`. Returned string would be `...[][]`.
|
|
|
|
* @param out
|
|
|
|
* @param array_sizes Receive maximally two array dimensions,
|
|
|
|
* the second element is set to 1 if the array is flat.
|
|
|
|
* Both are set to 1 if the input is not an array.
|
|
|
|
* @throw DeadlyImportError if more than 2 dimensions are
|
|
|
|
* encountered. */
|
|
|
|
static void ExtractArraySize(
|
|
|
|
const std::string& out,
|
|
|
|
size_t array_sizes[2]
|
|
|
|
);
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// special converters for primitive types
|
2015-05-19 03:57:13 +00:00
|
|
|
template <> inline void Structure :: Convert<int> (int& dest,const FileDatabase& db) const;
|
|
|
|
template <> inline void Structure :: Convert<short> (short& dest,const FileDatabase& db) const;
|
|
|
|
template <> inline void Structure :: Convert<char> (char& dest,const FileDatabase& db) const;
|
|
|
|
template <> inline void Structure :: Convert<float> (float& dest,const FileDatabase& db) const;
|
|
|
|
template <> inline void Structure :: Convert<double> (double& dest,const FileDatabase& db) const;
|
|
|
|
template <> inline void Structure :: Convert<Pointer> (Pointer& dest,const FileDatabase& db) const;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Describes a master file block header. Each master file sections holds n
|
|
|
|
* elements of a certain SDNA structure (or otherwise unspecified data). */
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
struct FileBlockHead
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
// points right after the header of the file block
|
|
|
|
StreamReaderAny::pos start;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string id;
|
|
|
|
size_t size;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// original memory address of the data
|
|
|
|
Pointer address;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// index into DNA
|
|
|
|
unsigned int dna_index;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// number of structure instances to follow
|
|
|
|
size_t num;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// file blocks are sorted by address to quickly locate specific memory addresses
|
|
|
|
bool operator < (const FileBlockHead& o) const {
|
|
|
|
return address.val < o.address.val;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// for std::upper_bound
|
|
|
|
operator const Pointer& () const {
|
|
|
|
return address;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// for std::upper_bound
|
|
|
|
inline bool operator< (const Pointer& a, const Pointer& b) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return a.val < b.val;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Utility to read all master file blocks in turn. */
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
class SectionParser
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** @param stream Inout stream, must point to the
|
|
|
|
* first section in the file. Call Next() once
|
|
|
|
* to have it read.
|
|
|
|
* @param ptr64 Pointer size in file is 64 bits? */
|
|
|
|
SectionParser(StreamReaderAny& stream,bool ptr64)
|
|
|
|
: stream(stream)
|
|
|
|
, ptr64(ptr64)
|
|
|
|
{
|
|
|
|
current.size = current.start = 0;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
const FileBlockHead& GetCurrent() const {
|
|
|
|
return current;
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Advance to the next section.
|
|
|
|
* @throw DeadlyImportError if the last chunk was passed. */
|
|
|
|
void Next();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
FileBlockHead current;
|
|
|
|
StreamReaderAny& stream;
|
|
|
|
bool ptr64;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Import statistics, i.e. number of file blocks read*/
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
class Statistics {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
Statistics ()
|
|
|
|
: fields_read ()
|
|
|
|
, pointers_resolved ()
|
|
|
|
, cache_hits ()
|
|
|
|
// , blocks_read ()
|
|
|
|
, cached_objects ()
|
|
|
|
{}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
/** total number of fields we read */
|
|
|
|
unsigned int fields_read;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
/** total number of resolved pointers */
|
|
|
|
unsigned int pointers_resolved;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
/** number of pointers resolved from the cache */
|
|
|
|
unsigned int cache_hits;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
/** number of blocks (from FileDatabase::entries)
|
|
|
|
we did actually read from. */
|
|
|
|
// unsigned int blocks_read;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
/** objects in FileData::cache */
|
|
|
|
unsigned int cached_objects;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** The object cache - all objects addressed by pointers are added here. This
|
|
|
|
* avoids circular references and avoids object duplication. */
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
template <template <typename> class TOUT>
|
2015-05-19 03:52:10 +00:00
|
|
|
class ObjectCache
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
typedef std::map< Pointer, TOUT<ElemBase> > StructureCache;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
ObjectCache(const FileDatabase& db)
|
|
|
|
: db(db)
|
|
|
|
{
|
|
|
|
// currently there are only ~400 structure records per blend file.
|
|
|
|
// we read only a small part of them and don't cache objects
|
|
|
|
// which we don't need, so this should suffice.
|
|
|
|
caches.reserve(64);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Check whether a specific item is in the cache.
|
|
|
|
* @param s Data type of the item
|
|
|
|
* @param out Output pointer. Unchanged if the
|
|
|
|
* cache doens't know the item yet.
|
|
|
|
* @param ptr Item address to look for. */
|
|
|
|
template <typename T> void get (
|
|
|
|
const Structure& s,
|
|
|
|
TOUT<T>& out,
|
|
|
|
const Pointer& ptr) const;
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Add an item to the cache after the item has
|
|
|
|
* been fully read. Do not insert anything that
|
|
|
|
* may be faulty or might cause the loading
|
|
|
|
* to abort.
|
|
|
|
* @param s Data type of the item
|
|
|
|
* @param out Item to insert into the cache
|
|
|
|
* @param ptr address (cache key) of the item. */
|
|
|
|
template <typename T> void set
|
|
|
|
(const Structure& s,
|
|
|
|
const TOUT<T>& out,
|
|
|
|
const Pointer& ptr);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
mutable vector<StructureCache> caches;
|
|
|
|
const FileDatabase& db;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
template <> class ObjectCache<Blender::vector>
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
ObjectCache(const FileDatabase&) {}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
template <typename T> void get(const Structure&, vector<T>&, const Pointer&) {}
|
|
|
|
template <typename T> void set(const Structure&, const vector<T>&, const Pointer&) {}
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2015-05-19 03:57:13 +00:00
|
|
|
# pragma warning(disable:4355)
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Memory representation of a full BLEND file and all its dependencies. The
|
|
|
|
* output aiScene is constructed from an instance of this data structure. */
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
class FileDatabase
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
template <template <typename> class TOUT> friend class ObjectCache;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
FileDatabase()
|
|
|
|
: _cacheArrays(*this)
|
|
|
|
, _cache(*this)
|
|
|
|
, next_cache_idx()
|
|
|
|
{}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// publicly accessible fields
|
|
|
|
bool i64bit;
|
|
|
|
bool little;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
DNA dna;
|
2016-04-05 21:23:53 +00:00
|
|
|
std::shared_ptr< StreamReaderAny > reader;
|
2015-05-19 03:57:13 +00:00
|
|
|
vector< FileBlockHead > entries;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
Statistics& stats() const {
|
|
|
|
return _stats;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// For all our templates to work on both shared_ptr's and vector's
|
|
|
|
// using the same code, a dummy cache for arrays is provided. Actually,
|
|
|
|
// arrays of objects are never cached because we can't easily
|
|
|
|
// ensure their proper destruction.
|
|
|
|
template <typename T>
|
2016-04-05 21:23:53 +00:00
|
|
|
ObjectCache<std::shared_ptr>& cache(std::shared_ptr<T>& /*in*/) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return _cache;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
template <typename T>
|
|
|
|
ObjectCache<vector>& cache(vector<T>& /*in*/) const {
|
|
|
|
return _cacheArrays;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-05-19 03:57:13 +00:00
|
|
|
mutable Statistics _stats;
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
mutable ObjectCache<vector> _cacheArrays;
|
2016-04-05 21:23:53 +00:00
|
|
|
mutable ObjectCache<std::shared_ptr> _cache;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
mutable size_t next_cache_idx;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2015-05-19 03:57:13 +00:00
|
|
|
# pragma warning(default:4355)
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
/** Factory to extract a #DNA from the DNA1 file block in a BLEND file. */
|
|
|
|
// -------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
class DNAParser
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
/** Bind the parser to a empty DNA and an input stream */
|
|
|
|
DNAParser(FileDatabase& db)
|
|
|
|
: db(db)
|
|
|
|
{}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// --------------------------------------------------------
|
|
|
|
/** Locate the DNA in the file and parse it. The input
|
|
|
|
* stream is expected to point to the beginning of the DN1
|
|
|
|
* chunk at the time this method is called and is
|
|
|
|
* undefined afterwards.
|
|
|
|
* @throw DeadlyImportError if the DNA cannot be read.
|
|
|
|
* @note The position of the stream pointer is undefined
|
|
|
|
* afterwards.*/
|
|
|
|
void Parse ();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
/** Obtain a reference to the extracted DNA information */
|
|
|
|
const Blender::DNA& GetDNA() const {
|
|
|
|
return db.dna;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
FileDatabase& db;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
} // end Blend
|
2015-05-19 03:48:29 +00:00
|
|
|
} // end Assimp
|
|
|
|
|
|
|
|
#include "BlenderDNA.inl"
|
|
|
|
|
|
|
|
#endif
|