coord: move free maths func from point to vector

This commit is contained in:
Danny Robson 2015-01-28 14:59:33 +11:00
parent 50b0bc37b2
commit 6abfd252d9
5 changed files with 44 additions and 51 deletions

View File

@ -26,6 +26,22 @@
#include <iterator> #include <iterator>
//-----------------------------------------------------------------------------
// HACK: point multiplication isn't defined, but it's way more convenient than
// casting between vector/coord
namespace util {
template <typename T>
util::point2f
operator* (T a, util::point2f b)
{
return {
a * b.data[0],
a * b.data[1]
};
}
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S> template <size_t S>
util::bezier<S>::bezier (const util::point2f (&_points)[S+1]) util::bezier<S>::bezier (const util::point2f (&_points)[S+1])
@ -65,7 +81,7 @@ namespace util {
auto v0 = pow2 (1 - t) * m_points[0]; auto v0 = pow2 (1 - t) * m_points[0];
auto v1 = 2 * (1 - t) * t * m_points[1]; auto v1 = 2 * (1 - t) * t * m_points[1];
auto v2 = pow2 (t) * m_points[2]; auto v2 = pow2 (t) * m_points[2];
return { return {
v0.x + v1.x + v2.x, v0.x + v1.x + v2.x,
@ -84,10 +100,10 @@ namespace util {
CHECK_GE (t, 0); CHECK_GE (t, 0);
CHECK_LE (t, 1); CHECK_LE (t, 1);
auto v0 = pow (1 - t, 3) * m_points[0]; auto v0 = pow (1 - t, 3) * m_points[0];
auto v1 = 3 * pow2 (1 - t) * t * m_points[1]; auto v1 = 3 * pow2 (1 - t) * t * m_points[1];
auto v2 = 3 * pow2 (1 - t) * t * m_points[2]; auto v2 = 3 * pow2 (1 - t) * t * m_points[2];
auto v3 = pow (t, 3) * m_points[3]; auto v3 = pow (t, 3) * m_points[3];
return { return {
v0.x + v1.x + v2.x + v3.x, v0.x + v1.x + v2.x + v3.x,

View File

@ -159,32 +159,6 @@ template <size_t S, typename T>
const util::point<S,T> util::point<S,T>::ORIGIN (T {0}); const util::point<S,T> util::point<S,T>::ORIGIN (T {0});
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::point<S,T>
util::operator* (const vector<S,T> &v, const point<S,T> &p) {
point<S,T> out;
for (size_t i = 0; i < S; ++i)
out.data[i] = p.data[i] + v.data[i];
return out;
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::point<S,T>
util::operator* (const point<S,T> &p, const vector<S,T> &v)
{ return v * p; }
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::point<S,T>
util::operator* (T a, const point<S,T> &b)
{ return b * a; }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
std::ostream& std::ostream&
@ -204,9 +178,6 @@ 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, const util::point<S,T>&); \
template util::point<S,T> util::operator* (const point<S,T>&, const vector<S,T>&); \
template util::point<S,T> util::operator* (const vector<S,T>&, const point<S,T>&); \
template util::point<S,T> util::operator* (T, const point<S,T>&);
#define INSTANTIATE(T) \ #define INSTANTIATE(T) \
INSTANTIATE_S_T(1,T) \ INSTANTIATE_S_T(1,T) \

View File

@ -46,7 +46,7 @@ namespace util {
point<S,T> operator* (T) const; point<S,T> operator* (T) const;
point<S,T> operator/ (T) const; point<S,T> operator/ (T) const;
vector<S,T> operator- (const point<S,T>&) const; vector<S,T> operator- (const point<S,T>&) const;
point<S,T> operator- (const vector<S,T>&) const; 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>&);
@ -66,11 +66,6 @@ namespace util {
void sanity (void) const; void sanity (void) const;
}; };
// 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>&);
template <size_t S, typename T> point<S,T> operator* (T, const point<S,T>&);
// 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&, const point<S,T>&);

View File

@ -230,6 +230,7 @@ util::vector<S,T>::magnitude (void) const {
} }
//-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
T T
util::vector<S,T>::magnitude2 (void) const { util::vector<S,T>::magnitude2 (void) const {
@ -353,24 +354,30 @@ util::vector<S,T>::sanity (void) const {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T, typename U>
util::vector<S,T> util::vector<S,T>
util::operator* (T a, const util::vector<S,T> &b) util::operator* (U a, const util::vector<S,T> &b)
{ return b * a; } {
return b * T(a);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T, typename U>
util::vector<S,T> util::vector<S,T>
util::operator+ (T a, const util::vector<S,T> &b) util::operator+ (U a, const util::vector<S,T> &b)
{ return b + a; } {
return b + T(a);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T, typename U>
util::vector<S,T> util::vector<S,T>
util::operator- (T a, const util::vector<S,T> &b) util::operator- (U a, const util::vector<S,T> &b)
{ return a + (-b); } {
return a + (-b);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -407,7 +414,9 @@ util::operator>> (const json::node &node, util::vector<S,T> &v) {
#define INSTANTIATE_S_T(S,T) \ #define INSTANTIATE_S_T(S,T) \
template struct util::vector<S,T>; \ template struct util::vector<S,T>; \
template util::vector<S,T> util::operator* (T, const util::vector<S,T>&); \ template util::vector<S,T> util::operator* (int, const util::vector<S,T>&); \
template util::vector<S,T> util::operator* (unsigned, const util::vector<S,T>&); \
template util::vector<S,T> util::operator* (float, const util::vector<S,T>&); \
template util::vector<S,T> util::operator+ (T, const util::vector<S,T>&); \ template util::vector<S,T> util::operator+ (T, const util::vector<S,T>&); \
template util::vector<S,T> util::operator- (T, const util::vector<S,T>&); \ template util::vector<S,T> util::operator- (T, const util::vector<S,T>&); \
template std::ostream& util::operator<< (std::ostream&, const util::vector<S,T> &v);\ template std::ostream& util::operator<< (std::ostream&, const util::vector<S,T> &v);\

View File

@ -72,9 +72,11 @@ namespace util {
vector<S,T>& normalise (void); vector<S,T>& normalise (void);
vector<S,T> normalised [[gnu::warn_unused_result]] (void) const; vector<S,T> normalised [[gnu::warn_unused_result]] (void) const;
// size operations
template <size_t D> vector<D,T> redim (void) const; template <size_t D> vector<D,T> redim (void) const;
template <size_t D> vector<D,T> redim (const util::vector<D,T> &fill) const; template <size_t D> vector<D,T> redim (const util::vector<D,T> &fill) const;
// constants
static const vector<S,T> ZERO; static const vector<S,T> ZERO;
void sanity (void) const; void sanity (void) const;
@ -87,9 +89,9 @@ namespace util {
template <typename T> vector<3,T> spherical_to_cartesian (const vector<3,T>&); template <typename T> vector<3,T> spherical_to_cartesian (const vector<3,T>&);
template <typename T> vector<3,T> cartesian_to_spherical (const vector<3,T>&); template <typename T> vector<3,T> cartesian_to_spherical (const vector<3,T>&);
template <size_t S, typename T> vector<S,T> operator* (T, const vector<S,T>&); template <size_t S, typename T, typename U> vector<S,T> operator* (U, const vector<S,T>&);
template <size_t S, typename T> vector<S,T> operator+ (T, const vector<S,T>&); template <size_t S, typename T, typename U> vector<S,T> operator+ (U, const vector<S,T>&);
template <size_t S, typename T> vector<S,T> operator- (T, const vector<S,T>&); template <size_t S, typename T, typename U> vector<S,T> operator- (U, const vector<S,T>&);
// output and serialisation operators // output and serialisation operators
template <size_t S, typename T> std::ostream& operator<< (std::ostream&, const vector<S,T>&); template <size_t S, typename T> std::ostream& operator<< (std::ostream&, const vector<S,T>&);