Initial revision
[0ad:0ad.git] / terrain / Vector3D.cpp
1 //***********************************************************
2 //
3 // Name:                Vector3D.Cpp
4 // Last Update: 28/1/02
5 // Author:              Poya Manouchehri
6 //
7 // Description: Provides an interface for a vector in R3 and
8 //                              allows vector and scalar operations on it
9 //
10 //***********************************************************
11
12 #include "Vector3D.H"
13
14 CVector3D::CVector3D ()
15 {
16         X = Y = Z = 0.0f;
17 }
18
19 CVector3D::CVector3D (float x, float y, float z)
20 {
21         X = x;
22         Y = y;
23         Z = z;
24 }
25
26 int CVector3D::operator == (CVector3D &vector)
27 {
28         if (X != vector.X ||
29                 Y != vector.Y ||
30                 Z != vector.Z)
31                 
32                 return 0;
33         
34         return 1;
35 }
36
37 int CVector3D::operator != (CVector3D &vector)
38 {
39         if (X != vector.X ||
40                 Y != vector.Y ||
41                 Z != vector.Z)
42                 
43                 return 1;
44         
45         return 0;
46 }
47
48 int CVector3D::operator ! ()
49 {
50         if (X != 0.0f ||
51                 Y != 0.0f ||
52                 Z != 0.0f)
53                 
54                 return 0;
55         
56         return 1;
57 }
58
59 //vector addition
60 CVector3D CVector3D::operator + (CVector3D &vector)
61 {
62         CVector3D Temp;
63
64         Temp.X = X + vector.X;
65         Temp.Y = Y + vector.Y;
66         Temp.Z = Z + vector.Z;
67
68         return Temp;
69 }
70
71 //vector addition/assignment
72 CVector3D &CVector3D::operator += (CVector3D &vector)
73 {
74         X += vector.X;
75         Y += vector.Y;
76         Z += vector.Z;
77
78         return *this;
79 }
80
81 //vector subtraction
82 CVector3D CVector3D::operator - (CVector3D &vector)
83 {
84         CVector3D Temp;
85
86         Temp.X = X - vector.X;
87         Temp.Y = Y - vector.Y;
88         Temp.Z = Z - vector.Z;
89
90         return Temp;
91 }
92
93 //vector subtrcation/assignment
94 CVector3D &CVector3D::operator -= (CVector3D &vector)
95 {
96         X -= vector.X;
97         Y -= vector.Y;
98         Z -= vector.Z;
99
100         return *this;
101 }
102
103 //scalar multiplication
104 CVector3D CVector3D::operator * (float value)
105 {
106         CVector3D Temp;
107
108         Temp.X = X * value;
109         Temp.Y = Y * value;
110         Temp.Z = Z * value;
111
112         return Temp;
113 }
114
115 //scalar multiplication/assignment
116 CVector3D CVector3D::operator *= (float value)
117 {
118         X *= value;
119         Y *= value;
120         Z *= value;
121
122         return *this;
123 }
124
125 void CVector3D::Set (float x, float y, float z)
126 {
127         X = x;
128         Y = y;
129         Z = z;
130 }
131
132 void CVector3D::Clear ()
133 {
134         X = Y = Z = 0.0f;
135 }
136
137 //Dot product
138 float CVector3D::Dot (CVector3D &vector)
139 {
140         return ( X * vector.X +
141                          Y * vector.Y +
142                          Z * vector.Z );
143 }
144
145 //Cross product
146 CVector3D CVector3D::Cross (CVector3D &vector)
147 {
148         CVector3D Temp;
149
150         Temp.X = (Y * vector.Z) - (Z * vector.Y);
151         Temp.Y = (Z * vector.X) - (X * vector.Z);
152         Temp.Z = (X * vector.Y) - (Y * vector.X);
153
154         return Temp;
155 }
156
157 float CVector3D::GetLength ()
158 {
159         return sqrtf ( SQR(X) + SQR(Y) + SQR(Z) );
160 }
161
162 void CVector3D::Normalize ()
163 {
164         float scale = 1.0f/GetLength ();
165
166         X *= scale;
167         Y *= scale;
168         Z *= scale;
169 }
170
171 SColor4ub CVector3D::ConvertToColor (float alpha_factor)
172 {
173     SColor4ub color;
174
175         color.R = (unsigned char)(127.0f * X + 128.0f);
176         color.G = (unsigned char)(127.0f * Y + 128.0f);
177         color.B = (unsigned char)(127.0f * Z + 128.0f);
178         color.A = (unsigned char)(255.0f * alpha_factor);
179
180         return color;
181 }