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