2011-06-23 22:04:51 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-06-23 22:04:51 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-06-23 22:04:51 +10:00
|
|
|
*
|
2016-03-11 12:48:19 +11:00
|
|
|
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
|
2011-06-23 22:04:51 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_POINT_HPP
|
|
|
|
#define __UTIL_POINT_HPP
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
#include "vector.hpp"
|
2015-03-06 01:15:52 +11:00
|
|
|
#include "coord.hpp"
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2014-12-19 14:59:16 +11:00
|
|
|
#include <type_traits>
|
2011-10-18 21:45:55 +11:00
|
|
|
|
2011-08-15 20:11:20 +10:00
|
|
|
namespace util {
|
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>
|
2015-04-09 17:47:35 +10:00
|
|
|
struct point : public coord::base<S,T,point,coord::xyzw>
|
2015-03-06 01:09:37 +11:00
|
|
|
{
|
2015-04-09 17:47:35 +10:00
|
|
|
using coord::base<S,T,util::point,coord::xyzw>::base;
|
2011-06-23 22:04:51 +10:00
|
|
|
|
2015-04-15 14:14:01 +10:00
|
|
|
vector<S,T> to (point) const;
|
|
|
|
vector<S,T> from (point) const;
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2015-02-19 13:19:27 +11:00
|
|
|
template <size_t D> point<D,T> homog (void) const;
|
|
|
|
|
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
|
|
|
|
point<S,T> origin (void);
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
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
|
|
|
|
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>
|
2015-10-06 15:20:06 +11:00
|
|
|
typename std::common_type<T,U>::type distance (point<S,T>, point<S,U>);
|
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>
|
2015-09-15 21:06:43 +10:00
|
|
|
constexpr typename std::common_type<T,U>::type distance2 (point<S,T>, point<S,U>);
|
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>
|
|
|
|
typename std::common_type<T,U>::type octile (point<2,T>, point<2,U>);
|
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>
|
2015-09-15 21:06:43 +10:00
|
|
|
constexpr typename std::common_type<T,U>::type manhattan (point<S,T>, point<S,U>);
|
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>
|
2015-09-15 21:06:43 +10:00
|
|
|
constexpr typename std::common_type<T,U>::type chebyshev (point<S,T>, point<S,U>);
|
2015-06-01 15:29:24 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// Convenience typedefs
|
2015-09-21 15:26:57 +10:00
|
|
|
template <typename T> using point1 = point<1,T>;
|
2015-07-24 01:34:44 +10:00
|
|
|
template <typename T> using point2 = point<2,T>;
|
|
|
|
template <typename T> using point3 = point<3,T>;
|
|
|
|
template <typename T> using point4 = point<4,T>;
|
2014-12-15 20:10:56 +11:00
|
|
|
|
2016-10-25 17:46:36 +11:00
|
|
|
template <size_t S> using pointi = point<S,int>;
|
2015-10-06 15:19:06 +11:00
|
|
|
template <size_t S> using pointf = point<S,float>;
|
|
|
|
|
2015-09-21 15:26:57 +10:00
|
|
|
typedef point1<float> point1f;
|
2015-07-24 01:34:44 +10:00
|
|
|
typedef point2<float> point2f;
|
|
|
|
typedef point3<float> point3f;
|
|
|
|
typedef point4<float> point4f;
|
2014-12-18 19:43:13 +11:00
|
|
|
|
2015-07-24 01:34:44 +10:00
|
|
|
typedef point2<double> point2d;
|
|
|
|
typedef point3<double> point3d;
|
|
|
|
typedef point4<double> point4d;
|
|
|
|
|
2016-10-25 17:46:36 +11:00
|
|
|
typedef point1<unsigned> point1u;
|
|
|
|
typedef point2<unsigned> point2u;
|
|
|
|
typedef point3<unsigned> point3u;
|
|
|
|
typedef point4<unsigned> point4u;
|
2015-07-24 01:34:44 +10:00
|
|
|
|
2016-10-25 17:46:36 +11:00
|
|
|
typedef point2<int> point2i;
|
|
|
|
typedef point3<int> point3i;
|
|
|
|
typedef point4<int> point4i;
|
2011-08-15 20:11:20 +10:00
|
|
|
}
|
2011-06-23 22:04:51 +10:00
|
|
|
|
2014-02-18 15:28:28 +11:00
|
|
|
#include "point.ipp"
|
2011-06-23 22:04:51 +10:00
|
|
|
|
|
|
|
#endif // __UTIL_POINT_HPP
|