Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Vec4.h
00001 /* 00002 File: Vec4.h 00003 00004 Function: Defines a length-4 vector. 00005 00006 Author(s): Andrew Willmott 00007 00008 Copyright: (c) 1995-2001, Andrew Willmott 00009 */ 00010 00011 #ifndef __Vec4__ 00012 #define __Vec4__ 00013 00014 #include "Vec3.h" 00015 00016 00017 // --- Vec4 Class ------------------------------------------------------------- 00018 00019 class Vec4 00020 { 00021 public: 00022 00023 // Constructors 00024 00025 Vec4(); 00026 Vec4(Real x, Real y, Real z, Real w); // [x, y, z, w] 00027 Vec4(const Vec4 &v); // Copy constructor 00028 Vec4(const Vec3 &v, Real w); // Hom. 3D vector 00029 Vec4(ZeroOrOne k); 00030 Vec4(Axis k); 00031 00032 // Accessor functions 00033 00034 Int Elts() const { return(4); }; 00035 00036 Real &operator [] (Int i); 00037 const Real &operator [] (Int i) const; 00038 00039 Real *Ref() const; // Return pointer to data 00040 00041 // Assignment operators 00042 00043 Vec4 &operator = (const Vec4 &a); 00044 Vec4 &operator = (ZeroOrOne k); 00045 Vec4 &operator = (Axis k); 00046 Vec4 &operator += (const Vec4 &a); 00047 Vec4 &operator -= (const Vec4 &a); 00048 Vec4 &operator *= (const Vec4 &a); 00049 Vec4 &operator *= (Real s); 00050 Vec4 &operator /= (const Vec4 &a); 00051 Vec4 &operator /= (Real s); 00052 00053 // Comparison operators 00054 00055 Bool operator == (const Vec4 &a) const; // v == a ? 00056 Bool operator != (const Vec4 &a) const; // v != a ? 00057 00058 // Arithmetic operators 00059 00060 Vec4 operator + (const Vec4 &a) const; // v + a 00061 Vec4 operator - (const Vec4 &a) const; // v - a 00062 Vec4 operator - () const; // -v 00063 Vec4 operator * (const Vec4 &a) const; // v * a (vx * ax, ...) 00064 Vec4 operator * (Real s) const; // v * s 00065 Vec4 operator / (const Vec4 &a) const; // v / a (vx / ax, ...) 00066 Vec4 operator / (Real s) const; // v / s 00067 00068 00069 // Initialisers 00070 00071 Vec4 &MakeZero(); // Zero vector 00072 Vec4 &MakeUnit(Int i, Real k = vl_one); // kI[i] 00073 Vec4 &MakeBlock(Real k = vl_one); // All-k vector 00074 00075 Vec4 &Normalise(); // normalise vector 00076 00077 // Private... 00078 00079 protected: 00080 00081 Real elt[4]; 00082 }; 00083 00084 00085 // --- Vec operators ---------------------------------------------------------- 00086 00087 inline Vec4 operator * (Real s, const Vec4 &v); // Left mult. by s 00088 inline Real dot(const Vec4 &a, const Vec4 &b); // v . a 00089 inline Real len(const Vec4 &v); // || v || 00090 inline Real sqrlen(const Vec4 &v); // v . v 00091 inline Vec4 norm(const Vec4 &v); // v / || v || 00092 inline Void normalise(Vec4 &v); // v = norm(v) 00093 Vec4 cross(const Vec4 &a, const Vec4 &b, const Vec4 &c); 00094 // 4D cross prod. 00095 Vec3 proj(const Vec4 &v); // hom. projection 00096 00097 //std::ostream &operator << (std::ostream &s, const Vec4 &v); 00098 //std::istream &operator >> (std::istream &s, Vec4 &v); 00099 00100 inline void printVec4(const Vec4 &v); 00101 00102 00103 // --- Inlines ---------------------------------------------------------------- 00104 00105 inline Real &Vec4::operator [] (Int i) 00106 { 00107 CheckRange(i, 0, 4, "(Vec4::[i]) index out of range"); 00108 return(elt[i]); 00109 } 00110 00111 inline const Real &Vec4::operator [] (Int i) const 00112 { 00113 CheckRange(i, 0, 4, "(Vec4::[i]) index out of range"); 00114 return(elt[i]); 00115 } 00116 00117 00118 inline Vec4::Vec4() 00119 { 00120 } 00121 00122 inline Vec4::Vec4(Real x, Real y, Real z, Real w) 00123 { 00124 elt[0] = x; 00125 elt[1] = y; 00126 elt[2] = z; 00127 elt[3] = w; 00128 } 00129 00130 inline Vec4::Vec4(const Vec4 &v) 00131 { 00132 elt[0] = v[0]; 00133 elt[1] = v[1]; 00134 elt[2] = v[2]; 00135 elt[3] = v[3]; 00136 } 00137 00138 inline Vec4::Vec4(const Vec3 &v, Real w) 00139 { 00140 elt[0] = v[0]; 00141 elt[1] = v[1]; 00142 elt[2] = v[2]; 00143 elt[3] = w; 00144 } 00145 00146 inline Real *Vec4::Ref() const 00147 { 00148 return((Real *) elt); 00149 } 00150 00151 inline Vec4 &Vec4::operator = (const Vec4 &v) 00152 { 00153 elt[0] = v[0]; 00154 elt[1] = v[1]; 00155 elt[2] = v[2]; 00156 elt[3] = v[3]; 00157 00158 return(SELF); 00159 } 00160 00161 inline Vec4 &Vec4::operator += (const Vec4 &v) 00162 { 00163 elt[0] += v[0]; 00164 elt[1] += v[1]; 00165 elt[2] += v[2]; 00166 elt[3] += v[3]; 00167 00168 return(SELF); 00169 } 00170 00171 inline Vec4 &Vec4::operator -= (const Vec4 &v) 00172 { 00173 elt[0] -= v[0]; 00174 elt[1] -= v[1]; 00175 elt[2] -= v[2]; 00176 elt[3] -= v[3]; 00177 00178 return(SELF); 00179 } 00180 00181 inline Vec4 &Vec4::operator *= (const Vec4 &v) 00182 { 00183 elt[0] *= v[0]; 00184 elt[1] *= v[1]; 00185 elt[2] *= v[2]; 00186 elt[3] *= v[3]; 00187 00188 return(SELF); 00189 } 00190 00191 inline Vec4 &Vec4::operator *= (Real s) 00192 { 00193 elt[0] *= s; 00194 elt[1] *= s; 00195 elt[2] *= s; 00196 elt[3] *= s; 00197 00198 return(SELF); 00199 } 00200 00201 inline Vec4 &Vec4::operator /= (const Vec4 &v) 00202 { 00203 elt[0] /= v[0]; 00204 elt[1] /= v[1]; 00205 elt[2] /= v[2]; 00206 elt[3] /= v[3]; 00207 00208 return(SELF); 00209 } 00210 00211 inline Vec4 &Vec4::operator /= (Real s) 00212 { 00213 elt[0] /= s; 00214 elt[1] /= s; 00215 elt[2] /= s; 00216 elt[3] /= s; 00217 00218 return(SELF); 00219 } 00220 00221 00222 inline Vec4 Vec4::operator + (const Vec4 &a) const 00223 { 00224 Vec4 result; 00225 00226 result[0] = elt[0] + a[0]; 00227 result[1] = elt[1] + a[1]; 00228 result[2] = elt[2] + a[2]; 00229 result[3] = elt[3] + a[3]; 00230 00231 return(result); 00232 } 00233 00234 inline Vec4 Vec4::operator - (const Vec4 &a) const 00235 { 00236 Vec4 result; 00237 00238 result[0] = elt[0] - a[0]; 00239 result[1] = elt[1] - a[1]; 00240 result[2] = elt[2] - a[2]; 00241 result[3] = elt[3] - a[3]; 00242 00243 return(result); 00244 } 00245 00246 inline Vec4 Vec4::operator - () const 00247 { 00248 Vec4 result; 00249 00250 result[0] = -elt[0]; 00251 result[1] = -elt[1]; 00252 result[2] = -elt[2]; 00253 result[3] = -elt[3]; 00254 00255 return(result); 00256 } 00257 00258 inline Vec4 Vec4::operator * (const Vec4 &a) const 00259 { 00260 Vec4 result; 00261 00262 result[0] = elt[0] * a[0]; 00263 result[1] = elt[1] * a[1]; 00264 result[2] = elt[2] * a[2]; 00265 result[3] = elt[3] * a[3]; 00266 00267 return(result); 00268 } 00269 00270 inline Vec4 Vec4::operator * (Real s) const 00271 { 00272 Vec4 result; 00273 00274 result[0] = elt[0] * s; 00275 result[1] = elt[1] * s; 00276 result[2] = elt[2] * s; 00277 result[3] = elt[3] * s; 00278 00279 return(result); 00280 } 00281 00282 inline Vec4 Vec4::operator / (const Vec4 &a) const 00283 { 00284 Vec4 result; 00285 00286 result[0] = elt[0] / a[0]; 00287 result[1] = elt[1] / a[1]; 00288 result[2] = elt[2] / a[2]; 00289 result[3] = elt[3] / a[3]; 00290 00291 return(result); 00292 } 00293 00294 inline Vec4 Vec4::operator / (Real s) const 00295 { 00296 Vec4 result; 00297 00298 result[0] = elt[0] / s; 00299 result[1] = elt[1] / s; 00300 result[2] = elt[2] / s; 00301 result[3] = elt[3] / s; 00302 00303 return(result); 00304 } 00305 00306 inline Vec4 operator * (Real s, const Vec4 &v) 00307 { 00308 return(v * s); 00309 } 00310 00311 inline Vec4 &Vec4::MakeZero() 00312 { 00313 elt[0] = vl_zero; elt[1] = vl_zero; elt[2] = vl_zero; elt[3] = vl_zero; 00314 return(SELF); 00315 } 00316 00317 inline Vec4 &Vec4::MakeBlock(Real k) 00318 { 00319 elt[0] = k; elt[1] = k; elt[2] = k; elt[3] = k; 00320 return(SELF); 00321 } 00322 00323 inline Vec4 &Vec4::Normalise() 00324 { 00325 Assert(sqrlen(SELF) > 0.0, "normalising length-zero vector"); 00326 SELF /= len(SELF); 00327 return(SELF); 00328 } 00329 00330 inline Vec4::Vec4(ZeroOrOne k) 00331 { 00332 MakeBlock(k); 00333 } 00334 00335 inline Vec4::Vec4(Axis k) 00336 { 00337 MakeUnit(k, vl_1); 00338 } 00339 00340 inline Vec4 &Vec4::operator = (ZeroOrOne k) 00341 { 00342 MakeBlock(k); 00343 00344 return(SELF); 00345 } 00346 00347 inline Vec4 &Vec4::operator = (Axis k) 00348 { 00349 MakeUnit(k, vl_1); 00350 00351 return(SELF); 00352 } 00353 00354 00355 inline Real dot(const Vec4 &a, const Vec4 &b) 00356 { 00357 return(a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]); 00358 } 00359 00360 inline Real len(const Vec4 &v) 00361 { 00362 return(sqrt(dot(v, v))); 00363 } 00364 00365 inline Real sqrlen(const Vec4 &v) 00366 { 00367 return(dot(v, v)); 00368 } 00369 00370 inline Vec4 norm(const Vec4 &v) 00371 { 00372 Assert(sqrlen(v) > 0.0, "normalising length-zero vector"); 00373 return(v / len(v)); 00374 } 00375 00376 inline Void normalise(Vec4 &v) 00377 { 00378 v /= len(v); 00379 } 00380 00381 inline void printVec4(const Vec4 &v) 00382 { 00383 printf("[%10f %10f %10f %10f]",v[0],v[1],v[2],v[3]); 00384 } 00385 00386 #endif
Generated on Wed Jul 13 2022 19:31:42 by
1.7.2