Advertisement
mrpowhs

Vec3.hpp

Feb 9th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.30 KB | None | 0 0
  1. /// Leírás: 3D vektor típus számítógépes grafikához és játékfejlesztéshez
  2. /// Írta: Seeting (Hegedüs Ádám)
  3. /// Elérhetőség: mrpowhs@gmail.com
  4.  
  5. #ifndef VEC3_HPP
  6. #define VEC3_HPP
  7.  
  8. #include <cmath>
  9. #define PI 3.14159265
  10.  
  11. namespace Math {
  12.  
  13. template <class T>
  14. inline T toDegree(T rad) { return rad*((T)180.0/(T)PI); }
  15.  
  16. template <class T>
  17. inline T toRadian(T angle) { return angle*((T)PI/(T)180.0); }
  18.  
  19. template <class T>
  20. inline T abs(T a)
  21. { return a > T(0) ? a : -a; }
  22.  
  23. template <class T>
  24. class Vec3
  25. {
  26. public:
  27.     /// KONSTRUKTOROK
  28.     Vec3() {v[0]=0; v[1]=0; v[2]=0;}                                                // Default konstruktor
  29.     Vec3(T x, T y=0, T z=0) {v[0]=x; v[1]=y; v[2]=z;}                               // Init konstruktor
  30.     Vec3(const Vec3& other) {v[0]=other.v[0]; v[1]=other.v[1]; v[2]=other.v[2];}    // Copy konstruktor
  31.     Vec3(const T arr[]) {v[0]=arr[0]; v[1]=arr[1]; v[2]=arr[2];}                    // Inicializálás tömbbel (vagy init list C++11 esetében)
  32.  
  33.     /// ELÉRÉS
  34.     // Lekérdezések
  35.     T x() const {return v[0];}
  36.     T y() const {return v[1];}
  37.     T z() const {return v[2];}
  38.     T operator () (int i) const {return v[i];}
  39.     T operator [] (int i) const {return v[i];}
  40.  
  41.     // Értékadások
  42.     T& x() {return v[0];}
  43.     T& y() {return v[1];}
  44.     T& z() {return v[2];}
  45.     T& operator() (int i) {return v[i];}
  46.     T& operator[] (int i) {return v[i];}
  47.     void operator= (const T arr[]) {v[0]=arr[0]; v[1]=arr[1]; v[2]=arr[2];}
  48.  
  49.     /// MÛVELETEK
  50.     // Értékadás
  51.     Vec3<T>& operator= (const Vec3<T>& other)
  52.     {
  53.         if (this != &other) {v[0]=other.v[0]; v[1]=other.v[1]; v[2]=other.v[2];}
  54.         return *this;
  55.     }
  56.  
  57.     // Nullvektor elõállítása
  58.     inline void zero() {v[0]=0; v[1]=0; v[2]=0;}
  59.  
  60.     // Összeadás
  61.     inline void operator+= (const Vec3& other)
  62.     {v[0]+=other.v[0]; v[1]+=other.v[1]; v[2]+=other.v[2];}
  63.     inline Vec3 operator+ (const Vec3& other) const
  64.     {return Vec3(v[0]+other.v[0], v[1]+other.v[1], v[2]+other.v[2]);}
  65.  
  66.     // Kivonás
  67.     inline void operator-= (const Vec3& other)
  68.     {v[0]-=other.v[0]; v[1]-=other.v[1]; v[2]-=other.v[2];}
  69.     inline Vec3 operator- (const Vec3& other) const
  70.     {return Vec3(v[0]-other.v[0], v[1]-other.v[1], v[2]-other.v[2]);}
  71.  
  72.     // Szorzás skalárral
  73.     inline void operator*= (const T& a)
  74.     {v[0]*=a; v[1]*=a; v[2]*=a;}
  75.     inline Vec3 operator* (const T& a) const
  76.     {return Vec3(v[0]*a, v[1]*a, v[2]*a);}
  77.  
  78.     // Osztás skalárral (a teljesség igényével)
  79.     inline void operator/= (const T& a)
  80.     {v[0]/=a; v[1]/=a; v[2]/=a;}
  81.     inline Vec3 operator/ (const T& a) const
  82.     {return Vec3(v[0]/a, v[1]/a, v[2]/a);}
  83.  
  84.     // Negálás
  85.     inline Vec3 operator- () const
  86.     {return Vec3(-v[0], -v[1], -v[2]);}
  87.  
  88.     // Vektor hossza
  89.     inline T length() const
  90.     {return std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);}
  91.     inline T lengthSquared() const
  92.     {return (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);}
  93.  
  94.     // Távolság a megadott vektortól
  95.     inline T distance(const Vec3& other) const
  96.     {
  97.         T x = other.v[0]-v[0];
  98.         T y = other.v[1]-v[1];
  99.         T z = other.v[2]-v[2];
  100.         return std::sqrt(x*x + y*y + z*z);
  101.     }
  102.     inline T distanceSquared(const Vec3& other) const
  103.     {
  104.         T x = other.v[0]-v[0];
  105.         T y = other.v[1]-v[1];
  106.         T z = other.v[2]-v[2];
  107.         return (x*x + y*y + z*z);
  108.     }
  109.  
  110.     // Egységvektorrá alakítás
  111.     inline void normalize()
  112.     {T len=length(); v[0]/=len; v[1]/=len; v[2]/=len;}
  113.  
  114.     // Dot product
  115.     inline T dot(const Vec3& other) const
  116.     {return v[0]*other.v[0] + v[1]*other.v[1] + v[2]*other.v[2];}
  117.  
  118.     // Cross product
  119.     inline Vec3 cross(const Vec3& other) const
  120.     {
  121.         return Vec3(y()*other.z() - z()*other.y(),
  122.                     z()*other.x() - x()*other.z(),
  123.                     x()*other.y() - y()*other.x());
  124.     }
  125.  
  126.     // Visszaverõdés
  127.     // R_r = R_in - 2N(R_in.N) egyenlõséget használva
  128.     inline Vec3 reflection(const Vec3& surface_normal) const
  129.     {
  130.         Vec3 N = normalized(surface_normal);
  131.         Vec3 R = normalized(*this);
  132.         return (R - ((T)2*N) * R.dot(N));
  133.     }
  134.  
  135.     // Ekvivalencia
  136.     inline bool operator== (const Vec3& other) const
  137.     {return v[0]==other.v[0] && v[1]==other.v[1] && v[2]==other.v[2];}
  138.  
  139. protected:
  140.     T v[3];
  141. };
  142.  
  143. // Szorzás skalárral bal oldalról
  144. template <class T>
  145. inline Vec3<T> operator* (const T& a, const Vec3<T>& v)
  146. {return Vec3<T>(v[0]*a, v[1]*a, v[2]*a);}
  147.  
  148. // Dot product
  149. template <class T>
  150. inline T dot(const Vec3<T>& v1, const Vec3<T>& v2)
  151. {return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];}
  152.  
  153. // Cross product
  154. template <class T>
  155. inline Vec3<T> cross(const Vec3<T>& a, const Vec3<T>& b)
  156. {
  157.     return Vec3<T>(a.y()*b.z() - a.z()*b.y(),
  158.                 a.z()*b.x() - a.x()*b.z(),
  159.                 a.x()*b.y() - a.y()*b.x());
  160. }
  161.  
  162. // Normalizált vektor
  163. template <class T>
  164. inline Vec3<T> normalized(const Vec3<T>& v)
  165. {return Vec3<T>(v/v.length());}
  166.  
  167. // Két vertex (vektorokkal prezentálva) távolsága
  168. template <class T>
  169. inline T distance(const Vec3<T>& a, const Vec3<T>& b)
  170. {
  171.     T x = b[0]-a[0];
  172.     T y = b[1]-a[1];
  173.     T z = b[2]-a[2];
  174.     return std::sqrt(x*x + y*y + z*z);
  175. }
  176.  
  177. template <class T>
  178. inline T distanceSquared(const Vec3<T>& a, const Vec3<T>& b)
  179. {
  180.     T x = b[0]-a[0];
  181.     T y = b[1]-a[1];
  182.     T z = b[2]-a[2];
  183.     return (x*x + y*y + z*z);
  184. }
  185.  
  186. // Két vektor álltal közbezárt szög
  187. template <class T>
  188. inline T angle(const Vec3<T>& a, const Vec3<T>& b)
  189. {return std::acos(dot(normalized(a), normalized(b)));}
  190.  
  191. template <class T>
  192. inline T angleEuler(const Vec3<T>& a, const Vec3<T>& b)
  193. {return std::acos(dot(normalized(a), normalized(b)))*((T)180.0/(T)PI);}
  194.  
  195. // Új vektor a koordináták abszolút értékével
  196. template <class T>
  197. inline Vec3<T> abs(const Vec3<T>& v)
  198. {return Vec3<T>(abs(v[0]), abs(v[1]), abs(v[2]));}
  199.  
  200. // Visszaverõdés
  201. // R_r = R_in - 2N(R_in.N) egyenlõséget használva
  202. template <class T>
  203. inline Vec3<T> reflection(const Vec3<T>& in, const Vec3<T>& surface_normal)
  204. {
  205.     Vec3<T> N = normalized(surface_normal);
  206.     Vec3<T> R = normalized(in);
  207.     return (R - ((T)2*N) * R.dot(N));
  208. }
  209.  
  210. typedef Vec3<float> Vec3f;
  211. typedef Vec3<double> Vec3d;
  212.  
  213. }   /// NÉVTÉR VÉGE
  214.  
  215. #endif // VEC3_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement