/* Open Asset Import Library (ASSIMP) ---------------------------------------------------------------------- Copyright (c) 2006-2010, ASSIMP Development Team All rights reserved. Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the 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 Development Team. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------- */ /** @file BlenderDNA.inl * @brief Blender `DNA` (file format specification embedded in * blend file itself) loader. */ #ifndef INCLUDED_AI_BLEND_DNA_INL #define INCLUDED_AI_BLEND_DNA_INL namespace Assimp { namespace Blender { //-------------------------------------------------------------------------------- const Field& Structure :: operator [] (const std::string& ss) const { std::map::const_iterator it = indices.find(ss); if (it == indices.end()) { throw Error((Formatter::format(), "BlendDNA: Did not find a field named `",ss,"` in structure `",name,"`" )); } return fields[(*it).second]; } //-------------------------------------------------------------------------------- const Field& Structure :: operator [] (const size_t i) const { if (i >= fields.size()) { throw Error((Formatter::format(), "BlendDNA: There is no field with index `",i,"` in structure `",name,"`" )); } return fields[i]; } //-------------------------------------------------------------------------------- template boost::shared_ptr Structure :: Convert( const FileDatabase& db) const { // FIXME: use boost::make_shared boost::shared_ptr s = boost::shared_ptr(new T()); Convert (*s.get(),db); return s; } //-------------------------------------------------------------------------------- template void Structure :: ReadFieldArray(T (& out)[M], const char* name, const FileDatabase& db) const { const StreamReaderAny::pos old = db.reader->GetCurrentPos(); try { const Field& f = (*this)[name]; const Structure& s = db.dna[f.type]; // is the input actually an array? if (!(f.flags & FieldFlag_Array)) { throw Error((Formatter::format(),"Field `",name,"` of structure `", this->name,"` ought to be an array of size ",M )); } db.reader->IncPtr(f.offset); // size conversions are always allowed, regardless of error_policy unsigned int i = 0; for(; i < std::min(f.array_sizes[0],M); ++i) { s.Convert(out[i],db); } for(; i < M; ++i) { _defaultInitializer()(out[i]); } } catch (const Error& e) { _defaultInitializer()(out,e.what()); } // and recover the previous stream position db.reader->SetCurrentPos(old); #ifndef ASSIMP_BUILD_BLENDER_NO_STATS ++db.stats().fields_read; #endif } //-------------------------------------------------------------------------------- template void Structure :: ReadFieldArray2(T (& out)[M][N], const char* name, const FileDatabase& db) const { const StreamReaderAny::pos old = db.reader->GetCurrentPos(); try { const Field& f = (*this)[name]; const Structure& s = db.dna[f.type]; // is the input actually an array? if (!(f.flags & FieldFlag_Array)) { throw Error((Formatter::format(),"Field `",name,"` of structure `", this->name,"` ought to be an array of size ",M,"*",N )); } db.reader->IncPtr(f.offset); // size conversions are always allowed, regardless of error_policy unsigned int i = 0; for(; i < std::min(f.array_sizes[0],M); ++i) { unsigned int j = 0; for(; j < std::min(f.array_sizes[1],N); ++j) { s.Convert(out[i][j],db); } for(; j < N; ++j) { _defaultInitializer()(out[i][j]); } } for(; i < M; ++i) { _defaultInitializer()(out[i]); } } catch (const Error& e) { _defaultInitializer()(out,e.what()); } // and recover the previous stream position db.reader->SetCurrentPos(old); #ifndef ASSIMP_BUILD_BLENDER_NO_STATS ++db.stats().fields_read; #endif } //-------------------------------------------------------------------------------- template class TOUT, typename T> void Structure :: ReadFieldPtr(TOUT& out, const char* name, const FileDatabase& db) const { const StreamReaderAny::pos old = db.reader->GetCurrentPos(); Pointer ptrval; const Field* f; try { f = &(*this)[name]; // sanity check, should never happen if the genblenddna script is right if (!(f->flags & FieldFlag_Pointer)) { throw Error((Formatter::format(),"Field `",name,"` of structure `", this->name,"` ought to be a pointer")); } db.reader->IncPtr(f->offset); Convert(ptrval,db); // actually it is meaningless on which Structure the Convert is called // because the `Pointer` argument triggers a special implementation. } catch (const Error& e) { _defaultInitializer()(out,e.what()); out.reset(); return; } // resolve the pointer and load the corresponding structure ResolvePointer(out,ptrval,db,*f); // and recover the previous stream position db.reader->SetCurrentPos(old); #ifndef ASSIMP_BUILD_BLENDER_NO_STATS ++db.stats().fields_read; #endif } //-------------------------------------------------------------------------------- template class TOUT, typename T, size_t N> void Structure :: ReadFieldPtr(TOUT (&out)[N], const char* name, const FileDatabase& db) const { // XXX see if we can reduce this to call to the 'normal' ReadFieldPtr const StreamReaderAny::pos old = db.reader->GetCurrentPos(); Pointer ptrval[N]; const Field* f; try { f = &(*this)[name]; // sanity check, should never happen if the genblenddna script is right if ((FieldFlag_Pointer|FieldFlag_Pointer) != (f->flags & (FieldFlag_Pointer|FieldFlag_Pointer))) { throw Error((Formatter::format(),"Field `",name,"` of structure `", this->name,"` ought to be a pointer AND an array")); } db.reader->IncPtr(f->offset); size_t i = 0; for(; i < std::min(f->array_sizes[0],N); ++i) { Convert(ptrval[i],db); } for(; i < N; ++i) { _defaultInitializer()(ptrval[i]); } // actually it is meaningless on which Structure the Convert is called // because the `Pointer` argument triggers a special implementation. } catch (const Error& e) { _defaultInitializer()(out,e.what()); for(size_t i = 0; i < N; ++i) { out[i].reset(); } return; } for(size_t i = 0; i < N; ++i) { // resolve the pointer and load the corresponding structure ResolvePointer(out[i],ptrval[i],db,*f); } // and recover the previous stream position db.reader->SetCurrentPos(old); #ifndef ASSIMP_BUILD_BLENDER_NO_STATS ++db.stats().fields_read; #endif } //-------------------------------------------------------------------------------- template void Structure :: ReadField(T& out, const char* name, const FileDatabase& db) const { const StreamReaderAny::pos old = db.reader->GetCurrentPos(); try { const Field& f = (*this)[name]; // find the structure definition pertaining to this field const Structure& s = db.dna[f.type]; db.reader->IncPtr(f.offset); s.Convert(out,db); } catch (const Error& e) { _defaultInitializer()(out,e.what()); } // and recover the previous stream position db.reader->SetCurrentPos(old); #ifndef ASSIMP_BUILD_BLENDER_NO_STATS ++db.stats().fields_read; #endif } //-------------------------------------------------------------------------------- template