Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Vec3.h

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

Generated at Sat Aug 5 00:16:49 2000 for Class Library by doxygen 1.1.0 written by Dimitri van Heesch, © 1997-2000