vrshoot

diff libs/assimp/FBXAnimation.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/FBXAnimation.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,313 @@
     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  FBXAnimation.cpp
    1.45 + *  @brief Assimp::FBX::AnimationCurve, Assimp::FBX::AnimationCurveNode, 
    1.46 + *         Assimp::FBX::AnimationLayer, Assimp::FBX::AnimationStack 
    1.47 + */
    1.48 +#include "AssimpPCH.h"
    1.49 +
    1.50 +#ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
    1.51 +
    1.52 +#include "FBXParser.h"
    1.53 +#include "FBXDocument.h"
    1.54 +#include "FBXImporter.h"
    1.55 +#include "FBXImportSettings.h"
    1.56 +#include "FBXDocumentUtil.h"
    1.57 +#include "FBXProperties.h"
    1.58 +
    1.59 +namespace Assimp {
    1.60 +namespace FBX {
    1.61 +
    1.62 +	using namespace Util;
    1.63 +
    1.64 +// ------------------------------------------------------------------------------------------------
    1.65 +AnimationCurve::AnimationCurve(uint64_t id, const Element& element, const std::string& name, const Document& doc)
    1.66 +: Object(id, element, name)
    1.67 +{
    1.68 +	const Scope& sc = GetRequiredScope(element);
    1.69 +	const Element& KeyTime = GetRequiredElement(sc,"KeyTime");
    1.70 +	const Element& KeyValueFloat = GetRequiredElement(sc,"KeyValueFloat");
    1.71 +
    1.72 +	ParseVectorDataArray(keys, KeyTime);
    1.73 +	ParseVectorDataArray(values, KeyValueFloat);
    1.74 +
    1.75 +	if(keys.size() != values.size()) {
    1.76 +		DOMError("the number of key times does not match the number of keyframe values",&KeyTime);
    1.77 +	}
    1.78 +	
    1.79 +	// check if the key times are well-ordered
    1.80 +	if(!std::equal(keys.begin(), keys.end() - 1, keys.begin() + 1, std::less<KeyTimeList::value_type>())) {
    1.81 +		DOMError("the keyframes are not in ascending order",&KeyTime);
    1.82 +	}
    1.83 +
    1.84 +	const Element* KeyAttrDataFloat = sc["KeyAttrDataFloat"];
    1.85 +	if(KeyAttrDataFloat) {
    1.86 +		ParseVectorDataArray(attributes, *KeyAttrDataFloat);
    1.87 +	}
    1.88 +
    1.89 +	const Element* KeyAttrFlags = sc["KeyAttrFlags"];
    1.90 +	if(KeyAttrFlags) {
    1.91 +		ParseVectorDataArray(flags, *KeyAttrFlags);
    1.92 +	}
    1.93 +}
    1.94 +
    1.95 +
    1.96 +// ------------------------------------------------------------------------------------------------
    1.97 +AnimationCurve::~AnimationCurve()
    1.98 +{
    1.99 +
   1.100 +}
   1.101 +
   1.102 +
   1.103 +// ------------------------------------------------------------------------------------------------
   1.104 +AnimationCurveNode::AnimationCurveNode(uint64_t id, const Element& element, const std::string& name, const Document& doc, 
   1.105 +	const char* const * target_prop_whitelist /*= NULL*/, size_t whitelist_size /*= 0*/)
   1.106 +: Object(id, element, name)
   1.107 +, target()
   1.108 +, doc(doc)
   1.109 +{
   1.110 +	const Scope& sc = GetRequiredScope(element);
   1.111 +	
   1.112 +	// find target node
   1.113 +	const char* whitelist[] = {"Model","NodeAttribute"};
   1.114 +	const std::vector<const Connection*>& conns = doc.GetConnectionsBySourceSequenced(ID(),whitelist,2);
   1.115 +
   1.116 +	BOOST_FOREACH(const Connection* con, conns) {
   1.117 +
   1.118 +		// link should go for a property
   1.119 +		if (!con->PropertyName().length()) {
   1.120 +			continue;
   1.121 +		}
   1.122 +
   1.123 +		if(target_prop_whitelist) {
   1.124 +			const char* const s = con->PropertyName().c_str();
   1.125 +			bool ok = false;
   1.126 +			for (size_t i = 0; i < whitelist_size; ++i) {
   1.127 +				if (!strcmp(s, target_prop_whitelist[i])) {
   1.128 +					ok = true;
   1.129 +					break;
   1.130 +				}
   1.131 +			}
   1.132 +
   1.133 +			if (!ok) {
   1.134 +				throw std::range_error("AnimationCurveNode target property is not in whitelist");
   1.135 +			}
   1.136 +		}
   1.137 +
   1.138 +		const Object* const ob = con->DestinationObject();
   1.139 +		if(!ob) {
   1.140 +			DOMWarning("failed to read destination object for AnimationCurveNode->Model link, ignoring",&element);
   1.141 +			continue;
   1.142 +		}
   1.143 +
   1.144 +		// XXX support constraints as DOM class
   1.145 +		//ai_assert(dynamic_cast<const Model*>(ob) || dynamic_cast<const NodeAttribute*>(ob));
   1.146 +		target = ob; 
   1.147 +		if(!target) {
   1.148 +			continue;
   1.149 +		}
   1.150 +
   1.151 +		prop = con->PropertyName();
   1.152 +		break;
   1.153 +	}
   1.154 +
   1.155 +	if(!target) {
   1.156 +		DOMWarning("failed to resolve target Model/NodeAttribute/Constraint for AnimationCurveNode",&element);
   1.157 +	}
   1.158 +
   1.159 +	props = GetPropertyTable(doc,"AnimationCurveNode.FbxAnimCurveNode",element,sc,false);
   1.160 +}
   1.161 +
   1.162 +
   1.163 +// ------------------------------------------------------------------------------------------------
   1.164 +AnimationCurveNode::~AnimationCurveNode()
   1.165 +{
   1.166 +
   1.167 +}
   1.168 +
   1.169 +
   1.170 +// ------------------------------------------------------------------------------------------------
   1.171 +const AnimationCurveMap& AnimationCurveNode::Curves() const
   1.172 +{
   1.173 +	if(curves.empty()) {
   1.174 +		// resolve attached animation curves
   1.175 +		const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(ID(),"AnimationCurve");
   1.176 +
   1.177 +		BOOST_FOREACH(const Connection* con, conns) {
   1.178 +
   1.179 +			// link should go for a property
   1.180 +			if (!con->PropertyName().length()) {
   1.181 +				continue;
   1.182 +			}
   1.183 +
   1.184 +			const Object* const ob = con->SourceObject();
   1.185 +			if(!ob) {
   1.186 +				DOMWarning("failed to read source object for AnimationCurve->AnimationCurveNode link, ignoring",&element);
   1.187 +				continue;
   1.188 +			}
   1.189 +
   1.190 +			const AnimationCurve* const anim = dynamic_cast<const AnimationCurve*>(ob);
   1.191 +			if(!anim) {
   1.192 +				DOMWarning("source object for ->AnimationCurveNode link is not an AnimationCurve",&element);
   1.193 +				continue;
   1.194 +			}
   1.195 +
   1.196 +			curves[con->PropertyName()] = anim;
   1.197 +		}
   1.198 +	}
   1.199 +
   1.200 +	return curves;
   1.201 +}
   1.202 +
   1.203 +
   1.204 +// ------------------------------------------------------------------------------------------------
   1.205 +AnimationLayer::AnimationLayer(uint64_t id, const Element& element, const std::string& name, const Document& doc)
   1.206 +: Object(id, element, name)
   1.207 +, doc(doc)
   1.208 +{
   1.209 +	const Scope& sc = GetRequiredScope(element);
   1.210 +
   1.211 +	// note: the props table here bears little importance and is usually absent
   1.212 +	props = GetPropertyTable(doc,"AnimationLayer.FbxAnimLayer",element,sc, true);
   1.213 +}
   1.214 +
   1.215 +
   1.216 +// ------------------------------------------------------------------------------------------------
   1.217 +AnimationLayer::~AnimationLayer()
   1.218 +{
   1.219 +
   1.220 +}
   1.221 +
   1.222 +
   1.223 +// ------------------------------------------------------------------------------------------------
   1.224 +AnimationCurveNodeList AnimationLayer::Nodes(const char* const * target_prop_whitelist /*= NULL*/, 
   1.225 +	size_t whitelist_size /*= 0*/) const
   1.226 +{
   1.227 +	AnimationCurveNodeList nodes;
   1.228 +
   1.229 +	// resolve attached animation nodes
   1.230 +	const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(ID(),"AnimationCurveNode");
   1.231 +	nodes.reserve(conns.size());
   1.232 +
   1.233 +	BOOST_FOREACH(const Connection* con, conns) {
   1.234 +
   1.235 +		// link should not go to a property
   1.236 +		if (con->PropertyName().length()) {
   1.237 +			continue;
   1.238 +		}
   1.239 +
   1.240 +		const Object* const ob = con->SourceObject();
   1.241 +		if(!ob) {
   1.242 +			DOMWarning("failed to read source object for AnimationCurveNode->AnimationLayer link, ignoring",&element);
   1.243 +			continue;
   1.244 +		}
   1.245 +
   1.246 +		const AnimationCurveNode* const anim = dynamic_cast<const AnimationCurveNode*>(ob);
   1.247 +		if(!anim) {
   1.248 +			DOMWarning("source object for ->AnimationLayer link is not an AnimationCurveNode",&element);
   1.249 +			continue;
   1.250 +		}
   1.251 +
   1.252 +		if(target_prop_whitelist) {
   1.253 +			const char* s = anim->TargetProperty().c_str();
   1.254 +			bool ok = false;
   1.255 +			for (size_t i = 0; i < whitelist_size; ++i) {
   1.256 +				if (!strcmp(s, target_prop_whitelist[i])) {
   1.257 +					ok = true;
   1.258 +					break;
   1.259 +				}
   1.260 +			}
   1.261 +			if(!ok) {
   1.262 +				continue;
   1.263 +			}
   1.264 +		}
   1.265 +		nodes.push_back(anim);
   1.266 +	}
   1.267 +
   1.268 +	return nodes; // pray for NRVO
   1.269 +}
   1.270 +
   1.271 +// ------------------------------------------------------------------------------------------------
   1.272 +AnimationStack::AnimationStack(uint64_t id, const Element& element, const std::string& name, const Document& doc)
   1.273 +: Object(id, element, name)
   1.274 +{
   1.275 +	const Scope& sc = GetRequiredScope(element);
   1.276 +
   1.277 +	// note: we don't currently use any of these properties so we shouldn't bother if it is missing
   1.278 +	props = GetPropertyTable(doc,"AnimationStack.FbxAnimStack",element,sc, true);
   1.279 +
   1.280 +	// resolve attached animation layers
   1.281 +	const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(ID(),"AnimationLayer");
   1.282 +	layers.reserve(conns.size());
   1.283 +
   1.284 +	BOOST_FOREACH(const Connection* con, conns) {
   1.285 +
   1.286 +		// link should not go to a property
   1.287 +		if (con->PropertyName().length()) {
   1.288 +			continue;
   1.289 +		}
   1.290 +
   1.291 +		const Object* const ob = con->SourceObject();
   1.292 +		if(!ob) {
   1.293 +			DOMWarning("failed to read source object for AnimationLayer->AnimationStack link, ignoring",&element);
   1.294 +			continue;
   1.295 +		}
   1.296 +
   1.297 +		const AnimationLayer* const anim = dynamic_cast<const AnimationLayer*>(ob);
   1.298 +		if(!anim) {
   1.299 +			DOMWarning("source object for ->AnimationStack link is not an AnimationLayer",&element);
   1.300 +			continue;
   1.301 +		}
   1.302 +		layers.push_back(anim);
   1.303 +	}
   1.304 +}
   1.305 +
   1.306 +
   1.307 +// ------------------------------------------------------------------------------------------------
   1.308 +AnimationStack::~AnimationStack()
   1.309 +{
   1.310 +
   1.311 +}
   1.312 +
   1.313 +} //!FBX
   1.314 +} //!Assimp
   1.315 +
   1.316 +#endif