#pragma once #ifndef TCG_SIZE_H #define TCG_SIZE_H namespace tcg { //********************************************************************************** // Bidimensional size class //********************************************************************************** template struct SizeT { typedef T value_type; public: T w, h; public: 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 operator+(const SizeT &a, const SizeT &b) { return SizeT(a.w + b.w, a.h + b.h); } friend SizeT operator-(const SizeT &a, const SizeT &b) { return SizeT(a.w - b.w, a.h - b.h); } template SizeT &operator*=(K k) { w *= k, h *= k; return *this; } template SizeT &operator/=(K k) { w /= k, h /= k; return *this; } template friend SizeT operator*(K k, const SizeT &a) { return SizeT(k * a.w, k * a.h); } template friend SizeT operator*(const SizeT &a, K k) { return SizeT(a.w * k, a.h * k); } template friend SizeT operator/(const SizeT &a, K k) { return SizeT(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 operator&(const SizeT &a, const SizeT &b) { SizeT tmp(a); return tmp &= b; } friend SizeT operator|(const SizeT &a, const SizeT &b) { SizeT tmp(a); return tmp |= b; } }; //------------------------------------------------------------------------ typedef SizeT SizeI; typedef SizeI Size; typedef SizeT SizeD; //********************************************************************************** // Tridimensional size class //********************************************************************************** template struct Size3T { typedef T value_type; public: T w, h, d; public: 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 operator+(const Size3T &a, const Size3T &b) { return SizeT(a.w + b.w, a.h + b.h, a.d + b.d); } friend Size3T operator-(const Size3T &a, const Size3T &b) { return SizeT(a.w - b.w, a.h - b.h, a.d + b.d); } template Size3T &operator*=(K k) { w *= k, h *= k, d *= k; return *this; } template Size3T &operator/=(K k) { w /= k, h /= k, d /= k; return *this; } template friend Size3T operator*(K k, const Size3T &a) { return Size3T(k * a.w, k * a.h, k * a.d); } template friend Size3T operator*(const Size3T &a, K k) { return Size3T(a.w * k, a.h * k, a.d * k); } template friend Size3T operator/(const Size3T &a, K k) { return Size3T(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 operator&(const Size3T &a, const Size3T &b) { Size3T tmp(a); return tmp &= b; } friend Size3T operator|(const Size3T &a, const Size3T &b) { Size3T tmp(a); return tmp |= b; } }; //------------------------------------------------------------------------ typedef Size3T Size3I; typedef Size3I Size3; typedef Size3T Size3D; //********************************************************************************** // N-dimensional size class //********************************************************************************** template struct SizeN { static const int size = N; typedef T value_type; public: T span[N]; public: SizeN() {} SizeN(const T &t) { std::fill(span, span + N, t); } template SizeN(It begin, It end) { std::copy(begin, end, span); } // To be completed... }; } // namespace tcg #endif // TCG_SIZE_H