1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #if !defined(AFX_Vector4_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
22 #define AFX_Vector4_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_
23 
24 #include <common/Vector.h>
25 
26 class Vector4
27 {
28 public:
Vector4()29 	Vector4()
30 	{
31 		V[0] = V[1] = V[2] = V[3] = 0.0f;
32 	}
33 
34 	Vector4(const Vector &v, float a = 1.0f)
35 	{
36 		V[0] = ((Vector &) v)[0];
37 		V[1] = ((Vector &) v)[1];
38 		V[2] = ((Vector &) v)[2];
39 		V[3] = a;
40 	}
41 
Vector4(const Vector4 & c1,const Vector4 & c2,float scal)42 	Vector4(const Vector4 &c1, const Vector4 &c2, float scal)
43 	{
44 		V[0] = (c1[0]*(1-scal) + c2[0]*scal);
45 		V[1] = (c1[1]*(1-scal) + c2[1]*scal);
46 		V[2] = (c1[2]*(1-scal) + c2[2]*scal);
47 		V[3] = (c1[3]*(1-scal) + c2[3]*scal);
48 	}
49 
Vector4(const Vector4 & v)50 	Vector4(const Vector4 &v)
51 	{
52 		V[0] = ((Vector4 &) v)[0];
53 		V[1] = ((Vector4 &) v)[1];
54 		V[2] = ((Vector4 &) v)[2];
55 		V[3] = ((Vector4 &) v)[3];
56 	}
57 
Vector4(const float Pt[4])58 	Vector4(const float Pt[4])
59 	{
60 		V[0] = Pt[0];
61 		V[1] = Pt[1];
62 		V[2] = Pt[2];
63 		V[3] = Pt[3];
64 	}
65 
66 	Vector4(const float ptA, const float ptB, const float ptC, float ptD=0.0f)
67 	{
68 		V[0] = ptA;
69 		V[1] = ptB;
70 		V[2] = ptC;
71 		V[3] = ptD;
72 	}
73 
74 	Vector4(const int ptA, const int ptB, const int ptC, const int ptD=0)
75 	{
76 		V[0] = (float) ptA;
77 		V[1] = (float) ptB;
78 		V[2] = (float) ptC;
79 		V[3] = (float) ptD;
80 	}
81 
zero()82 	void zero()
83 	{
84 		V[0] = V[1] = V[2] = V[3] = 0.0f;
85 	}
86 
87 	bool operator==(const Vector4 &Vin1)
88 	{
89 		return (Vin1.V[0]==V[0] && Vin1.V[1]==V[1] && Vin1.V[2]==V[2] && Vin1.V[3]==V[3]);
90 	}
91 
92 	bool operator!=(const Vector4 &Vin1)
93 	{
94 		return !((*this) == Vin1);
95 	}
96 
97 	Vector4 &operator+=(const Vector4 &qc)
98 	{
99 		V[0] += qc.V[0];
100 		V[1] += qc.V[1];
101 		V[2] += qc.V[2];
102 		V[3] += qc.V[3];
103 
104 		return *this;
105 	}
106 
107 	Vector4 operator*(const Vector4 &qc)
108 	{
109 		Vector4 qa;
110 		Vector4 &qb = *this;
111 
112 		// dQMultiply0 from ODE
113 		qa[0] = qb[0]*qc[0] - qb[1]*qc[1] - qb[2]*qc[2] - qb[3]*qc[3];
114 		qa[1] = qb[0]*qc[1] + qb[1]*qc[0] + qb[2]*qc[3] - qb[3]*qc[2];
115 		qa[2] = qb[0]*qc[2] + qb[2]*qc[0] + qb[3]*qc[1] - qb[1]*qc[3];
116 		qa[3] = qb[0]*qc[3] + qb[3]*qc[0] + qb[1]*qc[2] - qb[2]*qc[1];
117 
118 		return qa;
119 	}
120 
121 	///> component wise linear interpolation
lerp(Vector4 & c1,Vector4 & c2)122 	Vector4 lerp(Vector4 &c1, Vector4 &c2)
123 	{
124 		return Vector4(
125 			c1[0] * (1.0f - V[0]) + c2[0] * V[0],
126 			c1[1] * (1.0f - V[1]) + c2[1] * V[1],
127 			c1[2] * (1.0f - V[2]) + c2[2] * V[2],
128 			c1[3] * (1.0f - V[3]) + c2[3] * V[3]);
129 	}
130 
131 	void Normalize();
132 
133 	// Quaternion maths
134 	void setQuatFromAxisAndAngle(Vector &axis, float angle);
135 	void getRotationMatrix(float *R); // R = float[4*3];
136 	void getOpenGLRotationMatrix(float *R); // R = float[16];
137 	void getRelativeVector(Vector &result, Vector &position);
138 	static void dDQfromW(Vector4 &dq, Vector &w, Vector4 &q);
139 
140 	float &operator[](const int m) { DIALOG_ASSERT(m<=3); return V[m]; }
141 	float const &operator[](const int m) const { DIALOG_ASSERT(m<=3); return V[m]; }
142 
143 	operator float*() { return V; }
144 	static Vector4 &getNullVector();
145 
146 protected:
147 	float V[4];
148 
149 };
150 
151 #endif // !defined(AFX_Vector4_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
152 
153