vec3.hpp

Go to the documentation of this file.
00001 /*
00002     RenderStack  Support library for OpenGL 3+
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 VEC3_HPP
00028 #define VEC3_HPP
00029 
00030 #include <cmath>
00031 
00032 namespace renderstack {
00033 
00035 class vec3
00036 {
00037 public:
00038     vec3(float _x = 0, float _y = 0, float _z = 0)
00039     :   x(_x)
00040     ,   y(_y)
00041     ,   z(_z)
00042     {
00043     }
00044 
00045     vec3 operator+(vec3 const &v) const 
00046     {
00047         return vec3(x + v.x, y + v.y, z + v.z);
00048     }
00049 
00050     vec3 &operator+=(vec3 const &v)
00051     {
00052         x += v.x;
00053         y += v.y;
00054         z += v.z;
00055         return *this;
00056     }
00057 
00058     vec3 operator-(vec3 const &v) const
00059     {
00060         return vec3(x - v.x, y - v.y, z - v.z);
00061     }
00062 
00063     vec3 &operator-=(vec3 const &v) 
00064     {
00065         x -= v.x;
00066         y -= v.y;
00067         z -= v.z;
00068         return *this;
00069     }
00070 
00071     bool operator==(vec3 const &v) const
00072     {
00073         return 
00074             (x == v.x) && 
00075             (y == v.y) && 
00076             (z == v.z);
00077     }
00078     vec3 operator*(float f) const
00079     {
00080         return vec3(f * x, f * y, f * z);
00081     }
00082 
00083     vec3 &operator*=(float f) 
00084     {
00085         x *= f;
00086         y *= f;
00087         z *= f;
00088         return *this;
00089     }
00090 
00091     vec3 operator/(float f) const
00092     {
00093         float inv = 1.0f / f;
00094         return vec3(x * inv, y * inv, z * inv);
00095     }
00096 
00097     vec3 &operator/=(float f)
00098     {
00099         float inv = 1.f / f;
00100         x *= inv; 
00101         y *= inv; 
00102         z *= inv;
00103         return *this;
00104     }
00105 
00106     vec3 operator-() const 
00107     {
00108         return vec3(-x, -y, -z);
00109     }
00110 
00111     float operator[](int i) const 
00112     {
00113         return (&x)[i];
00114     }
00115 
00116     float &operator[](int i)
00117     {
00118         return (&x)[i];
00119     }
00120 
00121     float length_squared() const { return x * x + y * y + z * z; }
00122     float length() const { return std::sqrt(length_squared()); }
00123 
00124     float x;
00125     float y;
00126     float z;
00127 };
00128 
00129 inline float dot(vec3 const &v1, vec3 const &v2)
00130 {
00131     return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
00132 }
00133 
00134 inline vec3 cross(vec3 const &v1, vec3 const &v2)
00135 {
00136     return vec3(
00137         (v1.y * v2.z) - (v1.z * v2.y),
00138         (v1.z * v2.x) - (v1.x * v2.z),
00139         (v1.x * v2.y) - (v1.y * v2.x)
00140     );
00141 }
00142 
00143 inline vec3 normalize(vec3 const &v)
00144 {
00145     return v / v.length();
00146 }
00147 
00148 inline float radian_mod_2pi(float angle_radians)
00149 {
00150     while(angle_radians > RS_TWO_PI) angle_radians -= RS_TWO_PI;
00151     while(angle_radians < 0        ) angle_radians += RS_TWO_PI;
00152 
00153     return angle_radians;
00154 }
00155 
00156 inline vec3 hpb_mod_2pi(vec3 const &hpb)
00157 {
00158     float h = hpb.x;
00159     float p = hpb.y;
00160     float b = hpb.z;
00161 
00162     while(h > RS_TWO_PI) h -= RS_TWO_PI;
00163     while(h < 0        ) h += RS_TWO_PI;
00164     while(p > RS_TWO_PI) p -= RS_TWO_PI;
00165     while(p < 0        ) p += RS_TWO_PI;
00166     while(b > RS_TWO_PI) b -= RS_TWO_PI;
00167     while(b < 0        ) b += RS_TWO_PI;
00168 
00169     return vec3(h, p, b);
00170 }
00171 
00172 } 
00173 
00174 #endif 
00175 
Generated on Sun Apr 11 12:23:09 2010 for RenderStack by  doxygen 1.6.3