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