| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669 |
- /*
- Open Asset Import Library (assimp)
- ----------------------------------------------------------------------
- Copyright (c) 2006-2012, assimp 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 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 FBXDocument.cpp
- * @brief Implementation of the FBX DOM classes
- */
- #include "AssimpPCH.h"
- #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
- #include "FBXParser.h"
- #include "FBXDocument.h"
- #include "FBXUtil.h"
- #include "FBXImporter.h"
- namespace Assimp {
- namespace FBX {
- namespace {
- // ------------------------------------------------------------------------------------------------
- // signal DOM construction error, this is always unrecoverable. Throws DeadlyImportError.
- void DOMError(const std::string& message, const Token& token)
- {
- throw DeadlyImportError(Util::AddTokenText("FBX-DOM",message,&token));
- }
- // ------------------------------------------------------------------------------------------------
- void DOMError(const std::string& message, const Element* element = NULL)
- {
- if(element) {
- DOMError(message,element->KeyToken());
- }
- throw DeadlyImportError("FBX-DOM " + message);
- }
- // ------------------------------------------------------------------------------------------------
- // extract required compound scope
- const Scope& GetRequiredScope(const Element& el)
- {
- const Scope* const s = el.Compound();
- if(!s) {
- DOMError("expected compound scope",&el);
- }
- return *s;
- }
- // ------------------------------------------------------------------------------------------------
- // get token at a particular index
- const Token& GetRequiredToken(const Element& el, unsigned int index)
- {
- const TokenList& t = el.Tokens();
- if(index >= t.size()) {
- DOMError(Formatter::format( "missing token at index " ) << index,&el);
- }
- return *t[index];
- }
- // ------------------------------------------------------------------------------------------------
- // wrapper around ParseTokenAsID() with DOMError handling
- uint64_t ParseTokenAsID(const Token& t)
- {
- const char* err;
- const uint64_t i = ParseTokenAsID(t,err);
- if(err) {
- DOMError(err,t);
- }
- return i;
- }
- // ------------------------------------------------------------------------------------------------
- // wrapper around ParseTokenAsDim() with DOMError handling
- size_t ParseTokenAsDim(const Token& t)
- {
- const char* err;
- const size_t i = ParseTokenAsDim(t,err);
- if(err) {
- DOMError(err,t);
- }
- return i;
- }
- // ------------------------------------------------------------------------------------------------
- // wrapper around ParseTokenAsFloat() with DOMError handling
- float ParseTokenAsFloat(const Token& t)
- {
- const char* err;
- const float i = ParseTokenAsFloat(t,err);
- if(err) {
- DOMError(err,t);
- }
- return i;
- }
- // ------------------------------------------------------------------------------------------------
- // wrapper around ParseTokenAsInt() with DOMError handling
- int ParseTokenAsInt(const Token& t)
- {
- const char* err;
- const int i = ParseTokenAsInt(t,err);
- if(err) {
- DOMError(err,t);
- }
- return i;
- }
- // ------------------------------------------------------------------------------------------------
- // wrapper around ParseTokenAsString() with DOMError handling
- std::string ParseTokenAsString(const Token& t)
- {
- const char* err;
- const std::string& i = ParseTokenAsString(t,err);
- if(err) {
- DOMError(err,t);
- }
- return i;
- }
- // ------------------------------------------------------------------------------------------------
- // extract a required element from a scope, abort if the element cannot be found
- const Element& GetRequiredElement(const Scope& sc, const std::string& index, const Element* element = NULL)
- {
- const Element* el = sc[index];
- if(!el) {
- DOMError("did not find required element \"" + index + "\"",element);
- }
- return *el;
- }
- // ------------------------------------------------------------------------------------------------
- // read an array of float3 tuples
- void ReadVectorDataArray(std::vector<aiVector3D>& out, const Element& el)
- {
- out.clear();
- const TokenList& tok = el.Tokens();
- const size_t dim = ParseTokenAsDim(*tok[0]);
- // may throw bad_alloc if the input is rubbish, but this need
- // not to be prevented - importing would fail but we wouldn't
- // crash since assimp handles this case properly.
- out.reserve(dim);
- const Scope& scope = GetRequiredScope(el);
- const Element& a = GetRequiredElement(scope,"a",&el);
- if (a.Tokens().size() % 3 != 0) {
- DOMError("number of floats is not a multiple of three (3)",&el);
- }
- for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
- aiVector3D v;
- v.x = ParseTokenAsFloat(**it++);
- v.y = ParseTokenAsFloat(**it++);
- v.z = ParseTokenAsFloat(**it++);
- out.push_back(v);
- }
- }
- // ------------------------------------------------------------------------------------------------
- // read an array of float2 tuples
- void ReadVectorDataArray(std::vector<aiVector2D>& out, const Element& el)
- {
- out.clear();
- const TokenList& tok = el.Tokens();
- const size_t dim = ParseTokenAsDim(*tok[0]);
- // see notes in ReadVectorDataArray() above
- out.reserve(dim);
- const Scope& scope = GetRequiredScope(el);
- const Element& a = GetRequiredElement(scope,"a",&el);
- if (a.Tokens().size() % 2 != 0) {
- DOMError("number of floats is not a multiple of two (2)",&el);
- }
- for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
- aiVector2D v;
- v.x = ParseTokenAsFloat(**it++);
- v.y = ParseTokenAsFloat(**it++);
- out.push_back(v);
- }
- }
- // ------------------------------------------------------------------------------------------------
- // read an array of ints
- void ReadIntDataArray(std::vector<int>& out, const Element& el)
- {
- out.clear();
- const TokenList& tok = el.Tokens();
- const size_t dim = ParseTokenAsDim(*tok[0]);
- // see notes in ReadVectorDataArray()
- out.reserve(dim);
- const Scope& scope = GetRequiredScope(el);
- const Element& a = GetRequiredElement(scope,"a",&el);
- for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) {
- const int ival = ParseTokenAsInt(**it++);
- out.push_back(ival);
- }
- }
- } // end anon.
- // ------------------------------------------------------------------------------------------------
- LazyObject::LazyObject(const Element& element)
- : element(element)
- {
- }
- // ------------------------------------------------------------------------------------------------
- LazyObject::~LazyObject()
- {
- }
- // ------------------------------------------------------------------------------------------------
- const Object* LazyObject::Get()
- {
- if (object.get()) {
- return object.get();
- }
- const Token& key = element.KeyToken();
- const TokenList& tokens = element.Tokens();
- if(tokens.size() < 3) {
- DOMError("expected at least 3 tokens: id, name and class tag",&element);
- }
- const char* err;
- const std::string name = ParseTokenAsString(*tokens[1],err);
- if (err) {
- DOMError(err,&element);
- }
- const std::string classtag = ParseTokenAsString(*tokens[2],err);
- if (err) {
- DOMError(err,&element);
- }
- // this needs to be relatively fast since we do it a lot,
- // so avoid constructing strings all the time.
- const char* obtype = key.begin();
- if (!strncmp(obtype,"Geometry",static_cast<size_t>(key.end()-key.begin()))) {
- if (!strcmp(classtag.c_str(),"Mesh")) {
- object = new MeshGeometry(element,name);
- }
- }
- if (!object.get()) {
- //DOMError("failed to convert element to DOM object, class: " + classtag + ", name: " + name,&element);
- }
- return object.get();
- }
- // ------------------------------------------------------------------------------------------------
- Object::Object(const Element& element, const std::string& name)
- : element(element)
- , name(name)
- {
- }
- // ------------------------------------------------------------------------------------------------
- Object::~Object()
- {
- }
- // ------------------------------------------------------------------------------------------------
- Geometry::Geometry(const Element& element, const std::string& name)
- : Object(element,name)
- {
- }
- // ------------------------------------------------------------------------------------------------
- Geometry::~Geometry()
- {
- }
- // ------------------------------------------------------------------------------------------------
- MeshGeometry::MeshGeometry(const Element& element, const std::string& name)
- : Geometry(element,name)
- {
- const Scope* sc = element.Compound();
- if (!sc) {
- DOMError("failed to read Geometry object (class: Mesh), no data scope found");
- }
- // must have Mesh elements:
- const Element& Vertices = GetRequiredElement(*sc,"Vertices",&element);
- const Element& PolygonVertexIndex = GetRequiredElement(*sc,"PolygonVertexIndex",&element);
- // optional Mesh elements:
- const ElementCollection& Layer = sc->GetCollection("Layer");
- const ElementCollection& LayerElementMaterial = sc->GetCollection("LayerElementMaterial");
- const ElementCollection& LayerElementUV = sc->GetCollection("LayerElementUV");
- const ElementCollection& LayerElementNormal = sc->GetCollection("LayerElementNormal");
- std::vector<aiVector3D> tempVerts;
- ReadVectorDataArray(tempVerts,Vertices);
- std::vector<int> tempFaces;
- ReadIntDataArray(tempFaces,PolygonVertexIndex);
- vertices.reserve(tempFaces.size());
- faces.reserve(tempFaces.size() / 3);
- mapping_offsets.resize(tempVerts.size());
- mapping_counts.resize(tempVerts.size(),0);
- mappings.resize(tempFaces.size());
- const size_t vertex_count = tempVerts.size();
- // generate output vertices, computing an adjacency table to
- // preserve the mapping from fbx indices to *this* indexing.
- unsigned int count = 0;
- BOOST_FOREACH(int index, tempFaces) {
- const int absi = index < 0 ? (-index - 1) : index;
- if(static_cast<size_t>(absi) >= vertex_count) {
- DOMError("polygon vertex index out of range",&PolygonVertexIndex);
- }
- vertices.push_back(tempVerts[absi]);
- ++count;
- ++mapping_counts[absi];
- if (index < 0) {
- faces.push_back(count);
- count = 0;
- }
- }
- unsigned int cursor = 0;
- for (size_t i = 0, e = tempVerts.size(); i < e; ++i) {
- mapping_offsets[i] = cursor;
- cursor += mapping_counts[i];
- mapping_counts[i] = 0;
- }
- cursor = 0;
- BOOST_FOREACH(int index, tempFaces) {
- const int absi = index < 0 ? (-index - 1) : index;
- mappings[mapping_offsets[absi] + mapping_counts[absi]++] = cursor;
- }
- // ignore all but the first layer, but warn about any further layers
- for (ElementMap::const_iterator it = Layer.first; it != Layer.second; ++it) {
- const TokenList& tokens = (*it).second->Tokens();
- const char* err;
- const int index = ParseTokenAsInt(*tokens[0], err);
- if(err) {
- DOMError(err,&element);
- }
- if(index == 0) {
- const Scope& layer = GetRequiredScope(*(*it).second);
- ReadLayer(layer);
- }
- else {
- FBXImporter::LogWarn("ignoring additional geometry layers");
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- MeshGeometry::~MeshGeometry()
- {
- }
- // ------------------------------------------------------------------------------------------------
- void MeshGeometry::ReadLayer(const Scope& layer)
- {
- const ElementCollection& LayerElement = layer.GetCollection("LayerElement");
- for (ElementMap::const_iterator eit = LayerElement.first; eit != LayerElement.second; ++eit) {
- const Scope& elayer = GetRequiredScope(*(*eit).second);
- ReadLayerElement(elayer);
- }
- }
- // ------------------------------------------------------------------------------------------------
- void MeshGeometry::ReadLayerElement(const Scope& layerElement)
- {
- const Element& Type = GetRequiredElement(layerElement,"Type");
- const Element& TypedIndex = GetRequiredElement(layerElement,"TypedIndex");
- const std::string& type = ParseTokenAsString(GetRequiredToken(Type,0));
- const int typedIndex = ParseTokenAsInt(GetRequiredToken(TypedIndex,0));
- const Scope& top = GetRequiredScope(element);
- const ElementCollection candidates = top.GetCollection(type);
- for (ElementMap::const_iterator it = candidates.first; it != candidates.second; ++it) {
- const int index = ParseTokenAsInt(GetRequiredToken(*(*it).second,0));
- if(index == typedIndex) {
- ReadVertexData(type,typedIndex,GetRequiredScope(*(*it).second));
- return;
- }
- }
- FBXImporter::LogError(Formatter::format("failed to resolve vertex layer element: ")
- << type << ", index: " << typedIndex);
- }
- // ------------------------------------------------------------------------------------------------
- void MeshGeometry::ReadVertexData(const std::string& type, int index, const Scope& source)
- {
- const std::string& MappingInformationType = ParseTokenAsString(GetRequiredToken(
- GetRequiredElement(source,"MappingInformationType"),0)
- );
- const std::string& ReferenceInformationType = ParseTokenAsString(GetRequiredToken(
- GetRequiredElement(source,"ReferenceInformationType"),0)
- );
-
- if (type == "LayerElementUV") {
- if(index >= AI_MAX_NUMBER_OF_TEXTURECOORDS) {
- FBXImporter::LogError(Formatter::format("ignoring UV layer, maximum UV number exceeded: ")
- << index << " (limit is " << AI_MAX_NUMBER_OF_TEXTURECOORDS << ")" );
- return;
- }
- ReadVertexDataUV(uvs[index],source,MappingInformationType,ReferenceInformationType);
- }
- else if (type == "LayerElementMaterial") {
-
- materials.resize(vertices.size());
- std::vector<int> tempMaterials;
- ReadIntDataArray(tempMaterials,GetRequiredElement(source,"Materials"));
- if (MappingInformationType == "AllSame") {
- // easy - same material for all faces
- materials.assign(vertices.size(),tempMaterials[0]);
- }
- else {
- FBXImporter::LogError(Formatter::format("ignoring material assignments, unrecognized access type: ")
- << MappingInformationType << "," << ReferenceInformationType);
- }
- }
- else if (type == "LayerElementNormal") {
- ReadVertexDataNormals(normals,source,MappingInformationType,ReferenceInformationType);
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Lengthy utility function to read and resolve a FBX vertex data array - that is, the
- // output is in polygon vertex order. This logic is used for reading normals, UVs, colors,
- // tangents ..
- template <typename T>
- void ResolveVertexDataArray(std::vector<T>& data_out, const Scope& source,
- const std::string& MappingInformationType,
- const std::string& ReferenceInformationType,
- const char* dataElementName,
- const char* indexDataElementName,
- size_t vertex_count,
- const std::vector<unsigned int>& mapping_counts,
- const std::vector<unsigned int>& mapping_offsets,
- const std::vector<unsigned int>& mappings)
- {
- std::vector<T> tempUV;
- ReadVectorDataArray(tempUV,GetRequiredElement(source,dataElementName));
- // handle permutations of Mapping and Reference type - it would be nice to
- // deal with this more elegantly and with less redundancy, but right
- // now it seems unavoidable.
- if (MappingInformationType == "ByVertice" && ReferenceInformationType == "Direct") {
- data_out.resize(vertex_count);
- for (size_t i = 0, e = tempUV.size(); i < e; ++i) {
- const unsigned int istart = mapping_offsets[i], iend = istart + mapping_counts[i];
- for (unsigned int j = istart; j < iend; ++j) {
- data_out[mappings[j]] = tempUV[i];
- }
- }
- }
- else if (MappingInformationType == "ByVertice" && ReferenceInformationType == "IndexToDirect") {
- data_out.resize(vertex_count);
- std::vector<int> uvIndices;
- ReadIntDataArray(uvIndices,GetRequiredElement(source,indexDataElementName));
- for (size_t i = 0, e = uvIndices.size(); i < e; ++i) {
- const unsigned int istart = mapping_offsets[i], iend = istart + mapping_counts[i];
- for (unsigned int j = istart; j < iend; ++j) {
- if(static_cast<size_t>(uvIndices[i]) >= tempUV.size()) {
- DOMError("index out of range",&GetRequiredElement(source,indexDataElementName));
- }
- data_out[mappings[j]] = tempUV[uvIndices[i]];
- }
- }
- }
- else if (MappingInformationType == "ByPolygonVertex" && ReferenceInformationType == "Direct") {
- if (tempUV.size() != vertex_count) {
- FBXImporter::LogError("length of input data unexpected for ByPolygonVertex mapping");
- return;
- }
- data_out.swap(tempUV);
- }
- else if (MappingInformationType == "ByPolygonVertex" && ReferenceInformationType == "IndexToDirect") {
- data_out.resize(vertex_count);
- std::vector<int> uvIndices;
- ReadIntDataArray(uvIndices,GetRequiredElement(source,indexDataElementName));
- if (uvIndices.size() != vertex_count) {
- FBXImporter::LogError("length of input data unexpected for ByPolygonVertex mapping");
- return;
- }
- unsigned int next = 0;
- BOOST_FOREACH(int i, uvIndices) {
- if(static_cast<size_t>(i) >= tempUV.size()) {
- DOMError("index out of range",&GetRequiredElement(source,indexDataElementName));
- }
- data_out[next++] = tempUV[i];
- }
- }
- else {
- FBXImporter::LogError(Formatter::format("ignoring vertex data channel, unrecognized access type: ")
- << MappingInformationType << "," << ReferenceInformationType);
- }
- }
- // ------------------------------------------------------------------------------------------------
- void MeshGeometry::ReadVertexDataNormals(std::vector<aiVector3D>& normals_out, const Scope& source,
- const std::string& MappingInformationType,
- const std::string& ReferenceInformationType)
- {
- ResolveVertexDataArray(normals_out,source,MappingInformationType,ReferenceInformationType,
- "Normals",
- "NormalsIndex",
- vertices.size(),
- mapping_counts,
- mapping_offsets,
- mappings);
- }
- // ------------------------------------------------------------------------------------------------
- void MeshGeometry::ReadVertexDataUV(std::vector<aiVector2D>& uv_out, const Scope& source,
- const std::string& MappingInformationType,
- const std::string& ReferenceInformationType)
- {
- ResolveVertexDataArray(uv_out,source,MappingInformationType,ReferenceInformationType,
- "UV",
- "UVIndex",
- vertices.size(),
- mapping_counts,
- mapping_offsets,
- mappings);
- }
- // ------------------------------------------------------------------------------------------------
- Document::Document(const Parser& parser)
- : parser(parser)
- {
- const Scope& sc = parser.GetRootScope();
- const Element* const eobjects = sc["Objects"];
- if(!eobjects || !eobjects->Compound()) {
- DOMError("no Objects dictionary found");
- }
- const Scope* const sobjects = eobjects->Compound();
- BOOST_FOREACH(const ElementMap::value_type& el, sobjects->Elements()) {
-
- // extract ID
- const TokenList& tok = el.second->Tokens();
-
- if (tok.empty()) {
- DOMError("expected ID after object key",el.second);
- }
- const char* err;
- const uint64_t id = ParseTokenAsID(*tok[0], err);
- if(err) {
- DOMError(err,el.second);
- }
- objects[id] = new LazyObject(*el.second);
- // DEBUG - evaluate all objects
- const Object* o = objects[id]->Get();
- }
- }
- // ------------------------------------------------------------------------------------------------
- Document::~Document()
- {
-
- }
- } // !FBX
- } // !Assimp
- #endif
|