src/TexCoordinate.h

Go to the documentation of this file.
00001 #ifndef TEXCOORDINATE_H
00002 #define TEXCOORDINATE_H
00003 
00004 #include <iostream>
00005 #include <cmath>
00006 #include <cassert>
00007 
00008 #include "defines.h"
00009 
00010 
00015 class TexCoordinate
00016 {
00017     // our friends
00018     friend TexCoordinate    min(const TexCoordinate a, const TexCoordinate b);
00019     friend TexCoordinate    max(const TexCoordinate a, const TexCoordinate b);
00020     friend TexCoordinate    operator*(const TexCoordinate & w, float f);
00021     friend TexCoordinate    operator*(float f, const TexCoordinate & w);
00022     friend TexCoordinate    operator/(const TexCoordinate & w, float f);
00023     friend std::ostream & operator<<(std::ostream & o, const TexCoordinate & w);
00024 
00025 public:
00026     TexCoordinate()
00027     {
00028         mValues[0] = mValues[1] = 0.0f;
00029     }
00030 
00031     TexCoordinate(float x, float y)
00032     {
00033         mValues[0] = x;
00034         mValues[1] = y;
00035     }
00036 
00037     TexCoordinate(float f)
00038     {
00039         mValues[0] = f;
00040         mValues[1] = f;
00041     }
00042 
00045     inline void setX(float newx)
00046     {
00047     mValues[0] = newx;
00048     }
00049 
00052     inline float x() const
00053     {
00054         return mValues[0];
00055     }
00056 
00059     inline void setY(float newy)
00060     {
00061     mValues[1] = newy;
00062     }
00063 
00066     inline float y() const
00067     {
00068         return mValues[1];
00069     }
00070 
00072     inline float operator[](int i) const
00073     {
00074         return mValues[i];
00075     }
00076 
00077     inline TexCoordinate & operator=(const TexCoordinate & b)
00078     {
00079         mValues[0] = b.mValues[0];
00080         mValues[1] = b.mValues[1];
00081         return *this;
00082     }
00083 
00085     inline int maxDim() const
00086     {
00087         return (mValues[0] > mValues[1]) ? 0 : 1;
00088     }
00089 
00091     inline void setMin(TexCoordinate & other)
00092     {
00093         mValues[0] = std::min(mValues[0], other.mValues[0]);
00094         mValues[1] = std::min(mValues[1], other.mValues[1]);
00095     }
00096 
00098     inline void setMax(TexCoordinate & other)
00099     {
00100         mValues[0] = std::max(mValues[0], other.mValues[0]);
00101         mValues[1] = std::max(mValues[1], other.mValues[1]);
00102     }
00103 
00105     inline float dot(const TexCoordinate & b) const
00106     {
00107         return mValues[0]*b.mValues[0] + mValues[1]*b.mValues[1];
00108     }
00109 
00110 
00112     inline TexCoordinate fabs() const
00113     {
00114         return TexCoordinate(::fabs(mValues[0]), ::fabs(mValues[1]));
00115     }
00116 
00118     inline float length() const
00119     {
00120         return sqrtf(this->dot(*this));
00121     }
00122 
00124     inline TexCoordinate normal() const
00125     {
00126         assert(length() > EPSILON);     // avoid division by zero
00127         return *this * (1.0f / length());
00128     }
00129 
00131     inline void normalize()
00132     {
00133         assert(length() > EPSILON);     // avoid division by zero
00134         *this *= 1.0f / length();
00135     }
00136 
00138     inline TexCoordinate operator-() const
00139     {
00140         return TexCoordinate(-mValues[0], -mValues[1]);
00141     }
00142 
00144     inline void operator*=(float f)
00145     {
00146         mValues[0] *= f;
00147         mValues[1] *= f;
00148     }
00149 
00151     inline void scale(const TexCoordinate & w)
00152     {
00153         mValues[0] *= w.mValues[0];
00154         mValues[1] *= w.mValues[1];
00155     }
00156 
00158     inline TexCoordinate scaled(const TexCoordinate & w) const
00159     {
00160     return TexCoordinate( mValues[0] * w.mValues[0],
00161                   mValues[1] * w.mValues[1] );
00162     }
00163 
00165     inline void operator/=(float f)
00166     {
00167         (*this) *= (1.0f/f);
00168     }
00169 
00171     inline TexCoordinate operator+(const TexCoordinate & b) const
00172     {
00173         return TexCoordinate(mValues[0]+b.mValues[0], mValues[1]+b.mValues[1]);
00174     }
00175 
00177     inline TexCoordinate & operator+=(const TexCoordinate & b)
00178     {
00179         mValues[0] += b.mValues[0];
00180         mValues[1] += b.mValues[1];
00181         return *this;
00182     }
00183 
00185     inline TexCoordinate operator-(const TexCoordinate & b) const
00186     {
00187         return TexCoordinate(mValues[0]-b.mValues[0], mValues[1]-b.mValues[1]);
00188     }
00189 
00191     inline TexCoordinate & operator-=(const TexCoordinate & b)
00192     {
00193         mValues[0] -= b.mValues[0];
00194         mValues[1] -= b.mValues[1];
00195         return *this;
00196     }
00197 
00199     inline TexCoordinate reciprocal() const
00200     {
00201     return TexCoordinate( 1.0f/mValues[0], 1.0f/mValues[1] );
00202     }
00203 
00204 private:
00205     float mValues[2];
00206 };
00207 
00209 inline TexCoordinate min(const TexCoordinate a, const TexCoordinate b)
00210 {
00211     return TexCoordinate( std::min(a.mValues[0], b.mValues[0]),
00212               std::min(a.mValues[1], b.mValues[1]) );
00213 }
00214 
00216 inline TexCoordinate max(const TexCoordinate a, const TexCoordinate b)
00217 {
00218     return TexCoordinate( std::max(a.mValues[0], b.mValues[0]),
00219               std::max(a.mValues[1], b.mValues[1]) );
00220 }
00221 
00223 inline TexCoordinate operator*(const TexCoordinate & w, float f)
00224 {
00225     return TexCoordinate(f*w.mValues[0], f*w.mValues[1]);
00226 }
00227 
00229 inline TexCoordinate operator*(float f, const TexCoordinate & w)
00230 {
00231     return TexCoordinate(f*w.mValues[0], f*w.mValues[1]);
00232 }
00233 
00235 inline TexCoordinate operator/(const TexCoordinate & w, float f)
00236 {
00237     return w*(1.0f/f);
00238 }
00239 
00241 inline std::ostream & operator<<(std::ostream & o, const TexCoordinate & w)
00242 {
00243     o << "(" << w.mValues[0] << ", " << w.mValues[1] << ")";
00244     return o;
00245 }
00246 
00247 
00248 #endif
00249 

Generated on Fri Feb 1 00:01:42 2008 for Grayfall by  doxygen 1.5.1