Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_RECT_H
Toshihiro Shimizu 890ddd
#define TCG_RECT_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "point.h"
Toshihiro Shimizu 890ddd
#include "size.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// STD includes
Toshihiro Shimizu 890ddd
#include <limits></limits>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace tcg {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**********************************************************************************
Toshihiro Shimizu 890ddd
//    Bidimensional Rect  class
Toshihiro Shimizu 890ddd
//**********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename t=""></typename>
Toshihiro Shimizu 890ddd
struct RectT {
Shinya Kitaoka 120a6e
  T x0, y0, x1, y1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RectT() : x0((std::numeric_limits<t>::max)()), y0(x0), x1(-x0), y1(x1) {}</t>
Shinya Kitaoka 120a6e
  RectT(T x0_, T y0_, T x1_, T y1_) : x0(x0_), y0(y0_), x1(x1_), y1(y1_) {}
Shinya Kitaoka 120a6e
  RectT(const PointT<t> &p0, const PointT<t> &p1)</t></t>
Shinya Kitaoka 120a6e
      : x0(p0.x), y0(p0.y), x1(p1.x), y1(p1.y) {}
Shinya Kitaoka 120a6e
  RectT(const PointT<t> &p0, const SizeT<t> &size)</t></t>
Shinya Kitaoka 120a6e
      : x0(p0.x), y0(p0.y), x1(p0.x + size.w), y1(p0.y + size.h) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool empty() const { return (x1 <= x0) || (y1 <= y0); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  PointT<t> p0() const { return PointT<t>(x0, y0); }</t></t>
Shinya Kitaoka 120a6e
  PointT<t> p1() const { return PointT<t>(x1, y1); }</t></t>
Shinya Kitaoka 120a6e
  PointT<t> center() const { return PointT<t>((x0 + x1) / 2, (y0 + y1) / 2); }</t></t>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  T width() const { return x1 - x0; }
Shinya Kitaoka 120a6e
  T height() const { return y1 - y0; }
Shinya Kitaoka 120a6e
  SizeT<t> size() const { return SizeT<t>(width(), height()); }</t></t>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool operator==(const RectT &other) const {
Shinya Kitaoka 120a6e
    return x0 == other.x0 && y0 == other.y0 && x1 == other.x1 && y1 == other.y1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool operator!=(const RectT &other) const { return !operator==(other); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  RectT &operator+=(const PointT<t> &p) {</t>
Shinya Kitaoka 120a6e
    x0 += p.x, y0 += p.y, x1 += p.x, y1 += p.y;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  RectT &operator-=(const PointT<t> &p) {</t>
Shinya Kitaoka 120a6e
    x0 -= p.x, y0 -= p.y, x1 -= p.x, y1 -= p.y;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  friend RectT<t> operator+(const RectT<t> &r, const tcg::PointT<t> &p) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(r.x0 + p.x, r.y0 + p.y, r.x1 + p.x, r.y1 + p.y);</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend RectT<t> operator-(const RectT<t> &r, const tcg::PointT<t> &p) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(r.x0 - p.x, r.y0 - p.y, r.x1 - p.x, r.y1 - p.y);</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend RectT<t> operator+(const tcg::PointT<t> &p, const RectT<t> &r) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(p.x + r.x0, p.y + r.y0, p.x + r.x1, p.y + r.y1);</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend RectT<t> operator-(const tcg::PointT<t> &p, const RectT<t> &r) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(p.x - r.x0, p.y - r.y0, p.x - r.x1, p.y - r.y1);</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  template <typename k=""></typename>
Shinya Kitaoka 120a6e
  RectT &operator*=(K k) {
Shinya Kitaoka 120a6e
    x0 *= k, y0 *= k, x1 *= k, y1 *= k;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  template <typename k=""></typename>
Shinya Kitaoka 120a6e
  friend RectT<t> operator*(const RectT<t> &r, K k) {</t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(r.x0 * k, r.y0 * k, r.x1 * k, r.y1 * k);</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  template <typename k=""></typename>
Shinya Kitaoka 120a6e
  friend RectT<t> operator*(K k, const RectT<t> &r) {</t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(k * r.x0, k * r.y0, k * r.x1, k * r.y1);</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  RectT &operator|=(const RectT &other) {
Shinya Kitaoka 120a6e
    if (x0 > other.x0) x0 = other.x0;
Shinya Kitaoka 120a6e
    if (y0 > other.y0) y0 = other.y0;
Shinya Kitaoka 120a6e
    if (x1 < other.x1) x1 = other.x1;
Shinya Kitaoka 120a6e
    if (y1 < other.y1) y1 = other.y1;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  RectT &operator&=(const RectT &other) {
Shinya Kitaoka 120a6e
    if (x0 < other.x0) x0 = other.x0;
Shinya Kitaoka 120a6e
    if (y0 < other.y0) y0 = other.y0;
Shinya Kitaoka 120a6e
    if (x1 > other.x1) x1 = other.x1;
Shinya Kitaoka 120a6e
    if (y1 > other.y1) y1 = other.y1;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  RectT &operator|=(const PointT<t> &p) {</t>
Shinya Kitaoka 120a6e
    return operator|=(RectT(p.x, p.y, p.x, p.y));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  RectT &operator&=(const PointT<t> &p) {</t>
Shinya Kitaoka 120a6e
    return operator&=(RectT(p.x, p.y, p.x, p.y));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  friend RectT<t> operator|(const RectT<t> &a, const RectT<t> &b) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(a) |= b;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend RectT<t> operator&(const RectT<t> &a, const RectT<t> &b) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(a) &= b;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  friend RectT<t> operator|(const RectT<t> &r, const PointT<t> &p) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(r) |= p;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend RectT<t> operator&(const RectT<t> &r, const PointT<t> &p) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(r) &= p;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend RectT<t> operator|(const PointT<t> &p, const RectT<t> &r) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(r) |= p;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend RectT<t> operator&(const PointT<t> &p, const RectT<t> &r) {</t></t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(r) &= p;</t>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef RectT<double> RectD;</double>
Toshihiro Shimizu 890ddd
typedef RectT<int> RectI;</int>
Toshihiro Shimizu 890ddd
typedef RectI Rect;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**********************************************************************************
Toshihiro Shimizu 890ddd
//    Tridimensional Rect  class
Toshihiro Shimizu 890ddd
//**********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename t=""></typename>
Toshihiro Shimizu 890ddd
struct Rect3T {
Shinya Kitaoka 120a6e
  T x0, y0, z0, x1, y1, z1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Rect3T()
Shinya Kitaoka 120a6e
      : x0((std::numeric_limits<t>::max)())</t>
Shinya Kitaoka 120a6e
      , y0(x0)
Shinya Kitaoka 120a6e
      , z0(x0)
Shinya Kitaoka 120a6e
      , x1(-x0)
Shinya Kitaoka 120a6e
      , y1(x1)
Shinya Kitaoka 120a6e
      , z1(x1) {}
Shinya Kitaoka 120a6e
  Rect3T(T x0_, T y0_, T z0_, T x1_, T y1_, T z1_)
Shinya Kitaoka 120a6e
      : x0(x0_), y0(y0_), z0(z0_), x1(x1_), y1(y1_), z1(z1_) {}
Shinya Kitaoka 120a6e
  Rect3T(const Point3T<t> &p0, const Point3T<t> &p1)</t></t>
Shinya Kitaoka 120a6e
      : x0(p0.x), y0(p0.y), z0(p0.z) x1(p1.x), y1(p1.y), z1(p1.z) {}
Shinya Kitaoka 120a6e
  Rect3T(const Point3T<t> &p0, const Size3T<t> &size)</t></t>
Shinya Kitaoka 120a6e
      : x0(p0.x)
Shinya Kitaoka 120a6e
      , y0(p0.y)
Shinya Kitaoka 120a6e
      , z0(p0.z)
Shinya Kitaoka 120a6e
      , x1(p0.x + size.w)
Shinya Kitaoka 120a6e
      , y1(p0.y + size.h)
Shinya Kitaoka 120a6e
      , z1(p0.z + size.d) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool empty() const { return (x1 <= x0) || (y1 <= y0) || (z1 <= z0); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Point3T<t> p0() const { return Point3T<t>(x0, y0, z0); }</t></t>
Shinya Kitaoka 120a6e
  Point3T<t> p1() const { return Point3T<t>(x1, y1, z1); }</t></t>
Shinya Kitaoka 120a6e
  Point3T<t> center() const {</t>
Shinya Kitaoka 120a6e
    return Point3T<t>((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2);</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  T width() const { return x1 - x0; }
Shinya Kitaoka 120a6e
  T height() const { return y1 - y0; }
Shinya Kitaoka 120a6e
  T depth() const { return z1 - z0; }
Shinya Kitaoka 120a6e
  Size3T<t> size() const { return Size3T<t>(width(), height(), depth()); }</t></t>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool operator==(const Rect3T &other) const {
Shinya Kitaoka 120a6e
    return x0 == other.x0 && y0 == other.y0 && z0 == other.z0 &&
Shinya Kitaoka 120a6e
           x1 == other.x1 && y1 == other.y1 && z1 == other.z1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool operator!=(const Rect3T &other) const { return !operator==(other); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Rect3T &operator+=(const Point3T<t> &p) {</t>
Shinya Kitaoka 120a6e
    x0 += p.x, y0 += p.y, z0 += p.z, x1 += p.x, y1 += p.y, z1 += p.z;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  Rect3T &operator-=(const Point3T<t> &p) {</t>
Shinya Kitaoka 120a6e
    x0 -= p.x, y0 -= p.y, z0 -= p.z, x1 -= p.x, y1 -= p.y, z1 -= p.z;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator+(const Rect3T<t> &r, const tcg::Point3T<t> &p) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(r.x0 + p.x, r.y0 + p.y, r.z0 + p.z, r.x1 + p.x, r.y1 + p.y,</t>
Shinya Kitaoka 120a6e
                     r.z1 + p.z);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator-(const Rect3T<t> &r, const tcg::Point3T<t> &p) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(r.x0 - p.x, r.y0 - p.y, r.z0 - p.z, r.x1 - p.x, r.y1 - p.y,</t>
Shinya Kitaoka 120a6e
                     r.z1 - p.z);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator+(const tcg::Point3T<t> &p, const Rect3T<t> &r) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(p.x + r.x0, p.x + r.y0, p.x + r.z0, p.x + r.x1 + p.x, r.y1,</t>
Shinya Kitaoka 120a6e
                     p.x + r.z1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator-(const tcg::Point3T<t> &p, const Rect3T<t> &r) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(p.x - r.x0, p.y - r.y0, p.z - r.z0, p.x - r.x1, p.y - r.y1,</t>
Shinya Kitaoka 120a6e
                     p.z - r.z1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  template <typename k=""></typename>
Shinya Kitaoka 120a6e
  Rect3T &operator*=(K k) {
Shinya Kitaoka 120a6e
    x0 *= k, y0 *= k, z0 *= k, x1 *= k, y1 *= k, z1 *= k;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  template <typename k=""></typename>
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator*(const Rect3T<t> &r, K k) {</t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(r.x0 * k, r.y0 * k, r.z0 * k, r.x1 * k, r.y1 * k, r.z1 * k);</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  template <typename k=""></typename>
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator*(K k, const Rect3T<t> &r) {</t></t>
Shinya Kitaoka 120a6e
    return RectT<t>(k * r.x0, k * r.y0, k * r.z0, k * r.x1, k * r.y1, k * r.z1);</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Rect3T &operator|=(const Rect3T &other) {
Shinya Kitaoka 120a6e
    if (x0 > other.x0) x0 = other.x0;
Shinya Kitaoka 120a6e
    if (y0 > other.y0) y0 = other.y0;
Shinya Kitaoka 120a6e
    if (z0 > other.z0) z0 = other.z0;
Shinya Kitaoka 120a6e
    if (x1 < other.x1) x1 = other.x1;
Shinya Kitaoka 120a6e
    if (y1 < other.y1) y1 = other.y1;
Shinya Kitaoka 120a6e
    if (z1 < other.z1) z1 = other.z1;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Rect3T &operator&=(const Rect3T &other) {
Shinya Kitaoka 120a6e
    if (x0 < other.x0) x0 = other.x0;
Shinya Kitaoka 120a6e
    if (y0 < other.y0) y0 = other.y0;
Shinya Kitaoka 120a6e
    if (z0 < other.z0) z0 = other.z0;
Shinya Kitaoka 120a6e
    if (x1 > other.x1) x1 = other.x1;
Shinya Kitaoka 120a6e
    if (y1 > other.y1) y1 = other.y1;
Shinya Kitaoka 120a6e
    if (z1 > other.z1) z1 = other.z1;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Rect3T &operator|=(const Point3T<t> &p) {</t>
Shinya Kitaoka 120a6e
    return operator|=(RectT(p.x, p.y, p.z, p.x, p.y, p.z));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  Rect3T &operator&=(const Point3T<t> &p) {</t>
Shinya Kitaoka 120a6e
    return operator&=(RectT(p.x, p.y, p.z, p.x, p.y, p.z));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator|(const Rect3T<t> &a, const Rect3T<t> &b) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(a) |= b;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator&(const Rect3T<t> &a, const Rect3T<t> &b) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(a) &= b;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator|(const Rect3T<t> &r, const Point3T<t> &p) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(r) |= p;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator&(const Rect3T<t> &r, const Point3T<t> &p) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(r) &= p;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator|(const Point3T<t> &p, const Rect3T<t> &r) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(r) |= p;</t>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  friend Rect3T<t> operator&(const Point3T<t> &p, const Rect3T<t> &r) {</t></t></t>
Shinya Kitaoka 120a6e
    return Rect3T<t>(r) &= p;</t>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef Rect3T<double> Rect3D;</double>
Toshihiro Shimizu 890ddd
typedef Rect3T<int> Rect3I;</int>
Toshihiro Shimizu 890ddd
typedef RectI Rect;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TCG_RECT_H