vec4.hpp

Go to the documentation of this file.
00001 /*
00002     mesh3d
00003     Copyright (C) 2010  Timo Suoranta
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00027 #ifndef VEC4_HPP
00028 #define VEC4_HPP
00029 
00030 #include <cmath>
00031 
00032 namespace renderstack {
00033 
00035 class vec4
00036 {
00037 public:
00038     vec4(float _x = 0, float _y = 0, float _z = 0, float _w = 0)
00039     :   x(_x)
00040     ,   y(_y)
00041     ,   z(_z)
00042     ,   w(_w)
00043     {
00044     }
00045 
00046     vec4(vec3 const &v3, float _w = 0)
00047     :   x(v3.x)
00048     ,   y(v3.y)
00049     ,   z(v3.z)
00050     ,   w(_w)
00051     {
00052     }
00053 
00054     vec4 operator+(vec4 const &v) const 
00055     {
00056         return vec4(x + v.x, y + v.y, z + v.z, w + v.w);
00057     }
00058 
00059     vec4 &operator+=(vec4 const &v)
00060     {
00061         x += v.x;
00062         y += v.y;
00063         z += v.z;
00064         w += v.w;
00065         return *this;
00066     }
00067 
00068     vec4 operator-(vec4 const &v) const
00069     {
00070         return vec4(x - v.x, y - v.y, z - v.z, w - v.w);
00071     }
00072 
00073     vec4 &operator-=(vec4 const &v) 
00074     {
00075         x -= v.x;
00076         y -= v.y;
00077         z -= v.z;
00078         w -= v.w;
00079         return *this;
00080     }
00081 
00082     bool operator==(vec4 const &v) const
00083     {
00084         return 
00085             (x == v.x) && 
00086             (y == v.y) && 
00087             (z == v.z) &&
00088             (w == v.w);
00089     }
00090     vec4 operator*(float f) const
00091     {
00092         return vec4(f * x, f * y, f * z, f * w);
00093     }
00094 
00095     vec4 &operator*=(float f) 
00096     {
00097         x *= f;
00098         y *= f;
00099         z *= f;
00100         w *= f;
00101         return *this;
00102     }
00103 
00104     vec4 operator/(float f) const
00105     {
00106         float inv = 1.0f / f;
00107         return vec4(x * inv, y * inv, z * inv, w * inv);
00108     }
00109 
00110     vec4 &operator/=(float f)
00111     {
00112         float inv = 1.f / f;
00113         x *= inv;
00114         y *= inv;
00115         z *= inv;
00116         w *= inv;
00117         return *this;
00118     }
00119 
00120     vec4 operator-() const 
00121     {
00122         return vec4(-x, -y, -z, -w);
00123     }
00124 
00125     float operator[](int i) const 
00126     {
00127         return (&x)[i];
00128     }
00129 
00130     float &operator[](int i)
00131     {
00132         return (&x)[i];
00133     }
00134 
00135     float length_squared() const { return x * x + y * y + z * z + w * w; }
00136     float length() const { return std::sqrt(length_squared()); }
00137 
00138     float x;
00139     float y;
00140     float z;
00141     float w;
00142 };
00143 
00144 inline float dot(vec4 const &v1, vec4 const &v2)
00145 {
00146     return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v2.w;
00147 }
00148 
00149 inline vec4 normalize(vec4 const &v)
00150 {
00151     return v / v.length();
00152 }
00153 
00154 inline vec3 homogenize(vec4 const &v)
00155 {
00156     return vec3(v.x / v.w, v.y / v.w, v.z / v.w);
00157 }
00158 
00159 inline vec4 cross(vec4 const &r, vec4 const &s, vec4 const &t)
00160 {
00161     return vec4(
00162         r.y * s.z * t.w + r.z * s.w * t.y + r.w * s.y * t.z - r.y * s.w * t.z - r.z * s.y * t.w - r.w * s.z * t.y,
00163         r.x * s.w * t.z + r.z * s.x * t.w + r.w * s.z * t.x - r.x * s.z * t.w - r.z * s.w * t.x - r.w * s.x * t.z,
00164         r.x * s.y * t.w + r.y * s.w * t.x + r.w * s.x * t.y - r.x * s.w * t.y - r.y * s.x * t.w - r.w * s.y * t.x,
00165         r.x * s.z * t.y + r.y * s.x * t.z + r.z * s.y * t.x - r.x * s.y * t.z - r.y * s.z * t.x - r.z * s.x * t.y
00166     );
00167 }
00168 
00169 } 
00170 
00171 #endif 
00172 
Generated on Sun Apr 11 12:23:09 2010 for RenderStack by  doxygen 1.6.3