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"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
2012-06-13 15:50:47 +10:00
|
|
|
#include "types/casts.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-24 19:55:51 +11:00
|
|
|
#include <cmath>
|
2011-08-15 20:10:43 +10:00
|
|
|
#include <type_traits>
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-08-15 20:10:43 +10:00
|
|
|
using namespace util;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
2012-05-25 15:30:28 +10:00
|
|
|
region<T>::region (T _x, T _y, size_type _w, size_type _h):
|
2011-10-26 22:59:47 +11:00
|
|
|
x (_x),
|
|
|
|
y (_y),
|
|
|
|
w (_w),
|
|
|
|
h (_h)
|
2012-05-11 12:22:23 +10:00
|
|
|
{
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-26 21:43:55 +11:00
|
|
|
template <typename T>
|
|
|
|
region<T>&
|
2012-05-18 17:56:24 +10:00
|
|
|
region<T>::operator+= (const vector<2> &rhs) {
|
2011-10-26 21:43:55 +11:00
|
|
|
x += rhs.x;
|
|
|
|
y += rhs.y;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
2012-05-25 15:30:28 +10:00
|
|
|
typename region<T>::size_type
|
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>
|
2012-05-25 15:30:28 +10:00
|
|
|
typename region<T>::size_type
|
2011-10-24 19:55:51 +11:00
|
|
|
region<T>::diameter (void) const {
|
2012-05-25 15:30:28 +10:00
|
|
|
return static_cast<size_type> (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
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-26 21:43:55 +11:00
|
|
|
template <typename T>
|
2012-05-18 17:56:24 +10:00
|
|
|
point<2>
|
2011-10-26 21:43:55 +11:00
|
|
|
region<T>::base (void) const {
|
2012-05-18 17:56:24 +10:00
|
|
|
return { static_cast<double> (x), static_cast<double> (y) };
|
2011-10-26 21:43:55 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-20 23:53:18 +11:00
|
|
|
template <typename T>
|
2012-05-18 17:56:24 +10:00
|
|
|
point<2>
|
2011-10-20 23:53:18 +11:00
|
|
|
region<T>::centre (void) const {
|
2012-05-11 12:22:23 +10:00
|
|
|
double cx = x + static_cast<T>(w / 2.0),
|
2011-10-26 22:59:47 +11:00
|
|
|
cy = y + static_cast<T>(h / 2.0);
|
2011-10-20 23:53:18 +11:00
|
|
|
|
2012-05-18 17:56:24 +10:00
|
|
|
return { cx, cy };
|
2011-10-20 23:53:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-17 17:20:17 +11:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2012-05-18 17:56:24 +10:00
|
|
|
region<T>::includes (const point<2> &p) const {
|
2011-10-17 17:20:17 +11:00
|
|
|
return p.x >= x &&
|
|
|
|
p.y >= y &&
|
2012-06-13 15:50:47 +10: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
|
2012-05-18 17:56:24 +10:00
|
|
|
region<T>::contains (const point<2> &p) const {
|
2011-10-20 21:09:47 +11:00
|
|
|
return p.x > x &&
|
|
|
|
p.y > y &&
|
2012-06-13 15:50:47 +10: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 {
|
2012-05-11 12:22:23 +10:00
|
|
|
//return !overlap (rhs).empty ();
|
2012-06-13 15:50:47 +10:00
|
|
|
|
|
|
|
return x < sign_cast<T> (rhs.w) + rhs.x &&
|
|
|
|
rhs.x < sign_cast<T> ( w) + x &&
|
|
|
|
y < sign_cast<T> (rhs.h) + rhs.y &&
|
|
|
|
rhs.y < sign_cast<T> ( h) + y;
|
2011-10-10 22:37:27 +11:00
|
|
|
}
|
|
|
|
|
2012-05-11 12:22:23 +10:00
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-11 12:22:23 +10:00
|
|
|
template<typename T>
|
|
|
|
region<T>
|
|
|
|
region<T>::overlap (const region<T> &rhs) const {
|
2012-06-13 15:50:47 +10:00
|
|
|
T newx1 = max (x, rhs.x),
|
|
|
|
newy1 = max (y, rhs.y),
|
|
|
|
newx2 = min (x + sign_cast<T> (w), rhs.x + sign_cast<T> (rhs.w)),
|
|
|
|
newy2 = min (y + sign_cast<T> (h), rhs.y + sign_cast<T> (rhs.h));
|
2012-05-11 12:22:23 +10:00
|
|
|
|
2012-06-13 15:50:47 +10:00
|
|
|
if (newx2 < newx1 || newy2 < newy1)
|
|
|
|
throw std::logic_error ("No overlap");
|
|
|
|
|
|
|
|
size_type nw = sign_cast<size_type> (newx2 - newx1);
|
|
|
|
size_type nh = sign_cast<size_type> (newy2 - newy1);
|
|
|
|
return region<T> (newx1, newy1, nw, nh);
|
2012-05-11 12:22:23 +10:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2012-06-14 18:29:09 +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
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
2012-06-13 15:50:47 +10:00
|
|
|
void
|
|
|
|
region<T>::sanity (void) const {
|
|
|
|
static_assert(!std::is_floating_point<T>::value,
|
|
|
|
"Floating point types need width and height checks");
|
|
|
|
}
|
2011-08-29 14:40:05 +10:00
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// The desired iterator semantics have been difficult to nail down; is it
|
|
|
|
// edge-inclusive, left-bottom inclusive, purely exclusive, integral only?
|
|
|
|
// The code has been left here because it was a little annoying to write and
|
|
|
|
// we're likely to need it again some day.
|
|
|
|
#if 0
|
|
|
|
template <typename T>
|
|
|
|
typename region<T>::iterator&
|
|
|
|
region<T>::iterator::operator++ (void) {
|
|
|
|
if (++x > static_cast<T> (w)) {
|
|
|
|
x = a;
|
|
|
|
++y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename region<T>::iterator&
|
|
|
|
region<T>::iterator::operator* (void) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
region<T>::iterator::operator== (const iterator &rhs) const {
|
|
|
|
return almost_equal (rhs.x, x) && almost_equal (rhs.y, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
region<T>::iterator::operator!= (const iterator &rhs) const {
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename region<T>::iterator
|
|
|
|
region<T>::begin (void) {
|
|
|
|
return { x, y, x, w, h };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename region<T>::iterator
|
|
|
|
region<T>::end (void) {
|
|
|
|
return { x, y + sign_cast<T> (h) + 1, x, w, h };
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-08-29 14:40:05 +10:00
|
|
|
namespace util {
|
|
|
|
template <>
|
2012-06-13 15:50:47 +10:00
|
|
|
void region<double>::sanity (void) const {
|
|
|
|
CHECK (w >= 0 && h >= 0);
|
|
|
|
}
|
2012-02-09 14:55:07 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2012-06-13 15:50:47 +10:00
|
|
|
void region<float>::sanity (void) const {
|
|
|
|
CHECK (w >= 0 && h >= 0);
|
|
|
|
}
|
2011-08-29 14:40:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-20 23:53:30 +11:00
|
|
|
template <typename T>
|
|
|
|
std::ostream&
|
2012-06-13 15:50:47 +10:00
|
|
|
util::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-06-13 14:41:53 +10:00
|
|
|
namespace util {
|
|
|
|
template struct region<int32_t>;
|
|
|
|
template struct region<int64_t>;
|
|
|
|
template struct region<uint32_t>;
|
|
|
|
template struct region<uint64_t>;
|
|
|
|
template struct region<double>;
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
template std::ostream& operator<< (std::ostream&, const region< int32_t>&);
|
|
|
|
template std::ostream& operator<< (std::ostream&, const region< int64_t>&);
|
|
|
|
template std::ostream& operator<< (std::ostream&, const region<uint32_t>&);
|
|
|
|
template std::ostream& operator<< (std::ostream&, const region<uint64_t>&);
|
|
|
|
template std::ostream& operator<< (std::ostream&, const region< double>&);
|
2012-06-13 14:41:53 +10:00
|
|
|
}
|