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>
25 #include <dali/internal/update/gestures/pan-gesture-profiling.h>
38 const int MAX_GESTURE_AGE = 50; ///< maximum age of a gesture before disallowing its use in algorithm TODO: Possibly make this configurable.
39 const float ACCELERATION_THRESHOLD = 0.1f; ///< minimum pan velocity change to trigger dynamic change of prediction amount
40 const unsigned int DEFAULT_PREDICTION_INTERPOLATION = 5; ///< how much to interpolate pan position and displacement from last vsync time (in milliseconds)
41 const unsigned int DEFAULT_MAX_PREDICTION_INTERPOLATION = 32; ///< the upper bound of the range to clamp the prediction interpolation
42 const unsigned int DEFAULT_MIN_PREDICTION_INTERPOLATION = 0; ///< the lower bound of the range to clamp the prediction interpolation
43 const unsigned int DEFAULT_PREDICTION_INTERPOLATION_ADJUSTMENT = 2; ///< the amount of prediction interpolation to adjust (in milliseconds) each time when pan velocity changes
44 const float DEFAULT_SMOOTHING_AMOUNT = 0.25f; ///< how much to interpolate pan position and displacement from last vsync time
45 } // unnamed namespace
47 const PanGesture::PredictionMode PanGesture::DEFAULT_PREDICTION_MODE = PanGesture::PREDICTION_NONE;
48 const int PanGesture::NUM_PREDICTION_MODES = PanGesture::PREDICTION_1 + 1;
50 const PanGesture::SmoothingMode PanGesture::DEFAULT_SMOOTHING_MODE = PanGesture::SMOOTHING_LAST_VALUE;
51 const int PanGesture::NUM_SMOOTHING_MODES = PanGesture::SMOOTHING_LAST_VALUE + 1;
53 PanGesture* PanGesture::New()
55 return new PanGesture();
58 PanGesture::~PanGesture()
63 void PanGesture::AddGesture( const Dali::PanGesture& gesture )
65 mGestures[ mWritePosition ] = gesture;
67 // Update our write position.
69 mWritePosition %= PAN_GESTURE_HISTORY;
72 void PanGesture::RemoveOldHistory(PanInfoHistory& panHistory, unsigned int currentTime, unsigned int maxAge, unsigned int minEvents)
74 PanInfoHistoryConstIter endIter = panHistory.end();
75 PanInfoHistoryIter iter = panHistory.begin();
76 while( iter != endIter && panHistory.size() > minEvents)
78 PanInfo currentGesture = *iter;
79 if( currentTime < currentGesture.time + maxAge )
83 iter = panHistory.erase(iter);
84 endIter = panHistory.end();
87 // dont want more than 5 previous predictions for smoothing
88 iter = mPredictionHistory.begin();
89 while( mPredictionHistory.size() > 1 && iter != mPredictionHistory.end() )
91 iter = mPredictionHistory.erase(iter);
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;
109 Vector2 screenDisplacement = gestureOut.screen.displacement;
110 Vector2 localDisplacement = gestureOut.local.displacement;
112 bool havePreviousAcceleration = false;
113 bool previousVelocity = false;
114 float previousAccel = 0.0f;
115 unsigned int lastTime(0);
117 unsigned int interpolationTime = lastVSyncTime + mCurrentPredictionAmount;
119 if( interpolationTime > gestureOut.time ) // Guard against the rare case when gestureOut.time > (lastVSyncTime + mCurrentPredictionAmount)
121 interpolationTime -= gestureOut.time;
125 interpolationTime = 0u;
128 while( iter != endIter )
130 PanInfo currentGesture = *iter;
131 if( !previousVelocity )
133 // not yet set a previous velocity
134 screenVelocity = currentGesture.screen.velocity;
135 previousVelocity = true;
136 lastTime = currentGesture.time;
140 float previousValueWeight = (float)(MAX_GESTURE_AGE - (lastVSyncTime - lastTime)) / (float)MAX_GESTURE_AGE;
141 float velMag = currentGesture.screen.velocity.Length();
142 float velDiff = velMag - screenVelocity.Length();
143 float acceleration = 0.0f;
146 if (currentGesture.time > lastTime) // Guard against invalid timestamps
148 time = static_cast<float>( currentGesture.time - lastTime );
150 if( time > Math::MACHINE_EPSILON_1 )
152 acceleration = velDiff / time;
155 float newVelMag = 0.0f;
156 int currentInterpolation = interpolationTime;
157 if( !havePreviousAcceleration )
160 havePreviousAcceleration = true;
164 newVelMag = velMag + (((acceleration * (1.0f - previousValueWeight)) + (previousAccel * previousValueWeight)) * currentInterpolation);
167 if( velMag > Math::MACHINE_EPSILON_1 )
169 velMod = newVelMag / velMag;
171 gestureOut.screen.velocity = currentGesture.screen.velocity * velMod;
172 gestureOut.local.velocity = currentGesture.local.velocity * velMod;
173 screenDisplacement = gestureOut.screen.displacement + (gestureOut.screen.velocity * interpolationTime);
174 localDisplacement = gestureOut.local.displacement + (gestureOut.local.velocity * interpolationTime);
175 screenVelocity = currentGesture.screen.velocity;
176 localVelocity = currentGesture.local.velocity;
177 previousAccel = acceleration;
180 // gestureOut's position is currently equal to the last event's position and its displacement is equal to last frame's total displacement
181 // add interpolated distance and position to current
182 // work out interpolated velocity
183 gestureOut.screen.position = (gestureOut.screen.position - gestureOut.screen.displacement) + screenDisplacement;
184 gestureOut.local.position = (gestureOut.local.position - gestureOut.local.displacement) + localDisplacement;
185 gestureOut.screen.displacement = screenDisplacement;
186 gestureOut.local.displacement = localDisplacement;
187 gestureOut.time += interpolationTime;
190 void PanGesture::SmoothingAlgorithm1(bool justStarted, PanInfo& gestureOut, unsigned int lastVSyncTime)
194 gestureOut.screen.displacement = Vector2::ZERO;
195 gestureOut.screen.velocity = Vector2::ZERO;
196 gestureOut.local.displacement = Vector2::ZERO;
197 gestureOut.local.velocity = Vector2::ZERO;
201 gestureOut.screen.position -= (gestureOut.screen.position - mLastGesture.screen.position) * 0.5f * (1.0f - mSmoothingAmount);
202 gestureOut.local.position -= (gestureOut.local.position - mLastGesture.local.position) * 0.5f * (1.0f - mSmoothingAmount);
203 // make current displacement relative to previous update-frame now.
204 gestureOut.screen.displacement = gestureOut.screen.position - mLastGesture.screen.position;
205 gestureOut.local.displacement = gestureOut.local.position - mLastGesture.local.position;
206 // calculate velocity relative to previous update-frame
207 float timeDiff( gestureOut.time - mLastGesture.time );
208 gestureOut.screen.velocity = gestureOut.screen.displacement / timeDiff;
209 gestureOut.local.velocity = gestureOut.local.displacement / timeDiff;
213 void PanGesture::SmoothingAlgorithm2(bool justStarted, PanInfo& gestureOut, unsigned int lastVSyncTime)
216 mPredictionHistory.push_back(gestureOut);
218 // now smooth current pan event
219 PanInfoHistoryConstIter endIter = mPredictionHistory.end() - 1;
220 PanInfoHistoryIter iter = mPredictionHistory.begin();
222 float distanceMod = 1.0f;
224 while( iter != endIter )
226 PanInfo currentGesture = *iter;
227 float newDistanceMod = currentGesture.screen.displacement.Length() / gestureOut.screen.displacement.Length();
228 distanceMod = ((distanceMod * weight) + (newDistanceMod * (1.0f - weight)));
232 gestureOut.screen.position -= gestureOut.screen.displacement;
233 gestureOut.local.position -= gestureOut.local.displacement;
234 gestureOut.screen.displacement *= distanceMod;
235 gestureOut.local.displacement *= distanceMod;
236 gestureOut.screen.position += gestureOut.screen.displacement;
237 gestureOut.local.position += gestureOut.local.displacement;
240 bool PanGesture::ReadGestures( FrameGestureInfo& info, unsigned int currentTimestamp )
242 unsigned int previousReadPosition = 0;
243 bool eventFound = false;
244 info.frameGesture = mLastUnmodifiedGesture;
246 while( mReadPosition != mWritePosition )
248 // Copy the gesture first
249 PanInfo currentGesture( mGestures[mReadPosition] );
253 mProfiling->mRawData.push_back( PanGestureProfiling::Position( currentGesture.time, currentGesture.screen.position, currentGesture.screen.displacement, currentGesture.screen.velocity, currentGesture.state ) );
255 info.frameGesture.local.position = currentGesture.local.position;
256 info.frameGesture.local.velocity = currentGesture.local.velocity;
257 info.frameGesture.screen.position = currentGesture.screen.position;
258 info.frameGesture.screen.velocity = currentGesture.screen.velocity;
260 if( info.eventsThisFrame > 0 )
262 info.acceleration = currentGesture.screen.velocity.Length() - mGestures[previousReadPosition].screen.velocity.Length();
267 info.frameGesture.local.displacement = currentGesture.local.displacement;
268 info.frameGesture.screen.displacement = currentGesture.screen.displacement;
273 info.frameGesture.local.displacement += currentGesture.local.displacement;
274 info.frameGesture.screen.displacement += currentGesture.screen.displacement;
276 info.frameGesture.time = currentGesture.time;
278 // add event to history
279 mPanHistory.push_back( currentGesture );
280 if( currentGesture.state == Gesture::Started )
282 info.justStarted = true;
283 // clear just finished as we have started new pan
284 info.justFinished = false;
286 info.justFinished |= ( currentGesture.state == Gesture::Finished || currentGesture.state == Gesture::Cancelled );
288 // Update our read position.
289 previousReadPosition = mReadPosition;
290 ++info.eventsThisFrame;
292 mReadPosition %= PAN_GESTURE_HISTORY;
294 // This code does not determine if the data will be used.
298 bool PanGesture::ReadAndResampleGestures( FrameGestureInfo& info, unsigned int currentTimestamp )
300 PanInfo lastReadGesture;
301 while( mReadPosition != mWritePosition )
303 // Copy the gesture first
304 lastReadGesture = mGestures[mReadPosition];
307 mProfiling->mRawData.push_back( PanGestureProfiling::Position( lastReadGesture.time, lastReadGesture.screen.position,
308 lastReadGesture.screen.displacement, lastReadGesture.screen.velocity, lastReadGesture.state ) );
311 info.frameGesture.screen.position += lastReadGesture.screen.position;
312 info.frameGesture.local.position += lastReadGesture.local.position;
313 info.frameGesture.screen.velocity += lastReadGesture.screen.velocity;
314 info.frameGesture.local.velocity += lastReadGesture.local.velocity;
316 if( lastReadGesture.state == Gesture::Started )
318 // Clear just finished as we have started new pan.
319 info.justFinished = false;
320 info.justStarted = true;
324 info.justFinished |= ( lastReadGesture.state == Gesture::Finished || lastReadGesture.state == Gesture::Cancelled );
327 // Add event to history
328 mPanHistory.push_back( lastReadGesture );
330 // Update our read position.
331 ++info.eventsThisFrame;
333 mReadPosition %= PAN_GESTURE_HISTORY;
336 bool updateProperties = false;
337 if( info.eventsThisFrame > 0 )
339 // Some events were read this frame.
340 mTargetGesture = lastReadGesture;
342 if( info.eventsThisFrame > 1 )
344 info.frameGesture.screen.position /= info.eventsThisFrame;
345 info.frameGesture.local.position /= info.eventsThisFrame;
346 info.frameGesture.screen.velocity /= info.eventsThisFrame;
347 info.frameGesture.local.velocity /= info.eventsThisFrame;
349 info.frameGesture.screen.displacement = info.frameGesture.screen.position - mLastGesture.screen.position;
350 info.frameGesture.local.displacement = info.frameGesture.local.position - mLastGesture.local.position;
356 info.frameGesture.screen.displacement = lastReadGesture.screen.displacement;
357 info.frameGesture.local.displacement = lastReadGesture.local.displacement;
360 info.frameGesture.time = currentTimestamp;
362 updateProperties = true;
366 // 0 Events this frame.
369 mNotAtTarget = false;
370 info.frameGesture = mTargetGesture;
371 updateProperties = true;
375 info.frameGesture = mLastGesture;
379 return updateProperties;
382 bool PanGesture::UpdateProperties( unsigned int lastVSyncTime, unsigned int nextVSyncTime )
386 // clear current pan history
388 mPredictionHistory.clear();
391 FrameGestureInfo frameInfo;
392 bool updateProperties = false;
395 // If we are using a form of prediction, read all the input as-is.
396 if( mPredictionMode != PREDICTION_NONE )
398 // Read input required for prediction algorithms.
399 updateProperties = ReadGestures( frameInfo, lastVSyncTime );
403 // Read and resample input.
404 updateProperties = ReadAndResampleGestures( frameInfo, lastVSyncTime );
407 PanInfo frameGesture = frameInfo.frameGesture;
408 mLastUnmodifiedGesture = frameGesture;
410 // Process input data.
411 mInGesture |= frameInfo.justStarted;
417 mProfiling->mLatestData.push_back( PanGestureProfiling::Position( lastVSyncTime, frameGesture.screen.position,
418 frameGesture.screen.displacement, frameGesture.screen.velocity, frameGesture.state ) );
421 // Perform prediction.
422 switch( mPredictionMode )
424 case PREDICTION_NONE:
430 // Dynamically change the prediction amount according to the pan velocity acceleration.
431 if( !frameInfo.justStarted )
433 if( frameInfo.eventsThisFrame <= 1 )
435 frameInfo.acceleration = frameGesture.screen.velocity.Length() - mLastUnmodifiedGesture.screen.velocity.Length();
438 // Ignore tiny velocity fluctuation to avoid unnecessary prediction amount change
439 if( fabsf( frameInfo.acceleration ) > ACCELERATION_THRESHOLD )
441 mCurrentPredictionAmount += mPredictionAmountAdjustment * ( frameInfo.acceleration > Math::MACHINE_EPSILON_0 ? 1.0f : -1.0f );
442 if( mCurrentPredictionAmount > mMaxPredictionAmount + mPredictionAmountAdjustment ) // Guard against unsigned int overflow
444 mCurrentPredictionAmount = 0;
450 mCurrentPredictionAmount = mPredictionAmount; // Reset the prediction amount for each new gesture
453 mCurrentPredictionAmount = std::max( mMinPredictionAmount, std::min( mCurrentPredictionAmount, mMaxPredictionAmount ) );
455 // Calculate the delta of positions before the prediction
456 Vector2 deltaPosition = frameGesture.screen.position - mLastUnmodifiedGesture.screen.position;
458 // Make latest gesture equal to current gesture before interpolation
459 PredictiveAlgorithm1( frameInfo.eventsThisFrame, frameGesture, mPanHistory, lastVSyncTime, nextVSyncTime );
461 // Calculate the delta of positions after the prediction.
462 Vector2 deltaPredictedPosition = frameGesture.screen.position - mLastGesture.screen.position;
464 // If the change in the prediction has a different sign than the change in the actual position,
465 // there is overshot (i.e. the current prediction is too large). Return the previous prediction
466 // to give the user's finger a chance to catch up with where we have panned to.
467 bool overshotXAxis = false;
468 bool overshotYAxis = false;
469 if( (deltaPosition.x > Math::MACHINE_EPSILON_0 && deltaPredictedPosition.x < Math::MACHINE_EPSILON_0 )
470 || (deltaPosition.x < Math::MACHINE_EPSILON_0 && deltaPredictedPosition.x > Math::MACHINE_EPSILON_0 ) )
472 overshotXAxis = true;
473 frameGesture.screen.position.x = mLastGesture.screen.position.x;
476 if( (deltaPosition.y > Math::MACHINE_EPSILON_0 && deltaPredictedPosition.y < Math::MACHINE_EPSILON_0 )
477 || (deltaPosition.y < Math::MACHINE_EPSILON_0 && deltaPredictedPosition.y > Math::MACHINE_EPSILON_0 ) )
479 overshotYAxis = true;
480 frameGesture.screen.position.y = mLastGesture.screen.position.y;
483 // If there is overshot in one axis, reduce the possible overshot in the other axis,
484 // and reduce the prediction amount so that it doesn't overshoot as easily next time.
485 if(overshotXAxis || overshotYAxis)
487 mCurrentPredictionAmount -= mPredictionAmountAdjustment;
488 if( mCurrentPredictionAmount > mMaxPredictionAmount + mPredictionAmountAdjustment ) // Guard against unsigned int overflow
490 mCurrentPredictionAmount = 0;
492 mCurrentPredictionAmount = std::max( mMinPredictionAmount, std::min( mCurrentPredictionAmount, mMaxPredictionAmount ) );
494 if( overshotXAxis && !overshotYAxis )
496 frameGesture.screen.position.y = ( mLastGesture.screen.position.y + frameGesture.screen.position.y ) * 0.5f;
499 if( overshotYAxis && !overshotXAxis )
501 frameGesture.screen.position.x = ( mLastGesture.screen.position.x + frameGesture.screen.position.x ) * 0.5f;
505 updateProperties = true;
510 // Perform smoothing.
511 switch( mSmoothingMode )
518 case SMOOTHING_LAST_VALUE:
520 SmoothingAlgorithm1( frameInfo.justStarted, frameGesture, lastVSyncTime );
525 if( updateProperties )
527 // only update properties if event received
528 // set latest gesture to raw pan info with unchanged time
529 mPanning.Set( mInGesture & !frameInfo.justFinished );
530 mScreenPosition.Set( frameGesture.screen.position );
531 mScreenDisplacement.Set( frameGesture.screen.displacement );
532 mScreenVelocity.Set( frameGesture.screen.velocity );
533 mLocalPosition.Set( frameGesture.local.position );
534 mLocalDisplacement.Set( frameGesture.local.displacement );
535 mLocalVelocity.Set( frameGesture.local.velocity );
540 mProfiling->mAveragedData.push_back( PanGestureProfiling::Position( frameGesture.time, frameGesture.screen.position,
541 frameGesture.screen.displacement, frameGesture.screen.velocity, frameGesture.state ) );
545 mLastGesture = frameGesture;
547 mInGesture &= ~frameInfo.justFinished;
548 if( mProfiling && frameInfo.justFinished )
550 mProfiling->PrintData();
551 mProfiling->ClearData();
554 return updateProperties;
557 const GesturePropertyBool& PanGesture::GetPanningProperty() const
562 const GesturePropertyVector2& PanGesture::GetScreenPositionProperty() const
564 return mScreenPosition;
567 const GesturePropertyVector2& PanGesture::GetScreenVelocityProperty() const
569 return mScreenVelocity;
572 const GesturePropertyVector2& PanGesture::GetScreenDisplacementProperty() const
574 return mScreenDisplacement;
577 const GesturePropertyVector2& PanGesture::GetLocalPositionProperty() const
579 return mLocalPosition;
582 const GesturePropertyVector2& PanGesture::GetLocalDisplacementProperty() const
584 return mLocalDisplacement;
587 const GesturePropertyVector2& PanGesture::GetLocalVelocityProperty() const
589 return mLocalVelocity;
592 void PanGesture::SetPredictionMode(PredictionMode mode)
594 mPredictionMode = mode;
597 void PanGesture::SetPredictionAmount(unsigned int amount)
599 mPredictionAmount = amount;
602 void PanGesture::SetMaximumPredictionAmount(unsigned int amount)
604 mMaxPredictionAmount = amount;
607 void PanGesture::SetMinimumPredictionAmount(unsigned int amount)
609 mMinPredictionAmount = amount;
612 void PanGesture::SetPredictionAmountAdjustment(unsigned int amount)
614 mPredictionAmountAdjustment = amount;
617 void PanGesture::SetSmoothingMode(SmoothingMode mode)
619 mSmoothingMode = mode;
622 void PanGesture::SetSmoothingAmount(float amount)
624 mSmoothingAmount = amount;
627 void PanGesture::EnableProfiling()
631 mProfiling = new PanGestureProfiling();
635 void PanGesture::ResetDefaultProperties( BufferIndex updateBufferIndex )
637 mScreenPosition.Reset();
638 mScreenDisplacement.Reset();
639 mLocalPosition.Reset();
640 mLocalDisplacement.Reset();
644 PanGesture::PanGesture()
649 mNotAtTarget( false ),
651 mPredictionMode( DEFAULT_PREDICTION_MODE ),
652 mPredictionAmount( DEFAULT_PREDICTION_INTERPOLATION ),
653 mCurrentPredictionAmount( DEFAULT_PREDICTION_INTERPOLATION ),
654 mMaxPredictionAmount( DEFAULT_MAX_PREDICTION_INTERPOLATION ),
655 mMinPredictionAmount( DEFAULT_MIN_PREDICTION_INTERPOLATION ),
656 mPredictionAmountAdjustment( DEFAULT_PREDICTION_INTERPOLATION_ADJUSTMENT ),
657 mSmoothingMode( DEFAULT_SMOOTHING_MODE ),
658 mSmoothingAmount( DEFAULT_SMOOTHING_AMOUNT ),
664 } // namespace SceneGraph
666 } // namespace Internal