00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __SGL_VEC4_HPP
00026 #define __SGL_VEC4_HPP
00027
00028 #include <sgl.h>
00029
00030 template <class T> class sglVec3;
00031 template <class T> class sglMat4;
00032
00033
00034 template <class T>
00035 class sglVec4
00036 {
00037 public:
00038
00039 sglVec4() {};
00040
00041 sglVec4(T x, T y, T z, T w)
00042 {
00043 m_vector[0] = x; m_vector[1] = y;
00044 m_vector[2] = z; m_vector[3] = w;
00045 }
00046
00047 template <class S>
00048 sglVec4(const sglVec4<S>& vec)
00049 {
00050 m_vector[0] = (T)vec[0]; m_vector[1] = (T)vec[1];
00051 m_vector[2] = (T)vec[2]; m_vector[3] = (T)vec[3];
00052 }
00053
00054
00055 ~sglVec4() {};
00056
00057 inline T *getPtr() const { return (T *)m_vector; }
00058
00059
00060 inline T& operator[](unsigned int i) { return m_vector[i]; }
00061 inline const T& operator[](unsigned int i) const { return m_vector[i]; }
00062
00063
00064 template <class S>
00065 inline void set(S x, S y, S z, S w)
00066 {
00067 m_vector[0] = (T)x; m_vector[1] = (T)y;
00068 m_vector[2] = (T)z; m_vector[3] = (T)w;
00069 }
00070
00071
00072 template <class S>
00073 inline void set(sglVec3<S>& vec, S s = (S)1)
00074 {
00075 m_vector[0] = (T)vec[0]; m_vector[1] = (T)vec[1];
00076 m_vector[2] = (T)vec[2]; m_vector[3] = (T)s;
00077 }
00078
00079
00080 template <class S>
00081 inline sglVec4 &operator=(const sglVec4<S>& vec)
00082 {
00083 m_vector[0] = (T)vec[0]; m_vector[1] = (T)vec[1];
00084 m_vector[2] = (T)vec[2]; m_vector[3] = (T)vec[3];
00085 return *this;
00086 }
00087
00088
00089 template <class S>
00090 inline bool operator==(const sglVec4<S>& vec) const
00091 {
00092 return (m_vector[0] == (T)vec[0] && m_vector[1] == (T)vec[1] &&
00093 m_vector[2] == (T)vec[2] && m_vector[3] == (T)vec[3]);
00094 }
00095
00096 template <class S>
00097 inline bool operator!=(const sglVec4<S>& vec) const
00098 {
00099 return (m_vector[0] != (T)vec[0] || m_vector[1] != (T)vec[1] ||
00100 m_vector[2] != (T)vec[2] || m_vector[3] != (T)vec[3]);
00101 }
00102
00103 template <class S>
00104 inline bool operator<(const sglVec4<S>& vec) const
00105 {
00106 return (dot(*this) < vec.dot(vec));
00107 }
00108 template <class S>
00109 inline bool operator>(const sglVec4<S>& vec) const
00110 {
00111 return (dot(*this) > vec.dot(vec));
00112 }
00113
00114
00115 template <class S>
00116 inline sglVec4& operator+=(const sglVec4<S>& vec)
00117 {
00118 m_vector[0] += (T)vec[0]; m_vector[1] += (T)vec[1];
00119 m_vector[2] += (T)vec[2]; m_vector[3] += (T)vec[3];
00120 return *this;
00121 }
00122 template <class S>
00123 inline sglVec4& operator-=(const sglVec4<S>& vec)
00124 {
00125 m_vector[0] -= (T)vec[0]; m_vector[1] -= (T)vec[1];
00126 m_vector[2] -= (T)vec[2]; m_vector[3] -= (T)vec[3];
00127 return *this;
00128 }
00129 inline sglVec4& operator*=(const T s)
00130 {
00131 m_vector[0]*=s; m_vector[1]*=s; m_vector[2]*=s; m_vector[3]*=s;
00132 return *this;
00133 }
00134
00135 template <class S>
00136 inline const sglVec4<T> operator+(const sglVec4<S> &rhs) const
00137 {
00138 return sglVec4(m_vector[0]+(T)rhs[0],
00139 m_vector[1]+(T)rhs[1],
00140 m_vector[2]+(T)rhs[2],
00141 m_vector[3]+(T)rhs[3]);
00142 }
00143 template <class S>
00144 inline const sglVec4<T> operator-(const sglVec4<S> &rhs) const
00145 {
00146 return sglVec4(m_vector[0]-(T)rhs[0],
00147 m_vector[1]-(T)rhs[1],
00148 m_vector[2]-(T)rhs[2],
00149 m_vector[3]-(T)rhs[3]);
00150 }
00151 inline const sglVec4<T> operator*(T scalar) const
00152 {
00153 return sglVec4(m_vector[0]*scalar,
00154 m_vector[1]*scalar,
00155 m_vector[2]*scalar,
00156 m_vector[3]*scalar);
00157 }
00158
00159
00160 template <class S1, class S2>
00161 inline void add(const sglVec4<S1>& vec1, const sglVec4<S2>& vec2)
00162 {
00163 m_vector[0] = (T)vec1[0] + (T)vec2[0];
00164 m_vector[1] = (T)vec1[1] + (T)vec2[1];
00165 m_vector[2] = (T)vec1[2] + (T)vec2[2];
00166 m_vector[3] = (T)vec1[3] + (T)vec2[3];
00167 }
00168 template <class S1, class S2>
00169 inline void sub(const sglVec4<S1>& vec1, const sglVec4<S2>& vec2)
00170 {
00171 m_vector[0] = (T)vec1[0] - (T)vec2[0];
00172 m_vector[1] = (T)vec1[1] - (T)vec2[1];
00173 m_vector[2] = (T)vec1[2] - (T)vec2[2];
00174 m_vector[3] = (T)vec1[3] - (T)vec2[3];
00175 }
00176 template <class S>
00177 inline void scale(const sglVec4<S>& vec, T s)
00178 {
00179 m_vector[0] = (T)vec[0]*s; m_vector[1] = (T)vec[1]*s;
00180 m_vector[2] = (T)vec[2]*s; m_vector[3] = (T)vec[3]*s;
00181 }
00182
00183
00184 template <class S>
00185 inline bool almostEqual(const sglVec4<S>& vec, double eps) const
00186 {
00187 return (SGL_ALMOST_EQUAL(m_vector[0], vec[0], eps) &&
00188 SGL_ALMOST_EQUAL(m_vector[1], vec[1], eps) &&
00189 SGL_ALMOST_EQUAL(m_vector[2], vec[2], eps) &&
00190 SGL_ALMOST_EQUAL(m_vector[3], vec[3], eps));
00191 }
00192
00193
00194 template <class S>
00195 inline T dot(const sglVec4<S>& vec) const
00196 {
00197 return (m_vector[0]*(T)vec[0] + m_vector[1]*(T)vec[1] +
00198 m_vector[2]*(T)vec[2] + m_vector[3]*(T)vec[3]);
00199 }
00200
00201 template <class S, class R>
00202 inline static R dot(const sglVec4<R>& vec1, const sglVec4<S>& vec2)
00203 {
00204 return (vec1[0]*(R)vec2[0] + vec1[1]*(R)vec2[1] +
00205 vec1[2]*(R)vec2[2] + vec1[3]*(R)vec2[3]);
00206 }
00207
00208 inline T length() const
00209 {
00210 return (T) sqrt(dot(*this));
00211 }
00212
00213 template <class S>
00214 inline T distance(const sglVec4<S>& vec) const
00215 {
00216 return (T) sqrt(((T)vec[0] - m_vector[0])*((T)vec[0] - m_vector[0]) +
00217 ((T)vec[1] - m_vector[1])*((T)vec[1] - m_vector[1]) +
00218 ((T)vec[2] - m_vector[2])*((T)vec[2] - m_vector[2]) +
00219 ((T)vec[3] - m_vector[3])*((T)vec[3] - m_vector[3]));
00220 }
00221
00222 inline T normalize()
00223 {
00224 T len = length();
00225 if (len) *this *= (1.0/len);
00226 return len;
00227 }
00228
00229 template <class S>
00230 inline T normalize(const sglVec4<S>& vec)
00231 {
00232 T len = vec.length();
00233 if (len) scale(vec, 1.0/len);
00234 return len;
00235 }
00236
00237
00238 #if defined(TEMPLATE_OSTREAM_HACK)
00239 template <class S>
00240 friend ostream& operator<<(ostream& ostrm, const sglVec4<S>& vec);
00241
00242 template <class S>
00243 friend istream& operator>>(istream& istrm, sglVec4<S>& vec);
00244 #else
00245 friend ostream& operator<<(ostream& ostrm, const sglVec4<T>& vec)
00246 {
00247 return (ostrm << "(" << vec[0] << ", " << vec[1] << ", "
00248 << vec[2] << ", " << vec[3] << ")");
00249 }
00250
00251 friend istream& operator>>(istream& istrm, sglVec4<T>& vec)
00252 {
00253 return (istrm >> vec[0] >> vec[1] >> vec[2] >> vec[3]);
00254 }
00255 #endif
00256 private:
00257 T m_vector[4];
00258 };
00259
00260
00261 #if defined(TEMPLATE_OSTREAM_HACK)
00262 template <class T>
00263 inline ostream& operator<<(ostream& ostrm, const sglVec4<T>& vec)
00264 {
00265 return (ostrm << "(" << vec[0] << ", " << vec[1] << ", "
00266 << vec[2] << ", " << vec[3] << ")");
00267 }
00268
00269 template <class T>
00270 inline istream& operator>>(istream& istrm, sglVec4<T>& vec)
00271 {
00272 return (istrm >> vec[0] >> vec[1] >> vec[2] >> vec[3]);
00273 }
00274 #endif
00275
00276
00277 #endif