- Main Page
- Namespaces
- Classes
- Files

# VCRA-SZCZERBACKI-PointBasedRendering/ssVector.hpp

    00001 #ifndef __SSVECTOR_HPP
    00002 #define __SSVECTOR_HPP
    00003
    00004 #if !defined (__SSDEFS_HPP)
    00005 #       include "ssDefs.hpp"
    00006 #endif
    00007
    00008 #include <cmath>                                // sqrt()
    00009
    00010 namespace SS
    00011 {
    00012
    00013 class Matrix3x4;
    00014 class Matrix4x4;
    00015 class Matrix3x3;
    00016 class Matrix2x2;
    00017
    00018
    00019 /******************************************************************************
    00020  *
    00021  * Class:                       SS::Vector2
    00022  *
    00023  * Description:         Two-component vector
    00024  *
    00025  *****************************************************************************/
    00026
    00027 class Vector2
    00028 {
    00029 public:
    00030         float x,y;
    00031 inline                                  Vector2                 (void)                                                  : x(0.0f),y(0.0f)               {}
    00032 inline                                  Vector2                 (float cx, float cy)                    : x(cx),y(cy) {}
    00033 inline  const float&    operator[]		(int i) const                                       { SS_ASSERT (i>=0 && i < 2); return ((const float*)this)[i]; }
    00034 inline  float&                  operator[]		(int i)                                                     { SS_ASSERT (i>=0 && i < 2); return ((float*)this)[i]; }
    00035 inline  Vector2&                clear                   (void)                                                  { x = 0.0f; y = 0.0f; return *this; }
    00036 inline  Vector2&                set                             (float cx, float cy)                    { x = cx;  y = cy; return *this;        }
    00037 inline  bool                    operator==		(const Vector2& s) const            { return (x==s.x)&&(y==s.y); }
    00038 inline  bool                    operator!=		(const Vector2& s) const            { return (x!=s.x)||(y!=s.y); }
    00039 inline  float                   length                  (void) const                                    { return (float)sqrt(x*x+y*y); }
    00040 inline  float                   lengthSqr               (void) const                                    { return (x*x+y*y); }
    00041 inline  Vector2&                normalize               (float len = 1.0f)                              { double l = x*x+y*y; if(l!=0.0) { l = len / sqrt(l); x = (float)(x*l); y = (float)(y*l); } return *this; }
    00042 inline  void                    scale                   (const Vector2& v)                              { x*=v.x, y*=v.y; }
    00043 inline  Vector2&                operator+=		(const Vector2& v)                          { x += v.x, y += v.y; return *this; }
    00044 inline  Vector2&                operator-=		(const Vector2& v)                          { x -= v.x, y -= v.y; return *this; }
    00045 inline  Vector2&                operator*=		(float s)                                           { x = (x*s),   y = (y*s); return *this; }
    00046 inline  Vector2&                operator/=		(float s)                                           { s = (1.0f/s); x = (x*s), y = (y*s); return *this; }
    00047 inline  Vector2&                operator*=		(const Matrix2x2& m);
    00048
    00049 };
    00050
    00051 inline  Vector2                 operator+			(const Vector2& v1, const Vector2& v2)              { return Vector2(v1.x+v2.x, v1.y+v2.y); }
    00052 inline  Vector2                 operator-			(const Vector2& v1, const Vector2& v2)              { return Vector2(v1.x-v2.x, v1.y-v2.y); }
    00053 inline  Vector2                 operator*			(const Vector2& v,  const float s)                  { return Vector2(v.x*s, v.y*s); }
    00054 inline  Vector2                 operator*			(const float s,             const Vector2& v)               { return Vector2(v.x*s, v.y*s); }
    00055 inline  Vector2                 operator/			(const Vector2& v,  const float s)                  { return v*(1.0f/s); }
    00056 inline  Vector2                 operator-			(const Vector2& v)                                                  { return Vector2(-v.x, -v.y); }
    00057                 Vector2                 operator*			(const Vector2& v,  const Matrix2x2& m);
    00058 inline  float                   dot                                     (const Vector2& v1,     const Vector2& v2)              { return (v1.x*v2.x + v1.y*v2.y); }
    00059
    00060 /******************************************************************************
    00061  *
    00062  * Class:                       SS::Vector3
    00063  *
    00064  * Description:         Three-component vector
    00065  *
    00066  *****************************************************************************/
    00067
    00068 class Vector3
    00069 {
    00070 public:
    00071         float   x,y,z;
    00072
    00073 inline                                  Vector3         (void) : x(0.0f),y(0.0f),z(0.0f)                {}
    00074 inline                                  Vector3         (const Vector3& s)                                              { x = s.x; y = s.y; z = s.z; }
    00075 inline                                  Vector3         (float cx, float cy, float cz)                  : x(cx),y(cy),z(cz) {}
    00076 inline  const float&    operator[]	(int i) const                                                        { SS_ASSERT (i>=0 && i < 3); return ((const float*)this)[i]; }
    00077 inline  float&                  operator[]	(int i)                                                                      { SS_ASSERT (i>=0 && i < 3); return ((float*)this)[i]; }
    00078 inline  Vector3&                clear           (void)                                                                  { x = 0.0f; y = 0.0f; z = 0.0f; return *this; }
    00079 inline  Vector3&                set                     (float cx, float cy, float cz)                  { x = cx;  y = cy;  z = cz; return *this;       }
    00080 inline  Vector3&                operator+=	(const Vector3& v)                                           { x += v.x, y += v.y, z += v.z; return *this; }
    00081 inline  Vector3&                operator-=	(const Vector3& v)                                           { x -= v.x, y -= v.y, z -= v.z; return *this; }
    00082 inline  Vector3&                operator*=	(float s)                                                            { x = (x*s),   y = (y*s), z = (z*s); return *this; }
    00083 inline  Vector3&                operator/=	(float s)                                                            { s = (1.0f/s); x = (x*s), y = (y*s), z = (z*s); return *this; }
    00084 inline  Vector3&                operator*=	(const Matrix3x3& m);
    00085 inline  float                   operator|=	(const Vector3& v) const                             { return (x*v.x + y*v.y + z*v.z); }
    00086 inline  bool                    operator==	(const Vector3& v) const                             { return (x == v.x && y == v.y && z == v.z);    }
    00087 inline  bool                    operator!=	(const Vector3& v) const                             { return !(x == v.x && y == v.y && z == v.z);   }
    00088 inline  float                   length          (void) const                                                    { return (float)sqrt(x*x+y*y+z*z); }
    00089 inline  float                   lengthSqr       (void) const                                                    { return (x*x+y*y+z*z); }
    00090 inline  Vector3&                normalize       (float len = 1.0f)                                              { double l = x*x+y*y+z*z; if(l!=0.0) { l = len / sqrt(l); x = (float)(x*l); y = (float)(y*l); z = (float)(z*l); } return *this; }
    00091 inline  void                    scale           (const Vector3& v)                                              { x*=v.x, y*=v.y, z*=v.z; }
    00092
    00093
    00094 //inline        Vector3&                operator*=      (const Vector3& v)                                              { x = (x*v.x), y = (y*v.y), z = (z*v.z); return *this; }
    00095 //inline        Vector3&                operator/=      (const Vector3& v)                                              { x = (x/v.x), y = (y/v.y), z = (z/v.z); return *this; }
    00096 };
    00097
    00098 inline  Vector3                 operator+			(const Vector3& v1, const Vector3& v2)              { return Vector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
    00099 inline  Vector3                 operator-			(const Vector3& v1, const Vector3& v2)              { return Vector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
    00100 inline  Vector3                 operator*			(const Vector3& v,  const float s)                  { return Vector3(v.x*s, v.y*s, v.z*s); }
    00101 inline  Vector3                 operator*			(const float s,             const Vector3& v)               { return Vector3(v.x*s, v.y*s, v.z*s); }
    00102 inline  Vector3                 operator/			(const Vector3& v,  const float s)                  { return v*(1.0f/s); }
    00103 inline  Vector3                 operator-			(const Vector3& v)                                                  { return Vector3(-v.x, -v.y, -v.z); }
    00104 inline  Vector3                 cross                           (const Vector3& v1,     const Vector3& v2)              { return Vector3 ((v1.y*v2.z)-(v1.z*v2.y), (v1.z*v2.x)-(v1.x*v2.z), (v1.x*v2.y)-(v1.y*v2.x)); }
    00105                 Vector3                 operator*			(const Vector3& v,  const Matrix3x3& m);
    00106 inline  float                   dot                                     (const Vector3& v1,     const Vector3& v2)              { return (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z); }
    00107
    00108
    00109 /******************************************************************************
    00110  *
    00111  * Class:                       SS::Vector4
    00112  *
    00113  * Description:         Four-component vector
    00114  *
    00115  *****************************************************************************/
    00116
    00117 class Vector4
    00118 {
    00119 public:
    00120         float x,y,z,w;
    00121
    00122 inline                                  Vector4         (void)                                                                                  : x(0),y(0),z(0),w(0)                   {}
    00123 inline                                  Vector4         (float cx, float cy, float cz, float cw)                : x(cx),y(cy),z(cz),w(cw)               {}
    00124 inline                                  Vector4         (const Vector3& s, float cw)                                    : x(s.x),y(s.y),z(s.z),w(cw)    {}
    00125 inline                                  Vector4         (const Vector4& s)                                                              : x(s.x),y(s.y),z(s.z),w(s.w)   {}
    00126 inline  Vector4&                clear           (void)                                                                                  { x = (0); y = (0); z = (0); w = (0); return *this; }
    00127 inline  Vector4&                set                     (float cx, float cy, float cz, float cw)                { x = (cx), y = (cy), z = (cz), w = (cw); return *this; }
    00128 inline  const float&    operator[]	(int i) const                                                                        { SS_ASSERT (i>=0 && i < 4); return ((const float*)this)[i]; }
    00129 inline  float&                  operator[]	(int i)                                                                                      { SS_ASSERT (i>=0 && i < 4); return ((float*)this)[i]; }
    00130 inline  bool                    operator==	(const Vector4& v) const                                             { return (x == v.x && y == v.y && z == v.z && w == v.w);        }
    00131 inline  bool                    operator!=	(const Vector4& v) const                                             { return !(x == v.x && y == v.y && z == v.z && w == v.w);       }
    00132 inline  Vector4&                operator+=	(const Vector4& v)                                                           { x += v.x, y += v.y, z += v.z, w += v.w; return *this;         }
    00133 inline  Vector4&                operator-=	(const Vector4& v)                                                           { x -= v.x, y -= v.y, z -= v.z, w -= v.w; return *this;         }
    00134 inline  Vector4&                operator*=	(float s)                                                                            { x = (x*s), y = (y*s), z = (z*s), w = (w*s); return *this; }
    00135 inline  Vector4&                operator/=	(float s)                                                                            { s = (1.0f/s); x = (x*s), y = (y*s), z = (z*s); w = (w*s); return *this; }
    00136 inline  Vector4&                operator*=	(const Matrix4x4& m);
    00137 inline  float                   operator|=	(const Vector4& v) const                                             { return x*v.x + y*v.y + z*v.z + w*v.w; }
    00138 inline  float                   length          (void) const                                                                    { return (float)sqrt( x*x+y*y+z*z+w*w ); }
    00139 inline  float                   lengthSqr       (void) const                                                                    { return ( x*x+y*y+z*z+w*w ); }
    00140 inline  Vector4&                normalize       (double len = 1.0)                                                              { float l = length();   if(l!=0.0) *this *= ((float)(len/l)); return *this; }
    00141 inline  void                    scale           (const Vector4& v)                                                              { x = (x*v.x), y = (y*v.y), z = (z*v.z); w = (w*v.w); }
    00142
    00143 //inline        Vector4&                operator*=      (const Vector4& v)                                                              { x = (x*v.x), y = (y*v.y), z = (z*v.z); w = (w*v.w); return *this; }
    00144 };
    00145
    00146 inline  Vector4                 operator+	(const Vector4& v1, const Vector4& v2)        { return Vector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); }
    00147 inline  Vector4                 operator-	(const Vector4& v1, const Vector4& v2)        { return Vector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); }
    00148 inline  Vector4                 operator*	(const Vector4& v,    float s)                        { return Vector4(v.x*s, v.y*s, v.z*s, v.w*s); }
    00149 inline  Vector4                 operator*	(float s,                     const Vector4& v)       { return v*s; }
    00150 inline  Vector4                 operator/	(const Vector4& v,    float s)                        { SS_ASSERT(s!=0.0f); float r = 1.0f/s; return v*r; }
    00151 inline  Vector4                 operator-	(const Vector4& v)                                            { return Vector4(-v.x, -v.y, -v.z, -v.w); }
    00152                 Vector4                 operator*	(const Vector4& v,  const Matrix4x4& m);
    00153 inline  float                   dot                     (const Vector4& v1, const Vector4& v2)  { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; }
    00154
    00155 } // SS
    00156
    00157 //------------------------------------------------------------------------
    00158 #endif // __SSVECTOR_HPP