00001 /** 00002 * @file Vector3.h 00003 * Contains template class Vector3 of type V 00004 * 00005 * @author <a href="mailto:martin.kallnik@gmx.de" > Martin Kallnik</a> 00006 * @author Max Risler 00007 */ 00008 00009 #ifndef __Vector3_h__ 00010 #define __Vector3_h__ 00011 00012 #include "Tools/Streams/InOut.h" 00013 00014 /** This class represents a 3-vector */ 00015 template <class V> class Vector3 { 00016 public: 00017 /** The vector values */ 00018 V x,y,z; 00019 00020 /** Default constructor 4 gcc. */ 00021 Vector3<V>():x(0),y(0),z(0) 00022 { 00023 } 00024 00025 /** Default constructor. */ 00026 Vector3<V>(V x, V y, V z):x(x),y(y),z(z) 00027 { 00028 } 00029 00030 /** Assignment operator 00031 *\param other The other vector that is assigned to this one 00032 *\return A reference to this object after the assignment. 00033 */ 00034 Vector3<V>& operator=(const Vector3<V>& other) 00035 { 00036 x = other.x; 00037 y = other.y; 00038 z = other.z; 00039 return *this; 00040 } 00041 00042 /** Copy constructor 00043 *\param other The other vector that is copied to this one 00044 */ 00045 Vector3<V>(const Vector3<V>& other) {*this = other;} 00046 00047 /** Addition of another vector to this one. 00048 *\param other The other vector that will be added to this one 00049 *\return A reference to this object after the calculation. 00050 */ 00051 Vector3<V>& operator+=(const Vector3<V>& other) 00052 { 00053 x += other.x; 00054 y += other.y; 00055 z += other.z; 00056 return *this; 00057 } 00058 00059 /** Substraction of this vector from another one. 00060 *\param other The other vector this one will be substracted from 00061 *\return A reference to this object after the calculation. 00062 */ 00063 Vector3<V>& operator-=(const Vector3<V>& other) 00064 { 00065 x -= other.x; 00066 y -= other.y; 00067 z -= other.z; 00068 return *this; 00069 } 00070 00071 /** Multiplication of this vector by a factor. 00072 *\param factor The factor this vector is multiplied by 00073 *\return A reference to this object after the calculation. 00074 */ 00075 Vector3<V>& operator*=(const V& factor) 00076 { 00077 x *= factor; 00078 y *= factor; 00079 z *= factor; 00080 return *this; 00081 } 00082 00083 /** Division of this vector by a factor. 00084 *\param factor The factor this vector is divided by 00085 *\return A reference to this object after the calculation. 00086 */ 00087 Vector3<V>& operator/=(const V& factor) 00088 { 00089 if (factor == 0) return *this; 00090 x /= factor; 00091 y /= factor; 00092 z /= factor; 00093 return *this; 00094 } 00095 00096 /** Addition of another vector to this one. 00097 *\param other The other vector that will be added to this one 00098 *\return A new object that contains the result of the calculation. 00099 */ 00100 Vector3<V> operator+(const Vector3<V>& other) const 00101 {return Vector3<V>(*this) += other;} 00102 00103 /** Subtraction of another vector to this one. 00104 *\param other The other vector that will be added to this one 00105 *\return A new object that contains the result of the calculation. 00106 */ 00107 Vector3<V> operator-(const Vector3<V>& other) const 00108 {return Vector3<V>(*this) -= other;} 00109 00110 /** Inner product of this vector and another one. 00111 *\param other The other vector this one will be multiplied by 00112 *\return The inner product. 00113 */ 00114 V operator*(const Vector3<V>& other) const 00115 { 00116 return (x*other.x + y*other.y + z*other.z); 00117 } 00118 00119 /** Multiplication of this vector by a factor. 00120 *\param factor The factor this vector is multiplied by 00121 *\return A new object that contains the result of the calculation. 00122 */ 00123 Vector3<V> operator*(const V& factor) const 00124 {return Vector3<V>(*this) *= factor;} 00125 00126 /** Division of this vector by a factor. 00127 * 00128 *\param factor The factor this vector is divided by 00129 *\return A new object that contains the result of the calculation. 00130 */ 00131 Vector3<V> operator/(const V& factor) const 00132 {return Vector3<V>(*this) /= factor;} 00133 00134 /** Comparison of another vector with this one. 00135 *\param other The other vector that will be compared to this one 00136 *\return Whether the two vectors are equal. 00137 */ 00138 bool operator==(const Vector3<V>& other) const 00139 { 00140 return (x==other.x && y==other.y && z==other.z); 00141 } 00142 00143 /** Comparison of another vector with this one. 00144 *\param other The other vector that will be compared to this one 00145 *\return Whether the two vectors are unequal. 00146 */ 00147 bool operator!=(const Vector3<V>& other) const 00148 {return !(*this == other);} 00149 00150 00151 /** 00152 * array-like member access. 00153 * \param i index of coordinate 00154 * \return reference to x, y or z 00155 */ 00156 V& operator[](int i) 00157 { 00158 return (&x)[i]; 00159 } 00160 00161 /** Calculation of the length of this vector. 00162 *\return The length. 00163 */ 00164 V abs() const 00165 {return (V) sqrt(double((*this) * (*this)));} 00166 00167 /** Crossproduct of this vector and another vector. 00168 *\param other The factor this vector is multiplied with. 00169 *\return A new object that contains the result of the calculation. 00170 */ 00171 Vector3<V> operator^(const Vector3<V>& other) 00172 {return Vector3<V>(y * other.z - z * other.y, 00173 z * other.x - x * other.z, 00174 x * other.y - y * other.x);} 00175 00176 /** Crossproduct of this vector and another vector. 00177 *\param other The factor this vector is multiplied with. 00178 *\return A reference to this object after the calculation. 00179 */ 00180 Vector3<V>& operator^=(const Vector3<V>& other) 00181 {*this = *this ^ other; return *this;} 00182 00183 /** normalize this vector. 00184 *\param len The length, the vector should be normalized to, default=1. 00185 *\return the normalized vector. 00186 */ 00187 Vector3<V> normalize(V len) 00188 { 00189 if (abs() == 0) return *this; 00190 return *this = (*this * len) / abs(); 00191 } 00192 00193 /** normalize this vector. 00194 *\return the normalized vector. 00195 */ 00196 Vector3<V> normalize() 00197 { 00198 if (abs() == 0) return *this; 00199 return *this /= abs(); 00200 } 00201 }; 00202 00203 /** 00204 * Streaming operator that reads a Vector3<V> from a stream. 00205 * @param stream The stream from which is read. 00206 * @param vector3 The Vector3<V> object. 00207 * @return The stream. 00208 */ 00209 template <class V> In& operator>>(In& stream, Vector3<V>& vector3) 00210 { 00211 stream >> vector3.x; 00212 stream >> vector3.y; 00213 stream >> vector3.z; 00214 return stream; 00215 } 00216 00217 /** 00218 * Streaming operator that writes a Vector3<V> to a stream. 00219 * @param stream The stream to write on. 00220 * @param vector3 The Vector3<V> object. 00221 * @return The stream. 00222 */ 00223 template <class V> Out& operator<<(Out& stream, const Vector3<V>& vector3) 00224 { 00225 stream << vector3.x; 00226 stream << vector3.y; 00227 stream << vector3.z; 00228 return stream; 00229 } 00230 00231 #endif // __Vector3_h__ 00232 00233 /* 00234 * Change log : 00235 * 00236 * $Log: Vector3.h,v $ 00237 * Revision 1.3 2004/09/09 10:15:56 spranger 00238 * fixed doxygen-errors 00239 * 00240 * Revision 1.2 2004/06/14 23:18:41 kindler 00241 * - added [] operator for direct element access 00242 * 00243 * Revision 1.1.1.1 2004/05/22 17:37:14 cvsadm 00244 * created new repository GT2004_WM 00245 * 00246 * Revision 1.4 2004/03/10 08:11:12 roefer 00247 * abs() fixed 00248 * 00249 * Revision 1.3 2003/12/05 08:04:08 jhoffman 00250 * added normalize() to Vector3 00251 * 00252 * Revision 1.2 2003/12/02 13:44:56 cesarz 00253 * added streaming operators 00254 * 00255 * Revision 1.1 2003/10/07 10:13:24 cvsadm 00256 * Created GT2004 (M.J.) 00257 * 00258 * Revision 1.1.1.1 2003/07/02 09:40:28 cvsadm 00259 * created new repository for the competitions in Padova from the 00260 * tamara CVS (Tuesday 2:00 pm) 00261 * 00262 * removed unused solutions 00263 * 00264 * Revision 1.6 2003/03/10 18:22:51 dueffert 00265 * assignments replaced with initializations 00266 * 00267 * Revision 1.5 2002/11/19 15:43:04 dueffert 00268 * doxygen comments corrected 00269 * 00270 * Revision 1.4 2002/11/12 23:00:47 dueffert 00271 * started restore greenhills compatibility 00272 * 00273 * Revision 1.3 2002/10/14 13:14:25 dueffert 00274 * doxygen comments corrected 00275 * 00276 * Revision 1.2 2002/09/22 18:40:52 risler 00277 * added new math functions, removed GTMath library 00278 * 00279 * Revision 1.1 2002/09/22 13:10:50 risler 00280 * new Math headers added 00281 * 00282 * 00283 */