Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Leírás: 3D vektor típus számítógépes grafikához és játékfejlesztéshez
- /// Írta: Seeting (Hegedüs Ádám)
- /// Elérhetőség: mrpowhs@gmail.com
- #ifndef VEC3_HPP
- #define VEC3_HPP
- #include <cmath>
- #define PI 3.14159265
- namespace Math {
- template <class T>
- inline T toDegree(T rad) { return rad*((T)180.0/(T)PI); }
- template <class T>
- inline T toRadian(T angle) { return angle*((T)PI/(T)180.0); }
- template <class T>
- inline T abs(T a)
- { return a > T(0) ? a : -a; }
- template <class T>
- class Vec3
- {
- public:
- /// KONSTRUKTOROK
- Vec3() {v[0]=0; v[1]=0; v[2]=0;} // Default konstruktor
- Vec3(T x, T y=0, T z=0) {v[0]=x; v[1]=y; v[2]=z;} // Init konstruktor
- Vec3(const Vec3& other) {v[0]=other.v[0]; v[1]=other.v[1]; v[2]=other.v[2];} // Copy konstruktor
- 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)
- /// ELÉRÉS
- // Lekérdezések
- T x() const {return v[0];}
- T y() const {return v[1];}
- T z() const {return v[2];}
- T operator () (int i) const {return v[i];}
- T operator [] (int i) const {return v[i];}
- // Értékadások
- T& x() {return v[0];}
- T& y() {return v[1];}
- T& z() {return v[2];}
- T& operator() (int i) {return v[i];}
- T& operator[] (int i) {return v[i];}
- void operator= (const T arr[]) {v[0]=arr[0]; v[1]=arr[1]; v[2]=arr[2];}
- /// MÛVELETEK
- // Értékadás
- Vec3<T>& operator= (const Vec3<T>& other)
- {
- if (this != &other) {v[0]=other.v[0]; v[1]=other.v[1]; v[2]=other.v[2];}
- return *this;
- }
- // Nullvektor elõállítása
- inline void zero() {v[0]=0; v[1]=0; v[2]=0;}
- // Összeadás
- inline void operator+= (const Vec3& other)
- {v[0]+=other.v[0]; v[1]+=other.v[1]; v[2]+=other.v[2];}
- inline Vec3 operator+ (const Vec3& other) const
- {return Vec3(v[0]+other.v[0], v[1]+other.v[1], v[2]+other.v[2]);}
- // Kivonás
- inline void operator-= (const Vec3& other)
- {v[0]-=other.v[0]; v[1]-=other.v[1]; v[2]-=other.v[2];}
- inline Vec3 operator- (const Vec3& other) const
- {return Vec3(v[0]-other.v[0], v[1]-other.v[1], v[2]-other.v[2]);}
- // Szorzás skalárral
- inline void operator*= (const T& a)
- {v[0]*=a; v[1]*=a; v[2]*=a;}
- inline Vec3 operator* (const T& a) const
- {return Vec3(v[0]*a, v[1]*a, v[2]*a);}
- // Osztás skalárral (a teljesség igényével)
- inline void operator/= (const T& a)
- {v[0]/=a; v[1]/=a; v[2]/=a;}
- inline Vec3 operator/ (const T& a) const
- {return Vec3(v[0]/a, v[1]/a, v[2]/a);}
- // Negálás
- inline Vec3 operator- () const
- {return Vec3(-v[0], -v[1], -v[2]);}
- // Vektor hossza
- inline T length() const
- {return std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);}
- inline T lengthSquared() const
- {return (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);}
- // Távolság a megadott vektortól
- inline T distance(const Vec3& other) const
- {
- T x = other.v[0]-v[0];
- T y = other.v[1]-v[1];
- T z = other.v[2]-v[2];
- return std::sqrt(x*x + y*y + z*z);
- }
- inline T distanceSquared(const Vec3& other) const
- {
- T x = other.v[0]-v[0];
- T y = other.v[1]-v[1];
- T z = other.v[2]-v[2];
- return (x*x + y*y + z*z);
- }
- // Egységvektorrá alakítás
- inline void normalize()
- {T len=length(); v[0]/=len; v[1]/=len; v[2]/=len;}
- // Dot product
- inline T dot(const Vec3& other) const
- {return v[0]*other.v[0] + v[1]*other.v[1] + v[2]*other.v[2];}
- // Cross product
- inline Vec3 cross(const Vec3& other) const
- {
- return Vec3(y()*other.z() - z()*other.y(),
- z()*other.x() - x()*other.z(),
- x()*other.y() - y()*other.x());
- }
- // Visszaverõdés
- // R_r = R_in - 2N(R_in.N) egyenlõséget használva
- inline Vec3 reflection(const Vec3& surface_normal) const
- {
- Vec3 N = normalized(surface_normal);
- Vec3 R = normalized(*this);
- return (R - ((T)2*N) * R.dot(N));
- }
- // Ekvivalencia
- inline bool operator== (const Vec3& other) const
- {return v[0]==other.v[0] && v[1]==other.v[1] && v[2]==other.v[2];}
- protected:
- T v[3];
- };
- // Szorzás skalárral bal oldalról
- template <class T>
- inline Vec3<T> operator* (const T& a, const Vec3<T>& v)
- {return Vec3<T>(v[0]*a, v[1]*a, v[2]*a);}
- // Dot product
- template <class T>
- inline T dot(const Vec3<T>& v1, const Vec3<T>& v2)
- {return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];}
- // Cross product
- template <class T>
- inline Vec3<T> cross(const Vec3<T>& a, const Vec3<T>& b)
- {
- return Vec3<T>(a.y()*b.z() - a.z()*b.y(),
- a.z()*b.x() - a.x()*b.z(),
- a.x()*b.y() - a.y()*b.x());
- }
- // Normalizált vektor
- template <class T>
- inline Vec3<T> normalized(const Vec3<T>& v)
- {return Vec3<T>(v/v.length());}
- // Két vertex (vektorokkal prezentálva) távolsága
- template <class T>
- inline T distance(const Vec3<T>& a, const Vec3<T>& b)
- {
- T x = b[0]-a[0];
- T y = b[1]-a[1];
- T z = b[2]-a[2];
- return std::sqrt(x*x + y*y + z*z);
- }
- template <class T>
- inline T distanceSquared(const Vec3<T>& a, const Vec3<T>& b)
- {
- T x = b[0]-a[0];
- T y = b[1]-a[1];
- T z = b[2]-a[2];
- return (x*x + y*y + z*z);
- }
- // Két vektor álltal közbezárt szög
- template <class T>
- inline T angle(const Vec3<T>& a, const Vec3<T>& b)
- {return std::acos(dot(normalized(a), normalized(b)));}
- template <class T>
- inline T angleEuler(const Vec3<T>& a, const Vec3<T>& b)
- {return std::acos(dot(normalized(a), normalized(b)))*((T)180.0/(T)PI);}
- // Új vektor a koordináták abszolút értékével
- template <class T>
- inline Vec3<T> abs(const Vec3<T>& v)
- {return Vec3<T>(abs(v[0]), abs(v[1]), abs(v[2]));}
- // Visszaverõdés
- // R_r = R_in - 2N(R_in.N) egyenlõséget használva
- template <class T>
- inline Vec3<T> reflection(const Vec3<T>& in, const Vec3<T>& surface_normal)
- {
- Vec3<T> N = normalized(surface_normal);
- Vec3<T> R = normalized(in);
- return (R - ((T)2*N) * R.dot(N));
- }
- typedef Vec3<float> Vec3f;
- typedef Vec3<double> Vec3d;
- } /// NÉVTÉR VÉGE
- #endif // VEC3_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement