vrshoot

diff libs/assimp/TargetAnimation.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/TargetAnimation.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,246 @@
     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 +#include "AssimpPCH.h"
    1.45 +#include "TargetAnimation.h"
    1.46 +#include <algorithm>
    1.47 +
    1.48 +using namespace Assimp;
    1.49 +
    1.50 +
    1.51 +// ------------------------------------------------------------------------------------------------
    1.52 +KeyIterator::KeyIterator(const std::vector<aiVectorKey>* _objPos,
    1.53 +	const std::vector<aiVectorKey>* _targetObjPos,
    1.54 +	const aiVector3D*  defaultObjectPos /*= NULL*/,
    1.55 +	const aiVector3D*  defaultTargetPos /*= NULL*/)
    1.56 +		
    1.57 +		:	reachedEnd		(false)
    1.58 +		,	curTime			(-1.)
    1.59 +		,	objPos			(_objPos)
    1.60 +		,	targetObjPos	(_targetObjPos)
    1.61 +		,	nextObjPos		(0)
    1.62 +		,	nextTargetObjPos(0)
    1.63 +{
    1.64 +	// Generate default transformation tracks if necessary
    1.65 +	if (!objPos || objPos->empty())
    1.66 +	{
    1.67 +		defaultObjPos.resize(1);
    1.68 +		defaultObjPos.front().mTime  = 10e10;
    1.69 +
    1.70 +		if (defaultObjectPos)
    1.71 +			defaultObjPos.front().mValue = *defaultObjectPos;
    1.72 +
    1.73 +		objPos = & defaultObjPos;
    1.74 +	}
    1.75 +	if (!targetObjPos || targetObjPos->empty())
    1.76 +	{
    1.77 +		defaultTargetObjPos.resize(1);
    1.78 +		defaultTargetObjPos.front().mTime  = 10e10;
    1.79 +
    1.80 +		if (defaultTargetPos)
    1.81 +			defaultTargetObjPos.front().mValue = *defaultTargetPos;
    1.82 +
    1.83 +		targetObjPos = & defaultTargetObjPos;
    1.84 +	}
    1.85 +}
    1.86 +
    1.87 +// ------------------------------------------------------------------------------------------------
    1.88 +template <class T>
    1.89 +inline T Interpolate(const T& one, const T& two, float val)
    1.90 +{
    1.91 +	return one + (two-one)*val;
    1.92 +}
    1.93 +
    1.94 +// ------------------------------------------------------------------------------------------------
    1.95 +void KeyIterator::operator ++()
    1.96 +{
    1.97 +	// If we are already at the end of all keyframes, return
    1.98 +	if (reachedEnd) {
    1.99 +		return;
   1.100 +	}
   1.101 +
   1.102 +	// Now search in all arrays for the time value closest
   1.103 +	// to our current position on the time line
   1.104 +	double d0,d1;
   1.105 +	
   1.106 +	d0 = objPos->at      ( std::min<unsigned int> ( nextObjPos, objPos->size()-1)             ).mTime;
   1.107 +	d1 = targetObjPos->at( std::min<unsigned int> ( nextTargetObjPos, targetObjPos->size()-1) ).mTime;	
   1.108 +	
   1.109 +	// Easiest case - all are identical. In this
   1.110 +	// case we don't need to interpolate so we can
   1.111 +	// return earlier
   1.112 +	if ( d0 == d1 )
   1.113 +	{
   1.114 +		curTime = d0;
   1.115 +		curPosition = objPos->at(nextObjPos).mValue;
   1.116 +		curTargetPosition = targetObjPos->at(nextTargetObjPos).mValue;
   1.117 +
   1.118 +		// increment counters
   1.119 +		if (objPos->size() != nextObjPos-1)
   1.120 +			++nextObjPos;
   1.121 +
   1.122 +		if (targetObjPos->size() != nextTargetObjPos-1)
   1.123 +			++nextTargetObjPos;
   1.124 +	}
   1.125 +
   1.126 +	// An object position key is closest to us
   1.127 +	else if (d0 < d1)
   1.128 +	{
   1.129 +		curTime = d0;
   1.130 +
   1.131 +		// interpolate the other
   1.132 +		if (1 == targetObjPos->size() || !nextTargetObjPos)	{
   1.133 +			curTargetPosition = targetObjPos->at(0).mValue;
   1.134 +		}
   1.135 +		else
   1.136 +		{
   1.137 +			const aiVectorKey& last  = targetObjPos->at(nextTargetObjPos);
   1.138 +			const aiVectorKey& first = targetObjPos->at(nextTargetObjPos-1);
   1.139 +
   1.140 +			curTargetPosition = Interpolate(first.mValue, last.mValue, (float) (
   1.141 +				(curTime-first.mTime) / (last.mTime-first.mTime) ));
   1.142 +		}
   1.143 +
   1.144 +		if (objPos->size() != nextObjPos-1)
   1.145 +			++nextObjPos;
   1.146 +	}
   1.147 +	// A target position key is closest to us
   1.148 +	else
   1.149 +	{
   1.150 +		curTime = d1;
   1.151 +
   1.152 +		// interpolate the other
   1.153 +		if (1 == objPos->size() || !nextObjPos)	{
   1.154 +			curPosition = objPos->at(0).mValue;
   1.155 +		}
   1.156 +		else
   1.157 +		{
   1.158 +			const aiVectorKey& last  = objPos->at(nextObjPos);
   1.159 +			const aiVectorKey& first = objPos->at(nextObjPos-1);
   1.160 +
   1.161 +			curPosition = Interpolate(first.mValue, last.mValue, (float) (
   1.162 +				(curTime-first.mTime) / (last.mTime-first.mTime)));
   1.163 +		}
   1.164 +
   1.165 +		if (targetObjPos->size() != nextTargetObjPos-1)
   1.166 +			++nextTargetObjPos;
   1.167 +	}
   1.168 +
   1.169 +	if (nextObjPos >= objPos->size()-1 &&
   1.170 +		nextTargetObjPos >= targetObjPos->size()-1)
   1.171 +	{
   1.172 +		// We reached the very last keyframe
   1.173 +		reachedEnd = true;
   1.174 +	}
   1.175 +}
   1.176 +
   1.177 +// ------------------------------------------------------------------------------------------------
   1.178 +void TargetAnimationHelper::SetTargetAnimationChannel (
   1.179 +	const std::vector<aiVectorKey>* _targetPositions)
   1.180 +{
   1.181 +	ai_assert(NULL != _targetPositions);
   1.182 +	targetPositions = _targetPositions;
   1.183 +}
   1.184 +
   1.185 +// ------------------------------------------------------------------------------------------------
   1.186 +void TargetAnimationHelper::SetMainAnimationChannel (
   1.187 +	const std::vector<aiVectorKey>* _objectPositions)
   1.188 +{
   1.189 +	ai_assert(NULL != _objectPositions);
   1.190 +	objectPositions = _objectPositions;
   1.191 +}
   1.192 +
   1.193 +// ------------------------------------------------------------------------------------------------
   1.194 +void TargetAnimationHelper::SetFixedMainAnimationChannel(
   1.195 +	const aiVector3D& fixed)
   1.196 +{
   1.197 +	objectPositions = NULL; // just to avoid confusion
   1.198 +	fixedMain = fixed;
   1.199 +}
   1.200 +
   1.201 +// ------------------------------------------------------------------------------------------------
   1.202 +void TargetAnimationHelper::Process(std::vector<aiVectorKey>* distanceTrack)
   1.203 +{
   1.204 +	ai_assert(NULL != targetPositions && NULL != distanceTrack);
   1.205 +
   1.206 +	// TODO: in most cases we won't need the extra array
   1.207 +	std::vector<aiVectorKey>  real;
   1.208 +	
   1.209 +	std::vector<aiVectorKey>* fill = (distanceTrack == objectPositions ? &real : distanceTrack);
   1.210 +	fill->reserve(std::max( objectPositions->size(), targetPositions->size() ));
   1.211 +
   1.212 +	// Iterate through all object keys and interpolate their values if necessary.
   1.213 +	// Then get the corresponding target position, compute the difference
   1.214 +	// vector between object and target position. Then compute a rotation matrix
   1.215 +	// that rotates the base vector of the object coordinate system at that time
   1.216 +	// to match the diff vector. 
   1.217 +
   1.218 +	KeyIterator iter(objectPositions,targetPositions,&fixedMain);
   1.219 +	for (;!iter.Finished();++iter)
   1.220 +	{
   1.221 +		const aiVector3D&  position  = iter.GetCurPosition();
   1.222 +		const aiVector3D&  tposition = iter.GetCurTargetPosition();
   1.223 +
   1.224 +		// diff vector
   1.225 +		aiVector3D diff = tposition - position;
   1.226 +		float f = diff.Length();
   1.227 +
   1.228 +		// output distance vector
   1.229 +		if (f)
   1.230 +		{
   1.231 +			fill->push_back(aiVectorKey());
   1.232 +			aiVectorKey& v = fill->back();
   1.233 +			v.mTime  = iter.GetCurTime();
   1.234 +			v.mValue = diff;
   1.235 +
   1.236 +			diff /= f;
   1.237 +		}
   1.238 +		else
   1.239 +		{
   1.240 +			// FIXME: handle this
   1.241 +		}
   1.242 +
   1.243 +		// diff is now the vector in which our camera is pointing
   1.244 +	}
   1.245 +
   1.246 +	if (real.size()) {
   1.247 +		*distanceTrack = real;
   1.248 +	}
   1.249 +}