libcruft-util/noise/lerp.cpp

111 lines
3.3 KiB
C++
Raw Normal View History

/*
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
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
*
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-04-23 13:06:41 +10:00
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
*/
#include "lerp.hpp"
#include "debug.hpp"
#include "maths.hpp"
#include <cmath>
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
2015-05-18 14:55:20 +10:00
template <typename T>
T
util::lerp::sigmoid (T val)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
return -1 + 2 / (1 + std::exp (-2 * val));
}
2015-05-18 14:55:20 +10:00
template float util::lerp::sigmoid (float);
template double util::lerp::sigmoid (double);
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
2015-05-18 14:55:20 +10:00
template <typename T>
T
util::lerp::trunc (T a, T, T)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
return a;
}
template float util::lerp::trunc (float, float, float);
template double util::lerp::trunc (double, double, double);
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
2015-05-18 14:55:20 +10:00
template <typename T>
T
util::lerp::linear (T a, T b, T weight)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
CHECK (weight >= 0 && weight <= 1);
return a * (1 - weight) + b * weight;
}
2015-05-18 14:55:20 +10:00
template float util::lerp::linear (float, float, float);
template double util::lerp::linear (double, double, double);
2015-05-18 14:55:20 +10:00
//-----------------------------------------------------------------------------
template <typename T>
T
util::lerp::cosine (T a, T b, T weight)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
CHECK (weight >= 0 && weight <= 1);
T t = (1 - std::cos (weight * PI<T>)) * T(0.5);
return a * (1 - t) + b * t;
}
2015-05-18 14:55:20 +10:00
template float util::lerp::cosine (float, float, float);
template double util::lerp::cosine (double, double, double);
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
2015-05-18 14:55:20 +10:00
template <typename T>
T
util::lerp::cubic (T a, T b, T weight)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
CHECK (weight >= 0 && weight <= 1);
T t = weight * weight * (3 - 2 * weight);
return a * (1 - t) + b * t;
}
2015-05-18 14:55:20 +10:00
template float util::lerp::cubic (float, float, float);
template double util::lerp::cubic (double, double, double);
2015-05-18 14:34:48 +10:00
//-----------------------------------------------------------------------------
2015-05-18 14:55:20 +10:00
template <typename T>
T
util::lerp::quintic (T a, T b, T weight)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
CHECK (weight >= 0 && weight <= 1);
T t = weight * weight * weight * (weight * (weight * 6 - 15) + 10);
return a * (1 - t) + b * t;
}
2015-05-18 14:55:20 +10:00
template float util::lerp::quintic (float, float, float);
template double util::lerp::quintic (double, double, double);