[5809313] | 1 | // vector3.h |
---|
| 2 | // |
---|
| 3 | // C++ class for 3-element vectors |
---|
| 4 | // |
---|
[f8e4c56] | 5 | // Copyright (C) 2000-2002, Mark R. Shinwell. |
---|
[d67450e] | 6 | // Copyright (C) 2002-2004,2005,2006 Olly Betts |
---|
[5809313] | 7 | // |
---|
| 8 | // This program is free software; you can redistribute it and/or modify |
---|
| 9 | // it under the terms of the GNU General Public License as published by |
---|
| 10 | // the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | // (at your option) any later version. |
---|
| 12 | // |
---|
| 13 | // This program is distributed in the hope that it will be useful, |
---|
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | // GNU General Public License for more details. |
---|
| 17 | // |
---|
| 18 | // You should have received a copy of the GNU General Public License |
---|
| 19 | // along with this program; if not, write to the Free Software |
---|
[ecbc6c18] | 20 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[5809313] | 21 | // |
---|
| 22 | |
---|
| 23 | #ifndef Vector3_h |
---|
| 24 | #define Vector3_h |
---|
| 25 | |
---|
[27b8b59] | 26 | #include <math.h> |
---|
[156dc16] | 27 | |
---|
[5809313] | 28 | class Vector3 { |
---|
[d67450e] | 29 | protected: |
---|
[4a0e6b35] | 30 | double x, y, z; |
---|
[5809313] | 31 | |
---|
| 32 | public: |
---|
[4a0e6b35] | 33 | Vector3() : x(0.0), y(0.0), z(0.0) { } |
---|
| 34 | Vector3(double a, double b, double c) : x(a), y(b), z(c) { } |
---|
[5809313] | 35 | |
---|
[d67450e] | 36 | double GetX() const { return x; } |
---|
| 37 | double GetY() const { return y; } |
---|
| 38 | double GetZ() const { return z; } |
---|
[5809313] | 39 | |
---|
[27b8b59] | 40 | double magnitude() const { |
---|
| 41 | return sqrt(x*x + y*y + z*z); |
---|
| 42 | } |
---|
| 43 | |
---|
[5809313] | 44 | void normalise(); |
---|
| 45 | |
---|
[d67450e] | 46 | void assign(double a, double b, double c) { |
---|
[4a0e6b35] | 47 | x = a; y = b; z = c; |
---|
| 48 | } |
---|
[5809313] | 49 | |
---|
[3b7a8eb] | 50 | void assign(const Vector3 &v) { |
---|
| 51 | *this = v; |
---|
| 52 | } |
---|
| 53 | |
---|
[4a0e6b35] | 54 | friend Vector3 operator-(const Vector3& o) { |
---|
| 55 | return Vector3(-o.x, -o.y, -o.z); |
---|
[156dc16] | 56 | } |
---|
[4a0e6b35] | 57 | Vector3& operator*=(double); |
---|
| 58 | Vector3& operator/=(double); |
---|
[e4ec4b4] | 59 | Vector3& operator+=(const Vector3&); |
---|
| 60 | Vector3& operator-=(const Vector3&); |
---|
[4a0e6b35] | 61 | Vector3& operator=(const Vector3& o) { |
---|
| 62 | x = o.x; y = o.y; z = o.z; |
---|
| 63 | return *this; |
---|
| 64 | } |
---|
[5809313] | 65 | |
---|
[4a0e6b35] | 66 | friend Vector3 operator*(double, const Vector3&); |
---|
| 67 | friend Vector3 operator*(const Vector3& v, double f) { |
---|
[446e096] | 68 | return f * v; |
---|
| 69 | } |
---|
[156dc16] | 70 | friend Vector3 operator*(const Vector3&, const Vector3&); // cross product |
---|
[5809313] | 71 | friend Vector3 operator+(const Vector3&, const Vector3&); |
---|
[f8e4c56] | 72 | friend Vector3 operator-(const Vector3&, const Vector3&); |
---|
[e4ec4b4] | 73 | friend bool operator==(const Vector3&, const Vector3&); |
---|
[9b30732] | 74 | friend bool operator<(const Vector3&, const Vector3&); |
---|
[5809313] | 75 | friend double dot(const Vector3&, const Vector3&); |
---|
| 76 | }; |
---|
| 77 | |
---|
[e4ec4b4] | 78 | inline bool operator==(const Vector3& a, const Vector3& b) { |
---|
| 79 | return a.x == b.x && a.y == b.y && a.z == b.z; |
---|
| 80 | } |
---|
| 81 | |
---|
[9b30732] | 82 | // So we can use Vector3 as a key in a map... |
---|
| 83 | inline bool operator<(const Vector3& a, const Vector3& b) { |
---|
| 84 | if (a.x != b.x) return a.x < b.x; |
---|
| 85 | if (a.y != b.y) return a.y < b.y; |
---|
| 86 | return a.z < b.z; |
---|
| 87 | } |
---|
| 88 | |
---|
[5809313] | 89 | #endif |
---|