Closest Point
Find the Closest Point on a mesh
include/Vector.h
00001 /*Template class for Vector operations*/
00002 #pragma once
00003 
00004 #include <cmath>
00005 #include <iostream>
00006 #include <stdlib.h>
00007 
00008 namespace math {
00009 
00010 #define PI 3.14159265
00011 
00012 inline int randomRange(int minValue, int maxValue,unsigned int seed=0)
00013 {
00014     srand(seed);
00015     return minValue + (rand() % (maxValue - minValue + 1));
00016 }
00017 template <class T>
00018 class Vec2 {
00019 public:
00020     T x, y;
00021 
00022     Vec2(T x = 0, T y = 0) : x(x), y(y) { }
00023 
00024     inline void setval(T xs, T ys)
00025     {
00026         x = xs;
00027         y = ys;
00028     }
00029 
00030     inline const Vec2<T> operator *(T mul) const
00031     {
00032         return Vec2<T>(x * mul, y * mul);
00033     }
00034 
00035     inline const Vec2<T> operator *(const Vec2<T>& mul) const
00036     {
00037         return Vec2<T>(x * mul.x, y * mul.y);
00038     }
00039 
00040     inline const Vec2<T> operator /(T div) const
00041     {
00042         return Vec2<T>(x / div, y / div);
00043     }
00044 
00045     inline const Vec2<T> operator /(const Vec2<T>& div) const
00046     {
00047         return Vec2<T>(x / div.x, y / div.y);
00048     }
00049 
00050     inline const Vec2<T> operator +(T add) const
00051     {
00052         return Vec2<T>(x + add, y + add);
00053     }
00054 
00055     inline const Vec2<T> operator +(const Vec2<T>& add) const
00056     {
00057         return Vec2<T>(x + add.x, y + add.y);
00058     }
00059 
00060     inline const Vec2<T> operator -(T subtract) const
00061     {
00062         return Vec2<T>(x - subtract, y - subtract);
00063     }
00064 
00065     inline const Vec2<T> operator -(const Vec2<T>& subtract) const
00066     {
00067         return Vec2<T>(x - subtract.x, y - subtract.y);
00068     }
00069 
00070     inline Vec2<T> operator *=(T mul)
00071     {
00072         x *= mul;
00073         y *= mul;
00074         return *this;
00075     }
00076 
00077     inline Vec2<T> operator *=(const Vec2<T>& mul)
00078     {
00079         x *= mul.x;
00080         y *= mul.y;
00081         return *this;
00082     }
00083 
00084     inline Vec2<T> operator /=(T div)
00085     {
00086         x /= div;
00087         y /= div;
00088         return *this;
00089     }
00090 
00091     inline Vec2<T> operator /=(const Vec2<T>& div)
00092     {
00093         x /= div.x;
00094         y /= div.y;
00095         return *this;
00096     }
00097 
00098     inline Vec2<T> operator +=(T add)
00099     {
00100         x += add;
00101         y += add;
00102         return *this;
00103     }
00104 
00105     inline Vec2<T> operator +=(const Vec2<T>& add)
00106     {
00107         x += add.x;
00108         y += add.y;
00109         return *this;
00110     }
00111 
00112     inline Vec2<T> operator -=(T subtract)
00113     {
00114         x -= subtract;
00115         y -= subtract;
00116         return *this;
00117     }
00118 
00119     inline Vec2<T> operator -=(const Vec2<T>& subtract)
00120     {
00121         x -= subtract.x;
00122         y -= subtract.y;
00123         return *this;
00124     }
00125 
00126     inline bool operator ==(const Vec2<T>& other)
00127     {
00128         return x == other.x && y == other.y;
00129     }
00130 
00131     inline T length() const
00132     {
00133         return sqrt((x * x) + (y * y));
00134     }
00135 
00136     inline void limit(T limit)
00137     {
00138         T lengthSquared = x * x + y * y;
00139 
00140         if((lengthSquared > 0) && (lengthSquared > limit * limit))
00141         {
00142             double ratio = limit / sqrt(lengthSquared);
00143             x *= ratio;
00144             y *= ratio;
00145         }
00146     }
00147 
00148     inline Vec2<T> limited(const Vec2<T>& limit) const
00149     {
00150         T lengthSquared = x * x + y * y;
00151 
00152         if((lengthSquared > 0) && (lengthSquared > limit * limit))
00153         {
00154             double ratio = limit / sqrt(lengthSquared);
00155             return Vec2<T>(x * ratio, y * ratio);
00156         }
00157 
00158         else
00159         {
00160             return *this;
00161         }
00162     }
00163 
00164     inline void normalize()
00165     {
00166         double ratio = 1 / length();
00167         x *= ratio;
00168         y *= ratio;
00169     }
00170 
00171     inline Vec2<T> normalized() const
00172     {
00173         double ratio = 1 / length();
00174         return Vec2<T>(x * ratio, y * ratio);
00175     }
00176 
00177     inline T distance(const Vec2<T>& other) const
00178     {
00179         return sqrt(((other.x - x) * (other.x - x)) + ((other.y - y) * (other.y - y)));
00180     }
00181 };
00182 
00183 template <class T>
00184 class Vec3 {
00185 public:
00186     T x, y, z;
00187 
00188     Vec3(T x = 0, T y = 0, T z = 0) : x(x), y(y), z(z) { }
00189 
00190     inline const Vec3<T> operator *(T mul) const
00191     {
00192         return Vec3<T>(x * mul, y * mul, z * mul);
00193     }
00194 
00195     inline const Vec3<T> operator *(const Vec3<T>& mul) const
00196     {
00197         return Vec3<T>(x * mul.x, y * mul.y, z * mul.z);
00198     }
00199 
00200     inline const Vec3<T> operator /(T div) const
00201     {
00202         return Vec3<T>(x / div, y / div, z / div);
00203     }
00204 
00205     inline const Vec3<T> operator /(const Vec3<T>& div) const
00206     {
00207         return Vec3<T>(x / div.x, y / div.y, z / div.z);
00208     }
00209 
00210     inline const Vec3<T> operator +(T add) const
00211     {
00212         return Vec3<T>(x + add, y + add, z + add);
00213     }
00214 
00215     inline const Vec3<T> operator +(const Vec3<T>& add) const
00216     {
00217         return Vec3(x + add.x, y + add.y, z + add.z);
00218     }
00219 
00220     inline const Vec3<T> operator -(T subtract) const
00221     {
00222         return Vec3(x - subtract, y - subtract, z - subtract);
00223     }
00224 
00225     inline const Vec3<T> operator -(const Vec3<T>& subtract) const
00226     {
00227         return Vec3<T>(x - subtract.x, y - subtract.y, z - subtract.z);
00228     }
00229 
00230     inline Vec3<T> operator *=(T mul)
00231     {
00232         x *= mul;
00233         y *= mul;
00234         z *= mul;
00235         return *this;
00236     }
00237 
00238     inline Vec3<T> operator *=(const Vec3<T> mul)
00239     {
00240         x *= mul.x;
00241         y *= mul.y;
00242         z *= mul.z;
00243         return *this;
00244     }
00245 
00246     inline Vec3<T> operator /=(T div)
00247     {
00248         x /= div;
00249         y /= div;
00250         z /= div;
00251         return *this;
00252     }
00253 
00254     inline Vec3<T> operator /=(const Vec3<T> div)
00255     {
00256         x /= div.x;
00257         y /= div.y;
00258         z /= div.z;
00259         return *this;
00260     }
00261 
00262     inline Vec3<T> operator +=(T add)
00263     {
00264         x += add;
00265         y += add;
00266         z += add;
00267         return *this;
00268     }
00269 
00270     inline Vec3<T> operator +=(const Vec3<T> add)
00271     {
00272         x += add.x;
00273         y += add.y;
00274         z += add.z;
00275         return *this;
00276     }
00277 
00278     inline Vec3<T> operator -=(T subtract)
00279     {
00280         x -= subtract;
00281         y -= subtract;
00282         z -= subtract;
00283         return *this;
00284     }
00285 
00286     inline Vec3<T> operator -=(const Vec3<T> subtract)
00287     {
00288         x -= subtract.x;
00289         y -= subtract.y;
00290         z -= subtract.z;
00291         return *this;
00292     }
00293 
00294     inline bool operator ==(Vec3<T> other)
00295     {
00296         return x == other.x && y == other.y && z == other.z;
00297     }
00298 
00299     inline T length()
00300     {
00301         return sqrt((x * x) + (y * y) + (z * z));
00302     }
00303     inline T lengthSquared()
00304     {
00305         return (x * x) + (y * y) + (z * z);
00306     }
00307     inline void limit(T limit)
00308     {
00309         T lengthSquared = x * x + y * y + z * z;
00310 
00311         if((lengthSquared > 0) && (lengthSquared > limit * limit))
00312         {
00313             double ratio = limit / sqrt(lengthSquared);
00314             x *= ratio;
00315             y *= ratio;
00316             z *= ratio;
00317         }
00318     }
00319 
00320     inline Vec2<T> limited(const Vec2<T>& limit) const
00321     {
00322         T lengthSquared = x * x + y * y;
00323 
00324         if((lengthSquared > 0) && (lengthSquared > limit * limit))
00325         {
00326             double ratio = limit / std::sqrt(lengthSquared);
00327             return Vec3<T>(x * ratio, y * ratio, z * ratio);
00328         }
00329 
00330         else
00331         {
00332             return *this;
00333         }
00334     }
00335 
00336     inline void normalize()
00337     {
00338         double ratio = 1 / length();
00339         x *= ratio;
00340         y *= ratio;
00341         z *= ratio;
00342     }
00343 
00344     inline Vec3<T> normalized() const
00345     {
00346         double ratio = 1 / length();
00347         return Vec3<T>(x * ratio, y * ratio, z * ratio);
00348     }
00349 
00350     inline T distance(const Vec3<T>& other) const
00351     {
00352         return sqrt(((other.x - x) * (other.x - x)) + ((other.y - y) * (other.y - y)) + ((other.z - z) * (other.z - z)));
00353     }
00354 
00355     inline T dot(const Vec3<T>& other) const{
00356         return (x*other.x)+(y * other.y)+ (z * other.z);
00357     }
00358     inline Vec3<T> cross(const Vec3<T>& other) const
00359     {
00360         float i =  (y * other.z) - (other.y  * z);
00361         float j = (other.x * z) - (x * other.z);
00362         float k = (x * other.y) - (other.x * y);
00363         return Vec3<T>(i,j,k);
00364     }
00365 };
00366 
00367 typedef Vec2<int> Vec2i;
00368 typedef Vec2<float> Vec2f;
00369 typedef Vec2<double> Vec2d;
00370 typedef Vec3<int> Vec3i;
00371 typedef Vec3<float> Vec3f;
00372 typedef Vec3<double> Vec3d;
00373 
00374 } // math namespace
 All Classes