Neo  0.5.0
Developer Documentation
Vector3.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 __VECTOR3_H
27 #define __VECTOR3_H
28 
29 namespace Neo
30 {
31 class NEO_CORE_EXPORT Vector3
32 {
33 public:
34 
35  float x;
36  float y;
37  float z;
38 
39 public:
40 
41  Vector3(void):
42  x(0.0f),
43  y(0.0f),
44  z(0.0f)
45  {}
46 
47  Vector3(float newX, float newY, float newZ):
48  x(newX),
49  y(newY),
50  z(newZ)
51  {}
52 
53  Vector3(const float value):
54  x(value),
55  y(value),
56  z(value)
57  {}
58 
59  Vector3(const float * values):
60  x(*(values)),
61  y(*(values+1)),
62  z(*(values+2))
63  {}
64 
65  Vector3(const Vector3 & vec):
66  x(vec.x),
67  y(vec.y),
68  z(vec.z)
69  {}
70 
71  Vector3(const Vector4 & vec);
72 
73  Vector3(const Vector2 & vec):
74  x(vec.x),
75  y(vec.y),
76  z(0)
77  {}
78 
79 public:
80 
81  Vector3 add(const Vector3 & vec) const
82  {
83  return Vector3(x + vec.x, y + vec.y, z + vec.z);
84  }
85 
86  Vector3 substract(const Vector3 & vec) const
87  {
88  return Vector3(x - vec.x, y - vec.y, z - vec.z);
89  }
90 
91  Vector3 multiply(const float value) const
92  {
93  return Vector3(x * value, y * value, z * value);
94  }
95 
96  Vector3 divide(const float value) const
97  {
98  if (value == 0.0f) return Vector3(0.0f, 0.0f, 0.0f);
99  return Vector3(x / value, y / value, z / value);
100  }
101 
102  bool equals(const Vector3 & vec) const
103  {
104  return !((*this) == vec);
105  }
106 
107  inline Vector3 operator + (const Vector3 & vec) const
108  {
109  return Vector3(x + vec.x, y + vec.y, z + vec.z);
110  }
111 
112  inline Vector3 operator - (const Vector3 & vec) const
113  {
114  return Vector3(x - vec.x, y - vec.y, z - vec.z);
115  }
116 
117  inline Vector3 operator + (const float & value) const
118  {
119  return Vector3(x + value, y + value, z + value);
120  }
121 
122  inline Vector3 operator - (const float & value) const
123  {
124  return Vector3(x - value, y - value, z - value);
125  }
126 
127  inline Vector3 operator * (const float value) const
128  {
129  return Vector3(x * value, y * value, z * value);
130  }
131 
132  inline Vector3 operator * (const Vector3 & vec) const
133  {
134  return Vector3(x * vec.x, y * vec.y, z * vec.z);
135  }
136 
137  inline Vector3 operator / (const Vector3 & vec) const
138  {
139  return Vector3(x / vec.x, y / vec.y, z / vec.z);
140  }
141 
142  inline Vector3 operator / (const float value) const
143  {
144  if(value == 0) return Vector3(0.0f, 0.0f, 0.0f);
145  float i = 1.0f / value;
146  return Vector3(x*i, y*i, z*i);
147  }
148 
149  friend inline Vector3 operator * (float factor, const Vector3 & vec)
150  {
151  return vec * factor;
152  }
153 
154  inline bool operator == (const Vector3 & vec) const
155  {
156  if(x == vec.x && y == vec.y && z == vec.z)
157  return true;
158 
159  return false;
160  }
161 
162  inline bool operator != (const Vector3 & vec) const
163  {
164  return !((*this) == vec);
165  }
166 
167  inline void operator += (const Vector3 & vec)
168  {
169  x += vec.x;
170  y += vec.y;
171  z += vec.z;
172  }
173 
174  inline void operator -= (const Vector3 & vec)
175  {
176  x -= vec.x;
177  y -= vec.y;
178  z -= vec.z;
179  }
180 
181  inline void operator *= (const float value)
182  {
183  x *= value;
184  y *= value;
185  z *= value;
186  }
187 
188  inline void operator = (const float value)
189  {
190  x = value;
191  y = value;
192  z = value;
193  }
194 
195  inline void operator /= (const float value)
196  {
197  if(value == 0.0f)
198  return;
199 
200  float i = 1.0f / value;
201  x *= i;
202  y *= i;
203  z *= i;
204  }
205 
206  inline Vector3 operator - (void) const
207  {
208  return Vector3(-x, -y, -z);
209  }
210 
211  inline Vector3 operator + (void) const
212  {
213  return *this;
214  }
215 
216  inline operator float* () const
217  {
218  return (float*) this;
219  }
220 
221  inline operator const float* () const
222  {
223  return (const float*) this;
224  }
225 
226 public:
227 
228  inline void set(const float newX, const float newY, const float newZ)
229  {
230  x = newX;
231  y = newY;
232  z = newZ;
233  }
234 
235  inline void loadIdentity(void)
236  {
237  x = 0.0f;
238  y = 0.0f;
239  z = 0.0f;
240  }
241 
242  inline Vector3 crossProduct(const Vector3 & vec) const
243  {
244  return Vector3((y*vec.z) - (z*vec.y), (z*vec.x) - (x*vec.z), (x*vec.y) - (y*vec.x));
245  }
246 
247  inline float dotProduct(const Vector3 & vec) const
248  {
249  return (x*vec.x) + (y*vec.y) + (z*vec.z);
250  }
251 
252  void normalize();
253 
254  Vector3 getNormalized() const;
255 
256  inline float getLength() const
257  {
258  return sqrtf((x*x) + (y*y) + (z*z));
259  }
260 
261  inline float getSquaredLength() const
262  {
263  return (x*x) + (y*y) + (z*z);
264  }
265 
266  void rotateX(double angle);
267  void rotateY(double angle);
268  void rotateZ(double angle);
269  void rotateAxis(double angle, const Vector3 & axis);
270 
271  Vector3 getRotatedX(double angle) const;
272  Vector3 getRotatedY(double angle) const;
273  Vector3 getRotatedZ(double angle) const;
274  Vector3 getRotatedAxis(double angle, const Vector3 & axis) const;
275 
276  inline Vector3 lerp(const Vector3 & vec, float factor) const
277  {
278  return ((*this) * (1.0f - factor)) + (vec * factor);
279  }
280 };
281 }
282 
283 #endif
Definition: Vector4.h:31
Vector3(void)
Definition: Vector3.h:41
Vector3 divide(const float value) const
Definition: Vector3.h:96
Vector3 lerp(const Vector3 &vec, float factor) const
Definition: Vector3.h:276
Vector3 multiply(const float value) const
Definition: Vector3.h:91
Definition: Vector3.h:31
bool equals(const Vector3 &vec) const
Definition: Vector3.h:102
Vector3(float newX, float newY, float newZ)
Definition: Vector3.h:47
Vector3 crossProduct(const Vector3 &vec) const
Definition: Vector3.h:242
float getSquaredLength() const
Definition: Vector3.h:261
Vector3 substract(const Vector3 &vec) const
Definition: Vector3.h:86
Vector3(const Vector2 &vec)
Definition: Vector3.h:73
Vector3(const float *values)
Definition: Vector3.h:59
float dotProduct(const Vector3 &vec) const
Definition: Vector3.h:247
Vector3 add(const Vector3 &vec) const
Definition: Vector3.h:81
Definition: Vector2.h:31
float y
Definition: Vector3.h:36
float getLength() const
Definition: Vector3.h:256
Vector3(const Vector3 &vec)
Definition: Vector3.h:65
Vector3(const float value)
Definition: Vector3.h:53
void set(const float newX, const float newY, const float newZ)
Definition: Vector3.h:228
float x
Definition: Vector3.h:35
Definition: Color.h:29
void loadIdentity(void)
Definition: Vector3.h:235
float z
Definition: Vector3.h:37