00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef GENLIB2_VECTOR3_INCLUDED
00022 #define GENLIB2_VECTOR3_INCLUDED
00023
00024 #include <iostream>
00025
00026
00027
00028 using namespace std;
00029
00030
00031
00032
00033 namespace GenLib2
00034 {
00035
00036 template<class T>
00037 class Vector3
00038 {
00039 public:
00040
00041 Vector3() { x=0; y=0; z=0; }
00042 Vector3(T s) { init(s); }
00043 Vector3(T x, T y, T z) { init(x,y,z); }
00044 Vector3(const Vector3& that) { init(that.x,that.y,that.z); }
00045
00046 Vector3& init(T X, T Y, T Z) { x=X; y=Y; z=Z; return *this; }
00047 Vector3& init(T s) { x=s; y=s; z=s; return *this; }
00048
00049 Vector3& operator=(const Vector3& a) {
00050 return init((x=a.x),(y=a.y),(z=a.z));
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 Vector3& operator+=(const Vector3& a) {
00062 x += a.x; y += a.y; z += a.z;
00063 return *this;
00064 }
00065
00066 Vector3& operator-=(const Vector3& a) {
00067 x -= a.x; y -= a.y; z -= a.z;
00068 return *this;
00069 }
00070
00071 Vector3& operator*=(const Vector3& a) {
00072 x *= a.x; y *= a.y; z *= a.z;
00073 return *this;
00074 }
00075
00076 Vector3& operator/=(const Vector3& a) {
00077 x /= a.x; y /= a.y; z /= a.z;
00078 return *this;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 T x, y, z;
00108 };
00109
00110
00111
00112
00113
00114
00115 template<class T> inline
00116 Vector3<T> operator+(const Vector3<T>& a, const Vector3<T>& b)
00117 {
00118 Vector3<T> tmp(a.x + b.x, a.y + b.y, a.z + b.z);
00119 return tmp;
00120 }
00121
00122 template<class T> inline
00123 Vector3<T> operator-(const Vector3<T>& a, const Vector3<T>& b)
00124 {
00125 Vector3<T> tmp(a.x - b.x, a.y - b.y, a.z - b.z);
00126 return tmp;
00127 }
00128
00129 template<class T> inline
00130 Vector3<T> operator*(const Vector3<T>& a, const Vector3<T>& b)
00131 {
00132 Vector3<T> tmp(a.x * b.x, a.y * b.y, a.z * b.z);
00133 return tmp;
00134 }
00135
00136 template<class T> inline
00137 Vector3<T> operator/(const Vector3<T>& a, const Vector3<T>& b)
00138 {
00139 Vector3<T> tmp(a.x / b.x, a.y / b.y, a.z / b.z);
00140 return tmp;
00141 }
00142
00143
00144
00145
00146 template<class T> inline
00147 Vector3<T> operator*(const Vector3<T>& a, T s)
00148 {
00149 Vector3<T> tmp(a.x * s, a.y * s, a.z * s);
00150 return tmp;
00151 }
00152
00153 template<class T> inline
00154 Vector3<T> operator/(const Vector3<T>& a, T s)
00155 {
00156 Vector3<T> tmp(a.x / s, a.y / s, a.z / s);
00157 return tmp;
00158 }
00159
00160
00161
00162
00163 template<class T> inline
00164 bool operator==(const Vector3<T>& a, const Vector3<T>& b)
00165 {
00166 return (a.x == b.x && a.y == b.y && a.z == b.z);
00167 }
00168
00169 template<class T> inline
00170 bool operator!=(const Vector3<T>& a, const Vector3<T>& b)
00171 {
00172 return (a.x != b.x || a.y != b.y || a.z != b.z);
00173 }
00174
00175
00176
00177
00178 template<class T> inline
00179 istream& operator>>(istream& s, Vector3<T>& a)
00180 {
00181 T x = 0, y = 0, z = 0;
00182 char c = 0;
00183
00184 s >> c;
00185 if (c == '(')
00186 {
00187 s >> x >> c;
00188 if (c == ',') s >> y >> c;
00189 if (c == ',') s >> z >> c;
00190 if (c != ')') s.clear(ios::badbit);
00191 }
00192 else
00193 {
00194 s.putback(c);
00195 s >> x >> y >> z;
00196 }
00197
00198 if (s) a = Vector3<T>(x, y, z);
00199 return s;
00200 }
00201
00202
00203
00204
00205 template<class T> inline
00206 ostream& operator<<(ostream& s, const Vector3<T>& a)
00207 {
00208 return s << "( " << a.x << ", " << a.y << ", " << a.z << ")";
00209 }
00210
00211
00212
00213
00214
00215
00216 template<class T> inline
00217 T abs2(const Vector3<T>& a)
00218 {
00219 return (a.x * a.x + a.y * a.y + a.z * a.z);
00220 }
00221
00222 template<class T> inline
00223 T abs(const Vector3<T>& a)
00224 {
00225 return sqrt(abs2(a));
00226 }
00227
00228
00229
00230
00231 template<class T> inline
00232 T dot(const Vector3<T>& a, const Vector3<T>& b)
00233 {
00234 return (a.x * b.x + a.y * b.y + a.z * b.z);
00235 }
00236
00237
00238
00239
00240 template<class T> inline
00241 Vector3<T> cross(const Vector3<T>& a, const Vector3<T>& b)
00242 {
00243 Vector3<T> tmp(
00244 a.y * b.z - a.z * b.y,
00245 -(a.x * b.z - a.z * b.x),
00246 a.x * b.y - a.y * b.x);
00247 return tmp;
00248 }
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 }
00288
00289 #endif
00290