00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef SALT_VECTOR_H
00031 #define SALT_VECTOR_H
00032
00033
00034
00035
00036
00037 #include "tvector.h"
00038 #include <cstdlib>
00039 #include <climits>
00040
00041 namespace salt
00042 {
00043
00044 class Vector2f : public TVector2<float, Vector2f>
00045 {
00046
00047
00048
00049 public:
00051 f_inline Vector2f() : TVector2<float, Vector2f>() { }
00052 f_inline Vector2f(float x, float y) : TVector2<float, Vector2f>(x, y) { }
00053 explicit Vector2f(const float *f) : TVector2<float, Vector2f>() { SetData(f); }
00054 f_inline Vector2f(const Vector2f &v) : TVector2<float, Vector2f>(v) { }
00055 explicit Vector2f(float f) : TVector2<float, Vector2f>() { Fill(f); }
00056
00057 float GetAngleRad() const
00058 {
00059 const double length = Length();
00060 if (length == 0)
00061 {
00062 return 0.0;
00063 }
00064
00065 double rad = gArcCos(Get(0) / length);
00066
00067 if (Get(1) < 0)
00068 {
00069 rad = 2*M_PI - rad;
00070 }
00071
00072 return rad;
00073 }
00074
00075 float GetAngleDeg() const
00076 {
00077 return gRadToDeg(GetAngleRad());
00078 }
00079 };
00080
00081 class Vector3f : public TVector3<float, Vector3f>
00082 {
00083
00084
00085
00086 public:
00088 f_inline Vector3f() : TVector3<float, Vector3f>() { }
00089 f_inline Vector3f(float x, float y, float z) : TVector3<float, Vector3f>(x, y, z) { }
00090 explicit Vector3f(const float *f) : TVector3<float, Vector3f>() { SetData(f); }
00091 f_inline Vector3f(const Vector3f &v) : TVector3<float, Vector3f>(v) { }
00092 explicit Vector3f(float f) : TVector3<float, Vector3f>() { Fill(f); }
00093
00094 f_inline Vector3f Reflect(const Vector3f &normal)
00095 {
00096 float fac = 2.0f * (normal.x() * x() + normal.y() * y() + normal.z() * z());
00097 return Vector3f(fac * normal.x()-x(),
00098 fac * normal.y()-y(),
00099 fac * normal.z()-z());
00100 }
00101
00102 f_inline void RandomUnitVector()
00103 {
00104 x() = 1.0f - 2.0f*rand()/(float)RAND_MAX;
00105 y() = 1.0f - 2.0f*rand()/(float)RAND_MAX;
00106 z() = 1.0f - 2.0f*rand()/(float)RAND_MAX;
00107
00108 Normalize();
00109 }
00110 };
00111
00112 }
00113
00114 #endif //SALT_VECTOR_H