csgeom/vector3.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1998,1999,2000 by Jorrit Tyberghein 00003 Largely rewritten by Ivan Avramovic <ivan@avramovic.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 #ifndef __CS_VECTOR3_H__ 00021 #define __CS_VECTOR3_H__ 00022 00029 #ifndef __CS_CSSYSDEFS_H__ 00030 #error "cssysdef.h must be included in EVERY source file!" 00031 #endif 00032 00033 #include "csextern.h" 00034 00035 #include "csgeom/math3d_d.h" 00036 00040 class CS_CSGEOM_EXPORT csVector3 00041 { 00042 public: 00043 union 00044 { 00045 float f[3]; 00046 struct 00047 { 00049 float x; 00051 float y; 00053 float z; 00054 }; 00055 }; 00056 00062 csVector3 () {} 00063 00069 csVector3 (float m) : x(m), y(m), z(m) {} 00070 00072 csVector3 (float ix, float iy, float iz = 0) : x(ix), y(iy), z(iz) {} 00073 00075 csVector3 (const csVector3& v) : x(v.x), y(v.y), z(v.z) {} 00076 00078 csVector3 (const csDVector3&); 00079 00081 inline friend csVector3 operator+ (const csVector3& v1, const csVector3& v2) 00082 { return csVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); } 00083 00085 inline friend csDVector3 operator+ (const csDVector3& v1, const csVector3& v2) 00086 { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); } 00087 00089 inline friend csDVector3 operator+ (const csVector3& v1, const csDVector3& v2) 00090 { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); } 00091 00093 inline friend csVector3 operator- (const csVector3& v1, const csVector3& v2) 00094 { return csVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); } 00095 00097 inline friend csDVector3 operator- (const csVector3& v1, const csDVector3& v2) 00098 { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); } 00099 00101 inline friend csDVector3 operator- (const csDVector3& v1, const csVector3& v2) 00102 { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); } 00103 00105 inline friend float operator* (const csVector3& v1, const csVector3& v2) 00106 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; } 00107 00109 inline friend csVector3 operator% (const csVector3& v1, const csVector3& v2) 00110 { 00111 return csVector3 (v1.y*v2.z-v1.z*v2.y, 00112 v1.z*v2.x-v1.x*v2.z, 00113 v1.x*v2.y-v1.y*v2.x); 00114 } 00115 00117 void Cross (const csVector3 & px, const csVector3 & py) 00118 { 00119 x = px.y*py.z - px.z*py.y; 00120 y = px.z*py.x - px.x*py.z; 00121 z = px.x*py.y - px.y*py.x; 00122 } 00123 00125 inline friend csVector3 operator* (const csVector3& v, float f) 00126 { return csVector3(v.x*f, v.y*f, v.z*f); } 00127 00129 inline friend csVector3 operator* (float f, const csVector3& v) 00130 { return csVector3(v.x*f, v.y*f, v.z*f); } 00131 00133 inline friend csDVector3 operator* (const csVector3& v, double f) 00134 { return csDVector3(v) * f; } 00135 00137 inline friend csDVector3 operator* (double f, const csVector3& v) 00138 { return csDVector3(v) * f; } 00139 00141 inline friend csVector3 operator* (const csVector3& v, int f) 00142 { return v * (float)f; } 00143 00145 inline friend csVector3 operator* (int f, const csVector3& v) 00146 { return v * (float)f; } 00147 00149 inline friend csVector3 operator/ (const csVector3& v, float f) 00150 { f = 1.0f/f; return csVector3(v.x*f, v.y*f, v.z*f); } 00151 00153 inline friend csDVector3 operator/ (const csVector3& v, double f) 00154 { return csDVector3(v) / f; } 00155 00157 inline friend csVector3 operator/ (const csVector3& v, int f) 00158 { return v / (float)f; } 00159 00161 inline friend bool operator== (const csVector3& v1, const csVector3& v2) 00162 { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; } 00163 00165 inline friend bool operator!= (const csVector3& v1, const csVector3& v2) 00166 { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; } 00167 00169 inline friend csVector3 operator>> (const csVector3& v1, const csVector3& v2) 00170 { return v2*(v1*v2)/(v2*v2); } 00171 00173 inline friend csVector3 operator<< (const csVector3& v1, const csVector3& v2) 00174 { return v1*(v1*v2)/(v1*v1); } 00175 00177 inline friend bool operator< (const csVector3& v, float f) 00178 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; } 00179 00181 inline friend bool operator> (float f, const csVector3& v) 00182 { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; } 00183 00185 inline float operator[] (int n) const { return !n?x:n&1?y:z; } 00186 00188 inline float & operator[] (int n) { return !n?x:n&1?y:z; } 00189 00191 inline csVector3& operator+= (const csVector3& v) 00192 { 00193 x += v.x; 00194 y += v.y; 00195 z += v.z; 00196 00197 return *this; 00198 } 00199 00201 inline csVector3& operator-= (const csVector3& v) 00202 { 00203 x -= v.x; 00204 y -= v.y; 00205 z -= v.z; 00206 00207 return *this; 00208 } 00209 00211 inline csVector3& operator*= (float f) 00212 { x *= f; y *= f; z *= f; return *this; } 00213 00215 inline csVector3& operator/= (float f) 00216 { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; } 00217 00219 inline csVector3 operator+ () const { return *this; } 00220 00222 inline csVector3 operator- () const { return csVector3(-x,-y,-z); } 00223 00225 inline void Set (float sx, float sy, float sz) { x = sx; y = sy; z = sz; } 00226 00228 inline void Set (const csVector3& v) { x = v.x; y = v.y; z = v.z; } 00229 00231 float Norm () const; 00232 00234 float SquaredNorm () const 00235 { return x * x + y * y + z * z; } 00236 00242 csVector3 Unit () const { return (*this)/(this->Norm()); } 00243 00245 inline static float Norm (const csVector3& v) { return v.Norm(); } 00246 00248 inline static csVector3 Unit (const csVector3& v) { return v.Unit(); } 00249 00251 void Normalize (); 00252 00254 inline bool IsZero (float precision = SMALL_EPSILON) const 00255 { return (ABS(x) < precision) && (ABS(y) < precision) 00256 && (ABS(z) < precision); 00257 } 00258 }; 00259 00262 #endif // __CS_VECTOR3_H__
Generated for Crystal Space by doxygen 1.2.18