vrshoot

diff libs/assimp/FBXProperties.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/FBXProperties.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,207 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2012, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +
    1.44 +/** @file  FBXProperties.cpp
    1.45 + *  @brief Implementation of the FBX dynamic properties system
    1.46 + */
    1.47 +#include "AssimpPCH.h"
    1.48 +
    1.49 +#ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
    1.50 +
    1.51 +#include "FBXTokenizer.h"
    1.52 +#include "FBXParser.h"
    1.53 +#include "FBXDocument.h"
    1.54 +#include "FBXDocumentUtil.h"
    1.55 +#include "FBXProperties.h"
    1.56 +
    1.57 +namespace Assimp {
    1.58 +namespace FBX {
    1.59 +
    1.60 +	using namespace Util;
    1.61 +
    1.62 +// ------------------------------------------------------------------------------------------------
    1.63 +Property::Property()
    1.64 +{
    1.65 +}
    1.66 +
    1.67 +// ------------------------------------------------------------------------------------------------
    1.68 +Property::~Property()
    1.69 +{
    1.70 +}
    1.71 +
    1.72 +namespace {
    1.73 +
    1.74 +// ------------------------------------------------------------------------------------------------
    1.75 +// read a typed property out of a FBX element. The return value is NULL if the property cannot be read.
    1.76 +Property* ReadTypedProperty(const Element& element)
    1.77 +{
    1.78 +	ai_assert(element.KeyToken().StringContents() == "P");
    1.79 +
    1.80 +	const TokenList& tok = element.Tokens();
    1.81 +	ai_assert(tok.size() >= 5);
    1.82 +
    1.83 +	const std::string& s = ParseTokenAsString(*tok[1]);
    1.84 +	const char* const cs = s.c_str();
    1.85 +	if (!strcmp(cs,"KString")) {
    1.86 +		return new TypedProperty<std::string>(ParseTokenAsString(*tok[4]));
    1.87 +	}
    1.88 +	else if (!strcmp(cs,"bool") || !strcmp(cs,"Bool")) {
    1.89 +		return new TypedProperty<bool>(ParseTokenAsInt(*tok[4]) != 0);
    1.90 +	}
    1.91 +	else if (!strcmp(cs,"int") || !strcmp(cs,"enum")) {
    1.92 +		return new TypedProperty<int>(ParseTokenAsInt(*tok[4]));
    1.93 +	}
    1.94 +	else if (!strcmp(cs,"ULongLong")) {
    1.95 +		return new TypedProperty<uint64_t>(ParseTokenAsID(*tok[4]));
    1.96 +	}
    1.97 +	else if (!strcmp(cs,"Vector3D") || 
    1.98 +		!strcmp(cs,"ColorRGB") || 
    1.99 +		!strcmp(cs,"Vector") || 
   1.100 +		!strcmp(cs,"Color") || 
   1.101 +		!strcmp(cs,"Lcl Translation") || 
   1.102 +		!strcmp(cs,"Lcl Rotation") || 
   1.103 +		!strcmp(cs,"Lcl Scaling")
   1.104 +		) {
   1.105 +		return new TypedProperty<aiVector3D>(aiVector3D(
   1.106 +			ParseTokenAsFloat(*tok[4]),
   1.107 +			ParseTokenAsFloat(*tok[5]),
   1.108 +			ParseTokenAsFloat(*tok[6]))
   1.109 +		);
   1.110 +	}
   1.111 +	else if (!strcmp(cs,"double") || !strcmp(cs,"Number") || !strcmp(cs,"KTime") || !strcmp(cs,"Float")) {
   1.112 +		return new TypedProperty<float>(ParseTokenAsFloat(*tok[4]));
   1.113 +	}
   1.114 +	return NULL;
   1.115 +}
   1.116 +
   1.117 +
   1.118 +// ------------------------------------------------------------------------------------------------
   1.119 +// peek into an element and check if it contains a FBX property, if so return its name.
   1.120 +std::string PeekPropertyName(const Element& element)
   1.121 +{
   1.122 +	ai_assert(element.KeyToken().StringContents() == "P");
   1.123 +	const TokenList& tok = element.Tokens();
   1.124 +	if(tok.size() < 4) {
   1.125 +		return "";
   1.126 +	}
   1.127 +
   1.128 +	return ParseTokenAsString(*tok[0]);
   1.129 +}
   1.130 +
   1.131 +} //! anon
   1.132 +
   1.133 +
   1.134 +// ------------------------------------------------------------------------------------------------
   1.135 +PropertyTable::PropertyTable()
   1.136 +: templateProps()
   1.137 +, element()
   1.138 +{
   1.139 +}
   1.140 +
   1.141 +
   1.142 +// ------------------------------------------------------------------------------------------------
   1.143 +PropertyTable::PropertyTable(const Element& element, boost::shared_ptr<const PropertyTable> templateProps)
   1.144 +: templateProps(templateProps)
   1.145 +, element(&element)
   1.146 +{
   1.147 +	const Scope& scope = GetRequiredScope(element);
   1.148 +	BOOST_FOREACH(const ElementMap::value_type& v, scope.Elements()) {
   1.149 +		if(v.first != "P") {
   1.150 +			DOMWarning("expected only P elements in property table",v.second);
   1.151 +			continue;
   1.152 +		}
   1.153 +
   1.154 +		const std::string& name = PeekPropertyName(*v.second);
   1.155 +		if(!name.length()) {
   1.156 +			DOMWarning("could not read property name",v.second);
   1.157 +			continue;
   1.158 +		}
   1.159 +
   1.160 +		LazyPropertyMap::const_iterator it = lazyProps.find(name);
   1.161 +		if (it != lazyProps.end()) {
   1.162 +			DOMWarning("duplicate property name, will hide previous value: " + name,v.second);
   1.163 +			continue;
   1.164 +		}
   1.165 +
   1.166 +		lazyProps[name] = v.second;
   1.167 +	}
   1.168 +}
   1.169 +
   1.170 +
   1.171 +// ------------------------------------------------------------------------------------------------
   1.172 +PropertyTable::~PropertyTable()
   1.173 +{
   1.174 +	BOOST_FOREACH(PropertyMap::value_type& v, props) {
   1.175 +		delete v.second;
   1.176 +	}
   1.177 +}
   1.178 +
   1.179 +
   1.180 +// ------------------------------------------------------------------------------------------------
   1.181 +const Property* PropertyTable::Get(const std::string& name) const
   1.182 +{
   1.183 +	PropertyMap::const_iterator it = props.find(name);
   1.184 +	if (it == props.end()) {
   1.185 +		// hasn't been parsed yet?
   1.186 +		LazyPropertyMap::const_iterator lit = lazyProps.find(name);
   1.187 +		if(lit != lazyProps.end()) {
   1.188 +			props[name] = ReadTypedProperty(*(*lit).second);
   1.189 +			it = props.find(name);
   1.190 +
   1.191 +			ai_assert(it != props.end());
   1.192 +		}
   1.193 +
   1.194 +		if (it == props.end()) {
   1.195 +			// check property template
   1.196 +			if(templateProps) {
   1.197 +				return templateProps->Get(name);
   1.198 +			}
   1.199 +
   1.200 +			return NULL;
   1.201 +		}
   1.202 +	}
   1.203 +	
   1.204 +	return (*it).second;
   1.205 +}
   1.206 +
   1.207 +} //! FBX
   1.208 +} //! Assimp
   1.209 +
   1.210 +#endif