#include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
// EXTERNAL INCLUDES
+#include <cmath>
// INTERNAL INCLUDES
#include <dali/internal/update/gestures/pan-gesture-profiling.h>
namespace SceneGraph
{
+
namespace
{
-const int MAX_GESTURE_AGE = 50; ///< maximum age of a gesture before disallowing its use in algorithm
-const unsigned int DEFAULT_PREDICTION_INTERPOLATION = 0; ///< how much to interpolate pan position and displacement from last vsync time
-const float DEFAULT_SMOOTHING_AMOUNT = 1.0f; ///< how much to interpolate pan position and displacement from last vsync time
+const int MAX_GESTURE_AGE = 50; ///< maximum age of a gesture before disallowing its use in algorithm TODO: Possibly make this configurable.
+const float ACCELERATION_THRESHOLD = 0.1f; ///< minimum pan velocity change to trigger dynamic change of prediction amount
+const unsigned int DEFAULT_PREDICTION_INTERPOLATION = 5; ///< how much to interpolate pan position and displacement from last vsync time (in milliseconds)
+const unsigned int DEFAULT_MAX_PREDICTION_INTERPOLATION = 32; ///< the upper bound of the range to clamp the prediction interpolation
+const unsigned int DEFAULT_MIN_PREDICTION_INTERPOLATION = 0; ///< the lower bound of the range to clamp the prediction interpolation
+const unsigned int DEFAULT_PREDICTION_INTERPOLATION_ADJUSTMENT = 2; ///< the amount of prediction interpolation to adjust (in milliseconds) each time when pan velocity changes
+const float DEFAULT_SMOOTHING_AMOUNT = 0.25f; ///< how much to interpolate pan position and displacement from last vsync time
} // unnamed namespace
const PanGesture::PredictionMode PanGesture::DEFAULT_PREDICTION_MODE = PanGesture::PREDICTION_NONE;
float previousAccel = 0.0f;
unsigned int lastTime(0);
- unsigned int interpolationTime = lastVSyncTime + mPredictionAmount;
- if( interpolationTime > gestureOut.time ) // Guard against the rare case when gestureOut.time > (lastVSyncTime + mPredictionAmount)
+ unsigned int interpolationTime = lastVSyncTime + mCurrentPredictionAmount;
+
+ if( interpolationTime > gestureOut.time ) // Guard against the rare case when gestureOut.time > (lastVSyncTime + mCurrentPredictionAmount)
{
interpolationTime -= gestureOut.time;
}
void PanGesture::SmoothingAlgorithm1(bool justStarted, PanInfo& gestureOut, unsigned int lastVSyncTime)
{
- if( !justStarted )
+ if( justStarted )
+ {
+ gestureOut.screen.displacement = Vector2::ZERO;
+ gestureOut.screen.velocity = Vector2::ZERO;
+ gestureOut.local.displacement = Vector2::ZERO;
+ gestureOut.local.velocity = Vector2::ZERO;
+ }
+ else
{
gestureOut.screen.position -= (gestureOut.screen.position - mLastGesture.screen.position) * 0.5f * (1.0f - mSmoothingAmount);
gestureOut.local.position -= (gestureOut.local.position - mLastGesture.local.position) * 0.5f * (1.0f - mSmoothingAmount);
gestureOut.screen.displacement = gestureOut.screen.position - mLastGesture.screen.position;
gestureOut.local.displacement = gestureOut.local.position - mLastGesture.local.position;
// calculate velocity relative to previous update-frame
- unsigned int timeDiff( gestureOut.time - mLastGesture.time );
+ float timeDiff( gestureOut.time - mLastGesture.time );
gestureOut.screen.velocity = gestureOut.screen.displacement / timeDiff;
gestureOut.local.velocity = gestureOut.local.displacement / timeDiff;
}
gestureOut.local.position += gestureOut.local.displacement;
}
-bool PanGesture::UpdateProperties( unsigned int lastVSyncTime, unsigned int nextVSyncTime )
+bool PanGesture::ReadGestures( FrameGestureInfo& info, unsigned int currentTimestamp )
{
- if( !mInGesture )
- {
- // clear current pan history
- mPanHistory.clear();
- mPredictionHistory.clear();
- }
+ unsigned int previousReadPosition = 0;
+ bool eventFound = false;
+ info.frameGesture = mLastUnmodifiedGesture;
- // create an event for this frame
- bool justStarted ( false );
- bool justFinished ( false );
- bool eventFound( false );
-
- // Not going through array from the beginning, using it as a circular buffer and only using unread
- // values.
- int eventsThisFrame = 0;
-
- // create PanInfo to pass into prediction method
- mLastEventGesture = mEventGesture;
- mLastGesture = mLatestGesture;
- // add new gestures and work out one full gesture for the frame
- while(mReadPosition != mWritePosition)
+ while( mReadPosition != mWritePosition )
{
// Copy the gesture first
- PanInfo currentGesture(mGestures[mReadPosition]);
+ PanInfo currentGesture( mGestures[mReadPosition] );
if( mProfiling )
{
- mProfiling->mRawData.push_back( PanGestureProfiling::Position( currentGesture.time, currentGesture.screen.position ) );
+ mProfiling->mRawData.push_back( PanGestureProfiling::Position( currentGesture.time, currentGesture.screen.position, currentGesture.screen.displacement, currentGesture.screen.velocity, currentGesture.state ) );
+ }
+ info.frameGesture.local.position = currentGesture.local.position;
+ info.frameGesture.local.velocity = currentGesture.local.velocity;
+ info.frameGesture.screen.position = currentGesture.screen.position;
+ info.frameGesture.screen.velocity = currentGesture.screen.velocity;
+
+ if( info.eventsThisFrame > 0 )
+ {
+ info.acceleration = currentGesture.screen.velocity.Length() - mGestures[previousReadPosition].screen.velocity.Length();
}
- mEventGesture.local.position = currentGesture.local.position;
- mEventGesture.local.velocity = currentGesture.local.velocity;
- mEventGesture.screen.position = currentGesture.screen.position;
- mEventGesture.screen.velocity = currentGesture.screen.velocity;
+
if( !eventFound )
{
- mEventGesture.local.displacement = currentGesture.local.displacement;
- mEventGesture.screen.displacement = currentGesture.screen.displacement;
+ info.frameGesture.local.displacement = currentGesture.local.displacement;
+ info.frameGesture.screen.displacement = currentGesture.screen.displacement;
+ eventFound = true;
}
else
{
- mEventGesture.local.displacement += currentGesture.local.displacement;
- mEventGesture.screen.displacement += currentGesture.screen.displacement;
+ info.frameGesture.local.displacement += currentGesture.local.displacement;
+ info.frameGesture.screen.displacement += currentGesture.screen.displacement;
}
- eventFound = true;
- mEventGesture.time = currentGesture.time;
+ info.frameGesture.time = currentGesture.time;
// add event to history
- mPanHistory.push_back(currentGesture);
+ mPanHistory.push_back( currentGesture );
if( currentGesture.state == Gesture::Started )
{
- justStarted = true;
+ info.justStarted = true;
// clear just finished as we have started new pan
- justFinished = false;
+ info.justFinished = false;
}
- justFinished |= (currentGesture.state == Gesture::Finished || currentGesture.state == Gesture::Cancelled);
+ info.justFinished |= ( currentGesture.state == Gesture::Finished || currentGesture.state == Gesture::Cancelled );
// Update our read position.
- ++eventsThisFrame;
+ previousReadPosition = mReadPosition;
+ ++info.eventsThisFrame;
++mReadPosition;
mReadPosition %= PAN_GESTURE_HISTORY;
}
- mLatestGesture = mEventGesture;
+ // This code does not determine if the data will be used.
+ return false;
+}
+
+bool PanGesture::ReadAndResampleGestures( FrameGestureInfo& info, unsigned int currentTimestamp )
+{
+ PanInfo lastReadGesture;
+ while( mReadPosition != mWritePosition )
+ {
+ // Copy the gesture first
+ lastReadGesture = mGestures[mReadPosition];
+ if( mProfiling )
+ {
+ mProfiling->mRawData.push_back( PanGestureProfiling::Position( lastReadGesture.time, lastReadGesture.screen.position,
+ lastReadGesture.screen.displacement, lastReadGesture.screen.velocity, lastReadGesture.state ) );
+ }
+
+ info.frameGesture.screen.position += lastReadGesture.screen.position;
+ info.frameGesture.local.position += lastReadGesture.local.position;
+ info.frameGesture.screen.velocity += lastReadGesture.screen.velocity;
+ info.frameGesture.local.velocity += lastReadGesture.local.velocity;
+
+ if( lastReadGesture.state == Gesture::Started )
+ {
+ // Clear just finished as we have started new pan.
+ info.justFinished = false;
+ info.justStarted = true;
+ }
+ else
+ {
+ info.justFinished |= ( lastReadGesture.state == Gesture::Finished || lastReadGesture.state == Gesture::Cancelled );
+ }
+
+ // Add event to history
+ mPanHistory.push_back( lastReadGesture );
+
+ // Update our read position.
+ ++info.eventsThisFrame;
+ ++mReadPosition;
+ mReadPosition %= PAN_GESTURE_HISTORY;
+ }
+
+ bool updateProperties = false;
+ if( info.eventsThisFrame > 0 )
+ {
+ // Some events were read this frame.
+ mTargetGesture = lastReadGesture;
+
+ if( info.eventsThisFrame > 1 )
+ {
+ info.frameGesture.screen.position /= info.eventsThisFrame;
+ info.frameGesture.local.position /= info.eventsThisFrame;
+ info.frameGesture.screen.velocity /= info.eventsThisFrame;
+ info.frameGesture.local.velocity /= info.eventsThisFrame;
+
+ info.frameGesture.screen.displacement = info.frameGesture.screen.position - mLastGesture.screen.position;
+ info.frameGesture.local.displacement = info.frameGesture.local.position - mLastGesture.local.position;
+
+ mNotAtTarget = true;
+ }
+ else
+ {
+ info.frameGesture.screen.displacement = lastReadGesture.screen.displacement;
+ info.frameGesture.local.displacement = lastReadGesture.local.displacement;
+ }
+
+ info.frameGesture.time = currentTimestamp;
+
+ updateProperties = true;
+ }
+ else
+ {
+ // 0 Events this frame.
+ if( mNotAtTarget )
+ {
+ mNotAtTarget = false;
+ info.frameGesture = mTargetGesture;
+ updateProperties = true;
+ }
+ else
+ {
+ info.frameGesture = mLastGesture;
+ }
+ }
- mInGesture |= justStarted;
+ return updateProperties;
+}
+bool PanGesture::UpdateProperties( unsigned int lastVSyncTime, unsigned int nextVSyncTime )
+{
+ if( !mInGesture )
+ {
+ // clear current pan history
+ mPanHistory.clear();
+ mPredictionHistory.clear();
+ }
+
+ FrameGestureInfo frameInfo;
bool updateProperties = false;
- if ( mInGesture )
+ // Read input data.
+ // If we are using a form of prediction, read all the input as-is.
+ if( mPredictionMode != PREDICTION_NONE )
+ {
+ // Read input required for prediction algorithms.
+ updateProperties = ReadGestures( frameInfo, lastVSyncTime );
+ }
+ else
+ {
+ // Read and resample input.
+ updateProperties = ReadAndResampleGestures( frameInfo, lastVSyncTime );
+ }
+
+ PanInfo frameGesture = frameInfo.frameGesture;
+ mLastUnmodifiedGesture = frameGesture;
+
+ // Process input data.
+ mInGesture |= frameInfo.justStarted;
+ if( mInGesture )
{
+ // Profiling.
if( mProfiling )
{
- mProfiling->mLatestData.push_back( PanGestureProfiling::Position( lastVSyncTime, mEventGesture.screen.position ) );
+ mProfiling->mLatestData.push_back( PanGestureProfiling::Position( lastVSyncTime, frameGesture.screen.position,
+ frameGesture.screen.displacement, frameGesture.screen.velocity, frameGesture.state ) );
}
+ // Perform prediction.
switch( mPredictionMode )
{
case PREDICTION_NONE:
{
- updateProperties = eventFound;
- // dont want event time
- unsigned int time = mLastGesture.time;
- mLastGesture = mLastEventGesture;
- mLastGesture.time = time;
- mLatestGesture.time = lastVSyncTime;
break;
}
case PREDICTION_1:
{
- // make latest gesture equal to current gesture before interpolation
- PredictiveAlgorithm1(eventsThisFrame, mLatestGesture, mPanHistory, lastVSyncTime, nextVSyncTime);
+ // Dynamically change the prediction amount according to the pan velocity acceleration.
+ if( !frameInfo.justStarted )
+ {
+ if( frameInfo.eventsThisFrame <= 1 )
+ {
+ frameInfo.acceleration = frameGesture.screen.velocity.Length() - mLastUnmodifiedGesture.screen.velocity.Length();
+ }
+
+ // Ignore tiny velocity fluctuation to avoid unnecessary prediction amount change
+ if( fabsf( frameInfo.acceleration ) > ACCELERATION_THRESHOLD )
+ {
+ mCurrentPredictionAmount += mPredictionAmountAdjustment * ( frameInfo.acceleration > Math::MACHINE_EPSILON_0 ? 1.0f : -1.0f );
+ if( mCurrentPredictionAmount > mMaxPredictionAmount + mPredictionAmountAdjustment ) // Guard against unsigned int overflow
+ {
+ mCurrentPredictionAmount = 0;
+ }
+ }
+ }
+ else
+ {
+ mCurrentPredictionAmount = mPredictionAmount; // Reset the prediction amount for each new gesture
+ }
+
+ mCurrentPredictionAmount = std::max( mMinPredictionAmount, std::min( mCurrentPredictionAmount, mMaxPredictionAmount ) );
+
+ // Calculate the delta of positions before the prediction
+ Vector2 deltaPosition = frameGesture.screen.position - mLastUnmodifiedGesture.screen.position;
+
+ // Make latest gesture equal to current gesture before interpolation
+ PredictiveAlgorithm1( frameInfo.eventsThisFrame, frameGesture, mPanHistory, lastVSyncTime, nextVSyncTime );
+
+ // Calculate the delta of positions after the prediction.
+ Vector2 deltaPredictedPosition = frameGesture.screen.position - mLastGesture.screen.position;
+
+ // If the change in the prediction has a different sign than the change in the actual position,
+ // there is overshot (i.e. the current prediction is too large). Return the previous prediction
+ // to give the user's finger a chance to catch up with where we have panned to.
+ bool overshotXAxis = false;
+ bool overshotYAxis = false;
+ if( (deltaPosition.x > Math::MACHINE_EPSILON_0 && deltaPredictedPosition.x < Math::MACHINE_EPSILON_0 )
+ || (deltaPosition.x < Math::MACHINE_EPSILON_0 && deltaPredictedPosition.x > Math::MACHINE_EPSILON_0 ) )
+ {
+ overshotXAxis = true;
+ frameGesture.screen.position.x = mLastGesture.screen.position.x;
+ }
+
+ if( (deltaPosition.y > Math::MACHINE_EPSILON_0 && deltaPredictedPosition.y < Math::MACHINE_EPSILON_0 )
+ || (deltaPosition.y < Math::MACHINE_EPSILON_0 && deltaPredictedPosition.y > Math::MACHINE_EPSILON_0 ) )
+ {
+ overshotYAxis = true;
+ frameGesture.screen.position.y = mLastGesture.screen.position.y;
+ }
+
+ // If there is overshot in one axis, reduce the possible overshot in the other axis,
+ // and reduce the prediction amount so that it doesn't overshoot as easily next time.
+ if(overshotXAxis || overshotYAxis)
+ {
+ mCurrentPredictionAmount -= mPredictionAmountAdjustment;
+ if( mCurrentPredictionAmount > mMaxPredictionAmount + mPredictionAmountAdjustment ) // Guard against unsigned int overflow
+ {
+ mCurrentPredictionAmount = 0;
+ }
+ mCurrentPredictionAmount = std::max( mMinPredictionAmount, std::min( mCurrentPredictionAmount, mMaxPredictionAmount ) );
+
+ if( overshotXAxis && !overshotYAxis )
+ {
+ frameGesture.screen.position.y = ( mLastGesture.screen.position.y + frameGesture.screen.position.y ) * 0.5f;
+ }
+
+ if( overshotYAxis && !overshotXAxis )
+ {
+ frameGesture.screen.position.x = ( mLastGesture.screen.position.x + frameGesture.screen.position.x ) * 0.5f;
+ }
+ }
+
updateProperties = true;
break;
}
}
+ // Perform smoothing.
switch( mSmoothingMode )
{
case SMOOTHING_NONE:
{
- // no smoothing
+ // No smoothing
break;
}
case SMOOTHING_LAST_VALUE:
{
- SmoothingAlgorithm1(justStarted, mLatestGesture, lastVSyncTime);
+ SmoothingAlgorithm1( frameInfo.justStarted, frameGesture, lastVSyncTime );
break;
}
}
{
// only update properties if event received
// set latest gesture to raw pan info with unchanged time
- mPanning.Set( mInGesture & !justFinished );
- mScreenPosition.Set( mLatestGesture.screen.position );
- mScreenDisplacement.Set( mLatestGesture.screen.displacement );
- mScreenVelocity.Set( mLatestGesture.screen.velocity );
- mLocalPosition.Set( mLatestGesture.local.position );
- mLocalDisplacement.Set( mLatestGesture.local.displacement );
- mLocalVelocity.Set( mLatestGesture.local.velocity );
+ mPanning.Set( mInGesture & !frameInfo.justFinished );
+ mScreenPosition.Set( frameGesture.screen.position );
+ mScreenDisplacement.Set( frameGesture.screen.displacement );
+ mScreenVelocity.Set( frameGesture.screen.velocity );
+ mLocalPosition.Set( frameGesture.local.position );
+ mLocalDisplacement.Set( frameGesture.local.displacement );
+ mLocalVelocity.Set( frameGesture.local.velocity );
}
if( mProfiling )
{
- mProfiling->mAveragedData.push_back( PanGestureProfiling::Position( mLatestGesture.time, mLatestGesture.screen.position ) );
+ mProfiling->mAveragedData.push_back( PanGestureProfiling::Position( frameGesture.time, frameGesture.screen.position,
+ frameGesture.screen.displacement, frameGesture.screen.velocity, frameGesture.state ) );
}
}
- mInGesture &= ~justFinished;
+ mLastGesture = frameGesture;
- if( mProfiling && justFinished )
+ mInGesture &= ~frameInfo.justFinished;
+ if( mProfiling && frameInfo.justFinished )
{
mProfiling->PrintData();
mProfiling->ClearData();
mPredictionAmount = amount;
}
+void PanGesture::SetMaximumPredictionAmount(unsigned int amount)
+{
+ mMaxPredictionAmount = amount;
+}
+
+void PanGesture::SetMinimumPredictionAmount(unsigned int amount)
+{
+ mMinPredictionAmount = amount;
+}
+
+void PanGesture::SetPredictionAmountAdjustment(unsigned int amount)
+{
+ mPredictionAmountAdjustment = amount;
+}
+
void PanGesture::SetSmoothingMode(SmoothingMode mode)
{
mSmoothingMode = mode;
}
PanGesture::PanGesture()
-: mGestures(),
+: mPanning(),
+ mGestures(),
mWritePosition( 0 ),
mReadPosition( 0 ),
+ mNotAtTarget( false ),
mInGesture( false ),
- mPredictionMode(DEFAULT_PREDICTION_MODE),
- mPredictionAmount(DEFAULT_PREDICTION_INTERPOLATION),
- mSmoothingMode(DEFAULT_SMOOTHING_MODE),
- mSmoothingAmount(DEFAULT_SMOOTHING_AMOUNT),
+ mPredictionMode( DEFAULT_PREDICTION_MODE ),
+ mPredictionAmount( DEFAULT_PREDICTION_INTERPOLATION ),
+ mCurrentPredictionAmount( DEFAULT_PREDICTION_INTERPOLATION ),
+ mMaxPredictionAmount( DEFAULT_MAX_PREDICTION_INTERPOLATION ),
+ mMinPredictionAmount( DEFAULT_MIN_PREDICTION_INTERPOLATION ),
+ mPredictionAmountAdjustment( DEFAULT_PREDICTION_INTERPOLATION_ADJUSTMENT ),
+ mSmoothingMode( DEFAULT_SMOOTHING_MODE ),
+ mSmoothingAmount( DEFAULT_SMOOTHING_AMOUNT ),
mProfiling( NULL )
{
}
+
} // namespace SceneGraph
} // namespace Internal