Line # Revision Author
1 1 moose #ifndef __XGL_VECTOR_H__
2 #define __XGL_VECTOR_H__
3
4 #include <math.h>
5
6 #ifndef MAX
7 #define MAX(a, b) ((a) > (b) ? (a) : (b))
8 #endif
9 #ifndef MIN
10 #define MIN(a, b) ((a) < (b) ? (a) : (b))
11 #endif
12
13 class Vector
14 {
15 public:
16 double x, y, z;
17
18 // CtorVector3DDtor
19 Vector() : x(0), y(0), z(0)
20 {
21 }
22 Vector (float _c) : x(_c), y(_c), z(_c)
23 {
24 }
25 Vector(float _x, float _y, float _z) : x(_x), y(_y), z(_z)
26 {
27 }
28 Vector(const Vector &v)
29 {
30 x = v.x;
31 y = v.y;
32 z = v.z;
33 }
34
35 // Operators
36 inline Vector operator += (Vector v)
37 {
38 x += v.x;
39 y += v.y;
40 z += v.z;
41
42 return *this;
43 }
44 inline Vector operator + (Vector v)
45 {
46 return Vector(x + v.x, y + v.y, z + v.z);
47 }
48 inline Vector operator - ()
49 {
50 return Vector(-x, -y, -z);
51 }
52 inline Vector operator -= (Vector v)
53 {
54 x -= v.x;
55 y -= v.y;
56 z -= v.z;
57
58 return *this;
59 }
60 inline Vector operator - (Vector v)
61 {
62 return Vector(x - v.x, y - v.y, z - v.z);
63 }
64 inline Vector operator *= (float f)
65 {
66 x *= f;
67 y *= f;
68 z *= f;
69
70 return *this;
71 }
72 inline Vector operator /= (float f)
73 {
74 x /= f;
75 y /= f;
76 z /= f;
77
78 return *this;
79 }
80 inline Vector operator * (float f)
81 {
82 return Vector(x * f, y * f, z * f);
83 }
84 inline Vector operator / (float f)
85 {
86 return Vector(x / f, y / f, z / f);
87 }
88
89 // Dot and cross product
90 inline float Dot(Vector v)
91 {
92 return x*v.x + y*v.y + z*v.z;
93 }
94 inline Vector Cross(Vector v)
95 {
96 Vector t;
97
98 // a x b = (ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx)
99 t.x = y * v.z - z * v.y;
100 t.y = z * v.x - x * v.z;
101 t.z = x * v.y - y * v.x;
102
103 return t;
104 }
105
106 // Some utilities
107 inline Vector Normalize()
108 {
109 float r = sqrt(x*x + y*y + z*z);
110 x /= r;
111 y /= r;
112 z /= r;
113
114 return *this;
115 }
116
117 inline void Clamp(float min, float max)
118 {
119 x = MIN(MAX(min, x), max);
120 y = MIN(MAX(min, y), max);
121 z = MIN(MAX(min, z), max);
122 }
123 };
124
125 #endif /* __XGL_VECTOR_H__ */