Bart Janssens / SVL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Vec.h Source File

Vec.h

00001 /*
00002     File:           Vec.h
00003 
00004     Function:       Defines a generic resizeable vector.
00005 
00006     Author(s):      Andrew Willmott
00007 
00008     Copyright:      (c) 1995-2001, Andrew Willmott
00009  */
00010 
00011 #ifndef __Vec__
00012 #define __Vec__
00013 #include "Basics.h"
00014 #include "Constants.h"
00015 #include "Utils.h"
00016 
00017 class Vec2;
00018 class Vec3;
00019 class Vec4;
00020 
00021 
00022 // --- Vec Class --------------------------------------------------------------
00023 
00024 class Vec
00025 {
00026 public:
00027 
00028     // Constructors
00029 
00030                 Vec();                          // Null vector
00031     explicit    Vec(Int n);                     // n-element vector
00032                 Vec(Int n, double elt0, ...);   // e.g. Vec(3, 1.1, 2.0, 3.4)
00033                 Vec(Int n, Real *data);         // Vector reference
00034                 Vec(const Vec &v);              // Copy constructor
00035                 Vec(const Vec2 &v);             // reference to a Vec2
00036                 Vec(const Vec3 &v);             // reference to a Vec3
00037                 Vec(const Vec4 &v);             // reference to a Vec4
00038                 Vec(Int n, ZeroOrOne);          // Zero or all-ones vector
00039                 Vec(Int n, Axis a);             // Unit vector
00040                ~Vec();                          // Destructor
00041 
00042     // Accessor functions
00043 
00044     Int         Elts() const;
00045 
00046     Real        &operator [] (Int i);
00047     Real        operator [] (Int i) const;
00048 
00049     Void        SetSize(Int n);                 // Resize the vector
00050     Real        *Ref() const;                   // Return pointer to data
00051 
00052     // Assignment operators
00053 
00054     Vec         &operator =  (const Vec &v);    // v = a etc.
00055     Vec         &operator =  (ZeroOrOne k);
00056     Vec         &operator =  (Axis a);
00057     Vec         &operator =  (const Vec2 &v);
00058     Vec         &operator =  (const Vec3 &v);
00059     Vec         &operator =  (const Vec4 &v);
00060 
00061     // In-Place operators
00062 
00063     Vec         &operator += (const Vec &v);
00064     Vec         &operator -= (const Vec &v);
00065     Vec         &operator *= (const Vec &v);
00066     Vec         &operator *= (Real s);
00067     Vec         &operator /= (const Vec &v);
00068     Vec         &operator /= (Real s);
00069 
00070     //  Vector initialisers
00071 
00072     Vec         &MakeZero();
00073     Vec         &MakeUnit(Int i, Real k = vl_one);
00074     Vec         &MakeBlock(Real k = vl_one);
00075 
00076     Vec         &Normalise();                   // Normalise vector
00077     Vec         &Clamp(Real fuzz);
00078     Vec         &Clamp();
00079 
00080     Bool        IsRef() const { return((elts & VL_REF_FLAG) != 0); };
00081 
00082     // Private...
00083 
00084 protected:
00085 
00086     Real        *data;
00087     UInt        elts;
00088 };
00089 
00090 
00091 // --- Vec Comparison Operators -----------------------------------------------
00092 
00093 Bool            operator == (const Vec &a, const Vec &b);
00094 Bool            operator != (const Vec &a, const Vec &b);
00095 
00096 
00097 // --- Vec Arithmetic Operators -----------------------------------------------
00098 
00099 Vec             operator + (const Vec &a, const Vec &b);
00100 Vec             operator - (const Vec &a, const Vec &b);
00101 Vec             operator - (const Vec &v);
00102 Vec             operator * (const Vec &a, const Vec &b);
00103 Vec             operator * (const Vec &v, Real s);
00104 Vec             operator / (const Vec &a, const Vec &b);
00105 Vec             operator / (const Vec &v, Real s);
00106 Vec             operator * (Real s, const Vec &v);
00107 
00108 Real            dot(const Vec &a, const Vec &b);// v . a
00109 inline Real     len(const Vec &v);              // || v ||
00110 inline Real     sqrlen(const Vec &v);           // v . v
00111 inline Vec      norm(const Vec &v);             // v / || v ||
00112 inline Void     normalise(Vec &v);              // v = norm(v)
00113 
00114 Vec             clamped(const Vec &v, Real fuzz);
00115 Vec             clamped(const Vec &v);
00116 
00117 
00118 // --- Vec Input & Output -----------------------------------------------------
00119 
00120 //std::ostream         &operator << (std::ostream &s, const Vec &v);
00121 //std::istream         &operator >> (std::istream &s, Vec &v);
00122 
00123 inline void printVec(const Vec &v);
00124 
00125 // --- Sub-vector functions ---------------------------------------------------
00126 
00127 inline Vec      sub(const Vec &v, Int start, Int length);
00128 inline Vec      first(const Vec &v, Int length);
00129 inline Vec      last(const Vec &v, Int length);
00130 
00131 
00132 // --- Vec inlines ------------------------------------------------------------
00133 
00134 inline Vec::Vec() : data(0), elts(0)
00135 {
00136 }
00137 
00138 inline Vec::Vec(Int n) : elts(n)
00139 {
00140     Assert(n > 0,"(Vec) illegal vector size");
00141 
00142     data = new Real[n];
00143 }
00144 
00145 inline Vec::Vec(Int n, Real *data) : data(data), elts(n | VL_REF_FLAG)
00146 {
00147 }
00148 
00149 inline Int Vec::Elts() const
00150 {
00151     return(elts & VL_REF_MASK);
00152 }
00153 
00154 inline Real &Vec::operator [] (Int i)
00155 {
00156     CheckRange(i, 0, Elts(), "Vec::[i]");
00157 
00158     return(data[i]);
00159 }
00160 
00161 inline Real Vec::operator [] (Int i) const
00162 {
00163     CheckRange(i, 0, Elts(), "Vec::[i]");
00164 
00165     return(data[i]);
00166 }
00167 
00168 inline Real *Vec::Ref() const
00169 {
00170     return(data);
00171 }
00172 
00173 inline Vec &Vec::operator = (ZeroOrOne k)
00174 {
00175     MakeBlock(k);
00176 
00177     return(SELF);
00178 }
00179 
00180 inline Vec &Vec::operator = (Axis a)
00181 {
00182     MakeUnit(a);
00183 
00184     return(SELF);
00185 }
00186 
00187 inline Real len(const Vec &v)
00188 {
00189     return(sqrt(dot(v, v)));
00190 }
00191 
00192 inline Real sqrlen(const Vec &v)
00193 {
00194     return(dot(v, v));
00195 }
00196 
00197 inline Vec norm(const Vec &v)
00198 {
00199     Assert(sqrlen(v) > 0.0, "normalising length-zero vector");
00200     return(v / len(v));
00201 }
00202 
00203 inline Void normalise(Vec &v)
00204 {
00205     v /= len(v);
00206 }
00207 
00208 inline Vec sub(const Vec &v, Int start, Int length)
00209 {
00210     Assert(start >= 0 && length > 0 && start + length <= v.Elts(),
00211         "(sub(Vec)) illegal subset of vector");
00212 
00213     return(Vec(length, v.Ref() + start));
00214 }
00215 
00216 inline Vec first(const Vec &v, Int length)
00217 {
00218     Assert(length > 0 && length <= v.Elts(),
00219         "(first(Vec)) illegal subset of vector");
00220 
00221     return(Vec(length, v.Ref()));
00222 }
00223 
00224 inline Vec last(const Vec &v, Int length)
00225 {
00226     Assert(length > 0 && length <= v.Elts(),
00227         "(last(Vec)) illegal subset of vector");
00228 
00229     return(Vec(length, v.Ref() + v.Elts() - length));
00230 }
00231 
00232 inline Vec &Vec::Normalise()
00233 {
00234     Assert(sqrlen(SELF) > 0.0, "normalising length-zero vector");
00235     SELF /= len(SELF);
00236     return(SELF);
00237 }
00238 
00239 inline void printVec(const Vec &v)
00240 {    
00241     Int i;
00242     printf("[");
00243     if (v.Elts() > 0)
00244     {
00245         printf("%10f",v[0]);
00246         
00247         for (i = 1; i < v.Elts(); i++)
00248             printf(" %10f", v[i]);
00249     }
00250     printf("]");
00251 }
00252 
00253 
00254 #endif
00255