2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
24 #include <dali/internal/update/gestures/pan-gesture-profiling.h>
36 const int MAX_GESTURE_AGE = 50; ///< maximum age of a gesture before disallowing its use in algorithm
37 const unsigned int DEFAULT_PREDICTION_INTERPOLATION = 0; ///< how much to interpolate pan position and displacement from last vsync time
38 } // unnamed namespace
40 const PanGesture::PredictionMode PanGesture::DEFAULT_PREDICTION_MODE = PanGesture::AVERAGE;
41 const int PanGesture::NUM_PREDICTION_MODES = PanGesture::PREDICTION_2 + 1;
43 PanGesture* PanGesture::New()
45 return new PanGesture();
48 PanGesture::~PanGesture()
53 void PanGesture::AddGesture( const Dali::PanGesture& gesture )
55 mGestures[ mWritePosition ] = gesture;
57 // Update our write position.
59 mWritePosition %= PAN_GESTURE_HISTORY;
62 void PanGesture::RemoveOldHistory(PanInfoHistory& panHistory, uint currentTime, uint maxAge, uint minEvents)
64 PanInfoHistoryConstIter endIter = panHistory.end();
65 PanInfoHistoryIter iter = panHistory.begin();
66 while( iter != endIter && panHistory.size() > minEvents)
68 PanInfo currentGesture = *iter;
69 if( currentTime < currentGesture.time + maxAge )
73 iter = panHistory.erase(iter);
74 endIter = panHistory.end();
78 void PanGesture::SimpleAverageAlgorithm(bool justStarted, PanInfo& gestureOut)
84 gestureOut.screen.position += mLastEventGesture.screen.position;
85 gestureOut.local.position += mLastEventGesture.local.position;
86 gestureOut.screen.position *= 0.5f;
87 gestureOut.local.position *= 0.5f;
88 // make current displacement relative to previous update-frame now.
89 gestureOut.screen.displacement = gestureOut.screen.position - mLastEventGesture.screen.position;
90 gestureOut.local.displacement = gestureOut.local.position - mLastEventGesture.local.position;
95 void PanGesture::PredictiveAlgorithm1(int eventsThisFrame, PanInfo& gestureOut, PanInfoHistory& panHistory, unsigned int lastVSyncTime, unsigned int nextVSyncTime)
97 RemoveOldHistory(panHistory, lastVSyncTime, MAX_GESTURE_AGE, 0);
98 size_t panHistorySize = panHistory.size();
99 if( panHistorySize == 0 )
101 // cant do any prediction without a history
105 PanInfoHistoryConstIter endIter = panHistory.end();
106 PanInfoHistoryIter iter = panHistory.begin();
107 Vector2 screenVelocity = gestureOut.screen.velocity;
108 Vector2 localVelocity = gestureOut.local.velocity;
110 bool havePreviousAcceleration = false;
111 bool previousVelocity = false;
112 float previousAccel = 0.0f;
113 unsigned int lastTime(0);
114 while( iter != endIter )
116 PanInfo currentGesture = *iter;
117 if( !previousVelocity )
119 // not yet set a previous velocity
120 screenVelocity = currentGesture.screen.velocity;
121 previousVelocity = true;
122 lastTime = currentGesture.time;
126 float velMag = currentGesture.screen.velocity.Length();
127 float velDiff = velMag - screenVelocity.Length();
128 float acceleration = 0.0f;
129 float time = (float)(currentGesture.time - lastTime);
130 if( time > Math::MACHINE_EPSILON_1 )
132 acceleration = velDiff / time;
134 float interpolationTime = (float)((int)lastVSyncTime - (int)currentGesture.time);
135 float newVelMag = 0.0f;
136 if( !havePreviousAcceleration )
138 newVelMag = velMag + (acceleration * interpolationTime);
139 havePreviousAcceleration = true;
143 newVelMag = velMag + (((acceleration + previousAccel) * 0.5f) * interpolationTime);
146 if( velMag > Math::MACHINE_EPSILON_1 )
148 velMod = newVelMag / velMag;
150 gestureOut.screen.velocity = currentGesture.screen.velocity * velMod;
151 gestureOut.local.velocity = currentGesture.local.velocity * velMod;
152 screenVelocity = currentGesture.screen.velocity;
153 localVelocity = currentGesture.local.velocity;
154 previousAccel = acceleration;
157 // gestureOut's position is currently equal to the last event's position and its displacement is equal to last frame's total displacement
158 // add interpolated distance and position to current
159 float interpolationTime = (float)((int)lastVSyncTime - (int)gestureOut.time);
160 // work out interpolated velocity
161 gestureOut.screen.displacement = (gestureOut.screen.velocity * interpolationTime);
162 gestureOut.local.displacement = (gestureOut.local.velocity * interpolationTime);
163 gestureOut.screen.position += gestureOut.screen.displacement;
164 gestureOut.local.position += gestureOut.local.displacement;
165 gestureOut.time += (lastVSyncTime - gestureOut.time);
168 void PanGesture::PredictiveAlgorithm2(int eventsThisFrame, PanInfo& gestureOut, PanInfoHistory& panHistory, unsigned int lastVSyncTime, unsigned int nextVSyncTime)
170 // TODO - adapt PredictiveAlgorithm1 with better smoothing, still under development
171 RemoveOldHistory(panHistory, lastVSyncTime, MAX_GESTURE_AGE, 0);
172 size_t panHistorySize = panHistory.size();
173 if( panHistorySize == 0 )
175 // cant do any prediction without a history
179 PanInfoHistoryConstIter endIter = panHistory.end();
180 PanInfoHistoryIter iter = panHistory.begin();
181 Vector2 screenVelocity = gestureOut.screen.velocity;
182 Vector2 localVelocity = gestureOut.local.velocity;
184 bool havePreviousAcceleration = false;
185 bool previousVelocity = false;
186 float previousAccel = 0.0f;
187 unsigned int lastTime(0);
188 while( iter != endIter )
190 PanInfo currentGesture = *iter;
191 if( !previousVelocity )
193 // not yet set a previous velocity
194 screenVelocity = currentGesture.screen.velocity;
195 previousVelocity = true;
196 lastTime = currentGesture.time;
200 float previousValueWeight = (float)(MAX_GESTURE_AGE - (lastVSyncTime - lastTime)) / (float)MAX_GESTURE_AGE;
201 float velMag = currentGesture.screen.velocity.Length();
202 float velDiff = velMag - screenVelocity.Length();
203 float acceleration = 0.0f;
204 float time = (float)(currentGesture.time - lastTime);
205 if( time > Math::MACHINE_EPSILON_1 )
207 acceleration = velDiff / time;
209 float interpolationTime = (float)((int)lastVSyncTime - (int)currentGesture.time);
210 float newVelMag = 0.0f;
211 if( !havePreviousAcceleration )
213 newVelMag = velMag + (acceleration * interpolationTime);
214 havePreviousAcceleration = true;
218 newVelMag = velMag + (((acceleration * (1.0f - previousValueWeight)) + (previousAccel * previousValueWeight)) * interpolationTime);
221 if( velMag > Math::MACHINE_EPSILON_1 )
223 velMod = newVelMag / velMag;
225 gestureOut.screen.velocity = currentGesture.screen.velocity * velMod;
226 gestureOut.local.velocity = currentGesture.local.velocity * velMod;
227 screenVelocity = currentGesture.screen.velocity;
228 localVelocity = currentGesture.local.velocity;
229 previousAccel = acceleration;
232 // gestureOut's position is currently equal to the last event's position and its displacement is equal to last frame's total displacement
233 // add interpolated distance and position to current
234 unsigned int interpolationTime = (lastVSyncTime + mPredictionAmount) - gestureOut.time;
235 // work out interpolated velocity
236 gestureOut.screen.displacement = (gestureOut.screen.velocity * interpolationTime);
237 gestureOut.local.displacement = (gestureOut.local.velocity * interpolationTime);
238 gestureOut.screen.position += gestureOut.screen.displacement;
239 gestureOut.local.position += gestureOut.local.displacement;
240 gestureOut.time += interpolationTime;
243 bool PanGesture::UpdateProperties( unsigned int lastVSyncTime, unsigned int nextVSyncTime )
245 bool propertiesUpdated( false );
249 // clear current pan history
253 // create an event for this frame
254 bool justStarted ( false );
255 bool justFinished ( false );
256 bool eventFound( false );
258 // Not going through array from the beginning, using it as a circular buffer and only using unread
260 int eventsThisFrame = 0;
262 // create PanInfo to pass into prediction method
263 PanInfo nextGesture = mEventGesture;
264 // add new gestures and work out one full gesture for the frame
265 while(mReadPosition != mWritePosition)
267 // Copy the gesture first
268 PanInfo currentGesture(mGestures[mReadPosition]);
272 mProfiling->mRawData.push_back( PanGestureProfiling::Position( currentGesture.time, currentGesture.screen.position ) );
274 nextGesture.local.position = currentGesture.local.position;
275 nextGesture.local.velocity = currentGesture.local.velocity;
276 nextGesture.screen.position = currentGesture.screen.position;
277 nextGesture.screen.velocity = currentGesture.screen.velocity;
280 nextGesture.local.displacement = currentGesture.local.displacement;
281 nextGesture.screen.displacement = currentGesture.screen.displacement;
285 nextGesture.local.displacement += currentGesture.local.displacement;
286 nextGesture.screen.displacement += currentGesture.screen.displacement;
289 nextGesture.time = currentGesture.time;
291 // add event to history
292 mPanHistory.push_back(currentGesture);
293 justStarted |= (currentGesture.state == Gesture::Started);
294 if( currentGesture.state == Gesture::Started )
297 // clear just finished as we have started new pan
298 justFinished = false;
300 justFinished |= (currentGesture.state == Gesture::Finished || currentGesture.state == Gesture::Cancelled);
302 // Update our read position.
305 mReadPosition %= PAN_GESTURE_HISTORY;
307 // set nextGesture to last gesture so it's position is correct and velocity is same as last frame
308 mEventGesture = nextGesture;
310 mInGesture |= justStarted;
312 bool updateProperties = false;
318 mProfiling->mLatestData.push_back( PanGestureProfiling::Position( lastVSyncTime, mEventGesture.screen.position ) );
321 switch( mPredictionMode )
325 updateProperties = eventFound;
330 SimpleAverageAlgorithm(justStarted, nextGesture);
331 // make latest gesture equal to current gesture after averaging
332 updateProperties = true;
337 // make latest gesture equal to current gesture before interpolation
338 PredictiveAlgorithm1(eventsThisFrame, nextGesture, mPanHistory, lastVSyncTime, nextVSyncTime);
339 updateProperties = true;
344 // make latest gesture equal to current gesture before interpolation
345 PredictiveAlgorithm2(eventsThisFrame, nextGesture, mPanHistory, lastVSyncTime, nextVSyncTime);
346 updateProperties = true;
351 // always keep latest gesture up to date with event gesture
352 mLatestGesture = nextGesture;
354 if( updateProperties )
356 // only update properties if event received
357 // set latest gesture to raw pan info with unchanged time
358 mPanning.Set( mInGesture & !justFinished );
359 mScreenPosition.Set( nextGesture.screen.position );
360 mScreenDisplacement.Set( nextGesture.screen.displacement );
361 mLocalPosition.Set( nextGesture.local.position );
362 mLocalDisplacement.Set( nextGesture.local.displacement );
364 propertiesUpdated = true;
369 mProfiling->mAveragedData.push_back( PanGestureProfiling::Position( nextGesture.time, nextGesture.screen.position ) );
372 mLastEventGesture = mEventGesture;
374 mInGesture &= ~justFinished;
376 if( mProfiling && justFinished )
378 mProfiling->PrintData();
379 mProfiling->ClearData();
382 return propertiesUpdated;
385 const GesturePropertyBool& PanGesture::GetPanningProperty() const
390 const GesturePropertyVector2& PanGesture::GetScreenPositionProperty() const
392 return mScreenPosition;
395 const GesturePropertyVector2& PanGesture::GetScreenDisplacementProperty() const
397 return mScreenDisplacement;
400 const GesturePropertyVector2& PanGesture::GetLocalPositionProperty() const
402 return mLocalPosition;
405 const GesturePropertyVector2& PanGesture::GetLocalDisplacementProperty() const
407 return mLocalDisplacement;
410 void PanGesture::SetPredictionMode(PredictionMode mode)
412 mPredictionMode = mode;
415 void PanGesture::SetPredictionAmount(unsigned int amount)
417 mPredictionAmount = amount;
420 void PanGesture::EnableProfiling()
424 mProfiling = new PanGestureProfiling();
428 void PanGesture::ResetDefaultProperties( BufferIndex updateBufferIndex )
430 mScreenPosition.Reset();
431 mScreenDisplacement.Reset();
432 mLocalPosition.Reset();
433 mLocalDisplacement.Reset();
437 PanGesture::PanGesture()
442 mPredictionMode(DEFAULT_PREDICTION_MODE),
443 mPredictionAmount(DEFAULT_PREDICTION_INTERPOLATION),
448 } // namespace SceneGraph
450 } // namespace Internal