vrshoot

view 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 source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 #include "AssimpPCH.h"
42 #include "TargetAnimation.h"
43 #include <algorithm>
45 using namespace Assimp;
48 // ------------------------------------------------------------------------------------------------
49 KeyIterator::KeyIterator(const std::vector<aiVectorKey>* _objPos,
50 const std::vector<aiVectorKey>* _targetObjPos,
51 const aiVector3D* defaultObjectPos /*= NULL*/,
52 const aiVector3D* defaultTargetPos /*= NULL*/)
54 : reachedEnd (false)
55 , curTime (-1.)
56 , objPos (_objPos)
57 , targetObjPos (_targetObjPos)
58 , nextObjPos (0)
59 , nextTargetObjPos(0)
60 {
61 // Generate default transformation tracks if necessary
62 if (!objPos || objPos->empty())
63 {
64 defaultObjPos.resize(1);
65 defaultObjPos.front().mTime = 10e10;
67 if (defaultObjectPos)
68 defaultObjPos.front().mValue = *defaultObjectPos;
70 objPos = & defaultObjPos;
71 }
72 if (!targetObjPos || targetObjPos->empty())
73 {
74 defaultTargetObjPos.resize(1);
75 defaultTargetObjPos.front().mTime = 10e10;
77 if (defaultTargetPos)
78 defaultTargetObjPos.front().mValue = *defaultTargetPos;
80 targetObjPos = & defaultTargetObjPos;
81 }
82 }
84 // ------------------------------------------------------------------------------------------------
85 template <class T>
86 inline T Interpolate(const T& one, const T& two, float val)
87 {
88 return one + (two-one)*val;
89 }
91 // ------------------------------------------------------------------------------------------------
92 void KeyIterator::operator ++()
93 {
94 // If we are already at the end of all keyframes, return
95 if (reachedEnd) {
96 return;
97 }
99 // Now search in all arrays for the time value closest
100 // to our current position on the time line
101 double d0,d1;
103 d0 = objPos->at ( std::min<unsigned int> ( nextObjPos, objPos->size()-1) ).mTime;
104 d1 = targetObjPos->at( std::min<unsigned int> ( nextTargetObjPos, targetObjPos->size()-1) ).mTime;
106 // Easiest case - all are identical. In this
107 // case we don't need to interpolate so we can
108 // return earlier
109 if ( d0 == d1 )
110 {
111 curTime = d0;
112 curPosition = objPos->at(nextObjPos).mValue;
113 curTargetPosition = targetObjPos->at(nextTargetObjPos).mValue;
115 // increment counters
116 if (objPos->size() != nextObjPos-1)
117 ++nextObjPos;
119 if (targetObjPos->size() != nextTargetObjPos-1)
120 ++nextTargetObjPos;
121 }
123 // An object position key is closest to us
124 else if (d0 < d1)
125 {
126 curTime = d0;
128 // interpolate the other
129 if (1 == targetObjPos->size() || !nextTargetObjPos) {
130 curTargetPosition = targetObjPos->at(0).mValue;
131 }
132 else
133 {
134 const aiVectorKey& last = targetObjPos->at(nextTargetObjPos);
135 const aiVectorKey& first = targetObjPos->at(nextTargetObjPos-1);
137 curTargetPosition = Interpolate(first.mValue, last.mValue, (float) (
138 (curTime-first.mTime) / (last.mTime-first.mTime) ));
139 }
141 if (objPos->size() != nextObjPos-1)
142 ++nextObjPos;
143 }
144 // A target position key is closest to us
145 else
146 {
147 curTime = d1;
149 // interpolate the other
150 if (1 == objPos->size() || !nextObjPos) {
151 curPosition = objPos->at(0).mValue;
152 }
153 else
154 {
155 const aiVectorKey& last = objPos->at(nextObjPos);
156 const aiVectorKey& first = objPos->at(nextObjPos-1);
158 curPosition = Interpolate(first.mValue, last.mValue, (float) (
159 (curTime-first.mTime) / (last.mTime-first.mTime)));
160 }
162 if (targetObjPos->size() != nextTargetObjPos-1)
163 ++nextTargetObjPos;
164 }
166 if (nextObjPos >= objPos->size()-1 &&
167 nextTargetObjPos >= targetObjPos->size()-1)
168 {
169 // We reached the very last keyframe
170 reachedEnd = true;
171 }
172 }
174 // ------------------------------------------------------------------------------------------------
175 void TargetAnimationHelper::SetTargetAnimationChannel (
176 const std::vector<aiVectorKey>* _targetPositions)
177 {
178 ai_assert(NULL != _targetPositions);
179 targetPositions = _targetPositions;
180 }
182 // ------------------------------------------------------------------------------------------------
183 void TargetAnimationHelper::SetMainAnimationChannel (
184 const std::vector<aiVectorKey>* _objectPositions)
185 {
186 ai_assert(NULL != _objectPositions);
187 objectPositions = _objectPositions;
188 }
190 // ------------------------------------------------------------------------------------------------
191 void TargetAnimationHelper::SetFixedMainAnimationChannel(
192 const aiVector3D& fixed)
193 {
194 objectPositions = NULL; // just to avoid confusion
195 fixedMain = fixed;
196 }
198 // ------------------------------------------------------------------------------------------------
199 void TargetAnimationHelper::Process(std::vector<aiVectorKey>* distanceTrack)
200 {
201 ai_assert(NULL != targetPositions && NULL != distanceTrack);
203 // TODO: in most cases we won't need the extra array
204 std::vector<aiVectorKey> real;
206 std::vector<aiVectorKey>* fill = (distanceTrack == objectPositions ? &real : distanceTrack);
207 fill->reserve(std::max( objectPositions->size(), targetPositions->size() ));
209 // Iterate through all object keys and interpolate their values if necessary.
210 // Then get the corresponding target position, compute the difference
211 // vector between object and target position. Then compute a rotation matrix
212 // that rotates the base vector of the object coordinate system at that time
213 // to match the diff vector.
215 KeyIterator iter(objectPositions,targetPositions,&fixedMain);
216 for (;!iter.Finished();++iter)
217 {
218 const aiVector3D& position = iter.GetCurPosition();
219 const aiVector3D& tposition = iter.GetCurTargetPosition();
221 // diff vector
222 aiVector3D diff = tposition - position;
223 float f = diff.Length();
225 // output distance vector
226 if (f)
227 {
228 fill->push_back(aiVectorKey());
229 aiVectorKey& v = fill->back();
230 v.mTime = iter.GetCurTime();
231 v.mValue = diff;
233 diff /= f;
234 }
235 else
236 {
237 // FIXME: handle this
238 }
240 // diff is now the vector in which our camera is pointing
241 }
243 if (real.size()) {
244 *distanceTrack = real;
245 }
246 }