Blender V2.61 - r43446

AUD_3DMath.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * Copyright 2009-2011 Jörg Hermann Müller
00005  *
00006  * This file is part of AudaSpace.
00007  *
00008  * Audaspace is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * AudaSpace is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Audaspace; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00030 #ifndef AUD_3DMATH
00031 #define AUD_3DMATH
00032 
00033 #include <cmath>
00034 #include <cstring>
00035 
00039 class AUD_Vector3
00040 {
00041 private:
00045     union
00046     {
00047         float m_v[3];
00048         struct
00049         {
00050             float m_x;
00051             float m_y;
00052             float m_z;
00053         };
00054     };
00055 
00056 public:
00063     inline AUD_Vector3(float x = 0, float y = 0, float z = 0) :
00064         m_x(x), m_y(y), m_z(z)
00065     {
00066     }
00067 
00072     inline const float& x() const
00073     {
00074         return m_x;
00075     }
00076 
00081     inline const float& y() const
00082     {
00083         return m_y;
00084     }
00085 
00090     inline const float& z() const
00091     {
00092         return m_z;
00093     }
00094 
00099     inline void get(float* destination) const
00100     {
00101         memcpy(destination, m_v, sizeof(m_v));
00102     }
00103 
00108     inline float* get()
00109     {
00110         return m_v;
00111     }
00112 
00117     inline const float* get() const
00118     {
00119         return m_v;
00120     }
00121 
00126     inline float length() const
00127     {
00128         return sqrt(m_x*m_x + m_y*m_y + m_z*m_z);
00129     }
00130 
00136     inline AUD_Vector3 cross(const AUD_Vector3& op) const
00137     {
00138         return AUD_Vector3(m_y * op.m_z - m_z * op.m_y,
00139                            m_z * op.m_x - m_x * op.m_z,
00140                            m_x * op.m_y - m_y * op.m_x);
00141     }
00142 
00148     inline float operator*(const AUD_Vector3& op) const
00149     {
00150         return m_x * op.m_x + m_y * op.m_y + m_z * op.m_z;
00151     }
00152 
00158     inline AUD_Vector3 operator*(const float& op) const
00159     {
00160         return AUD_Vector3(m_x * op, m_y * op, m_z * op);
00161     }
00162 
00168     inline AUD_Vector3 operator+(const AUD_Vector3& op) const
00169     {
00170         return AUD_Vector3(m_x + op.m_x, m_y + op.m_y, m_z + op.m_z);
00171     }
00172 
00178     inline AUD_Vector3 operator-(const AUD_Vector3& op) const
00179     {
00180         return AUD_Vector3(m_x - op.m_x, m_y - op.m_y, m_z - op.m_z);
00181     }
00182 
00187     inline AUD_Vector3 operator-() const
00188     {
00189         return AUD_Vector3(-m_x, -m_y, -m_z);
00190     }
00191 
00197     inline AUD_Vector3& operator-=(const AUD_Vector3& op)
00198     {
00199         m_x -= op.m_x;
00200         m_y -= op.m_y;
00201         m_z -= op.m_z;
00202         return *this;
00203     }
00204 };
00205 
00209 class AUD_Quaternion
00210 {
00211 private:
00215     union
00216     {
00217         float m_v[4];
00218         struct
00219         {
00220             float m_w;
00221             float m_x;
00222             float m_y;
00223             float m_z;
00224         };
00225     };
00226 
00227 public:
00235     inline AUD_Quaternion(float w = 1, float x = 0, float y = 0, float z = 0) :
00236         m_w(w), m_x(x), m_y(y), m_z(z)
00237     {
00238     }
00239 
00244     inline const float& w() const
00245     {
00246         return m_w;
00247     }
00248 
00253     inline const float& x() const
00254     {
00255         return m_x;
00256     }
00257 
00262     inline const float& y() const
00263     {
00264         return m_y;
00265     }
00266 
00271     inline const float& z() const
00272     {
00273         return m_z;
00274     }
00275 
00280     inline void get(float* destination) const
00281     {
00282         memcpy(destination, m_v, sizeof(m_v));
00283     }
00284 
00289     inline float* get()
00290     {
00291         return m_v;
00292     }
00293 
00298     inline const float* get() const
00299     {
00300         return m_v;
00301     }
00302 
00308     inline AUD_Vector3 getLookAt() const
00309     {
00310         return AUD_Vector3(-2 * (m_w * m_y + m_x * m_z),
00311                             2 * (m_x * m_w - m_z * m_y),
00312                             2 * (m_x * m_x + m_y * m_y) - 1);
00313     }
00314 
00320     inline AUD_Vector3 getUp() const
00321     {
00322         return AUD_Vector3(2 * (m_x * m_y - m_w * m_z),
00323                             1 - 2 * (m_x * m_x + m_z * m_z),
00324                             2 * (m_w * m_x + m_y * m_z));
00325     }
00326 };
00327 
00328 #endif //AUD_3DMATH