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/>.
|
|
|
|
*
|
2015-01-15 14:01:05 +11:00
|
|
|
* Copyright 2010-2015 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
|
|
|
|
2015-01-15 14:01:05 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>::region (point<2,T> _point,
|
|
|
|
extent<size_type> _size):
|
2015-01-15 14:01:05 +11:00
|
|
|
x (_point.x),
|
|
|
|
y (_point.y),
|
|
|
|
w (_size.w),
|
|
|
|
h (_size.h)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>::region (position_type _x,
|
|
|
|
position_type _y,
|
|
|
|
size_type _w,
|
|
|
|
size_type _h):
|
2011-10-26 22:59:47 +11:00
|
|
|
x (_x),
|
|
|
|
y (_y),
|
|
|
|
w (_w),
|
|
|
|
h (_h)
|
2015-01-16 14:43:50 +11:00
|
|
|
{ ; }
|
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>
|
2015-01-21 23:33:35 +11:00
|
|
|
typename util::region<T>::size_type
|
|
|
|
util::region<T>::area (void) const
|
|
|
|
{
|
|
|
|
return w * h;
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-24 19:55:51 +11:00
|
|
|
template <typename T>
|
2015-01-21 23:33:35 +11:00
|
|
|
typename util::region<T>::size_type
|
|
|
|
util::region<T>::diameter (void) const
|
|
|
|
{
|
2014-12-15 20:10:56 +11:00
|
|
|
return static_cast<size_type> (std::sqrt (w * w + h * h));
|
2011-10-24 19:55:51 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-07-30 23:51:55 +10:00
|
|
|
template <typename T>
|
|
|
|
void
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>::scale (T factor)
|
|
|
|
{
|
2014-12-15 20:10:56 +11:00
|
|
|
x -= (w * factor - w) / T{2};
|
|
|
|
y -= (h * factor - h) / T{2};
|
2013-07-30 23:51:55 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
w = w * factor;
|
|
|
|
h = h * factor;
|
2013-07-30 23:51:55 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>::empty (void) const
|
|
|
|
{
|
|
|
|
return almost_zero (area ());
|
|
|
|
}
|
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>
|
2015-01-21 23:33:35 +11:00
|
|
|
util::point<2,T>
|
|
|
|
util::region<T>::base (void) const
|
|
|
|
{
|
2014-12-15 20:10:56 +11:00
|
|
|
return { x, y };
|
2011-10-26 21:43:55 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-20 23:53:18 +11:00
|
|
|
template <typename T>
|
2015-01-21 23:33:35 +11:00
|
|
|
util::point<2,T>
|
|
|
|
util::region<T>::centre (void) const
|
|
|
|
{
|
2014-12-15 20:10:56 +11:00
|
|
|
T cx = x + w / T{2},
|
|
|
|
cy = y + h / T{2};
|
2011-10-20 23:53:18 +11:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
return point<2,T> { cx, cy };
|
2011-10-20 23:53:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-09-18 13:52:11 +10:00
|
|
|
template <typename T>
|
2015-01-21 23:33:35 +11:00
|
|
|
util::point<2,T>
|
|
|
|
util::region<T>::closest (point<2,T> p) const
|
|
|
|
{
|
2013-09-18 13:52:11 +10:00
|
|
|
return {
|
|
|
|
p.x < x ? x :
|
|
|
|
p.x > x + w ? x + w :
|
|
|
|
p.x,
|
|
|
|
|
|
|
|
p.y < y ? y :
|
2013-09-19 15:15:38 +10:00
|
|
|
p.y > y + h ? y + h :
|
2013-09-18 13:52:11 +10:00
|
|
|
p.y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-17 17:20:17 +11:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>::includes (const point<2,T> &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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-20 21:09:47 +11:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>::contains (const point<2,T> &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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-06-15 16:38:57 +10:00
|
|
|
// FIXME: This will fail with an actual infinite range (NaNs will be generated
|
|
|
|
// in the conditionals).
|
2011-10-10 22:37:27 +11:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>::intersects (const util::region<T> &rhs) const
|
|
|
|
{
|
2012-06-15 16:38:57 +10:00
|
|
|
return x < rhs.x + rhs.w &&
|
|
|
|
rhs.x < x + w &&
|
|
|
|
y < rhs.y + rhs.h &&
|
|
|
|
rhs.y < y + h;
|
2011-10-10 22:37:27 +11:00
|
|
|
}
|
|
|
|
|
2012-05-11 12:22:23 +10:00
|
|
|
|
2013-07-30 23:52:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-08-05 16:38:05 +10:00
|
|
|
template <typename T>
|
|
|
|
void
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>::constrain (point<2,T> &p) const
|
|
|
|
{
|
2014-12-15 20:10:56 +11:00
|
|
|
p.x = std::min (std::max (p.x, x), x + w);
|
|
|
|
p.y = std::min (std::max (p.y, y), y + h);
|
2013-08-05 16:38:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-07-30 23:52:09 +10:00
|
|
|
template <typename T>
|
2015-01-21 23:33:35 +11:00
|
|
|
util::point<2,T>
|
|
|
|
util::region<T>::constrained (const point<2,T> &p) const
|
2014-12-15 20:10:56 +11:00
|
|
|
{
|
|
|
|
point<2,T> v;
|
|
|
|
v.x = std::min (std::max (p.x, x), x + w);
|
|
|
|
v.y = std::min (std::max (p.y, y), y + h);
|
2013-07-30 23:52:09 +10:00
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-11 12:22:23 +10:00
|
|
|
template<typename T>
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>
|
|
|
|
util::region<T>::intersection (const util::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);
|
2015-01-21 23:33:35 +11:00
|
|
|
return util::region<T> (newx1, newy1, nw, nh);
|
2012-05-11 12:22:23 +10:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:37:00 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
util::region<T>
|
|
|
|
util::region<T>::inset (T mag)
|
|
|
|
{
|
|
|
|
CHECK_GE (w - x, 2 * mag);
|
|
|
|
CHECK_GE (h - y, 2 * mag);
|
|
|
|
|
|
|
|
return { x + mag, y + mag, w - 2 * mag, h - 2 * mag };
|
|
|
|
}
|
|
|
|
|
2015-01-22 14:58:29 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
util::region<T>&
|
2015-02-04 15:44:51 +11:00
|
|
|
util::region<T>::expand (T _w, T _h)
|
2015-01-22 14:58:29 +11:00
|
|
|
{
|
2015-02-04 15:44:51 +11:00
|
|
|
x -= _w;
|
|
|
|
y -= _h;
|
|
|
|
w += _w * 2;
|
|
|
|
h += _h * 2;
|
2015-01-22 14:58:29 +11:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-04 15:44:51 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
util::region<T>&
|
|
|
|
util::region<T>::expand (T mag)
|
|
|
|
{
|
|
|
|
return expand (mag, mag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-22 14:58:29 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
util::region<T>
|
2015-02-04 15:44:51 +11:00
|
|
|
util::region<T>::expanded (T _w, T _h) const
|
2015-01-22 14:58:29 +11:00
|
|
|
{
|
|
|
|
return {
|
2015-02-04 15:44:51 +11:00
|
|
|
x - _w,
|
|
|
|
y - _h,
|
|
|
|
w + _w * 2,
|
|
|
|
h + _h * 2,
|
2015-01-22 14:58:29 +11:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:44:51 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
util::region<T>
|
|
|
|
util::region<T>::expanded (T mag) const
|
|
|
|
{
|
|
|
|
return expanded (mag, mag);
|
|
|
|
}
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2015-01-21 23:33:35 +11:00
|
|
|
util::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
|
|
|
|
|
|
|
|
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
|
2015-01-21 23:33:35 +11:00
|
|
|
util::region<T>::sanity (void) const {
|
2015-01-16 14:43:50 +11:00
|
|
|
CHECK_GE (w, 0);
|
|
|
|
CHECK_GE (h, 0);
|
2012-06-13 15:50:47 +10:00
|
|
|
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
|
|
|
//-----------------------------------------------------------------------------
|
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 {
|
2015-01-21 23:33:35 +11:00
|
|
|
CHECK_GE (w, 0);
|
|
|
|
CHECK_GE (h, 0);
|
2012-06-13 15:50:47 +10:00
|
|
|
}
|
2012-02-09 14:55:07 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2012-06-13 15:50:47 +10:00
|
|
|
void region<float>::sanity (void) const {
|
2015-01-21 23:33:35 +11:00
|
|
|
CHECK_GE (w, 0);
|
|
|
|
CHECK_GE (h, 0);
|
2012-06-13 15:50:47 +10:00
|
|
|
}
|
2011-08-29 14:40:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-16 23:35:50 +11:00
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
/// The largest specifiable finite region. Specifically does not allow infinities.
|
2012-06-15 16:38:57 +10:00
|
|
|
template <typename T>
|
2015-01-21 23:33:35 +11:00
|
|
|
const util::region<T>
|
2015-02-16 23:35:50 +11:00
|
|
|
util::region<T>::MAX (
|
|
|
|
std::numeric_limits<T>::lowest (),
|
|
|
|
std::numeric_limits<T>::lowest (),
|
|
|
|
std::numeric_limits<T>::max (),
|
|
|
|
std::numeric_limits<T>::max ()
|
|
|
|
);
|
2012-06-15 16:38:57 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2015-01-21 23:33:35 +11:00
|
|
|
const util::region<T>
|
|
|
|
util::region<T>::UNIT (0, 0, 1, 1);
|
2012-06-15 16:38:57 +10:00
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-20 23:53:30 +11:00
|
|
|
template <typename T>
|
|
|
|
std::ostream&
|
2015-01-21 23:33:35 +11:00
|
|
|
util::operator<< (std::ostream &os, const util::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<uint32_t>;
|
|
|
|
template struct region<uint64_t>;
|
2012-06-15 16:38:57 +10:00
|
|
|
template struct region<float>;
|
2012-06-13 14:41:53 +10:00
|
|
|
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>&);
|
2014-12-15 20:10:56 +11:00
|
|
|
template std::ostream& operator<< (std::ostream&, const region< float>&);
|
2012-06-14 18:29:09 +10:00
|
|
|
template std::ostream& operator<< (std::ostream&, const region< double>&);
|
2012-06-13 14:41:53 +10:00
|
|
|
}
|