2011-06-21 20:26:32 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-06-21 20:26:32 +10:00
|
|
|
*
|
2017-08-11 15:15:44 +10:00
|
|
|
* Copyright 2010-2017 Danny Robson <danny@nerdcruft.net>
|
2011-06-21 20:26:32 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "region.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2019-05-17 12:26:08 +10:00
|
|
|
#include "debug/assert.hpp"
|
2016-03-11 12:48:19 +11:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "coord/iostream.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2017-05-16 17:06:36 +10:00
|
|
|
#include <array>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::region;
|
2017-08-11 15:15:44 +10:00
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
|
2015-02-20 21:53:34 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>::region (extent_t _extent):
|
2016-12-12 17:06:04 +11:00
|
|
|
region (point_t::origin (), _extent)
|
2015-03-07 03:20:28 +11:00
|
|
|
{
|
2019-07-12 15:27:34 +10:00
|
|
|
CHECK_SANITY (*this);
|
2015-03-07 03:20:28 +11:00
|
|
|
}
|
2015-02-20 21:53:34 +11:00
|
|
|
|
|
|
|
|
2015-01-15 14:01:05 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>::region (point_t _p,
|
2015-03-06 01:09:37 +11:00
|
|
|
extent_t _e):
|
|
|
|
p (_p),
|
|
|
|
e (_e)
|
2015-03-07 03:20:28 +11:00
|
|
|
{
|
2019-07-12 15:27:34 +10:00
|
|
|
CHECK_SANITY (*this);
|
2015-03-07 03:20:28 +11:00
|
|
|
}
|
2015-01-15 14:01:05 +11:00
|
|
|
|
|
|
|
|
2015-02-17 18:25:29 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>::region (point_t _a,
|
2015-03-03 19:43:09 +11:00
|
|
|
point_t _b):
|
2015-09-09 18:44:20 +10:00
|
|
|
region (_a, extent_t { _b - _a })
|
2015-03-07 03:20:28 +11:00
|
|
|
{
|
2017-07-28 14:23:20 +10:00
|
|
|
// This check must allow for zero area (but non-zero dimension) regions.
|
|
|
|
// Some code paths need to support this degenerate case. It's ugly but
|
|
|
|
// simplifies generalisation. eg, vertical linear bezier curves.
|
|
|
|
CHECK (all (_a <= _b));
|
2015-03-06 01:09:37 +11:00
|
|
|
|
2019-07-12 15:27:34 +10:00
|
|
|
CHECK_SANITY (*this);
|
2015-02-17 18:25:29 +11:00
|
|
|
}
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-02-17 16:24:16 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
typename cruft::region<S,T>::extent_t
|
|
|
|
cruft::region<S,T>::magnitude (void) const
|
2015-02-17 16:24:16 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
return e;
|
2015-02-17 16:24:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-02 18:48:09 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
typename cruft::region<S,T>::extent_t
|
|
|
|
cruft::region<S,T>::magnitude (extent_t _e)
|
2015-03-02 18:48:09 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
e = _e;
|
|
|
|
return e;
|
2015-03-02 18:48:09 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2015-02-17 16:25:41 +11:00
|
|
|
bool
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>::empty (void) const
|
2015-01-21 23:33:35 +11:00
|
|
|
{
|
|
|
|
return almost_zero (area ());
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
typename cruft::region<S,T>::point_t
|
|
|
|
cruft::region<S,T>::base (void) const
|
2015-01-21 23:33:35 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
return p;
|
2011-10-26 21:43:55 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-17 16:24:33 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
typename cruft::region<S,T>::point_t
|
|
|
|
cruft::region<S,T>::away (void) const
|
2015-02-17 16:24:33 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
return p + e;
|
2015-02-17 16:24:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
typename cruft::region<S,T>::point_t
|
|
|
|
cruft::region<S,T>::centre (void) const
|
2015-01-21 23:33:35 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
return p + e / T{2};
|
2011-10-20 23:53:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
typename cruft::region<S,T>::point_t
|
|
|
|
cruft::region<S,T>::closest (point_t q) const
|
2015-01-21 23:33:35 +11:00
|
|
|
{
|
2019-09-26 08:08:06 +10:00
|
|
|
return clamp (q, p, p + e);
|
2013-09-18 13:52:11 +10: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).
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2011-10-10 22:37:27 +11:00
|
|
|
bool
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>::intersects (region<S,T> rhs) const
|
2015-01-21 23:33:35 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
for (size_t i = 0; i < S; ++i)
|
2017-08-11 15:15:44 +10:00
|
|
|
if ( p[i] >= rhs.p[i] + rhs.e[i] ||
|
2015-03-06 01:09:37 +11:00
|
|
|
rhs.p[i] >= p[i] + e[i])
|
|
|
|
{ return false; }
|
|
|
|
|
|
|
|
return true;
|
2011-10-10 22:37:27 +11:00
|
|
|
}
|
|
|
|
|
2012-05-11 12:22:23 +10:00
|
|
|
|
2013-07-30 23:52:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2017-08-11 15:19:14 +10:00
|
|
|
typename region<S,T>::point_t
|
|
|
|
region<S,T>::constrain (point_t q) const noexcept
|
2015-01-21 23:33:35 +11:00
|
|
|
{
|
2019-09-26 08:08:06 +10:00
|
|
|
return closest (q);
|
2013-07-30 23:52:09 +10:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:33:35 +11:00
|
|
|
|
2012-06-14 18:29:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template<size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>
|
|
|
|
cruft::region<S,T>::intersection (region<S,T> rhs) const
|
2015-01-21 23:33:35 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
// find the intersection corners
|
|
|
|
point_t a, b;
|
2012-05-11 12:22:23 +10:00
|
|
|
|
2015-03-06 01:09:37 +11:00
|
|
|
for (size_t i = 0; i < S; ++i) {
|
2018-08-05 14:42:02 +10:00
|
|
|
a[i] = cruft::max (p[i], rhs.p[i]);
|
|
|
|
b[i] = cruft::min (p[i] + e[i], rhs.p[i] + rhs.e[i]);
|
2012-06-13 15:50:47 +10:00
|
|
|
|
2015-03-06 01:09:37 +11:00
|
|
|
if (b[i] < a[i])
|
|
|
|
throw std::logic_error ("no overlap");
|
|
|
|
}
|
|
|
|
|
|
|
|
return { a, b };
|
2012-05-11 12:22:23 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-06 01:09:37 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <size_t S, typename T>
|
2017-08-11 14:39:46 +10:00
|
|
|
bool
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>::covers (region<S, T> r) const noexcept
|
2015-03-06 01:09:37 +11:00
|
|
|
{
|
2017-08-11 14:39:46 +10:00
|
|
|
return all (p <= r.p) && all (p + e >= r.p + r.e);
|
2015-03-06 01:09:37 +11:00
|
|
|
}
|
|
|
|
|
2017-08-11 14:39:46 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>
|
|
|
|
cruft::region<S,T>::inset (T mag) const
|
2015-01-21 23:37:00 +11:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
return inset (cruft::vector<S,T> {mag});
|
2015-01-21 23:37:00 +11:00
|
|
|
}
|
|
|
|
|
2015-01-22 14:58:29 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>
|
|
|
|
cruft::region<S,T>::inset (vector<S,T> mag) const
|
2015-01-22 14:58:29 +11:00
|
|
|
{
|
2017-08-09 17:17:55 +10:00
|
|
|
// ensure we have enough space to trim off our total extent
|
2017-11-23 17:24:11 +11:00
|
|
|
CHECK (all (e >= T{2} * mag));
|
2015-01-22 14:58:29 +11:00
|
|
|
|
2015-03-06 01:09:37 +11:00
|
|
|
return {
|
2017-08-09 17:17:55 +10:00
|
|
|
p + mag,
|
|
|
|
e - T{2} * mag
|
2015-03-06 01:09:37 +11:00
|
|
|
};
|
2015-02-04 15:44:51 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-22 14:58:29 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>
|
|
|
|
cruft::region<S,T>::expand (vector<S,T> v) const
|
2015-01-22 14:58:29 +11:00
|
|
|
{
|
|
|
|
return {
|
2015-03-06 01:09:37 +11:00
|
|
|
p - v,
|
|
|
|
e + v * T{2}
|
2015-01-22 14:58:29 +11:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:44:51 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>
|
|
|
|
cruft::region<S,T>::expand (T mag) const
|
2015-02-04 15:44:51 +11:00
|
|
|
{
|
2017-08-09 17:28:53 +10:00
|
|
|
return expand (vector<S,T> {mag});
|
2015-02-04 15:44:51 +11:00
|
|
|
}
|
|
|
|
|
2015-02-20 15:26:59 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>
|
|
|
|
cruft::region<S,T>::operator+ (vector<S,T> rhs) const
|
2015-02-20 15:26:59 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
return { p + rhs, e };
|
2015-02-20 15:26:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 16:06:46 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>
|
|
|
|
cruft::region<S,T>::operator- (vector<S,T> rhs) const
|
2015-02-20 16:06:46 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
return { p - rhs, e };
|
2015-02-20 16:06:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 15:26:59 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2011-05-23 17:18:52 +10:00
|
|
|
bool
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::region<S,T>::operator== (region rhs) const
|
2015-01-21 23:33:35 +11:00
|
|
|
{
|
2015-03-06 01:09:37 +11:00
|
|
|
return p == rhs.p && e == rhs.e;
|
2015-01-21 23:33:35 +11:00
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2020-08-21 11:34:52 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
cruft::region<2,T>
|
|
|
|
cruft::rotate90 (cruft::region<2,T> obj, int steps)
|
|
|
|
{
|
2020-09-24 08:03:41 +10:00
|
|
|
CHECK_INCLUSIVE (steps, 0, 3);
|
2020-08-21 11:34:52 +10:00
|
|
|
|
|
|
|
switch (steps) {
|
|
|
|
case 0:
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return region2<T> {
|
|
|
|
point2<T> {
|
|
|
|
T(obj.p.x - obj.e.h),
|
|
|
|
obj.p.y
|
|
|
|
},
|
|
|
|
extent2<T> {
|
|
|
|
obj.e.h,
|
|
|
|
obj.e.w,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return region2<T> {
|
|
|
|
point2<T> {
|
|
|
|
obj.p.x,
|
|
|
|
T(obj.p.y - obj.e.h),
|
|
|
|
},
|
|
|
|
obj.e,
|
|
|
|
};
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
return region2<T> {
|
|
|
|
obj.p,
|
|
|
|
extent2<T> {
|
|
|
|
obj.e.h,
|
|
|
|
obj.e.w,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template cruft::region2<i32> cruft::rotate90 (cruft::region2<i32>, int);
|
|
|
|
template cruft::region2<i64> cruft::rotate90 (cruft::region2<i64>, int);
|
|
|
|
template cruft::region2<f32> cruft::rotate90 (cruft::region2<f32>, int);
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-12 17:07:53 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-03-03 04:13:29 +11:00
|
|
|
template <size_t S, typename T>
|
2011-10-20 23:53:30 +11:00
|
|
|
std::ostream&
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::operator<< (std::ostream &os, const cruft::region<S,T> &rhs) {
|
2016-12-21 16:45:08 +11:00
|
|
|
return os << "{ position: " << rhs.p << ", extent: " << rhs.e << " }";
|
2011-10-20 23:53:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-07 03:20:28 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::debug {
|
2015-03-07 03:20:28 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
struct validator<cruft::region<S,T>> {
|
|
|
|
static bool is_valid (const cruft::region<S,T> &r)
|
2015-03-07 03:20:28 +11:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
return cruft::debug::is_valid (r.p) && cruft::debug::is_valid (r.e);
|
2015-03-07 03:20:28 +11:00
|
|
|
}
|
|
|
|
};
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2015-03-06 17:52:40 +11:00
|
|
|
|
|
|
|
|
2015-03-07 03:20:28 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2020-08-21 11:34:52 +10:00
|
|
|
#define INSTANTIATE_S_T(S,T) \
|
|
|
|
template struct cruft::region<S,T>; \
|
|
|
|
template std::ostream& cruft::operator<< (std::ostream&, const region<S,T>&); \
|
2018-08-05 14:42:02 +10:00
|
|
|
template struct cruft::debug::validator<cruft::region<S,T>>;
|
2015-03-07 03:20:28 +11:00
|
|
|
|
|
|
|
#define INSTANTIATE(T) \
|
|
|
|
INSTANTIATE_S_T(2,T) \
|
|
|
|
INSTANTIATE_S_T(3,T)
|
|
|
|
|
2020-08-21 11:34:52 +10:00
|
|
|
INSTANTIATE(i16);
|
|
|
|
INSTANTIATE(i32);
|
|
|
|
INSTANTIATE(i64);
|
2016-10-25 17:47:08 +11:00
|
|
|
|
2020-08-21 11:34:52 +10:00
|
|
|
INSTANTIATE(u16)
|
|
|
|
INSTANTIATE(u32)
|
|
|
|
INSTANTIATE(u64)
|
2016-10-25 17:47:08 +11:00
|
|
|
|
2020-08-21 11:34:52 +10:00
|
|
|
INSTANTIATE(f32)
|
|
|
|
INSTANTIATE(f64)
|