2011-06-23 22:04:51 +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/>.
|
|
|
|
*
|
2014-02-18 15:28:28 +11:00
|
|
|
* Copyright 2011-14 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"
|
2012-05-18 17:56:24 +10:00
|
|
|
#include "detail/coord.hpp"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <iostream>
|
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.
|
2014-12-15 20:10:56 +11:00
|
|
|
template <size_t S, typename T>
|
|
|
|
struct point : public detail::coord<S,T> {
|
2012-05-18 17:56:24 +10:00
|
|
|
static_assert (S > 0, "point dimensions must be strictly positive.");
|
|
|
|
|
|
|
|
point ();
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename... U>
|
|
|
|
point (U ...u): detail::coord<S,T> {std::forward<U> (u)...} { ; }
|
2011-06-23 22:04:51 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// point operators
|
2014-12-19 14:59:16 +11:00
|
|
|
template <typename U> typename std::common_type<T,U>::type distance (const point<S,U> &) const;
|
|
|
|
template <typename U> typename std::common_type<T,U>::type distance2 (const point<S,U> &) const;
|
|
|
|
template <typename U> typename std::common_type<T,U>::type manhattan (const point<S,U> &) const;
|
2011-09-07 21:51:13 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
vector<S,T> to (const point&) const;
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// arithetic operators
|
|
|
|
point<S,T>& operator*= (T);
|
|
|
|
point<S,T> operator* (T) const;
|
|
|
|
point<S,T> operator- (const point<S,T>&) const;
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
point<S,T> operator- (const vector<S,T>&) const;
|
|
|
|
point<S,T>& operator-= (const vector<S,T>&);
|
|
|
|
point<S,T> operator+ (const vector<S,T>&) const;
|
|
|
|
point<S,T>& operator+= (const vector<S,T>&);
|
2011-10-18 21:45:55 +11:00
|
|
|
|
2014-12-19 14:59:50 +11:00
|
|
|
// logical operators
|
|
|
|
bool operator== (const point<S,T>&) const;
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <size_t D> point<D,T> redim (void) const;
|
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
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// free maths operators
|
|
|
|
template <size_t S, typename T> point<S,T> operator* (const vector<S,T>&, const point<S,T>&);
|
|
|
|
template <size_t S, typename T> point<S,T> operator* (const point<S,T>&, const vector<S,T>&);
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// iostream operators
|
|
|
|
template <size_t S, typename T>
|
|
|
|
std::ostream& operator<< (std::ostream&, const point<S,T>&);
|
2012-05-26 18:02:11 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// Convenience typedefs
|
|
|
|
typedef point<2,float> point2f;
|
|
|
|
typedef point<3,float> point3f;
|
|
|
|
|
|
|
|
typedef point<2,double> point2d;
|
|
|
|
typedef point<3,double> point3d;
|
2014-12-18 19:43:13 +11:00
|
|
|
|
|
|
|
typedef point<2,size_t> point2u;
|
2011-08-15 20:11:20 +10:00
|
|
|
}
|
2011-06-23 22:04:51 +10:00
|
|
|
|
2014-12-19 15:00:33 +11:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template <>
|
|
|
|
template <size_t S, typename T>
|
|
|
|
struct hash<util::point<S,T>> {
|
|
|
|
size_t operator() (const util::point<S,T> &p) const {
|
|
|
|
std::hash<T> h;
|
|
|
|
|
|
|
|
size_t k = 0;
|
|
|
|
for (size_t i = 0; i < S; ++i)
|
|
|
|
k = h (p.data[i] ^ k);
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:28:28 +11:00
|
|
|
#include "point.ipp"
|
2011-06-23 22:04:51 +10:00
|
|
|
|
|
|
|
#endif // __UTIL_POINT_HPP
|