tahoma2d/toonz/sources/include/tcg/size.h

219 lines
4.8 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TCG_SIZE_H
#define TCG_SIZE_H
2016-06-15 18:43:10 +12:00
namespace tcg {
2016-03-19 06:57:51 +13:00
//**********************************************************************************
// Bidimensional size class
//**********************************************************************************
template <typename T>
struct SizeT {
2016-06-15 18:43:10 +12:00
typedef T value_type;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
T w, h;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
SizeT() : w(), h() {}
SizeT(T w_, T h_) : w(w_), h(h_) {}
bool empty() const { return (w <= 0) || (h <= 0); }
SizeT &operator+=(const SizeT &other) {
w += other.w, h += other.h;
return *this;
}
SizeT &operator-=(const SizeT &other) {
w -= other.w, h -= other.h;
return *this;
}
friend SizeT<T> operator+(const SizeT<T> &a, const SizeT<T> &b) {
return SizeT<T>(a.w + b.w, a.h + b.h);
}
friend SizeT<T> operator-(const SizeT<T> &a, const SizeT<T> &b) {
return SizeT<T>(a.w - b.w, a.h - b.h);
}
template <typename K>
SizeT &operator*=(K k) {
w *= k, h *= k;
return *this;
}
template <typename K>
SizeT &operator/=(K k) {
w /= k, h /= k;
return *this;
}
template <typename K>
friend SizeT<T> operator*(K k, const SizeT<T> &a) {
return SizeT<T>(k * a.w, k * a.h);
}
template <typename K>
friend SizeT<T> operator*(const SizeT<T> &a, K k) {
return SizeT<T>(a.w * k, a.h * k);
}
template <typename K>
friend SizeT<T> operator/(const SizeT<T> &a, K k) {
return SizeT<T>(a.w / k, a.h / k);
}
SizeT &operator&=(const SizeT &other) {
if (other.w < w) w = other.w;
if (other.h < h) h = other.h;
return *this;
}
SizeT &operator|=(const SizeT &other) {
if (other.w > w) w = other.w;
if (other.h > h) h = other.h;
return *this;
}
friend SizeT<T> operator&(const SizeT<T> &a, const SizeT<T> &b) {
SizeT<T> tmp(a);
return tmp &= b;
}
friend SizeT<T> operator|(const SizeT<T> &a, const SizeT<T> &b) {
SizeT<T> tmp(a);
return tmp |= b;
}
2016-03-19 06:57:51 +13:00
};
//------------------------------------------------------------------------
typedef SizeT<int> SizeI;
typedef SizeI Size;
typedef SizeT<double> SizeD;
//**********************************************************************************
// Tridimensional size class
//**********************************************************************************
template <typename T>
struct Size3T {
2016-06-15 18:43:10 +12:00
typedef T value_type;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
T w, h, d;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
Size3T() : w(), h(), d() {}
Size3T(T w_, T h_, T d_) : w(w_), h(h_), d(d_) {}
bool empty() const { return (w <= 0) || (h <= 0) || (d <= 0); }
Size3T &operator+=(const Size3T &other) {
w += other.w, h += other.h, d += other.d;
return *this;
}
Size3T &operator-=(const Size3T &other) {
w -= other.w, h -= other.h, d -= other.d;
return *this;
}
friend Size3T<T> operator+(const Size3T<T> &a, const Size3T<T> &b) {
return SizeT<T>(a.w + b.w, a.h + b.h, a.d + b.d);
}
friend Size3T<T> operator-(const Size3T<T> &a, const Size3T<T> &b) {
return SizeT<T>(a.w - b.w, a.h - b.h, a.d + b.d);
}
template <typename K>
Size3T &operator*=(K k) {
w *= k, h *= k, d *= k;
return *this;
}
template <typename K>
Size3T &operator/=(K k) {
w /= k, h /= k, d /= k;
return *this;
}
template <typename K>
friend Size3T<T> operator*(K k, const Size3T<T> &a) {
return Size3T<T>(k * a.w, k * a.h, k * a.d);
}
template <typename K>
friend Size3T<T> operator*(const Size3T<T> &a, K k) {
return Size3T<T>(a.w * k, a.h * k, a.d * k);
}
template <typename K>
friend Size3T<T> operator/(const Size3T<T> &a, K k) {
return Size3T<T>(a.w / k, a.h / k, a.d / k);
}
Size3T &operator&=(const Size3T &other) {
if (other.w < w) w = other.w;
if (other.h < h) h = other.h;
if (other.d < d) d = other.d;
return *this;
}
Size3T &operator|=(const Size3T &other) {
if (other.w > w) w = other.w;
if (other.h > h) h = other.h;
if (other.d > d) d = other.d;
return *this;
}
friend Size3T<T> operator&(const Size3T<T> &a, const Size3T<T> &b) {
Size3T<T> tmp(a);
return tmp &= b;
}
friend Size3T<T> operator|(const Size3T<T> &a, const Size3T<T> &b) {
Size3T<T> tmp(a);
return tmp |= b;
}
2016-03-19 06:57:51 +13:00
};
//------------------------------------------------------------------------
typedef Size3T<int> Size3I;
typedef Size3I Size3;
typedef Size3T<double> Size3D;
//**********************************************************************************
// N-dimensional size class
//**********************************************************************************
template <int N, typename T>
struct SizeN {
2016-06-15 18:43:10 +12:00
static const int size = N;
typedef T value_type;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
T span[N];
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
SizeN() {}
SizeN(const T &t) { std::fill(span, span + N, t); }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
template <typename It>
SizeN(It begin, It end) {
std::copy(begin, end, span);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// To be completed...
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
} // namespace tcg
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
#endif // TCG_SIZE_H