2017-12-13 23:28:35 +00:00
|
|
|
/*
|
|
|
|
* Modern effects for a modern Streamer
|
|
|
|
* Copyright (C) 2017 Michael Fabian Dirks
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2019-01-14 10:23:21 +00:00
|
|
|
#include <cinttypes>
|
2018-12-23 18:57:07 +00:00
|
|
|
#include <cmath>
|
util-math: Add SizeFromString method
Converts a String to an std::pair of int64_t (long long), which contain the size or 0 if none could be parsed. Any delimiter except digits(0-9), a minus sign(-) or a plus sign(+) between width and height are allowed. If a plus or minus sign is used as a delimiter, it must immediately be followed by the second size number. This allows for formats such as: 100x100, 100:100, 100p100, 100@100, 100+100 and so on, but not formats such as 100+:100, 100ThisIsSomeReall+yLongText100, etc.
The parameter 'allowSquare' also determines what to do when the height parameter is not found. A value of true will have the function return <width,width> instead of <width,0>.
2018-03-05 15:36:20 +00:00
|
|
|
#include <string>
|
2018-09-28 12:17:43 +00:00
|
|
|
#include <utility>
|
2017-12-13 23:28:35 +00:00
|
|
|
|
2018-01-08 14:23:25 +00:00
|
|
|
// OBS
|
2019-01-14 22:21:29 +00:00
|
|
|
#ifdef _MSC_VER
|
2019-01-14 10:23:21 +00:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4201)
|
2019-01-14 22:21:29 +00:00
|
|
|
#endif
|
2018-04-23 15:53:27 +00:00
|
|
|
#include <graphics/vec2.h>
|
|
|
|
#include <graphics/vec3.h>
|
|
|
|
#include <graphics/vec4.h>
|
2019-01-14 22:21:29 +00:00
|
|
|
#ifdef _MSC_VER
|
2019-01-14 10:23:21 +00:00
|
|
|
#pragma warning(pop)
|
2019-01-14 22:21:29 +00:00
|
|
|
#endif
|
2018-01-08 14:23:25 +00:00
|
|
|
|
|
|
|
// Constants
|
2018-09-28 12:17:43 +00:00
|
|
|
#define PI 3.1415926535897932384626433832795
|
|
|
|
#define PI2 6.283185307179586476925286766559
|
|
|
|
#define PI2_SQROOT 2.506628274631000502415765284811
|
2017-12-13 23:28:35 +00:00
|
|
|
|
2018-09-28 12:17:43 +00:00
|
|
|
inline double_t Gaussian1D(double_t x, double_t o)
|
|
|
|
{
|
2017-12-13 23:28:35 +00:00
|
|
|
double_t c = (x / o);
|
|
|
|
double_t b = exp(-0.5 * c * c);
|
|
|
|
double_t a = (1.0 / (o * PI2_SQROOT));
|
|
|
|
return a * b;
|
|
|
|
}
|
2017-12-14 00:36:07 +00:00
|
|
|
|
2018-09-28 12:17:43 +00:00
|
|
|
inline double_t Bilateral1D(double_t x, double_t o)
|
|
|
|
{
|
2018-04-28 10:45:23 +00:00
|
|
|
double_t c = (x / 0);
|
|
|
|
double_t d = c * c;
|
|
|
|
double_t b = exp(-0.5 * d) / o;
|
|
|
|
return 0.39894 * b; // Seems to be (1.0 / (1 * PI2_SQROOT)) * b, otherwise no difference from Gaussian Blur
|
|
|
|
}
|
|
|
|
|
2018-09-28 12:17:43 +00:00
|
|
|
inline size_t GetNearestPowerOfTwoAbove(size_t v)
|
|
|
|
{
|
2017-12-14 02:00:31 +00:00
|
|
|
return 1ull << size_t(ceil(log10(double(v)) / log10(2.0)));
|
2017-12-14 00:36:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 12:17:43 +00:00
|
|
|
inline size_t GetNearestPowerOfTwoBelow(size_t v)
|
|
|
|
{
|
2017-12-14 02:00:31 +00:00
|
|
|
return 1ull << size_t(floor(log10(double(v)) / log10(2.0)));
|
2017-12-14 00:36:07 +00:00
|
|
|
}
|
2018-01-08 14:23:25 +00:00
|
|
|
|
|
|
|
namespace util {
|
2019-03-01 10:14:06 +00:00
|
|
|
struct vec2a : public vec2 {
|
2019-01-24 04:14:17 +00:00
|
|
|
// 16-byte Aligned version of vec2
|
|
|
|
static void* operator new(size_t count);
|
|
|
|
static void* operator new[](size_t count);
|
|
|
|
static void operator delete(void* p);
|
|
|
|
static void operator delete[](void* p);
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2019-01-14 10:23:21 +00:00
|
|
|
__declspec(align(16))
|
2019-01-24 04:14:17 +00:00
|
|
|
#endif
|
2019-03-01 10:14:06 +00:00
|
|
|
struct vec3a : public vec3 {
|
2019-01-24 04:14:17 +00:00
|
|
|
// 16-byte Aligned version of vec3
|
2019-01-14 10:23:21 +00:00
|
|
|
static void* operator new(size_t count);
|
|
|
|
static void* operator new[](size_t count);
|
2019-01-24 04:14:17 +00:00
|
|
|
static void operator delete(void* p);
|
|
|
|
static void operator delete[](void* p);
|
2018-01-08 14:23:25 +00:00
|
|
|
};
|
|
|
|
|
2019-01-24 04:14:17 +00:00
|
|
|
#ifdef _MSC_VER
|
2019-01-14 10:23:21 +00:00
|
|
|
__declspec(align(16))
|
2019-01-24 04:14:17 +00:00
|
|
|
#endif
|
2019-03-01 10:14:06 +00:00
|
|
|
struct vec4a : public vec4 {
|
2019-01-24 04:14:17 +00:00
|
|
|
// 16-byte Aligned version of vec4
|
2019-01-14 10:23:21 +00:00
|
|
|
static void* operator new(size_t count);
|
|
|
|
static void* operator new[](size_t count);
|
2019-01-24 04:14:17 +00:00
|
|
|
static void operator delete(void* p);
|
|
|
|
static void operator delete[](void* p);
|
2018-01-08 14:23:25 +00:00
|
|
|
};
|
util-math: Add SizeFromString method
Converts a String to an std::pair of int64_t (long long), which contain the size or 0 if none could be parsed. Any delimiter except digits(0-9), a minus sign(-) or a plus sign(+) between width and height are allowed. If a plus or minus sign is used as a delimiter, it must immediately be followed by the second size number. This allows for formats such as: 100x100, 100:100, 100p100, 100@100, 100+100 and so on, but not formats such as 100+:100, 100ThisIsSomeReall+yLongText100, etc.
The parameter 'allowSquare' also determines what to do when the height parameter is not found. A value of true will have the function return <width,width> instead of <width,0>.
2018-03-05 15:36:20 +00:00
|
|
|
|
|
|
|
std::pair<int64_t, int64_t> SizeFromString(std::string text, bool allowSquare = true);
|
2018-09-28 12:17:43 +00:00
|
|
|
|
|
|
|
namespace math {
|
|
|
|
// Proven by tests to be the fastest implementation on Intel and AMD CPUs.
|
|
|
|
// Ranking: log10, loop < bitscan < pow
|
|
|
|
// loop and log10 trade blows, usually almost identical.
|
|
|
|
// loop is used for integers, log10 for anything else.
|
|
|
|
template<typename T>
|
|
|
|
inline bool is_power_of_two(T v)
|
|
|
|
{
|
2018-09-28 12:23:26 +00:00
|
|
|
return T(1ull << uint64_t(floor(log10(T(v)) / log10(2.0)))) == v;
|
2018-09-28 12:17:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline bool is_power_of_two_loop(T v)
|
|
|
|
{
|
|
|
|
bool have_bit = false;
|
2018-09-28 19:21:40 +00:00
|
|
|
for (size_t index = 0; index < (sizeof(T) * 8); index++) {
|
2018-09-28 12:17:43 +00:00
|
|
|
bool cur = (v & (1ull << index)) != 0;
|
|
|
|
if (cur) {
|
|
|
|
if (have_bit)
|
|
|
|
return false;
|
|
|
|
have_bit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma push_macro("is_power_of_two_as_loop")
|
2018-09-28 12:23:26 +00:00
|
|
|
#define is_power_of_two_as_loop(x) \
|
2018-09-28 12:17:43 +00:00
|
|
|
template<> \
|
|
|
|
inline bool is_power_of_two(x v) \
|
|
|
|
{ \
|
|
|
|
return is_power_of_two_loop(v); \
|
2019-01-14 10:23:21 +00:00
|
|
|
}
|
2019-02-01 07:33:20 +00:00
|
|
|
is_power_of_two_as_loop(int8_t);
|
|
|
|
is_power_of_two_as_loop(uint8_t);
|
|
|
|
is_power_of_two_as_loop(int16_t);
|
|
|
|
is_power_of_two_as_loop(uint16_t);
|
|
|
|
is_power_of_two_as_loop(int32_t);
|
|
|
|
is_power_of_two_as_loop(uint32_t);
|
|
|
|
is_power_of_two_as_loop(int64_t);
|
|
|
|
is_power_of_two_as_loop(uint64_t);
|
2018-09-30 18:50:09 +00:00
|
|
|
#undef is_power_of_two_as_loop
|
2018-09-28 12:17:43 +00:00
|
|
|
#pragma pop_macro("is_power_of_two_as_loop")
|
2018-09-28 12:23:26 +00:00
|
|
|
|
2019-02-01 07:33:20 +00:00
|
|
|
template<typename T>
|
|
|
|
inline uint64_t get_power_of_two_exponent_floor(T v)
|
2018-09-28 12:23:26 +00:00
|
|
|
{
|
|
|
|
return uint64_t(floor(log10(T(v)) / log10(2.0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2018-09-29 14:57:41 +00:00
|
|
|
inline uint64_t get_power_of_two_exponent_ceil(T v)
|
2018-09-28 12:23:26 +00:00
|
|
|
{
|
|
|
|
return uint64_t(ceil(log10(T(v)) / log10(2.0)));
|
|
|
|
}
|
2019-02-01 07:33:20 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T gaussian(T x, T o /*, T u = 0*/)
|
|
|
|
{
|
|
|
|
// u/µ can be simulated by subtracting that value from x.
|
|
|
|
static const double_t pi = 3.1415926535897932384626433832795;
|
|
|
|
static const double_t two_pi = pi * 2.;
|
|
|
|
static const double_t two_pi_sqroot = 2.506628274631000502415765284811; //sqrt(two_pi);
|
|
|
|
|
|
|
|
if (o == 0) {
|
|
|
|
return T(std::numeric_limits<double_t>::infinity());
|
|
|
|
}
|
|
|
|
|
|
|
|
// g(x) = (1 / o√(2Π)) * e(-(1/2) * ((x-u)/o)²)
|
|
|
|
double_t left_e = 1. / (o * two_pi_sqroot);
|
|
|
|
double_t mid_right_e = ((x /* - u*/) / o);
|
|
|
|
double_t right_e = -0.5 * mid_right_e * mid_right_e;
|
|
|
|
double_t final = left_e * exp(right_e);
|
|
|
|
|
|
|
|
return T(final);
|
|
|
|
}
|
2018-09-28 12:17:43 +00:00
|
|
|
} // namespace math
|
|
|
|
} // namespace util
|