|
|
531f67 |
#ifndef COMMON_H
|
|
|
531f67 |
#define COMMON_H
|
|
|
531f67 |
|
|
|
fdabb2 |
|
|
|
531f67 |
#include <cmath></cmath>
|
|
|
531f67 |
#include <cstdlib></cstdlib>
|
|
|
0662a2 |
#include <algorithm></algorithm>
|
|
|
531f67 |
|
|
|
fdabb2 |
#include <glibmm refptr.h=""></glibmm>
|
|
|
fdabb2 |
|
|
|
531f67 |
|
|
|
531f67 |
typedef float ColorReal;
|
|
|
531f67 |
typedef double Real;
|
|
|
531f67 |
typedef int Int;
|
|
|
531f67 |
typedef unsigned int UInt;
|
|
|
531f67 |
typedef long long int LongInt;
|
|
|
531f67 |
typedef unsigned long long int ULongInt;
|
|
|
531f67 |
|
|
|
fdabb2 |
using Glib::RefPtr;
|
|
|
fdabb2 |
|
|
|
531f67 |
|
|
|
fdabb2 |
const Real real_precision = 1e-10;
|
|
|
5f2fc2 |
const Real real_precision_sqr = real_precision * real_precision;
|
|
|
531f67 |
|
|
|
531f67 |
|
|
|
531f67 |
class Shared {
|
|
|
531f67 |
private:
|
|
|
531f67 |
int ref_count;
|
|
|
531f67 |
public:
|
|
|
fdabb2 |
Shared(): ref_count(1) { }
|
|
|
fdabb2 |
Shared(const Shared&): ref_count(1) { }
|
|
|
531f67 |
virtual ~Shared() { };
|
|
|
531f67 |
|
|
|
531f67 |
inline Shared& operator=(const Shared&) { return *this; }
|
|
|
fdabb2 |
inline void reference() {
|
|
|
fdabb2 |
++ref_count;
|
|
|
fdabb2 |
}
|
|
|
fdabb2 |
inline void unreference() {
|
|
|
fdabb2 |
if (--ref_count <= 0)
|
|
|
fdabb2 |
delete this;
|
|
|
fdabb2 |
}
|
|
|
531f67 |
};
|
|
|
531f67 |
|
|
|
531f67 |
|
|
|
531f67 |
class Color {
|
|
|
531f67 |
public:
|
|
|
531f67 |
typedef ColorReal Channel;
|
|
|
531f67 |
union {
|
|
|
531f67 |
struct { Channel channels[4]; };
|
|
|
531f67 |
struct { Channel r, g, b, a; };
|
|
|
531f67 |
};
|
|
|
531f67 |
|
|
|
531f67 |
inline explicit Color(Channel r = 0.0, Channel g = 0.0, Channel b = 0.0, Channel a = 0.0):
|
|
|
531f67 |
r(r), g(g), b(b), a(a) { }
|
|
|
4dc49c |
|
|
|
4dc49c |
inline Color operator+ (const Color &other) const
|
|
|
4dc49c |
{ return Color(r + other.r, g + other.g, b + other.b, a + other.a); }
|
|
|
4dc49c |
inline Color operator- (const Color &other) const
|
|
|
4dc49c |
{ return Color(r - other.r, g - other.g, b - other.b, a - other.a); }
|
|
|
4dc49c |
inline Color operator* (Channel k) const
|
|
|
4dc49c |
{ return Color(r*k, g*k, b*k, a*k); }
|
|
|
4dc49c |
|
|
|
4dc49c |
inline Color& operator+= (const Color &other)
|
|
|
4dc49c |
{ r += other.r, g += other.g, b += other.b, a += other.a; return *this; }
|
|
|
4dc49c |
inline Color& operator-= (const Color &other)
|
|
|
4dc49c |
{ r -= other.r, g -= other.g, b -= other.b, a -= other.a; return *this; }
|
|
|
4dc49c |
inline Color& operator*= (Channel k)
|
|
|
4dc49c |
{ r *= k, g *= k, b *= k, a *= k; return *this; }
|
|
|
4dc49c |
|
|
|
4dc49c |
inline Color get_mult_alpha() const
|
|
|
4dc49c |
{ return Color(r*a, g*a, b*a, a); }
|
|
|
4dc49c |
inline Color get_demult_alpha() const {
|
|
|
4dc49c |
if (fabs(a) < real_precision) return *this;
|
|
|
4dc49c |
Channel k = 1/a;
|
|
|
4dc49c |
return Color(r*k, g*k, b*k, a);
|
|
|
4dc49c |
}
|
|
|
4dc49c |
|
|
|
4dc49c |
inline void mult_alpha()
|
|
|
4dc49c |
{ r*=a, g*=a, b*=a; }
|
|
|
4dc49c |
inline void demult_alpha() {
|
|
|
4dc49c |
if (fabs(a) < real_precision) return;
|
|
|
4dc49c |
Real k = 1/a;
|
|
|
4dc49c |
r*=k, g*=k, b*=k;
|
|
|
4dc49c |
}
|
|
|
531f67 |
};
|
|
|
531f67 |
|
|
|
a1942e |
|
|
|
a1942e |
inline bool real_less(const Real &a, const Real &b)
|
|
|
a1942e |
{ return a < b - real_precision; }
|
|
|
a1942e |
inline bool real_greater(const Real &a, const Real &b)
|
|
|
a1942e |
{ return real_less(b, a); }
|
|
|
a1942e |
inline bool real_less_or_equal(const Real &a, const Real &b)
|
|
|
a1942e |
{ return !real_less(b, a); }
|
|
|
a1942e |
inline bool real_greater_or_equal(const Real &a, const Real &b)
|
|
|
a1942e |
{ return !real_less(a, b); }
|
|
|
a1942e |
inline bool real_equal(const Real &a, const Real &b)
|
|
|
a1942e |
{ return !real_less(a, b) && !real_less(b, a); }
|
|
|
a1942e |
inline bool real_not_equal(const Real &a, const Real &b)
|
|
|
a1942e |
{ return real_less(a, b) || real_less(b, a); }
|
|
|
a1942e |
|
|
|
a1942e |
|
|
|
531f67 |
inline int clz(const Int &x)
|
|
|
531f67 |
{ return x ? __builtin_clz(x) : 32; }
|
|
|
531f67 |
inline int clz(const UInt &x)
|
|
|
531f67 |
{ return x ? __builtin_clz(x) : 32; }
|
|
|
531f67 |
inline int clz(const LongInt &x)
|
|
|
531f67 |
{ return x ? __builtin_clzll(x) : 64; }
|
|
|
531f67 |
inline int clz(const ULongInt &x)
|
|
|
531f67 |
{ return x ? __builtin_clzll(x) : 64; }
|
|
|
531f67 |
|
|
|
531f67 |
|
|
|
0662a2 |
inline Real clamp(Real x, Real min, Real max)
|
|
|
0662a2 |
{ return std::max(min, std::min(max, x)); }
|
|
|
0662a2 |
inline int iclamp(int x, int min, int max)
|
|
|
0662a2 |
{ return std::max(min, std::min(max, x)); }
|
|
|
0662a2 |
|
|
|
531f67 |
inline Real flip_clamp(Real x) {
|
|
|
531f67 |
x -= floor(0.5*x)*2.0;
|
|
|
531f67 |
return 1.0 - fabs(1.0 - x);
|
|
|
538bb7 |
};
|
|
|
531f67 |
|
|
|
531f67 |
#endif
|