Bart Janssens / SVL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Vec3.h Source File

Vec3.h

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