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 #include "Tools/Streams/Streamable.h" 00014 00015 /** This class represents a 3-vector */ 00016 template <class V> class Vector3 : public Streamable{ 00017 public: 00018 virtual void serialize(In* in, Out* out) 00019 { 00020 STREAM_REGISTER_BEGIN(); 00021 STREAM( x); 00022 STREAM( y); 00023 STREAM( z); 00024 STREAM_REGISTER_FINISH(); 00025 } 00026 /** The vector values */ 00027 V x,y,z; 00028 00029 /** Default constructor 4 gcc. */ 00030 Vector3<V>():x(0),y(0),z(0) 00031 { 00032 } 00033 00034 /** Default constructor. */ 00035 Vector3<V>(V x, V y, V z):x(x),y(y),z(z) 00036 { 00037 } 00038 00039 /** Assignment operator 00040 *\param other The other vector that is assigned to this one 00041 *\return A reference to this object after the assignment. 00042 */ 00043 Vector3<V>& operator=(const Vector3<V>& other) 00044 { 00045 x = other.x; 00046 y = other.y; 00047 z = other.z; 00048 return *this; 00049 } 00050 00051 /** Copy constructor 00052 *\param other The other vector that is copied to this one 00053 */ 00054 Vector3<V>(const Vector3<V>& other):Streamable() {*this = other;} 00055 00056 /** Addition of another vector to this one. 00057 *\param other The other vector that will be added to this one 00058 *\return A reference to this object after the calculation. 00059 */ 00060 Vector3<V>& operator+=(const Vector3<V>& other) 00061 { 00062 x += other.x; 00063 y += other.y; 00064 z += other.z; 00065 return *this; 00066 } 00067 00068 /** Substraction of this vector from another one. 00069 *\param other The other vector this one will be substracted from 00070 *\return A reference to this object after the calculation. 00071 */ 00072 Vector3<V>& operator-=(const Vector3<V>& other) 00073 { 00074 x -= other.x; 00075 y -= other.y; 00076 z -= other.z; 00077 return *this; 00078 } 00079 00080 /** Multiplication of this vector by a factor. 00081 *\param factor The factor this vector is multiplied by 00082 *\return A reference to this object after the calculation. 00083 */ 00084 Vector3<V>& operator*=(const V& factor) 00085 { 00086 x *= factor; 00087 y *= factor; 00088 z *= factor; 00089 return *this; 00090 } 00091 00092 /** Division of this vector by a factor. 00093 *\param factor The factor this vector is divided by 00094 *\return A reference to this object after the calculation. 00095 */ 00096 Vector3<V>& operator/=(const V& factor) 00097 { 00098 if (factor == 0) return *this; 00099 x /= factor; 00100 y /= factor; 00101 z /= factor; 00102 return *this; 00103 } 00104 00105 /** Addition of another vector to this one. 00106 *\param other The other vector that will be added to this one 00107 *\return A new object that contains the result of the calculation. 00108 */ 00109 Vector3<V> operator+(const Vector3<V>& other) const 00110 {return Vector3<V>(*this) += other;} 00111 00112 /** Subtraction of another vector to this one. 00113 *\param other The other vector that will be added to this one 00114 *\return A new object that contains the result of the calculation. 00115 */ 00116 Vector3<V> operator-(const Vector3<V>& other) const 00117 {return Vector3<V>(*this) -= other;} 00118 00119 /** Inner product of this vector and another one. 00120 *\param other The other vector this one will be multiplied by 00121 *\return The inner product. 00122 */ 00123 V operator*(const Vector3<V>& other) const 00124 { 00125 return (x*other.x + y*other.y + z*other.z); 00126 } 00127 00128 /** Multiplication of this vector by a factor. 00129 *\param factor The factor this vector is multiplied by 00130 *\return A new object that contains the result of the calculation. 00131 */ 00132 Vector3<V> operator*(const V& factor) const 00133 {return Vector3<V>(*this) *= factor;} 00134 00135 /** Division of this vector by a factor. 00136 * 00137 *\param factor The factor this vector is divided by 00138 *\return A new object that contains the result of the calculation. 00139 */ 00140 Vector3<V> operator/(const V& factor) const 00141 {return Vector3<V>(*this) /= factor;} 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 equal. 00146 */ 00147 bool operator==(const Vector3<V>& other) const 00148 { 00149 return (x==other.x && y==other.y && z==other.z); 00150 } 00151 00152 /** Comparison of another vector with this one. 00153 *\param other The other vector that will be compared to this one 00154 *\return Whether the two vectors are unequal. 00155 */ 00156 bool operator!=(const Vector3<V>& other) const 00157 {return !(*this == other);} 00158 00159 00160 /** 00161 * array-like member access. 00162 * \param i index of coordinate 00163 * \return reference to x, y or z 00164 */ 00165 V& operator[](int i) 00166 { 00167 return (&x)[i]; 00168 } 00169 00170 /** Calculation of the length of this vector. 00171 *\return The length. 00172 */ 00173 V abs() const 00174 {return (V) sqrt(double((*this) * (*this)));} 00175 00176 /** Crossproduct of this vector and another vector. 00177 *\param other The factor this vector is multiplied with. 00178 *\return A new object that contains the result of the calculation. 00179 */ 00180 Vector3<V> operator^(const Vector3<V>& other) 00181 {return Vector3<V>(y * other.z - z * other.y, 00182 z * other.x - x * other.z, 00183 x * other.y - y * other.x);} 00184 00185 /** Crossproduct of this vector and another vector. 00186 *\param other The factor this vector is multiplied with. 00187 *\return A reference to this object after the calculation. 00188 */ 00189 Vector3<V>& operator^=(const Vector3<V>& other) 00190 {*this = *this ^ other; return *this;} 00191 00192 /** normalize this vector. 00193 *\param len The length, the vector should be normalized to, default=1. 00194 *\return the normalized vector. 00195 */ 00196 Vector3<V> normalize(V len) 00197 { 00198 V lenghtOfVector = abs(); 00199 if (lenghtOfVector == 0) return *this; 00200 return *this = (*this * len) / lenghtOfVector; 00201 } 00202 00203 /** normalize this vector. 00204 *\return the normalized vector. 00205 */ 00206 Vector3<V> normalize() 00207 { 00208 V lenghtOfVector = abs(); 00209 if (lenghtOfVector == 0) return *this; 00210 return *this /= lenghtOfVector; 00211 } 00212 }; 00213 00214 /** 00215 * Streaming operator that reads a Vector3<V> from a stream. 00216 * @param stream The stream from which is read. 00217 * @param vector3 The Vector3<V> object. 00218 * @return The stream. 00219 */ 00220 /* 00221 template <class V> In& operator>>(In& stream, Vector3<V>& vector3) 00222 { 00223 stream >> vector3.x; 00224 stream >> vector3.y; 00225 stream >> vector3.z; 00226 return stream; 00227 } 00228 */ 00229 /** 00230 * Streaming operator that writes a Vector3<V> to a stream. 00231 * @param stream The stream to write on. 00232 * @param vector3 The Vector3<V> object. 00233 * @return The stream. 00234 */ 00235 /* 00236 template <class V> Out& operator<<(Out& stream, const Vector3<V>& vector3) 00237 { 00238 stream << vector3.x; 00239 stream << vector3.y; 00240 stream << vector3.z; 00241 return stream; 00242 } 00243 */ 00244 #endif // __Vector3_h__ 00245
1.3.6