libcruft-util/region.cpp

406 lines
9.9 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/>.
*
2015-01-15 14:01:05 +11:00
* Copyright 2010-2015 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>
2015-02-20 21:53:34 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>::region (extent_t _extent):
region (point_t::ORIGIN, _extent)
2015-02-20 21:53:34 +11:00
{ ; }
2015-01-15 14:01:05 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>::region (point_t _p,
extent_t _e):
p (_p),
e (_e)
2015-01-15 14:01:05 +11:00
{ ; }
2015-02-17 18:25:29 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>::region (point_t _a,
point_t _b):
2015-02-20 21:53:34 +11:00
region (_a, _b - _a)
{ ; }
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>::region (std::array<T,S*2> args)
2015-02-17 18:25:29 +11:00
{
std::copy (&args[0], &args[S], p.data);
std::copy (&args[S], &args[S*2], e.data);
2015-02-17 18:25:29 +11:00
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>::region (position_type _p,
size_type _e):
region (point_t {_p}, extent_t {_e})
{
}
2011-05-23 17:18:52 +10:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
typename util::region<S,T>::size_type
util::region<S,T>::area (void) const
2015-01-21 23:33:35 +11:00
{
return e.area ();
2015-01-21 23:33:35 +11:00
}
2011-05-23 17:18:52 +10:00
2015-01-21 23:33:35 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
typename util::region<S,T>::size_type
util::region<S,T>::diameter (void) const
2015-01-21 23:33:35 +11:00
{
return e.diameter ();
}
2015-02-17 16:24:16 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
typename util::region<S,T>::extent_t
util::region<S,T>::magnitude (void) const
2015-02-17 16:24:16 +11:00
{
return e;
2015-02-17 16:24:16 +11:00
}
2015-03-02 18:48:09 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
typename util::region<S,T>::extent_t
util::region<S,T>::magnitude (extent_t _e)
2015-03-02 18:48:09 +11:00
{
e = _e;
return e;
2015-03-02 18:48:09 +11:00
}
2015-01-21 23:33:35 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
2013-07-30 23:51:55 +10:00
void
util::region<S,T>::scale (T factor)
2015-01-21 23:33:35 +11:00
{
auto o = (e * factor - e) / T{2};
p -= o;
e *= factor;
2013-07-30 23:51:55 +10:00
}
2015-01-21 23:33:35 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
2015-02-17 16:25:41 +11:00
bool
util::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
//-----------------------------------------------------------------------------
template <size_t S, typename T>
typename util::region<S,T>::point_t
util::region<S,T>::base (void) const
2015-01-21 23:33:35 +11:00
{
return p;
2011-10-26 21:43:55 +11:00
}
2015-02-17 16:24:33 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
typename util::region<S,T>::point_t
util::region<S,T>::away (void) const
2015-02-17 16:24:33 +11:00
{
return p + e;
2015-02-17 16:24:33 +11:00
}
2015-01-21 23:33:35 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
typename util::region<S,T>::point_t
util::region<S,T>::centre (void) const
2015-01-21 23:33:35 +11:00
{
return p + e / T{2};
2011-10-20 23:53:18 +11:00
}
2015-01-21 23:33:35 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
typename util::region<S,T>::point_t
util::region<S,T>::closest (point_t q) const
2015-01-21 23:33:35 +11:00
{
point_t out;
2013-09-18 13:52:11 +10:00
for (size_t i = 0; i < S; ++i)
out[i] = q[i] < p[i] ? p[i] :
q[i] > p[i] ? p[i] + e[i] :
q[i];
return out;
2013-09-18 13:52:11 +10:00
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
bool
util::region<S,T>::includes (point_t q) const
2015-01-21 23:33:35 +11:00
{
for (size_t i = 0; i < S; ++i)
if (q[i] < p[i] || q[i] > p[i] + e[i])
return false;
return true;
}
2015-01-21 23:33:35 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
2011-10-20 21:09:47 +11:00
bool
util::region<S,T>::contains (point_t q) const
2015-01-21 23:33:35 +11:00
{
for (size_t i = 0; i < S; ++i)
if (q[i] <= p[i] || q[i] >= p[i] + e[i])
return false;
return true;
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).
template <size_t S, typename T>
2011-10-10 22:37:27 +11:00
bool
util::region<S,T>::intersects (region<S,T> rhs) const
2015-01-21 23:33:35 +11:00
{
for (size_t i = 0; i < S; ++i)
if (p[i] >= rhs.p[i] + rhs.e[i] ||
rhs.p[i] >= p[i] + e[i])
{ return false; }
return true;
2011-10-10 22:37:27 +11:00
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
void
util::region<S,T>::constrain (point_t &q) const
2015-01-21 23:33:35 +11:00
{
for (size_t i = 0; i < S; ++i)
q[i] = limit (q[i], p[i], p[i] + e[i]);
}
2015-01-21 23:33:35 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
typename util::region<S,T>::point_t
util::region<S,T>::constrained (point_t q) const
{
constrain (q);
return q;
}
2015-01-21 23:33:35 +11:00
//-----------------------------------------------------------------------------
template<size_t S, typename T>
util::region<S,T>
util::region<S,T>::intersection (region<S,T> rhs) const
2015-01-21 23:33:35 +11:00
{
// find the intersection corners
point_t a, b;
for (size_t i = 0; i < S; ++i) {
a[i] = max (p[i], rhs.p[i]);
b[i] = min (p[i] + e[i], rhs.p[i] + rhs.e[i]);
if (b[i] < a[i])
throw std::logic_error ("no overlap");
}
return { a, b };
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>&
util::region<S,T>::resize (extent<S,T> _e)
{
e = _e;
return *this;
}
2015-01-21 23:37:00 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>
util::region<S,T>::inset (T mag)
2015-01-21 23:37:00 +11:00
{
// ensure we have enough space to inset
CHECK (std::all_of (std::begin (e.data),
std::end (e.data),
[mag] (auto i) { return i >= 2 * mag; }));
2015-01-21 23:37:00 +11:00
return {
p + mag,
e - 2 * mag
};
2015-01-21 23:37:00 +11:00
}
2015-01-22 14:58:29 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>&
util::region<S,T>::expand (vector<S,T> v)
2015-01-22 14:58:29 +11:00
{
p -= v;
e += v * T{2};
2015-01-22 14:58:29 +11:00
return *this;
}
2015-02-04 15:44:51 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>&
util::region<S,T>::expand (T mag)
2015-02-04 15:44:51 +11:00
{
return expand ({mag});
2015-02-04 15:44:51 +11:00
}
2015-01-22 14:58:29 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>
util::region<S,T>::expanded (vector<S,T> v) const
2015-01-22 14:58:29 +11:00
{
return {
p - v,
e + v * T{2}
2015-01-22 14:58:29 +11:00
};
}
2015-02-04 15:44:51 +11:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>
util::region<S,T>::expanded (T mag) const
2015-02-04 15:44:51 +11:00
{
return expanded ({mag});
2015-02-04 15:44:51 +11:00
}
2015-02-20 15:26:59 +11:00
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
util::region<S,T>
util::region<S,T>::operator+ (vector<S,T> rhs) const
2015-02-20 15:26:59 +11:00
{
return { p + rhs, e };
2015-02-20 15:26:59 +11:00
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::region<S,T>
util::region<S,T>::operator- (vector<S,T> rhs) const
{
return { p - rhs, e };
}
2015-02-20 15:26:59 +11:00
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
2011-05-23 17:18:52 +10:00
bool
util::region<S,T>::operator== (region rhs) const
2015-01-21 23:33:35 +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
//-----------------------------------------------------------------------------
template <size_t S, typename T>
void
util::region<S,T>::sanity (void) const {
CHECK_GE (e.area (), 0);
if (std::is_floating_point<T>::value) {
CHECK_GE (min (e), 0);
}
2011-08-29 14:40:05 +10:00
}
///----------------------------------------------------------------------------
/// The largest specifiable finite region.
///
/// Starts at half the minimum value to allow the width to cover some positive
/// range rather than just cancelling out the lowest value for signed types.
///
/// Specifically does not allow infinities. Use/define INFINITE when required.
template <size_t S, typename T>
const util::region<S,T>
util::region<S,T>::MAX (
std::numeric_limits<T>::lowest () / 2,
std::numeric_limits<T>::max ()
);
2012-06-15 16:38:57 +10:00
template <size_t S, typename T>
const util::region<S,T>
util::region<S,T>::UNIT ({0}, {1});
2012-06-15 16:38:57 +10:00
//-----------------------------------------------------------------------------
template <size_t S, typename T>
2011-10-20 23:53:30 +11:00
std::ostream&
util::operator<< (std::ostream &os, const util::region<S,T> &rhs) {
os << "region(" << rhs.p << ", " << rhs.e << ")";
2011-10-20 23:53:30 +11:00
return os;
}
//-----------------------------------------------------------------------------
namespace util {
#define INSTANTIATE_S_T(S,T) \
template struct region<S,T>; \
template std::ostream& operator<< (std::ostream&, const region<S,T>&);
#define INSTANTIATE(T) \
INSTANTIATE_S_T(2,T) \
INSTANTIATE_S_T(3,T)
INSTANTIATE(uint32_t)
INSTANTIATE(uint64_t)
INSTANTIATE(float)
INSTANTIATE(double)
}