2011-06-23 22:04:51 +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-23 22:04:51 +10:00
|
|
|
*
|
2018-04-16 15:54:08 +10:00
|
|
|
* Copyright 2011-2018 Danny Robson <danny@nerdcruft.net>
|
2011-06-23 22:04:51 +10:00
|
|
|
*/
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#ifndef CRUFT_UTIL_POINT_HPP
|
|
|
|
#define CRUFT_UTIL_POINT_HPP
|
2011-06-23 22:04:51 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
#include "vector.hpp"
|
2015-03-06 01:15:52 +11:00
|
|
|
#include "coord.hpp"
|
2018-02-28 11:49:13 +11:00
|
|
|
#include "maths.hpp"
|
2018-04-18 21:44:36 +10:00
|
|
|
#include "view.hpp"
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#include <algorithm>
|
2014-12-19 14:59:16 +11:00
|
|
|
#include <type_traits>
|
2011-10-18 21:45:55 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2012-05-18 17:56:24 +10:00
|
|
|
/// An n-dimensional position in space.
|
2017-05-24 15:15:40 +10:00
|
|
|
///
|
|
|
|
/// \tparam S number of dimensions
|
|
|
|
/// \tparam T the underlying per-dimension datatype
|
2014-12-15 20:10:56 +11:00
|
|
|
template <size_t S, typename T>
|
2017-11-22 17:03:00 +11:00
|
|
|
struct point : public coord::base<S,T,point<S,T>>
|
2015-03-06 01:09:37 +11:00
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
using coord::base<S,T,point<S,T>>::base;
|
2011-06-23 22:04:51 +10:00
|
|
|
|
2018-04-16 15:54:30 +10:00
|
|
|
// use a forwarding assignment operator so that we can let the base
|
|
|
|
// take care of the many different types of parameters. otherwise we
|
|
|
|
// have to deal with scalar, vector, initializer_list, ad nauseum.
|
|
|
|
template <typename Arg>
|
|
|
|
point&
|
|
|
|
operator= (Arg&&arg)
|
|
|
|
{
|
|
|
|
coord::base<S,T,point<S,T>>::operator=(std::forward<Arg> (arg));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-04-16 15:54:08 +10:00
|
|
|
vector<S,T> to (point dst) const { return dst - *this; }
|
|
|
|
vector<S,T> from (point src) const { return *this - src; }
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
/// expand point to use homogenous coordinates of a higher dimension.
|
|
|
|
/// ie, fill with (0,..,0,1)
|
2018-04-17 14:26:23 +10:00
|
|
|
point<S+1,T>
|
2018-02-28 11:49:13 +11:00
|
|
|
homog (void) const
|
|
|
|
{
|
2018-04-17 14:26:23 +10:00
|
|
|
return this->template redim<S+1> (1);
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
2015-02-19 13:19:27 +11:00
|
|
|
|
2017-07-28 14:22:21 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
static constexpr
|
|
|
|
auto min (void)
|
|
|
|
{
|
|
|
|
return point { std::numeric_limits<T>::lowest () };
|
|
|
|
}
|
2015-01-05 18:08:20 +11:00
|
|
|
|
2017-07-28 14:22:21 +10:00
|
|
|
//-------------------------------------------------------------------
|
|
|
|
static constexpr
|
|
|
|
auto max (void)
|
|
|
|
{
|
|
|
|
return point { std::numeric_limits<T>::max () };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
static constexpr
|
2018-02-28 11:49:13 +11:00
|
|
|
point<S,T> origin (void)
|
|
|
|
{
|
|
|
|
return point<S,T> {0};
|
|
|
|
}
|
2017-07-28 14:22:21 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2011-10-18 21:45:55 +11:00
|
|
|
void sanity (void) const;
|
2011-08-15 20:11:20 +10:00
|
|
|
};
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2018-04-18 21:44:36 +10:00
|
|
|
|
|
|
|
// Convenience typedefs
|
|
|
|
template <typename T> using point1 = point<1,T>;
|
|
|
|
template <typename T> using point2 = point<2,T>;
|
|
|
|
template <typename T> using point3 = point<3,T>;
|
|
|
|
template <typename T> using point4 = point<4,T>;
|
|
|
|
|
|
|
|
template <size_t S> using pointi = point<S,int>;
|
|
|
|
template <size_t S> using pointf = point<S,float>;
|
|
|
|
|
|
|
|
typedef point1<float> point1f;
|
|
|
|
typedef point2<float> point2f;
|
|
|
|
typedef point3<float> point3f;
|
|
|
|
typedef point4<float> point4f;
|
|
|
|
|
|
|
|
typedef point2<double> point2d;
|
|
|
|
typedef point3<double> point3d;
|
|
|
|
typedef point4<double> point4d;
|
|
|
|
|
|
|
|
typedef point1<unsigned> point1u;
|
|
|
|
typedef point2<unsigned> point2u;
|
|
|
|
typedef point3<unsigned> point3u;
|
|
|
|
typedef point4<unsigned> point4u;
|
|
|
|
|
|
|
|
typedef point2<int> point2i;
|
|
|
|
typedef point3<int> point3i;
|
|
|
|
typedef point4<int> point4i;
|
|
|
|
|
|
|
|
|
2017-05-24 15:15:40 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-06-01 15:29:24 +10:00
|
|
|
// distance operators
|
2017-05-24 15:15:40 +10:00
|
|
|
|
|
|
|
/// computes the exact euclidean distance between two points.
|
2015-06-01 15:29:24 +10:00
|
|
|
template <size_t S, typename T, typename U>
|
2018-02-28 11:49:13 +11:00
|
|
|
typename std::common_type<T,U>::type
|
|
|
|
distance (point<S,T> a, point<S,U> b)
|
|
|
|
{
|
|
|
|
using type_t = typename std::common_type<T,U>::type;
|
|
|
|
static_assert (std::is_floating_point<type_t>::value,
|
|
|
|
"sqrt likely requires fractional types");
|
|
|
|
|
|
|
|
return std::sqrt (distance2 (a, b));
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:29:24 +10:00
|
|
|
|
2017-05-24 15:15:40 +10:00
|
|
|
/// computes the squared euclidean distance between two points.
|
|
|
|
///
|
|
|
|
/// useful if you just need to compare distances because it avoids a sqrt
|
|
|
|
/// operation.
|
2015-06-01 15:29:24 +10:00
|
|
|
template <size_t S, typename T, typename U>
|
2018-02-28 11:49:13 +11:00
|
|
|
constexpr typename std::common_type<T,U>::type
|
|
|
|
distance2 (point<S,T> a, point<S,U> b)
|
|
|
|
{
|
2018-04-09 12:30:22 +10:00
|
|
|
return sum (pow (a - b, 2));
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
2015-06-01 15:29:24 +10:00
|
|
|
|
2017-05-24 15:15:40 +10:00
|
|
|
/// computes the octile distance between two points. that is, the shortest
|
|
|
|
/// distance between `a' and `b' where travel is only allowed beween the 8
|
|
|
|
/// grid neighbours and cost for diagonals is proportionally larger than
|
|
|
|
/// cardinal movement. see also: chebyshev.
|
2015-10-06 15:20:06 +11:00
|
|
|
template <typename T, typename U>
|
2018-02-28 11:49:13 +11:00
|
|
|
typename std::common_type<T,U>::type
|
|
|
|
octile (point<2,T> a, point<2,U> b)
|
|
|
|
{
|
|
|
|
using type_t = typename std::common_type<T,U>::type;
|
|
|
|
static_assert (!std::is_integral<type_t>::value,
|
|
|
|
"octile requires more than integer precision");
|
|
|
|
|
|
|
|
const type_t D1 = 1;
|
|
|
|
const type_t D2 = std::sqrt (type_t {2});
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto diff = cruft::abs (a - b);
|
2018-02-28 11:49:13 +11:00
|
|
|
|
|
|
|
// distance for axis-aligned walks
|
|
|
|
auto axis = D1 * (diff.x + diff.y);
|
|
|
|
|
|
|
|
// the savings from diagonal walks
|
2018-08-05 14:42:02 +10:00
|
|
|
auto diag = (D2 - 2 * D1) * cruft::min (diff);
|
2018-02-28 11:49:13 +11:00
|
|
|
|
|
|
|
return axis + diag;
|
|
|
|
}
|
|
|
|
|
2015-09-22 18:32:11 +10:00
|
|
|
|
2017-05-24 15:15:40 +10:00
|
|
|
/// computes the manhattan distance between two points. that is, the
|
|
|
|
/// distance where travel is only allowed along cardinal directions.
|
2015-06-01 15:29:24 +10:00
|
|
|
template <size_t S, typename T, typename U>
|
2018-02-28 11:49:13 +11:00
|
|
|
constexpr typename std::common_type<T,U>::type
|
|
|
|
manhattan (point<S,T> a, point<S,U> b)
|
|
|
|
{
|
|
|
|
return sum (abs (a - b));
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:29:24 +10:00
|
|
|
|
2017-05-24 15:15:40 +10:00
|
|
|
/// computes the cheyvshev distance between two points. that is, the
|
|
|
|
/// shortest distance between `a' and `b' where travel is only allowed
|
|
|
|
/// between the 8 grid neighbours and cost for diagonals is the same as
|
|
|
|
/// cardinal movement. see also: octile.
|
2015-06-01 15:29:24 +10:00
|
|
|
template <size_t S, typename T, typename U>
|
2018-02-28 11:49:13 +11:00
|
|
|
constexpr typename std::common_type<T,U>::type
|
|
|
|
chebyshev (point<S,T> a, point<S,U> b)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
return cruft::max (abs (a - b));
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
2015-06-01 15:29:24 +10:00
|
|
|
|
2015-07-24 01:34:44 +10:00
|
|
|
|
2018-04-18 21:44:36 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// returns the most distant pair of points in a set
|
|
|
|
//
|
|
|
|
// performance has no guarantees. in fact it's probably spectacularly slow.
|
|
|
|
//
|
|
|
|
// especially given we have nothing to accelerate lookups with. if you
|
|
|
|
// want it to be fast it may be an idea to construct a bounding volume and
|
|
|
|
// pass those vertices instead.
|
|
|
|
template <size_t S, typename T>
|
|
|
|
std::pair<
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point<S,T>,
|
|
|
|
cruft::point<S,T>
|
2018-04-18 21:44:36 +10:00
|
|
|
>
|
2018-08-05 14:42:02 +10:00
|
|
|
furthest (cruft::view<const cruft::point<S,T>*>);
|
2018-04-26 16:30:16 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// computes the mean point across a view of points
|
|
|
|
template <typename InputT>
|
|
|
|
auto
|
2018-08-05 14:42:02 +10:00
|
|
|
center (cruft::view<InputT> points)
|
2018-04-26 16:30:16 +10:00
|
|
|
{
|
|
|
|
CHECK_NEZ (points.size ());
|
|
|
|
|
|
|
|
using point_type = typename std::iterator_traits<InputT>::value_type;
|
|
|
|
using value_type = typename point_type::value_type;
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::vector<point_type::elements,value_type> accum = 0;
|
2018-04-26 16:30:16 +10:00
|
|
|
|
|
|
|
for (auto const &i: points)
|
2018-08-05 14:42:02 +10:00
|
|
|
accum += i.template as<cruft::vector> ();
|
2018-04-26 16:30:16 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
return (accum / points.size ()).template as<cruft::point> ();
|
2018-04-26 16:30:16 +10:00
|
|
|
}
|
2011-08-15 20:11:20 +10:00
|
|
|
}
|
2011-06-23 22:04:51 +10:00
|
|
|
|
|
|
|
#endif // __UTIL_POINT_HPP
|