2012-01-04 17:08:03 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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
|
2012-01-04 17:08:03 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-01-04 17:08:03 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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.
|
2012-01-04 17:08:03 +11:00
|
|
|
*
|
2012-04-23 13:06:41 +10:00
|
|
|
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
2012-01-04 17:08:03 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lerp.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "maths.hpp"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-01-04 17:08:03 +11:00
|
|
|
double
|
2015-05-18 14:42:28 +10:00
|
|
|
util::lerp::sigmoid (double val) {
|
2012-01-04 17:08:03 +11:00
|
|
|
return -1.0 + 2.0 / (1.0 + exp (-2.0 * val));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-18 14:42:28 +10:00
|
|
|
double util::lerp::trunc (double a, double, double)
|
2012-05-17 14:17:42 +10:00
|
|
|
{ return a; }
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-01-04 17:08:03 +11:00
|
|
|
double
|
2015-05-18 14:42:28 +10:00
|
|
|
util::lerp::linear (double a, double b, double weight) {
|
2012-05-17 14:18:03 +10:00
|
|
|
CHECK (weight >= 0.0 && weight <= 1.0);
|
|
|
|
return a * (1.0 - weight) + b * weight;
|
2012-01-04 17:08:03 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-01-04 17:08:03 +11:00
|
|
|
double
|
2015-05-18 14:42:28 +10:00
|
|
|
util::lerp::cosine (double a, double b, double weight) {
|
2012-05-17 14:18:03 +10:00
|
|
|
CHECK (weight >= 0.0 && weight <= 1.0);
|
2015-04-29 17:45:39 +10:00
|
|
|
double t = (1.0 - cos (weight * PI<double>)) * 0.5;
|
2012-01-04 17:08:03 +11:00
|
|
|
|
2012-05-17 14:18:03 +10:00
|
|
|
return a * (1.0 - t) + b * t;
|
2012-01-04 17:08:03 +11:00
|
|
|
}
|
|
|
|
|
2012-05-17 14:17:42 +10:00
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-17 14:17:42 +10:00
|
|
|
double
|
2015-05-18 14:42:28 +10:00
|
|
|
util::lerp::cubic (double a, double b, double weight) {
|
2012-05-17 14:17:42 +10:00
|
|
|
CHECK (weight >= 0.0 && weight <= 1.0);
|
|
|
|
double t = weight * weight * (3.0 - 2.0 * weight);
|
|
|
|
return a * (1.0 - t) + b * t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 14:34:48 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-17 14:17:42 +10:00
|
|
|
double
|
2015-05-18 14:42:28 +10:00
|
|
|
util::lerp::quintic (double a, double b, double weight) {
|
2012-05-22 14:12:30 +10:00
|
|
|
CHECK (weight >= 0.0 && weight <= 1.0);
|
2012-05-17 14:17:42 +10:00
|
|
|
double t = weight * weight * weight * (weight * (weight * 6.0 - 15.0) + 10.0);
|
|
|
|
return a * (1.0 - t) + b * t;
|
|
|
|
}
|