point: templatise distance functions

This commit is contained in:
Danny Robson 2014-12-19 14:59:16 +11:00
parent 67b03fd10f
commit 1bb4c752f1
3 changed files with 48 additions and 38 deletions

View File

@ -37,41 +37,6 @@ util::point<S,T>::point ()
{ ; } { ; }
//-----------------------------------------------------------------------------
template <size_t S, typename T>
T
util::point<S,T>::distance (const point<S,T> &other) const {
// TODO: this should not truncate on integral types
return static_cast<T> (
std::sqrt (distance2 (other))
);
}
template <size_t S, typename T>
T
util::point<S,T>::distance2 (const point<S,T> &other) const {
T total { 0 };
for (size_t i = 0; i < S; ++i)
total += pow2 (this->data[i] - other.data[i]);
return total;
}
template <size_t S, typename T>
T
util::point<S,T>::manhattan (const point<S,T> &other) const {
T total { 0 };
for (size_t i = 0; i < S; ++i)
total += ::abs (this->data[i] - other.data[i]);
return total;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
util::point<S,T>& util::point<S,T>&

View File

@ -26,6 +26,7 @@
#include <array> #include <array>
#include <initializer_list> #include <initializer_list>
#include <iostream> #include <iostream>
#include <type_traits>
namespace util { namespace util {
/// An n-dimensional position in space. /// An n-dimensional position in space.
@ -39,9 +40,9 @@ namespace util {
point (U ...u): detail::coord<S,T> {std::forward<U> (u)...} { ; } point (U ...u): detail::coord<S,T> {std::forward<U> (u)...} { ; }
// point operators // point operators
T distance (const point &) const; template <typename U> typename std::common_type<T,U>::type distance (const point<S,U> &) const;
T distance2 (const point &) const; template <typename U> typename std::common_type<T,U>::type distance2 (const point<S,U> &) const;
T manhattan (const point &) const; template <typename U> typename std::common_type<T,U>::type manhattan (const point<S,U> &) const;
vector<S,T> to (const point&) const; vector<S,T> to (const point&) const;

View File

@ -17,9 +17,53 @@
* Copyright 2014 Danny Robson <danny@nerdcruft.net> * Copyright 2014 Danny Robson <danny@nerdcruft.net>
*/ */
#include "maths.hpp"
#include <algorithm> #include <algorithm>
namespace util { namespace util {
//-------------------------------------------------------------------------
template <size_t S, typename T>
template <typename U>
typename std::common_type<T,U>::type
util::point<S,T>::distance (const point<S,U> &rhs) const {
return std::sqrt (distance2 (rhs));
}
//-------------------------------------------------------------------------
template <size_t S, typename T>
template <typename U>
typename std::common_type<T,U>::type
util::point<S,T>::distance2 (const point<S,U> &rhs) const {
typedef typename std::common_type<T,U>::type result_t;
result_t sum { 0 };
for (size_t i = 0; i < S; ++i)
sum += pow2 (this->data[i] - rhs.data[i]);
return sum;
}
//-------------------------------------------------------------------------
template <size_t S, typename T>
template <typename U>
typename std::common_type<T,U>::type
util::point<S,T>::manhattan (const point<S,U> &rhs) const {
typedef typename std::common_type<T,U>::type result_t;
result_t sum { 0 };
for (size_t i = 0; i < S; ++i)
sum += std::abs (this->data[i] - rhs.data[i]);
return sum;
}
//-------------------------------------------------------------------------
template<size_t S, typename T> template<size_t S, typename T>
template<size_t D> template<size_t D>
point<D,T> point<S,T>::redim (void) const { point<D,T> point<S,T>::redim (void) const {