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 //***********************************************************
12 #include "Vector3D.H"
14 CVector3D::CVector3D ()
15 {
16         X = Y = Z = 0.0f;
17 }
19 CVector3D::CVector3D (float x, float y, float z)
20 {
21         X = x;
22         Y = y;
23         Z = z;
24 }
26 int CVector3D::operator == (CVector3D &vector)
27 {
28         if (X != vector.X ||
29                 Y != vector.Y ||
30                 Z != vector.Z)
32                 return 0;
34         return 1;
35 }
37 int CVector3D::operator != (CVector3D &vector)
38 {
39         if (X != vector.X ||
40                 Y != vector.Y ||
41                 Z != vector.Z)
43                 return 1;
45         return 0;
46 }
48 int CVector3D::operator ! ()
49 {
50         if (X != 0.0f ||
51                 Y != 0.0f ||
52                 Z != 0.0f)
54                 return 0;
56         return 1;
57 }
60 CVector3D CVector3D::operator + (CVector3D &vector)
61 {
62         CVector3D Temp;
64         Temp.X = X + vector.X;
65         Temp.Y = Y + vector.Y;
66         Temp.Z = Z + vector.Z;
68         return Temp;
69 }
72 CVector3D &CVector3D::operator += (CVector3D &vector)
73 {
74         X += vector.X;
75         Y += vector.Y;
76         Z += vector.Z;
78         return *this;
79 }
81 //vector subtraction
82 CVector3D CVector3D::operator - (CVector3D &vector)
83 {
84         CVector3D Temp;
86         Temp.X = X - vector.X;
87         Temp.Y = Y - vector.Y;
88         Temp.Z = Z - vector.Z;
90         return Temp;
91 }
93 //vector subtrcation/assignment
94 CVector3D &CVector3D::operator -= (CVector3D &vector)
95 {
96         X -= vector.X;
97         Y -= vector.Y;
98         Z -= vector.Z;
100         return *this;
101 }
103 //scalar multiplication
104 CVector3D CVector3D::operator * (float value)
105 {
106         CVector3D Temp;
108         Temp.X = X * value;
109         Temp.Y = Y * value;
110         Temp.Z = Z * value;
112         return Temp;
113 }
115 //scalar multiplication/assignment
116 CVector3D CVector3D::operator *= (float value)
117 {
118         X *= value;
119         Y *= value;
120         Z *= value;
122         return *this;
123 }
125 void CVector3D::Set (float x, float y, float z)
126 {
127         X = x;
128         Y = y;
129         Z = z;
130 }
132 void CVector3D::Clear ()
133 {
134         X = Y = Z = 0.0f;
135 }
137 //Dot product
138 float CVector3D::Dot (CVector3D &vector)
139 {
140         return ( X * vector.X +
141                          Y * vector.Y +
142                          Z * vector.Z );
143 }
145 //Cross product
146 CVector3D CVector3D::Cross (CVector3D &vector)
147 {
148         CVector3D Temp;
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);
154         return Temp;
155 }
157 float CVector3D::GetLength ()
158 {
159         return sqrtf ( SQR(X) + SQR(Y) + SQR(Z) );
160 }
162 void CVector3D::Normalize ()
163 {
164         float scale = 1.0f/GetLength ();
166         X *= scale;
167         Y *= scale;
168         Z *= scale;
169 }
171 SColor4ub CVector3D::ConvertToColor (float alpha_factor)
172 {
173     SColor4ub color;
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);
180         return color;
181 }