From 247d47f73300bdacfe09a7887c47674343311bd3 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sat, 26 May 2012 18:02:11 +1000 Subject: [PATCH] Add more scalar operators --- point.cpp | 36 +++++++++++++++- point.hpp | 3 ++ vector.cpp | 124 +++++++++++++++++++++++++++++++++++++---------------- vector.hpp | 8 ++-- 4 files changed, 129 insertions(+), 42 deletions(-) diff --git a/point.cpp b/point.cpp index 55ccdfc9..9e733167 100644 --- a/point.cpp +++ b/point.cpp @@ -25,12 +25,15 @@ using namespace std; +#pragma GCC optimize("-O3") +//----------------------------------------------------------------------------- template util::point::point () { ; } +//----------------------------------------------------------------------------- template double util::point::distance (const util::point &other) const { @@ -49,7 +52,6 @@ util::point::distance2 (const util::point &other) const { } - template double util::point::manhattan (const util::point &other) const { @@ -61,6 +63,7 @@ util::point::manhattan (const util::point &other) const { } +//----------------------------------------------------------------------------- template util::point& util::point::operator*= (double f) { @@ -113,6 +116,7 @@ util::point::operator- (const util::point &rhs) const { } +//----------------------------------------------------------------------------- template util::vector util::point::to (const util::point &rhs) const { @@ -124,6 +128,7 @@ util::point::to (const util::point &rhs) const { } +//----------------------------------------------------------------------------- template void util::point::sanity (void) const { @@ -133,6 +138,35 @@ util::point::sanity (void) const { } +//----------------------------------------------------------------------------- +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<1> util::operator* (const vector<1>&, const point<1>&); +template util::point<2> util::operator* (const vector<2>&, const point<2>&); +template util::point<3> util::operator* (const vector<3>&, const point<3>&); + + +template +util::point +util::operator* (const point &p, const vector &v) + { return v * p; } + + +template util::point<1> util::operator* (const point<1>&, const vector<1>&); +template util::point<2> util::operator* (const point<2>&, const vector<2>&); +template util::point<3> util::operator* (const point<3>&, const vector<3>&); + + +//----------------------------------------------------------------------------- template std::ostream& util::operator<< (std::ostream &os, const util::point &p) { diff --git a/point.hpp b/point.hpp index 206d443b..48b02e35 100644 --- a/point.hpp +++ b/point.hpp @@ -59,6 +59,9 @@ namespace util { typedef point<2> point2; typedef point<3> point3; + template point operator* (const vector&, const point&); + template point operator* (const point&, const vector&); + template std::ostream& operator<< (std::ostream&, const util::point&); } diff --git a/vector.cpp b/vector.cpp index 0bd3c0d7..514fec24 100644 --- a/vector.cpp +++ b/vector.cpp @@ -28,17 +28,21 @@ #include #include +#pragma GCC optimize("-O3") +//----------------------------------------------------------------------------- using namespace util; using std::begin; using std::end; +//----------------------------------------------------------------------------- template util::vector::vector () { ; } +//----------------------------------------------------------------------------- template util::vector util::vector::operator* (double rhs) const { @@ -80,6 +84,7 @@ util::vector::operator*= (const util::vector &rhs) { } +//----------------------------------------------------------------------------- template util::vector util::vector::operator/ (double rhs) const { @@ -100,6 +105,7 @@ util::vector::operator/= (double rhs) { } +//----------------------------------------------------------------------------- template util::vector util::vector::operator+ (const util::vector &rhs) const { @@ -111,6 +117,37 @@ util::vector::operator+ (const util::vector &rhs) const { } +template +util::vector +util::vector::operator+ (double rhs) const { + util::vector out; + + for (size_t i = 0; i < S; ++i) + out.data[i] = this->data[i] + rhs; + return out; +} + + +template +util::vector& +util::vector::operator+= (const util::vector &rhs) { + for (size_t i = 0; i < S; ++i) + this->data[i] += rhs.data[i]; + + return *this; +} + + +template +util::vector& +util::vector::operator+= (double rhs) { + for (size_t i = 0; i < S; ++i) + this->data[i] += rhs; + return *this; +} + + +//----------------------------------------------------------------------------- template util::vector util::vector::operator- (void) const { @@ -164,25 +201,7 @@ util::vector::operator-= (double rhs) { } -template -util::vector& -util::vector::operator+= (const util::vector &rhs) { - for (size_t i = 0; i < S; ++i) - this->data[i] += rhs.data[i]; - - return *this; -} - - -template -util::vector& -util::vector::operator+= (double rhs) { - for (size_t i = 0; i < S; ++i) - this->data[i] += rhs; - return *this; -} - - +//----------------------------------------------------------------------------- template util::vector& util::vector::operator= (const util::vector &rhs) { @@ -203,6 +222,7 @@ util::vector::operator== (const util::vector &rhs) const { } +//----------------------------------------------------------------------------- template double util::vector::magnitude (void) const { @@ -220,16 +240,6 @@ util::vector::magnitude2 (void) const { } -template -double -util::vector::dot (const util::vector &rhs) const { - double total = 0.0; - for (size_t i = 0; i < S; ++i) - total += this->data[i] * rhs.data[i]; - return total; -} - - template util::vector& util::vector::normalise (void) { @@ -255,6 +265,26 @@ util::vector::normalised (void) const { } +//----------------------------------------------------------------------------- +template +double +util::vector::dot (const util::vector &rhs) const { + double total = 0.0; + for (size_t i = 0; i < S; ++i) + total += this->data[i] * rhs.data[i]; + return total; +} + + +util::vector<3> +util::cross (const util::vector<3> &a, const util::vector<3> &b) { + return { a.y * b.z - a.z * b.y, + a.z * b.x - a.x * b.z, + a.x * b.y - a.y * b.x }; +} + + +//----------------------------------------------------------------------------- util::vector<3> util::spherical_to_cartesian (const util::vector<3> &s) { return { @@ -277,6 +307,7 @@ util::cartesian_to_spherical (const util::vector<3> &c) { } +//----------------------------------------------------------------------------- template bool util::vector::is_zero (void) const { @@ -295,14 +326,7 @@ util::vector::sanity (void) const { } -util::vector<3> -util::cross (const util::vector<3> &a, const util::vector<3> &b) { - return { a.y * b.z - a.z * b.y, - a.z * b.x - a.x * b.z, - a.x * b.y - a.y * b.x }; -} - - +//----------------------------------------------------------------------------- template util::vector util::operator* (double a, const util::vector &b) @@ -314,6 +338,29 @@ template util::vector<2> util::operator* (double, const util::vector<2>&); template util::vector<3> util::operator* (double, const util::vector<3>&); +template +util::vector +util::operator+ (double a, const util::vector &b) + { return b + a; } + + +template util::vector<1> util::operator+ (double, const util::vector<1>&); +template util::vector<2> util::operator+ (double, const util::vector<2>&); +template util::vector<3> util::operator+ (double, const util::vector<3>&); + + +template +util::vector +util::operator- (double a, const util::vector &b) + { return a + (-b); } + + +template util::vector<1> util::operator- (double, const util::vector<1>&); +template util::vector<2> util::operator- (double, const util::vector<2>&); +template util::vector<3> util::operator- (double, const util::vector<3>&); + + +//----------------------------------------------------------------------------- template std::ostream& util::operator<< (std::ostream &os, const util::vector &v) { @@ -330,6 +377,7 @@ template std::ostream& util::operator<< (std::ostream&, const util::vector<2> &v template std::ostream& util::operator<< (std::ostream&, const util::vector<3> &v); +//----------------------------------------------------------------------------- template const json::node& util::operator>> (const json::node &node, util::vector &v) { @@ -352,11 +400,13 @@ template const json::node& util::operator>> (const json::node&, util::vector<2>& template const json::node& util::operator>> (const json::node&, util::vector<3>&); +//----------------------------------------------------------------------------- template struct util::vector<1>; template struct util::vector<2>; template struct util::vector<3>; +//----------------------------------------------------------------------------- namespace util { template <> vector<1> random (void) { util::vector<1> out; randomise (out.data); return out; } template <> vector<2> random (void) { util::vector<2> out; randomise (out.data); return out; } diff --git a/vector.hpp b/vector.hpp index 83480a05..56725c66 100644 --- a/vector.hpp +++ b/vector.hpp @@ -88,11 +88,11 @@ namespace util { typedef vector<3> vector3; - template - util::vector operator* (double, const util::vector&); + template util::vector operator* (double, const util::vector&); + template util::vector operator+ (double, const util::vector&); + template util::vector operator- (double, const util::vector&); - template - std::ostream& operator<< (std::ostream&, const util::vector&); + template std::ostream& operator<< (std::ostream&, const util::vector&); template const json::node& operator>> (const json::node&, util::vector&);