Neo  0.5.0
Developer Documentation
Vector4.h
Go to the documentation of this file.
1 //========================================================================
2 // Copyright (c) 2003-2011 Anael Seghezzi <www.maratis3d.com>
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would
15 // be appreciated but is not required.
16 //
17 // 2. Altered source versions must be plainly marked as such, and must not
18 // be misrepresented as being the original software.
19 //
20 // 3. This notice may not be removed or altered from any source
21 // distribution.
22 //
23 //========================================================================
24 
25 
26 #ifndef __VECTOR4_H
27 #define __VECTOR4_H
28 
29 namespace Neo
30 {
31 class NEO_CORE_EXPORT Vector4
32 {
33 public:
34 
35  float x;
36  float y;
37  float z;
38  float w;
39 
40 public:
41 
42  Vector4(void):
43  x(0.0f),
44  y(0.0f),
45  z(0.0f),
46  w(0.0f)
47  {}
48 
49  Vector4(float newX, float newY, float newZ, float newW):
50  x(newX),
51  y(newY),
52  z(newZ),
53  w(newW)
54  {}
55 
56  Vector4(const float value):
57  x(value),
58  y(value),
59  z(value),
60  w(value)
61  {}
62 
63  Vector4(const float * values):
64  x(*(values)),
65  y(*(values + 1)),
66  z(*(values + 2)),
67  w(*(values + 3))
68  {}
69 
70  Vector4(const Vector4 & vec):
71  x(vec.x),
72  y(vec.y),
73  z(vec.z),
74  w(vec.w)
75  {}
76 
77  Vector4(const Vector3 & vec):
78  x(vec.x),
79  y(vec.y),
80  z(vec.z),
81  w(1.0f)
82  {}
83 
84  Vector4(const Color & color);
85 
86 public:
87 
88  Vector4 add(const Vector4 & vec) const
89  {
90  return Vector4(x + vec.x, y + vec.y, z + vec.z, w + vec.w);
91  }
92 
93  Vector4 substract(const Vector4 & vec) const
94  {
95  return Vector4(x - vec.x, y - vec.y, z - vec.z, w - vec.w);
96  }
97 
98  Vector4 multiply(const float value) const
99  {
100  return Vector4(x * value, y * value, z * value, w * value);
101  }
102 
103  Vector4 divide(const float value) const
104  {
105  if (value == 0.0f) return Vector4(0.0f, 0.0f, 0.0f, 0.0f);
106  return Vector4(x / value, y / value, z / value, w / value);
107  }
108 
109  bool equals(const Vector4 & vec) const
110  {
111  return !((*this) == vec);
112  }
113 
114  Vector4 operator + (const Vector4 & vec) const
115  {
116  return Vector4(x + vec.x, y + vec.y, z + vec.z, w + vec.w);
117  }
118 
119  Vector4 operator - (const Vector4 & vec) const
120  {
121  return Vector4(x - vec.x, y - vec.y, z - vec.z, w - vec.w);
122  }
123 
124  Vector4 operator * (const float value) const
125  {
126  return Vector4(x * value, y * value, z * value, w * value);
127  }
128 
129  Vector4 operator / (const float value) const
130  {
131  if(value == 0.0f) return Vector4(0.0f, 0.0f, 0.0f, 0.0f);
132  return Vector4(x / value, y / value, z / value, w / value);
133  }
134 
135  friend Vector4 operator * (float factor, const Vector4 & vec)
136  {
137  return vec * factor;
138  }
139 
140  bool operator == (const Vector4 & vec) const
141  {
142  if(x == vec.x && y == vec.y && z == vec.z && w == vec.w){
143  return true;
144  }
145 
146  return false;
147  }
148 
149  bool operator != (const Vector4 & vec) const
150  {
151  return !((*this) == vec);
152  }
153 
154  void operator += (const Vector4 & vec)
155  {
156  x += vec.x;
157  y += vec.y;
158  z += vec.z;
159  w += vec.w;
160  }
161 
162  void operator -= (const Vector4 & vec)
163  {
164  x -= vec.x;
165  y -= vec.y;
166  z -= vec.z;
167  w -= vec.w;
168  }
169 
170  void operator *= (const float value)
171  {
172  x *= value;
173  y *= value;
174  z *= value;
175  w *= value;
176  }
177 
178  void operator /= (const float value)
179  {
180  if(value == 0.0f){
181  return;
182  }
183  else
184  {
185  x /= value;
186  y /= value;
187  z /= value;
188  w /= value;
189  }
190  }
191 
192  Vector4 operator - (void) const
193  {
194  return Vector4(-x, -y, -z, -w);
195  }
196 
197  Vector4 operator + (void) const
198  {
199  return (*this);
200  }
201 
202  operator float* () const
203  {
204  return (float*) this;
205  }
206 
207  operator const float* () const
208  {
209  return (const float*) this;
210  }
211 
212  operator Vector3();
213 
214 public:
215 
216  void set(float newX, float newY, float newZ, float newW)
217  {
218  x = newX;
219  y = newY;
220  z = newZ;
221  w = newW;
222  }
223 
224  void loadIdentity(void)
225  {
226  x = 0.0f;
227  y = 0.0f;
228  z = 0.0f;
229  w = 0.0f;
230  }
231 
232  float dotProduct(const Vector4 & vec)
233  {
234  return x*vec.x + y*vec.y + z*vec.z + w*vec.w;
235  }
236 
237  Vector4 lerp(const Vector4 & vec, float factor) const
238  {
239  return ((*this) * (1.0f - factor)) + (vec * factor);
240  }
241 };
242 }
243 
244 #endif
Definition: Vector4.h:31
bool equals(const Vector4 &vec) const
Definition: Vector4.h:109
Vector4(const float *values)
Definition: Vector4.h:63
Vector4 substract(const Vector4 &vec) const
Definition: Vector4.h:93
float w
Definition: Vector4.h:38
Definition: Vector3.h:31
float y
Definition: Vector4.h:36
float x
Definition: Vector4.h:35
Vector4(const Vector3 &vec)
Definition: Vector4.h:77
Vector4(const Vector4 &vec)
Definition: Vector4.h:70
float dotProduct(const Vector4 &vec)
Definition: Vector4.h:232
Vector4 lerp(const Vector4 &vec, float factor) const
Definition: Vector4.h:237
Definition: Color.h:31
void loadIdentity(void)
Definition: Vector4.h:224
void set(float newX, float newY, float newZ, float newW)
Definition: Vector4.h:216
Vector4 add(const Vector4 &vec) const
Definition: Vector4.h:88
Vector4(const float value)
Definition: Vector4.h:56
Vector4 divide(const float value) const
Definition: Vector4.h:103
Vector4(void)
Definition: Vector4.h:42
Definition: Color.h:29
Vector4(float newX, float newY, float newZ, float newW)
Definition: Vector4.h:49
float z
Definition: Vector4.h:37
Vector4 multiply(const float value) const
Definition: Vector4.h:98