Qubet  1.0
The ultimate QGL addicting videogame!
 All Classes Functions Variables Pages
vector3f.h
1 // Qubet - Copyright (C) 2011
2 // Enrico Bacis
3 // Daniele Ciriello
4 
5 // Qubet is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef VECTOR3F_H
20 #define VECTOR3F_H
21 
22 #include <QtOpenGL>
23 #include <math.h>
24 
31 struct Vector3f
32 {
33  GLfloat x;
34  GLfloat y;
35  GLfloat z;
37  //Constructors
42  {
43  x = 0;
44  y = 0;
45  z = 0;
46  }
47 
53  Vector3f(const Vector3f &v)
54  {
55  x = v.x;
56  y = v.y;
57  z = v.z;
58  }
59 
65  Vector3f(const Vector3f *v)
66  {
67  x = v->x;
68  y = v->y;
69  z = v->z;
70  }
71 
79  Vector3f(const GLfloat _x, const GLfloat _y, const GLfloat _z)
80  {
81  x = _x;
82  y = _y;
83  z = _z;
84  }
85 
91  Vector3f(const GLfloat a[3])
92  {
93  x = a[0];
94  y = a[1];
95  z = a[2];
96  }
97 
103  inline GLvoid operator=(const GLfloat a[3])
104  {
105  x = a[0];
106  y = a[1];
107  z = a[2];
108  }
109 
115  inline GLvoid operator+=(const Vector3f a)
116  {
117  x += a.x;
118  y += a.y;
119  z += a.z;
120  }
121 
127  inline GLvoid copyInto(GLfloat a[3])
128  {
129  x = a[0];
130  y = a[1];
131  z = a[2];
132  }
133 
140  inline GLfloat &operator[](GLint i)
141  {
142  switch(i)
143  {
144  case 0:
145  return x;
146 
147  case 1:
148  return y;
149 
150  case 2:
151  return z;
152  }
153 
154  }
155 
162  inline const GLfloat &operator[](GLint i) const
163  {
164  switch(i)
165  {
166  case 0:
167  return x;
168 
169  case 1:
170  return y;
171 
172  case 2:
173  return z;
174  }
175  }
176 
185  inline GLfloat lengthSq()
186  {
187  return pow(x,2) + pow(y,2) + pow(z,2);
188  }
189 
199  inline GLfloat length()
200  {
201  return sqrt(lengthSq());
202  }
203 
207  inline GLvoid normalize()
208  {
209  GLfloat l = length();
210  x = x/l;
211  y = y/l;
212  z = z/l;
213  }
214 
218  inline GLvoid zero()
219  {
220  x = 0;
221  y = 0;
222  z = 0;
223  }
224 
225  //check if it is zero
231  inline GLboolean isZero()
232  {
233  if ((x == 0) && (y == 0) && (z == 0))
234  return true;
235  else
236  return false;
237  }
238 
246  inline GLvoid set(const GLfloat _x, const GLfloat _y, const GLfloat _z)
247  {
248  x = _x;
249  y = _y;
250  z = _z;
251  }
252 
253  // Products
254 
261  inline GLfloat dot(const Vector3f &v)
262  {
263  return x*v.x + y*v.y + z*v.z;
264  }
265 
272  inline Vector3f *cross(const Vector3f &v)
273  {
274  GLfloat newx = y*v.z - z*v.y;
275  GLfloat newy = z*v.x - x*v.z;
276  GLfloat newz = x*v.y - y*v.x;
277  return new Vector3f(newx, newy, newz);
278  }
279 
280  // Projections
281 
285  inline GLvoid projectOnXY()
286  {
287  z = 0;
288  }
289 
293  inline GLvoid projectOnYX()
294  {
295  return projectOnXY();
296  }
297 
301  inline GLvoid projectOnXZ()
302  {
303  y = 0;
304  }
305 
309  inline GLvoid projectOnZX()
310  {
311  return projectOnXZ();
312  }
313 
317  inline GLvoid projectOnYZ()
318  {
319  x = 0;
320  }
321 
325  inline GLvoid projectOnZY()
326  {
327  return projectOnYZ();
328  }
329 
330  // Rotations
331 
337  inline GLvoid rotateX(const GLfloat angle)
338  {
339  GLfloat newy = y*cos(angle) - z*sin(angle);
340  z = y*sin(angle) + z*cos(angle);
341  y = newy;
342  }
343 
349  inline GLvoid rotateY(const GLfloat angle)
350  {
351  GLfloat newx = x*cos(angle) + z*sin(angle);
352  z = -x*sin(angle) + z*cos(angle);
353  x = newx;
354  }
355 
361  inline GLvoid rotateZ(const GLfloat angle)
362  {
363  GLfloat newx = x*cos(angle) - y*sin(angle);
364  y = x*sin(angle) + y*cos(angle);
365  x = newx;
366  }
367 
374  inline GLvoid rotate(const Vector3f &axis, const GLfloat angle)
375  {
376  // calcolo la phi per portare il sistema di riferimento di axis sul piano YZ
377  Vector3f axy = new Vector3f(axis);
378  axy.projectOnXY();
379  GLfloat phi = atan2(axy.x, axy.y);
380 
381  // ruoto il sistema di riferimento sul piano YZ
382  Vector3f *a1 = new Vector3f(axis);
383  a1->rotateZ(phi);
384  rotateZ(phi);
385 
386  // calcolo la theta per spostare axis sull'asse Z
387  GLfloat theta = atan2(a1->y, a1->z);
388 
389  // ruoto solo il punto nel nuovo sistema di riferimento, l'asse ora è Z
390  rotateX(theta);
391 
392  // effettuo la rotazione richiesta
393  rotateZ(angle);
394 
395  // torno al sistema di riferimento originale di axis
396  rotateX(-theta);
397  rotateZ(-phi);
398  }
399 
406  {
407  Vector3f *ortho;
408  if ((x == 0) && (z == 0))
409  ortho = new Vector3f(1, 0, 0);
410 
411  else if (z == 0)
412  ortho = new Vector3f(0, 0, 1);
413 
414  else
415  ortho = new Vector3f(1, 0, -x/z);
416 
417  ortho->normalize();
418  return ortho;
419  }
420 
426  inline QString toString()
427  {
428  return QString("x=" + QString::number(x) + " y=" + QString::number(y) + " z=" + QString::number(z));
429  }
430 };
431 
432 // Operators
433 
442 inline GLboolean operator==(const Vector3f &v1, const Vector3f &v2)
443 {
444  if ((v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z))
445  return true;
446  else
447  return false;
448 }
449 
458 inline Vector3f operator+(const Vector3f &v1, const Vector3f &v2)
459 {
460  return Vector3f(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
461 }
462 
471 inline Vector3f operator-(const Vector3f &v1, const Vector3f &v2)
472 {
473  return Vector3f(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
474 }
475 
484 inline Vector3f operator*(const Vector3f &v, const GLfloat &a)
485 {
486  return Vector3f(a*v.x, a*v.y, a*v.z);
487 }
488 
497 inline Vector3f operator*(const GLfloat &a, const Vector3f &v)
498 {
499  return operator*(v, a);
500 }
501 
509 inline Vector3f operator-(const Vector3f &v)
510 {
511  return operator*(v, -1);
512 }
513 
514 #endif // VECTOR3F_H
GLfloat x
Definition: vector3f.h:33
GLvoid rotateX(const GLfloat angle)
Rotate on the x axis.
Definition: vector3f.h:337
GLvoid rotateY(const GLfloat angle)
Rotate on the y axis.
Definition: vector3f.h:349
GLvoid projectOnZX()
Compute the ZX plan projection.
Definition: vector3f.h:309
GLfloat z
Definition: vector3f.h:35
Vector3f(const GLfloat _x, const GLfloat _y, const GLfloat _z)
Create a Vector3f using the coordinates of parameters.
Definition: vector3f.h:79
GLvoid projectOnXZ()
Compute the XZ plan projection.
Definition: vector3f.h:301
GLfloat length()
Compute the length of the vector.
Definition: vector3f.h:199
GLvoid rotateZ(const GLfloat angle)
Rotate on the z axis.
Definition: vector3f.h:361
GLvoid rotate(const Vector3f &axis, const GLfloat angle)
Rotate on the parameter given axis.
Definition: vector3f.h:374
GLvoid set(const GLfloat _x, const GLfloat _y, const GLfloat _z)
Set the vector coordinates.
Definition: vector3f.h:246
GLvoid projectOnYX()
Compute the YX plan projection.
Definition: vector3f.h:293
GLvoid zero()
Set all cordinates of the vector to zero.
Definition: vector3f.h:218
GLfloat dot(const Vector3f &v)
Compute the dot product of the vector with the vector v.
Definition: vector3f.h:261
GLvoid projectOnZY()
Compute the ZY plan projection.
Definition: vector3f.h:325
Vector3f * cross(const Vector3f &v)
Compute the cross product of the vector with the vector v.
Definition: vector3f.h:272
Vector3f()
Create a new Vector3f in coordinates (0, 0, 0).
Definition: vector3f.h:41
GLvoid projectOnYZ()
Compute the YZ plan projection.
Definition: vector3f.h:317
GLvoid copyInto(GLfloat a[3])
Copy the array a content in the vector.
Definition: vector3f.h:127
Structure that rapresents a Vector of float in a 3-D space.
Definition: vector3f.h:31
GLfloat & operator[](GLint i)
Operator [] overloading function.
Definition: vector3f.h:140
Vector3f(const Vector3f *v)
Create a new Vector3f copying the coordinates of v.
Definition: vector3f.h:65
Vector3f(const Vector3f &v)
Create a new Vector3f copying the coordinates of v.
Definition: vector3f.h:53
Vector3f(const GLfloat a[3])
Create a Vector3f using the coordinates of array a[].
Definition: vector3f.h:91
GLvoid operator=(const GLfloat a[3])
Operator = overloading function.
Definition: vector3f.h:103
GLvoid projectOnXY()
Compute the XY plan projection.
Definition: vector3f.h:285
GLfloat y
Definition: vector3f.h:34
GLvoid normalize()
Normalize a vector.
Definition: vector3f.h:207
GLvoid operator+=(const Vector3f a)
Operator += overloading function.
Definition: vector3f.h:115
GLboolean isZero()
Check if the vector is (0, 0, 0).
Definition: vector3f.h:231
GLfloat lengthSq()
Compute the length^2 of the vector.
Definition: vector3f.h:185
QString toString()
Return the QString of coordinates.
Definition: vector3f.h:426
const GLfloat & operator[](GLint i) const
Operator [] overloading function.
Definition: vector3f.h:162
Vector3f * getOneOrthogonal()
Compute one of the orthogonal vectors of the vector.
Definition: vector3f.h:405