2011-06-21 20:26:32 +10:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim 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 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libgim 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 libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2012-04-23 13:06:41 +10:00
|
|
|
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
2011-06-21 20:26:32 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#include "region.hpp"
|
|
|
|
|
2011-08-15 20:10:43 +10:00
|
|
|
#include "enable_if.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
#include "debug.hpp"
|
|
|
|
|
2011-10-24 19:55:51 +11:00
|
|
|
#include <cmath>
|
2011-08-15 20:10:43 +10:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
using namespace util;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2011-10-26 22:59:47 +11:00
|
|
|
region<T>::region (T _x, T _y, T _w, T _h):
|
|
|
|
x (_x),
|
|
|
|
y (_y),
|
|
|
|
w (_w),
|
|
|
|
h (_h)
|
2011-05-23 17:18:52 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2011-10-26 21:43:55 +11:00
|
|
|
template <typename T>
|
|
|
|
region<T>&
|
|
|
|
region<T>::operator+= (const vector &rhs) {
|
|
|
|
x += rhs.x;
|
|
|
|
y += rhs.y;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
|
|
|
T
|
2011-08-29 14:40:05 +10:00
|
|
|
region<T>::area (void) const
|
2011-10-26 22:59:47 +11:00
|
|
|
{ return w * h; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-24 19:55:51 +11:00
|
|
|
template <typename T>
|
|
|
|
T
|
|
|
|
region<T>::diameter (void) const {
|
2011-10-26 22:59:47 +11:00
|
|
|
return (T)sqrt (w * w + h * h);
|
2011-10-24 19:55:51 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2011-08-29 14:40:05 +10:00
|
|
|
region<T>::empty (void) const
|
|
|
|
{ return almost_equal (area (), 0); }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-26 21:43:55 +11:00
|
|
|
template <typename T>
|
|
|
|
point
|
|
|
|
region<T>::base (void) const {
|
|
|
|
return { static_cast<double> (x), static_cast<double> (y), 0.0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-20 23:53:18 +11:00
|
|
|
template <typename T>
|
|
|
|
point
|
|
|
|
region<T>::centre (void) const {
|
2011-10-26 22:59:47 +11:00
|
|
|
double cx = x + static_cast<T>(w / 2.0),
|
|
|
|
cy = y + static_cast<T>(h / 2.0);
|
2011-10-20 23:53:18 +11:00
|
|
|
|
|
|
|
return { cx, cy, 0.0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-17 17:20:17 +11:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
region<T>::includes (const point &p) const {
|
|
|
|
return p.x >= x &&
|
|
|
|
p.y >= y &&
|
2011-10-26 22:59:47 +11:00
|
|
|
p.x <= x + w &&
|
|
|
|
p.y <= y + h;
|
2011-10-17 17:20:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-20 21:09:47 +11:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
region<T>::contains (const point& p) const {
|
|
|
|
return p.x > x &&
|
|
|
|
p.y > y &&
|
2011-10-26 22:59:47 +11:00
|
|
|
p.x < x + w &&
|
|
|
|
p.y < y + h;
|
2011-10-20 21:09:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-10 22:37:27 +11:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
region<T>::overlaps (const region<T> &rhs) const {
|
2011-10-26 22:59:47 +11:00
|
|
|
return x < rhs.x + rhs.w &&
|
|
|
|
x + w > rhs.x &&
|
|
|
|
y < rhs.y + rhs.h &&
|
|
|
|
y + h > rhs.y;
|
2011-10-10 22:37:27 +11:00
|
|
|
}
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2011-08-29 14:40:05 +10:00
|
|
|
region<T>::operator ==(const region& rhs) const
|
2011-10-26 22:59:47 +11:00
|
|
|
{ return almost_equal (x, rhs.x) &&
|
|
|
|
almost_equal (y, rhs.y) &&
|
|
|
|
almost_equal (w, rhs.w) &&
|
|
|
|
almost_equal (h, rhs.h); }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2011-08-29 14:40:05 +10:00
|
|
|
void region<T>::sanity (void) const
|
2011-10-26 22:59:47 +11:00
|
|
|
{ check (w >= 0 && h >= 0); }
|
2011-08-29 14:40:05 +10:00
|
|
|
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
template <>
|
|
|
|
void region<unsigned int>::sanity (void) const
|
|
|
|
{ return; }
|
2012-02-09 14:55:07 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void region<unsigned long>::sanity (void) const
|
|
|
|
{ return; }
|
2011-08-29 14:40:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-20 23:53:30 +11:00
|
|
|
template <typename T>
|
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream &os, const region<T> &rhs) {
|
2011-10-26 22:59:47 +11:00
|
|
|
os << "region(" << rhs.x << ", " << rhs.y << ", " << rhs.w << ", " << rhs.h << ")";
|
2011-10-20 23:53:30 +11:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-29 14:40:05 +10:00
|
|
|
template struct region<unsigned int>;
|
2012-02-09 14:55:07 +11:00
|
|
|
template struct region<unsigned long>;
|
2011-08-29 14:40:05 +10:00
|
|
|
template struct region<double>;
|
|
|
|
|
2011-10-20 23:53:30 +11:00
|
|
|
template std::ostream& operator<< (std::ostream&, const region<unsigned int>&);
|
|
|
|
template std::ostream& operator<< (std::ostream&, const region<double>&);
|
|
|
|
|