ScummVM API documentation
Vector3.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22/*
23 * Copyright (C) 2006-2010 - Frictional Games
24 *
25 * This file is part of HPL1 Engine.
26 */
27
28#ifndef HPL_VECTOR3_H
29#define HPL_VECTOR3_H
30
31#include "hpl1/engine/math/Vector2.h"
32
33#define VEC3_CONST_ARRAY(name, vec) const float name[] = {vec.x, vec.y, vec.z}
34
35namespace hpl {
36
37template<class T>
38class cVector3 {
39public:
40 T x, y, z;
41
42 constexpr cVector3() : x(0), y(0), z(0) {
43 }
44 constexpr cVector3(T aVal) : x(aVal), y(aVal), z(aVal) {
45 }
46 constexpr cVector3(T aX, T aY, T aZ) : x(aX), y(aY), z(aZ) {
47 }
48
49 constexpr cVector3(cVector3<T> const &aVec) : x(aVec.x), y(aVec.y), z(aVec.z) {
50 }
51
52 constexpr cVector3(cVector2<T> const &aVec) : x(aVec.x), y(aVec.y), z(0) {
53 }
54
55 static cVector3 fromArray(const float vec[3]) {
56 return cVector3<T>(vec[0], vec[1], vec[2]);
57 }
58
60 // Copy
62
63 inline cVector3<T> &operator=(const cVector3<T> &aVec) {
64 x = aVec.x;
65 y = aVec.y;
66 z = aVec.z;
67 return *this;
68 }
69
70 inline cVector3<T> &operator=(const cVector2<T> &aVec) {
71 x = aVec.x;
72 y = aVec.y;
73 return *this;
74 }
75
76 inline cVector3<T> &operator=(const T aVal) {
77 x = aVal;
78 y = aVal;
79 z = aVal;
80 return *this;
81 }
82
84 // Boolean
86
87 inline bool operator==(const cVector3<T> &aVec) const {
88 if (x == aVec.x && y == aVec.y && z == aVec.z)
89 return true;
90 else
91 return false;
92 }
93
94 inline bool operator!=(const cVector3<T> &aVec) const {
95 if (x == aVec.x && y == aVec.y && z == aVec.z)
96 return false;
97 else
98 return true;
99 }
100
101 inline bool operator<(const cVector3<T> &aVec) const {
102 if (x != aVec.x)
103 return x < aVec.x;
104 if (y != aVec.y)
105 return y < aVec.y;
106 return z < aVec.z;
107 }
108
109 inline bool operator>(const cVector3<T> &aVec) const {
110 if (x != aVec.x)
111 return x > aVec.x;
112 if (y != aVec.y)
113 return y > aVec.y;
114 return z > aVec.z;
115 }
116
118 // Vector3 Arithmetic
120
121 inline cVector3<T> operator+(const cVector3<T> &aVec) const {
122 cVector3<T> vec;
123 vec.x = x + aVec.x;
124 vec.y = y + aVec.y;
125 vec.z = z + aVec.z;
126 return vec;
127 }
128
129 inline cVector3<T> operator-(const cVector3<T> &aVec) const {
130 cVector3<T> vec;
131 vec.x = x - aVec.x;
132 vec.y = y - aVec.y;
133 vec.z = z - aVec.z;
134 return vec;
135 }
136
137 inline cVector3<T> operator*(const cVector3<T> &aVec) const {
138 cVector3<T> vec;
139 vec.x = x * aVec.x;
140 vec.y = y * aVec.y;
141 vec.z = z * aVec.z;
142 return vec;
143 }
144
145 inline cVector3<T> operator/(const cVector3<T> &aVec) const {
146 cVector3<T> vec;
147 vec.x = x / aVec.x;
148 vec.y = y / aVec.y;
149 vec.z = z / aVec.z;
150 return vec;
151 }
152
153 inline cVector3<T> &operator-=(const cVector3<T> &aVec) {
154 x -= aVec.x;
155 y -= aVec.y;
156 z -= aVec.z;
157 return *this;
158 }
159
160 inline cVector3<T> &operator+=(const cVector3<T> &aVec) {
161 x += aVec.x;
162 y += aVec.y;
163 z += aVec.z;
164 return *this;
165 }
166
167 inline cVector3<T> &operator*=(const cVector3<T> &aVec) {
168 x *= aVec.x;
169 y *= aVec.y;
170 z *= aVec.z;
171 return *this;
172 }
173
174 inline cVector3<T> &operator/=(const cVector3<T> &aVec) {
175 x /= aVec.x;
176 y /= aVec.y;
177 z /= aVec.z;
178 return *this;
179 }
180
182 // Vector2 Arithmetic
184
185 inline cVector3<T> operator+(const cVector2<T> &aVec) const {
186 cVector3<T> vec;
187 vec.x = x + aVec.x;
188 vec.y = y + aVec.y;
189 vec.z = z;
190 return vec;
191 }
192
193 inline cVector3<T> operator-(const cVector2<T> &aVec) const {
194 cVector3<T> vec;
195 vec.x = x - aVec.x;
196 vec.y = y - aVec.y;
197 vec.z = z;
198 return vec;
199 }
200
201 inline cVector3<T> operator*(const cVector2<T> &aVec) const {
202 cVector3<T> vec;
203 vec.x = x * aVec.x;
204 vec.y = y * aVec.y;
205 vec.z = z;
206 return vec;
207 }
208
209 inline cVector3<T> operator/(const cVector2<T> &aVec) const {
210 cVector3<T> vec;
211 vec.x = x / aVec.x;
212 vec.y = y / aVec.y;
213 vec.z = z;
214 return vec;
215 }
216
217 inline cVector3<T> &operator-=(const cVector2<T> &aVec) {
218 x -= aVec.x;
219 y -= aVec.y;
220 return *this;
221 }
222
223 inline cVector3<T> &operator+=(const cVector2<T> &aVec) {
224 x += aVec.x;
225 y += aVec.y;
226 return *this;
227 }
228
229 inline cVector3<T> &operator*=(const cVector2<T> &aVec) {
230 x *= aVec.x;
231 y *= aVec.y;
232 return *this;
233 }
234
235 inline cVector3<T> &operator/=(const cVector2<T> &aVec) {
236 x /= aVec.x;
237 y /= aVec.y;
238 return *this;
239 }
240
242 // Single Float Arithmetic
244
245 inline cVector3<T> operator/(const T &aVal) const {
246 cVector3<T> vec;
247 vec.x = x / aVal;
248 vec.y = y / aVal;
249 vec.z = z / aVal;
250 return vec;
251 }
252
253 inline cVector3<T> operator*(const T &aVal) const {
254 cVector3<T> vec;
255 vec.x = x * aVal;
256 vec.y = y * aVal;
257 vec.z = z * aVal;
258 return vec;
259 }
260
261 inline cVector3<T> operator+(const T &aVal) const {
262 cVector3<T> vec;
263 vec.x = x + aVal;
264 vec.y = y + aVal;
265 vec.z = z + aVal;
266 return vec;
267 }
268
269 cVector3<T> operator-(const T &aVal) const {
270 cVector3<T> vec;
271 vec.x = x - aVal;
272 vec.y = y - aVal;
273 vec.z = z - aVal;
274 return vec;
275 }
276
278 // Methods
280
281 inline void FromVec(const T *apVec) {
282 x = apVec[0];
283 y = apVec[1];
284 z = apVec[2];
285 }
286
287 inline T Length() {
288 return sqrt(x * x + y * y + z * z);
289 }
290
291 inline T SqrLength() {
292 return x * x + y * y + z * z;
293 }
294
300 T length = sqrt(x * x + y * y + z * z);
301
302 // Will also work for zero-sized vectors, but will change nothing
303 if (length > 1e-08) {
304 T InvLength = 1.0f / length;
305 x *= InvLength;
306 y *= InvLength;
307 z *= InvLength;
308 }
309
310 return length;
311 }
312
314 // Printing
316
317 tString ToString() const {
318 char buf[512];
319 snprintf(buf, 512, "%f : %f : %f", x, y, z);
320 tString str = buf;
321 return str;
322 }
323
324 tString ToFileString() const {
325 char buf[512];
326 snprintf(buf, 512, "%g %g %g", x, y, z);
327 tString str = buf;
328 return str;
329 }
330};
331
332} // namespace hpl
333
334#endif // HPL_VECTOR3_H
Definition: str.h:59
Definition: Vector2.h:36
Definition: Vector3.h:38
T Normalise()
Definition: Vector3.h:299