libcruft-util/region.cpp

187 lines
4.1 KiB
C++
Raw Normal View History

/*
* 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-05-23 17:18:52 +10:00
#include "region.hpp"
#include "debug.hpp"
#include "types/casts.hpp"
2011-05-23 17:18:52 +10: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>
region<T>::region (T _x, T _y, size_type _w, size_type _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<2> &rhs) {
2011-10-26 21:43:55 +11:00
x += rhs.x;
y += rhs.y;
return *this;
}
2011-05-23 17:18:52 +10:00
template <typename T>
typename region<T>::size_type
2011-08-29 14:40:05 +10:00
region<T>::area (void) const
{ return w * h; }
2011-05-23 17:18:52 +10:00
template <typename T>
typename region<T>::size_type
region<T>::diameter (void) const {
return static_cast<size_type> (sqrt (w * w + h * h));
}
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<2>
2011-10-26 21:43:55 +11:00
region<T>::base (void) const {
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>
point<2>
2011-10-20 23:53:18 +11:00
region<T>::centre (void) const {
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 };
2011-10-20 23:53:18 +11:00
}
template <typename T>
bool
region<T>::includes (const point<2> &p) const {
return p.x >= x &&
p.y >= y &&
p.x - x <= w &&
p.y - y <= h;
}
2011-10-20 21:09:47 +11:00
template <typename T>
bool
region<T>::contains (const point<2> &p) const {
2011-10-20 21:09:47 +11:00
return p.x > x &&
p.y > y &&
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 {
//return !overlap (rhs).empty ();
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
}
template<typename T>
region<T>
region<T>::overlap (const region<T> &rhs) const {
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));
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);
}
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
{ 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>
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
namespace util {
template <>
void region<double>::sanity (void) const {
CHECK (w >= 0 && h >= 0);
}
template <>
void region<float>::sanity (void) const {
CHECK (w >= 0 && h >= 0);
}
2011-08-29 14:40:05 +10:00
}
2011-10-20 23:53:30 +11:00
template <typename T>
std::ostream&
util::operator<< (std::ostream &os, const region<T> &rhs) {
os << "region(" << rhs.x << ", " << rhs.y << ", " << rhs.w << ", " << rhs.h << ")";
2011-10-20 23:53:30 +11:00
return os;
}
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>;
template std::ostream& operator<< (std::ostream&, const region<unsigned int>&);
template std::ostream& operator<< (std::ostream&, const region<double>&);
}