diff --git a/bezier.cpp b/bezier.cpp index 5eb647f8..bec9acf8 100644 --- a/bezier.cpp +++ b/bezier.cpp @@ -26,6 +26,22 @@ #include +//----------------------------------------------------------------------------- +// HACK: point multiplication isn't defined, but it's way more convenient than +// casting between vector/coord +namespace util { + template + util::point2f + operator* (T a, util::point2f b) + { + return { + a * b.data[0], + a * b.data[1] + }; + } +} + + //----------------------------------------------------------------------------- template util::bezier::bezier (const util::point2f (&_points)[S+1]) @@ -65,7 +81,7 @@ namespace util { auto v0 = pow2 (1 - t) * m_points[0]; auto v1 = 2 * (1 - t) * t * m_points[1]; - auto v2 = pow2 (t) * m_points[2]; + auto v2 = pow2 (t) * m_points[2]; return { v0.x + v1.x + v2.x, @@ -84,10 +100,10 @@ namespace util { CHECK_GE (t, 0); 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 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 { v0.x + v1.x + v2.x + v3.x, diff --git a/point.cpp b/point.cpp index 03f6a722..3a1626e7 100644 --- a/point.cpp +++ b/point.cpp @@ -159,32 +159,6 @@ template const util::point util::point::ORIGIN (T {0}); -//----------------------------------------------------------------------------- -template -util::point -util::operator* (const vector &v, const point &p) { - point out; - for (size_t i = 0; i < S; ++i) - out.data[i] = p.data[i] + v.data[i]; - - return out; -} - - -//----------------------------------------------------------------------------- -template -util::point -util::operator* (const point &p, const vector &v) - { return v * p; } - - -//----------------------------------------------------------------------------- -template -util::point -util::operator* (T a, const point &b) - { return b * a; } - - //----------------------------------------------------------------------------- template std::ostream& @@ -204,9 +178,6 @@ util::operator<< (std::ostream &os, const util::point &p) { #define INSTANTIATE_S_T(S,T) \ template struct util::point; \ template std::ostream& util::operator<< (std::ostream &os, const util::point&); \ -template util::point util::operator* (const point&, const vector&); \ -template util::point util::operator* (const vector&, const point&); \ -template util::point util::operator* (T, const point&); #define INSTANTIATE(T) \ INSTANTIATE_S_T(1,T) \ diff --git a/point.hpp b/point.hpp index f12be728..897de0ee 100644 --- a/point.hpp +++ b/point.hpp @@ -46,7 +46,7 @@ namespace util { point operator* (T) const; point operator/ (T) const; - vector operator- (const point&) const; + vector operator- (const point&) const; point operator- (const vector&) const; point& operator-= (const vector&); @@ -66,11 +66,6 @@ namespace util { void sanity (void) const; }; - // free maths operators - template point operator* (const vector&, const point&); - template point operator* (const point&, const vector&); - template point operator* (T, const point&); - // iostream operators template std::ostream& operator<< (std::ostream&, const point&); diff --git a/vector.cpp b/vector.cpp index b85dcba6..2c1c5a03 100644 --- a/vector.cpp +++ b/vector.cpp @@ -230,6 +230,7 @@ util::vector::magnitude (void) const { } +//----------------------------------------------------------------------------- template T util::vector::magnitude2 (void) const { @@ -353,24 +354,30 @@ util::vector::sanity (void) const { //----------------------------------------------------------------------------- -template +template util::vector -util::operator* (T a, const util::vector &b) - { return b * a; } +util::operator* (U a, const util::vector &b) +{ + return b * T(a); +} //----------------------------------------------------------------------------- -template +template util::vector -util::operator+ (T a, const util::vector &b) - { return b + a; } +util::operator+ (U a, const util::vector &b) +{ + return b + T(a); +} //----------------------------------------------------------------------------- -template +template util::vector -util::operator- (T a, const util::vector &b) - { return a + (-b); } +util::operator- (U a, const util::vector &b) +{ + return a + (-b); +} //----------------------------------------------------------------------------- @@ -407,7 +414,9 @@ util::operator>> (const json::node &node, util::vector &v) { #define INSTANTIATE_S_T(S,T) \ template struct util::vector; \ -template util::vector util::operator* (T, const util::vector&); \ +template util::vector util::operator* (int, const util::vector&); \ +template util::vector util::operator* (unsigned, const util::vector&); \ +template util::vector util::operator* (float, const util::vector&); \ template util::vector util::operator+ (T, const util::vector&); \ template util::vector util::operator- (T, const util::vector&); \ template std::ostream& util::operator<< (std::ostream&, const util::vector &v);\ diff --git a/vector.hpp b/vector.hpp index 8bf86c32..95b6ccb5 100644 --- a/vector.hpp +++ b/vector.hpp @@ -72,9 +72,11 @@ namespace util { vector& normalise (void); vector normalised [[gnu::warn_unused_result]] (void) const; + // size operations template vector redim (void) const; template vector redim (const util::vector &fill) const; + // constants static const vector ZERO; void sanity (void) const; @@ -87,9 +89,9 @@ namespace util { template vector<3,T> spherical_to_cartesian (const vector<3,T>&); template vector<3,T> cartesian_to_spherical (const vector<3,T>&); - template vector operator* (T, const vector&); - template vector operator+ (T, const vector&); - template vector operator- (T, const vector&); + template vector operator* (U, const vector&); + template vector operator+ (U, const vector&); + template vector operator- (U, const vector&); // output and serialisation operators template std::ostream& operator<< (std::ostream&, const vector&);