point: operate on value types, not references

This commit is contained in:
Danny Robson 2015-04-15 14:14:01 +10:00
parent 71d8a30769
commit 012f8107c6
3 changed files with 25 additions and 22 deletions

View File

@ -22,16 +22,15 @@
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
using namespace std; using util::point;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
util::vector<S,T> util::vector<S,T>
util::point<S,T>::to (const point<S,T> &rhs) const util::point<S,T>::to (point<S,T> rhs) const
{ {
util::vector<S,T> out; util::vector<S,T> out;
for (size_t i = 0; i < S; ++i) for (size_t i = 0; i < S; ++i)
out.data[i] = rhs.data[i] - this->data[i]; out.data[i] = rhs.data[i] - this->data[i];
return out; return out;
@ -41,7 +40,7 @@ util::point<S,T>::to (const point<S,T> &rhs) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
util::vector<S,T> util::vector<S,T>
util::point<S,T>::from (const point<S,T> &rhs) const util::point<S,T>::from (point<S,T> rhs) const
{ {
util::vector<S,T> out; util::vector<S,T> out;
for (size_t i = 0; i < S; ++i) for (size_t i = 0; i < S; ++i)
@ -53,10 +52,11 @@ util::point<S,T>::from (const point<S,T> &rhs) const
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
void void
util::point<S,T>::sanity (void) const { util::point<S,T>::sanity (void) const
CHECK (std::all_of (begin (this->data), {
end (this->data), CHECK (std::all_of (this->begin (),
[] (double i) { return !std::isnan (i); })); this->end (),
[] (auto i) { return !std::isnan (i); }));
} }
@ -68,7 +68,7 @@ const util::point<S,T> util::point<S,T>::ORIGIN (T {0});
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
std::ostream& std::ostream&
util::operator<< (std::ostream &os, const util::point<S,T> &p) { util::operator<< (std::ostream &os, util::point<S,T> p) {
os << "point" << S << "("; os << "point" << S << "(";
os << p.data[0]; os << p.data[0];
@ -81,9 +81,9 @@ util::operator<< (std::ostream &os, const util::point<S,T> &p) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#define INSTANTIATE_S_T(S,T) \ #define INSTANTIATE_S_T(S,T) \
template struct util::point<S,T>; \ template struct util::point<S,T>; \
template std::ostream& util::operator<< (std::ostream &os, const util::point<S,T>&); \ template std::ostream& util::operator<< (std::ostream &os, util::point<S,T>); \
#define INSTANTIATE(T) \ #define INSTANTIATE(T) \
INSTANTIATE_S_T(1,T) \ INSTANTIATE_S_T(1,T) \

View File

@ -34,12 +34,12 @@ namespace util {
using coord::base<S,T,util::point,coord::xyzw>::base; using coord::base<S,T,util::point,coord::xyzw>::base;
// point operators // point operators
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 distance (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 distance2 (point<S,U>) const;
template <typename U> typename std::common_type<T,U>::type manhattan (const point<S,U> &) const; template <typename U> typename std::common_type<T,U>::type manhattan (point<S,U>) const;
vector<S,T> to (const point&) const; vector<S,T> to (point) const;
vector<S,T> from (const point&) const; vector<S,T> from (point) const;
template <size_t D> point<D,T> homog (void) const; template <size_t D> point<D,T> homog (void) const;
@ -50,7 +50,7 @@ namespace util {
// iostream operators // iostream operators
template <size_t S, typename T> template <size_t S, typename T>
std::ostream& operator<< (std::ostream&, const point<S,T>&); std::ostream& operator<< (std::ostream&, point<S,T>);
// Convenience typedefs // Convenience typedefs
typedef point<2,float> point2f; typedef point<2,float> point2f;
@ -69,7 +69,7 @@ namespace util {
namespace std { namespace std {
template <size_t S, typename T> template <size_t S, typename T>
struct hash<util::point<S,T>> { struct hash<util::point<S,T>> {
size_t operator() (const util::point<S,T> &p) const { size_t operator() (util::point<S,T> p) const {
std::hash<T> h; std::hash<T> h;
size_t k = 0; size_t k = 0;

View File

@ -23,7 +23,8 @@ namespace util {
template <size_t S, typename T> template <size_t S, typename T>
template <typename U> template <typename U>
typename std::common_type<T,U>::type typename std::common_type<T,U>::type
util::point<S,T>::distance (const point<S,U> &rhs) const { util::point<S,T>::distance (point<S,U> rhs) const
{
return std::sqrt (distance2 (rhs)); return std::sqrt (distance2 (rhs));
} }
@ -32,7 +33,8 @@ namespace util {
template <size_t S, typename T> template <size_t S, typename T>
template <typename U> template <typename U>
typename std::common_type<T,U>::type typename std::common_type<T,U>::type
util::point<S,T>::distance2 (const point<S,U> &rhs) const { util::point<S,T>::distance2 (point<S,U> rhs) const
{
typedef typename std::common_type<T,U>::type result_t; typedef typename std::common_type<T,U>::type result_t;
result_t sum { 0 }; result_t sum { 0 };
@ -48,7 +50,8 @@ namespace util {
template <size_t S, typename T> template <size_t S, typename T>
template <typename U> template <typename U>
typename std::common_type<T,U>::type typename std::common_type<T,U>::type
util::point<S,T>::manhattan (const point<S,U> &rhs) const { util::point<S,T>::manhattan (point<S,U> rhs) const
{
typedef typename std::common_type<T,U>::type result_t; typedef typename std::common_type<T,U>::type result_t;
result_t sum { 0 }; result_t sum { 0 };