1 | /*
|
---|
2 | ---------------------------------------------------------------------
|
---|
3 | This file is part of Sandra Engine (real-time 3D engine)
|
---|
4 | Copyright (C) 2004 Jesus Gumbau Portales
|
---|
5 |
|
---|
6 | This library is free software; you can redistribute it and/or
|
---|
7 | modify it under the terms of the GNU Lesser General Public
|
---|
8 | License as published by the Free Software Foundation; either
|
---|
9 | version 2.1 of the License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | This library 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 GNU
|
---|
14 | Lesser General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU Lesser General Public
|
---|
17 | License along with this library; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
|
---|
19 | USA, or go to http://www.gnu.org/copyleft/lesser.txt.
|
---|
20 | ---------------------------------------------------------------------
|
---|
21 | */
|
---|
22 |
|
---|
23 | // 21-02-2001
|
---|
24 | // Jesus Gumbau Portales
|
---|
25 |
|
---|
26 | #ifndef __VECTOR_H__
|
---|
27 | #define __VECTOR_H__
|
---|
28 |
|
---|
29 | #include <math.h>
|
---|
30 |
|
---|
31 | #ifndef PI
|
---|
32 | #define PI 3.14159f
|
---|
33 | #endif
|
---|
34 | #define GradToRad(x) x*PI/180.0f
|
---|
35 |
|
---|
36 | class Vector
|
---|
37 | {
|
---|
38 | public:
|
---|
39 | Vector(double vx=0.0f, double vy=0.0f, double vz=0.0f, double vw=1.0f){ x=vx; y=vy; z=vz; w=vw; }
|
---|
40 | double x,y,z,w;
|
---|
41 | inline Vector operator*(double f) const { return Vector(x*f,y*f,z*f); }
|
---|
42 | inline void operator+=(const Vector &v){ x+=v.x; y+=v.y; z+=v.z; }
|
---|
43 | inline void operator+=(double f){ x+=f; y+=f; z+=f; }
|
---|
44 | inline void operator-=(const Vector &v){ x-=v.x; y-=v.y; z-=v.z; }
|
---|
45 | inline void operator-=(double f){ x-=f; y-=f; z-=f; }
|
---|
46 | inline Vector operator+(const Vector &v) const { return Vector(x+v.x,y+v.y,z+v.z); }
|
---|
47 | // Vector operator-(Vector &v){ return Vector(x-v.x,y-v.y,z-v.z); }
|
---|
48 | inline Vector operator-(const Vector &v) const { return Vector(x-v.x,y-v.y,z-v.z); }
|
---|
49 | inline double Module(void) const { return sqrtf(float(x*x) + float(y*y) + float(z*z)); }
|
---|
50 | inline bool operator==(const Vector &v) const { return (x==v.x && y==v.y && z==v.z); }
|
---|
51 | inline bool operator!=(const Vector &v) const { return (x!=v.x || y!=v.y || z!=v.z); }
|
---|
52 | inline Vector operator-(void) const { return Vector(-x,-y,-z); }
|
---|
53 | inline Vector operator/(double f) const { return (*this)*(1/f); }
|
---|
54 | inline Vector operator/=(double f) { x/=f; y/=f; z/=f; return (*this); }
|
---|
55 | inline Vector operator=(const Vector &v) { x=v.x; y=v.y; z=v.z; w=v.w; return Vector(x,y,z,w); }
|
---|
56 | inline Vector operator=(double f) { x=f; y=f; z=f; return Vector(x,y,z); }
|
---|
57 | inline bool operator<(const Vector &v) const { return (x<v.x && y<v.y && z<v.z && w<v.w); }
|
---|
58 | inline void Normalize(void){
|
---|
59 | double module=sqrtf(float(x*x) + float(y*y) + float(z*z));
|
---|
60 | x=x/module;
|
---|
61 | y=y/module;
|
---|
62 | z=z/module;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | static const Vector UNITY;
|
---|
67 |
|
---|
68 | const double &operator[](unsigned int i) const { return (&x)[i]; }
|
---|
69 | double &operator[](unsigned int i){ return (&x)[i]; }
|
---|
70 | };
|
---|
71 |
|
---|
72 | #endif
|
---|
73 |
|
---|