Vector4.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #if !defined(AFX_Vector4_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
00022 #define AFX_Vector4_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_
00023 
00024 #include <common/Vector.h>
00025 
00026 class Vector4  
00027 {
00028 public:
00029         Vector4()
00030         {
00031                 V[0] = V[1] = V[2] = V[3] = 0.0f;
00032         }
00033 
00034         Vector4(const Vector &v, float a = 1.0f)
00035         {
00036                 V[0] = ((Vector &) v)[0];
00037                 V[1] = ((Vector &) v)[1];
00038                 V[2] = ((Vector &) v)[2];
00039                 V[3] = a;
00040         }
00041 
00042         Vector4(const Vector4 &c1, const Vector4 &c2, float scal) 
00043         {
00044                 V[0] = (c1[0]*(1-scal) + c2[0]*scal);
00045                 V[1] = (c1[1]*(1-scal) + c2[1]*scal);
00046                 V[2] = (c1[2]*(1-scal) + c2[2]*scal);
00047                 V[3] = (c1[3]*(1-scal) + c2[3]*scal);
00048         }
00049 
00050         Vector4(const Vector4 &v)
00051         {
00052                 V[0] = ((Vector4 &) v)[0];
00053                 V[1] = ((Vector4 &) v)[1];
00054                 V[2] = ((Vector4 &) v)[2];
00055                 V[3] = ((Vector4 &) v)[3];
00056         }
00057 
00058         Vector4(const float Pt[4])
00059         {
00060                 V[0] = Pt[0];
00061                 V[1] = Pt[1];
00062                 V[2] = Pt[2];
00063                 V[3] = Pt[3];
00064         }
00065 
00066         Vector4(const float ptA, const float ptB, const float ptC, float ptD=0.0f)
00067         {
00068                 V[0] = ptA;
00069                 V[1] = ptB;
00070                 V[2] = ptC;
00071                 V[3] = ptD;
00072         }
00073 
00074         Vector4(const int ptA, const int ptB, const int ptC, const int ptD=0)
00075         {
00076                 V[0] = (float) ptA;
00077                 V[1] = (float) ptB;
00078                 V[2] = (float) ptC;
00079                 V[3] = (float) ptD;
00080         }
00081 
00082         void zero()
00083         {
00084                 V[0] = V[1] = V[2] = V[3] = 0.0f;
00085         }
00086 
00087         bool operator==(const Vector4 &Vin1)
00088         {
00089                 return (Vin1.V[0]==V[0] && Vin1.V[1]==V[1] && Vin1.V[2]==V[2] && Vin1.V[3]==V[3]);
00090         }
00091 
00092         bool operator!=(const Vector4 &Vin1)
00093         {
00094                 return !((*this) == Vin1);
00095         }
00096 
00097         Vector4 &operator+=(const Vector4 &qc)
00098         {
00099                 V[0] += qc.V[0];
00100                 V[1] += qc.V[1];
00101                 V[2] += qc.V[2];
00102                 V[3] += qc.V[3];
00103 
00104                 return *this;
00105         }
00106 
00107         Vector4 operator*(const Vector4 &qc)
00108         {
00109                 Vector4 qa;
00110                 Vector4 &qb = *this;
00111                 
00112                 // dQMultiply0 from ODE
00113                 qa[0] = qb[0]*qc[0] - qb[1]*qc[1] - qb[2]*qc[2] - qb[3]*qc[3];
00114                 qa[1] = qb[0]*qc[1] + qb[1]*qc[0] + qb[2]*qc[3] - qb[3]*qc[2];
00115                 qa[2] = qb[0]*qc[2] + qb[2]*qc[0] + qb[3]*qc[1] - qb[1]*qc[3];
00116                 qa[3] = qb[0]*qc[3] + qb[3]*qc[0] + qb[1]*qc[2] - qb[2]*qc[1];
00117         
00118                 return qa;
00119         }
00120 
00121         ///> component wise linear interpolation
00122         Vector4 lerp(Vector4 &c1, Vector4 &c2) 
00123         {
00124                 return Vector4(
00125                         c1[0] * (1.0f - V[0]) + c2[0] * V[0],
00126                         c1[1] * (1.0f - V[1]) + c2[1] * V[1],
00127                         c1[2] * (1.0f - V[2]) + c2[2] * V[2],
00128                         c1[3] * (1.0f - V[3]) + c2[3] * V[3]);
00129         }
00130 
00131         void Normalize();
00132 
00133         // Quaternion maths
00134         void setQuatFromAxisAndAngle(Vector &axis, float angle);
00135         void getRotationMatrix(float *R); // R = float[4*3];
00136         void getOpenGLRotationMatrix(float *R); // R = float[16];
00137         void getRelativeVector(Vector &result, Vector &position);
00138         static void dDQfromW(Vector4 &dq, Vector &w, Vector4 &q);
00139 
00140         float &operator[](const int m) { DIALOG_ASSERT(m<=3); return V[m]; }
00141         float const &operator[](const int m) const { DIALOG_ASSERT(m<=3); return V[m]; }
00142 
00143         operator float*() { return V; }
00144         static Vector4 &getNullVector();
00145 
00146 protected:
00147         float V[4];
00148 
00149 };
00150 
00151 #endif // !defined(AFX_Vector4_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
00152 

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3