1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/*
* Copyright 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "interpolator.h"
#include <math.h>
#include "interpolator.h"
namespace ndk_helper
{
//-------------------------------------------------
//Ctor
//-------------------------------------------------
Interpolator::Interpolator()
{
list_params_.clear();
}
//-------------------------------------------------
//Dtor
//-------------------------------------------------
Interpolator::~Interpolator()
{
list_params_.clear();
}
void Interpolator::Clear()
{
list_params_.clear();
}
Interpolator& Interpolator::Set( const float start,
const float dest,
const INTERPOLATOR_TYPE type,
const double duration )
{
//init the parameters for the interpolation process
start_time_ = PerfMonitor::GetCurrentTime();
dest_time_ = start_time_ + duration;
type_ = type;
start_value_ = start;
dest_value_ = dest;
return *this;
}
Interpolator& Interpolator::Add( const float dest,
const INTERPOLATOR_TYPE type,
const double duration )
{
InterpolatorParams param;
param.dest_value_ = dest;
param.type_ = type;
param.duration_ = duration;
list_params_.push_back( param );
return *this;
}
bool Interpolator::Update( const double current_time, float& p )
{
bool bContinue;
if( current_time >= dest_time_ )
{
p = dest_value_;
if( list_params_.size() )
{
InterpolatorParams& item = list_params_.front();
Set( dest_value_, item.dest_value_, item.type_, item.duration_ );
list_params_.pop_front();
bContinue = true;
}
else
{
bContinue = false;
}
}
else
{
float t = (float) (current_time - start_time_);
float d = (float) (dest_time_ - start_time_);
float b = start_value_;
float c = dest_value_ - start_value_;
p = GetFormula( type_, t, b, d, c );
bContinue = true;
}
return bContinue;
}
float Interpolator::GetFormula( const INTERPOLATOR_TYPE type,
const float t,
const float b,
const float d,
const float c )
{
float t1;
switch( type )
{
case INTERPOLATOR_TYPE_LINEAR:
// simple linear interpolation - no easing
return (c * t / d + b);
case INTERPOLATOR_TYPE_EASEINQUAD:
// quadratic (t^2) easing in - accelerating from zero velocity
t1 = t / d;
return (c * t1 * t1 + b);
case INTERPOLATOR_TYPE_EASEOUTQUAD:
// quadratic (t^2) easing out - decelerating to zero velocity
t1 = t / d;
return (-c * t1 * (t1 - 2) + b);
case INTERPOLATOR_TYPE_EASEINOUTQUAD:
// quadratic easing in/out - acceleration until halfway, then deceleration
t1 = t / d / 2;
if( t1 < 1 )
return (c / 2 * t1 * t1 + b);
else
{
t1 = t1 - 1;
return (-c / 2 * (t1 * (t1 - 2) - 1) + b);
}
case INTERPOLATOR_TYPE_EASEINCUBIC:
// cubic easing in - accelerating from zero velocity
t1 = t / d;
return (c * t1 * t1 * t1 + b);
case INTERPOLATOR_TYPE_EASEOUTCUBIC:
// cubic easing in - accelerating from zero velocity
t1 = t / d - 1;
return (c * (t1 * t1 * t1 + 1) + b);
case INTERPOLATOR_TYPE_EASEINOUTCUBIC:
// cubic easing in - accelerating from zero velocity
t1 = t / d / 2;
if( t1 < 1 )
return (c / 2 * t1 * t1 * t1 + b);
else
{
t1 -= 2;
return (c / 2 * (t1 * t1 * t1 + 2) + b);
}
case INTERPOLATOR_TYPE_EASEINQUART:
// quartic easing in - accelerating from zero velocity
t1 = t / d;
return (c * t1 * t1 * t1 * t1 + b);
case INTERPOLATOR_TYPE_EASEINEXPO:
// exponential (2^t) easing in - accelerating from zero velocity
if( t == 0 )
return b;
else
return (c * powf( 2, (10 * (t / d - 1)) ) + b);
case INTERPOLATOR_TYPE_EASEOUTEXPO:
// exponential (2^t) easing out - decelerating to zero velocity
if( t == d )
return (b + c);
else
return (c * (-powf( 2, -10 * t / d ) + 1) + b);
default:
return 0;
}
}
} //namespace ndkHelper
|