File indexing completed on 2024-05-12 15:23:45
0001 /* 0002 SPDX-FileCopyrightText: 2012 Andrew Stepanenko 0003 0004 Modified by Jasem Mutlaq <mutlaqja@ikarustech.com> for KStars: 0005 SPDX-FileCopyrightText: 2012 Jasem Mutlaq <mutlaqja@ikarustech.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #pragma once 0011 0012 #include <cmath> 0013 0014 namespace GuiderUtils 0015 { 0016 class Vector 0017 { 0018 public: 0019 double x, y, z; 0020 Vector() 0021 { 0022 x = y = z = 0.0; 0023 }; 0024 explicit Vector(double v) 0025 { 0026 x = y = z = v; 0027 }; 0028 Vector(const Vector &v) 0029 { 0030 x = v.x; 0031 y = v.y; 0032 z = v.z; 0033 }; 0034 Vector(double vx, double vy, double vz) 0035 { 0036 x = vx; 0037 y = vy; 0038 z = vz; 0039 }; 0040 ~Vector() = default; 0041 0042 Vector &operator=(const Vector &v) 0043 { 0044 x = v.x; 0045 y = v.y; 0046 z = v.z; 0047 return *this; 0048 }; 0049 Vector &operator=(double f) 0050 { 0051 x = y = z = f; 0052 return *this; 0053 }; 0054 Vector operator-() const; 0055 Vector &operator+=(const Vector &); 0056 Vector &operator-=(const Vector &); 0057 Vector &operator*=(const Vector &); 0058 Vector &operator*=(double); 0059 Vector &operator/=(double); 0060 0061 friend Vector operator+(const Vector &, const Vector &); 0062 friend Vector operator-(const Vector &, const Vector &); 0063 friend Vector operator*(const Vector &, const Vector &); 0064 friend Vector operator*(double, const Vector &); 0065 friend Vector operator*(const Vector &, double); 0066 friend Vector operator/(const Vector &, double); 0067 friend Vector operator/(const Vector &, const Vector &); 0068 friend double operator&(const Vector &u, const Vector &v) 0069 { 0070 return u.x * v.x + u.y * v.y + u.z * v.z; 0071 }; 0072 friend Vector operator^(const Vector &, const Vector &); 0073 double operator!() const 0074 { 0075 return (double)sqrt(x * x + y * y + z * z); 0076 }; 0077 double &operator[](int n) 0078 { 0079 return *(&x + n); 0080 }; 0081 int operator<(double v) 0082 { 0083 return x < v && y < v && z < v; 0084 }; 0085 int operator>(double v) 0086 { 0087 return x > v && y > v && z > v; 0088 }; 0089 }; 0090 0091 inline Vector Vector ::operator-() const 0092 { 0093 return Vector(-x, -y, -z); 0094 } 0095 0096 inline Vector operator+(const Vector &u, const Vector &v) 0097 { 0098 return Vector(u.x + v.x, u.y + v.y, u.z + v.z); 0099 } 0100 0101 inline Vector operator-(const Vector &u, const Vector &v) 0102 { 0103 return Vector(u.x - v.x, u.y - v.y, u.z - v.z); 0104 } 0105 0106 inline Vector operator*(const Vector &u, const Vector &v) 0107 { 0108 return Vector(u.x * v.x, u.y * v.y, u.z * v.z); 0109 } 0110 0111 inline Vector operator*(const Vector &u, double f) 0112 { 0113 return Vector(u.x * f, u.y * f, u.z * f); 0114 } 0115 0116 inline Vector operator*(double f, const Vector &v) 0117 { 0118 return Vector(f * v.x, f * v.y, f * v.z); 0119 } 0120 0121 inline Vector operator/(const Vector &v, double f) 0122 { 0123 return Vector(v.x / f, v.y / f, v.z / f); 0124 } 0125 0126 inline Vector operator/(const Vector &u, const Vector &v) 0127 { 0128 return Vector(u.x / v.x, u.y / v.y, u.z / v.z); 0129 } 0130 0131 inline Vector &Vector ::operator+=(const Vector &v) 0132 { 0133 x += v.x; 0134 y += v.y; 0135 z += v.z; 0136 return *this; 0137 } 0138 0139 inline Vector &Vector ::operator-=(const Vector &v) 0140 { 0141 x -= v.x; 0142 y -= v.y; 0143 z -= v.z; 0144 return *this; 0145 } 0146 0147 inline Vector &Vector ::operator*=(double v) 0148 { 0149 x *= v; 0150 y *= v; 0151 z *= v; 0152 return *this; 0153 } 0154 0155 inline Vector &Vector ::operator*=(const Vector &v) 0156 { 0157 x *= v.x; 0158 y *= v.y; 0159 z *= v.z; 0160 return *this; 0161 } 0162 0163 inline Vector &Vector ::operator/=(double v) 0164 { 0165 x /= v; 0166 y /= v; 0167 z /= v; 0168 return *this; 0169 } 0170 0171 inline Vector Normalize(const Vector &v) 0172 { 0173 return v / !v; 0174 }; 0175 Vector RndVector(); 0176 Vector &Clip(Vector &); 0177 0178 } // namespace GuiderUtils