gwnavruntime/math/vec3f.h Source File

vec3f.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 // primary contact: GUAL - secondary contact: LAPA
9 #ifndef Navigation_Vec3f_H
10 #define Navigation_Vec3f_H
11 
13 #include <math.h>
14 
15 namespace Kaim
16 {
17 
22 class Vec3f
23 {
24  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
25 
26 public:
27  // ---------------------------------- Creation & Initialization ----------------------------------
28 
30  Vec3f() : x(0.0f), y(0.0f), z(0.0f) {}
31 
33  Vec3f(KyFloat32 _x, KyFloat32 _y, KyFloat32 _z) : x(_x), y(_y), z(_z) {}
34 
37  explicit Vec3f(const KyFloat32* coords) { Set(coords); }
38 
40  explicit Vec3f(const Vec2f& v) { Set(v); }
41 
43  Vec3f(const Vec2f& v, KyFloat32 _z) { Set(v, _z); }
44 
46  KY_INLINE void Set(KyFloat32 _x, KyFloat32 _y, KyFloat32 _z) { x = _x; y = _y; z = _z; }
47 
50  KY_INLINE void Set(const KyFloat32* coords) { x = coords[0]; y = coords[1]; z = coords[2]; }
51 
53  KY_INLINE void Set(const Vec2f& v) { x = v.x; y = v.y; z = 0.0f; }
54 
56  KY_INLINE void Set(const Vec2f& v, KyFloat32 _z) { x = v.x; y = v.y; z = _z; }
57 
58 
59  // -------------------------- Operators --------------------------------
60 
63  KY_INLINE KyFloat32& operator[](KyInt32 i) { return (&x)[i]; }
64 
67  KY_INLINE KyFloat32 operator[](KyInt32 i) const { return (&x)[i]; }
68 
70  KY_INLINE bool operator==(const Vec3f& v) const { return x == v.x && y == v.y && z == v.z; }
71 
73  KY_INLINE bool operator!=(const Vec3f& v) const { return x != v.x || y != v.y || z != v.z; }
74 
76  KY_INLINE bool operator<(const Vec3f& v) const;
77  KY_INLINE bool operator>(const Vec3f& v) const;
78  KY_INLINE bool operator<=(const Vec3f& v) const;
79  KY_INLINE bool operator>=(const Vec3f& v) const;
80 
82  KY_INLINE Vec3f& operator*=(KyFloat32 s) { x *= s; y *= s; z *= s; return *this; }
83 
85  KY_INLINE Vec3f& operator/=(KyFloat32 d) { return operator*=(1.0f / d); }
86 
89  KY_INLINE Vec3f& operator+=(const Vec3f& v) { x += v.x; y += v.y; z += v.z; return *this; }
90 
93  KY_INLINE Vec3f& operator-=(const Vec3f& v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
94 
96  KY_INLINE Vec3f operator*(KyFloat32 s) const { return Vec3f(x * s, y * s, z * s); }
97 
99  KY_INLINE Vec3f operator/(KyFloat32 d) const { return operator*(1.0f / d); }
100 
103  KY_INLINE Vec3f operator+(const Vec3f& v) const { return Vec3f(x + v.x, y + v.y, z + v.z); }
104 
107  KY_INLINE Vec3f operator-(const Vec3f& v) const { return Vec3f(x - v.x, y - v.y, z - v.z); }
108 
110  KY_INLINE Vec3f operator-() const { return Vec3f(-x, -y, -z); }
111 
113  KY_INLINE KyFloat32 operator*(const Vec3f& v) const { return x * v.x + y * v.y + z * v.z; } // DOT PRODUCT
114 
116  KY_INLINE Vec3f operator^(const Vec3f& v) const { return Vec3f( (y * v.z - z * v.y), (z * v.x - x * v.z), (x * v.y - y * v.x) ); } // CROSS PRODUCT
118 
119  // -------------------------- Operations with Vec2f --------------------------------
123  KY_INLINE Vec3f& operator+=(const Vec2f& v) { x += v.x; y += v.y; return *this; }
124 
127  KY_INLINE Vec3f& operator-=(const Vec2f& v) { x -= v.x; y -= v.y; return *this; }
128 
131  KY_INLINE Vec3f operator+(const Vec2f& v) const { return Vec3f(x + v.x, y + v.y, z); }
132 
135  KY_INLINE Vec3f operator-(const Vec2f& v) const { return Vec3f(x - v.x, y - v.y, z); }
136 
138  // -------------------------- Main interface --------------------------------
139 
141  KY_INLINE void Cross(const Vec3f& u, const Vec3f& v);
142 
144  KY_INLINE KyFloat32 GetSquareLength() const { return x * x + y * y + z * z; }
145 
147  KY_INLINE KyFloat32 GetSquareLength2d() const { return x * x + y * y; }
148 
150  KY_INLINE KyFloat32 GetLength() const { return Sqrtf(x * x + y * y + z * z); }
151 
153  KY_INLINE KyFloat32 GetLength2d() const { return Sqrtf(x * x + y * y); }
154 
157  KY_INLINE KyFloat32 Normalize();
158 
164  KY_INLINE KyFloat32 GetNormalized(Vec3f& normalized) const;
166  KY_INLINE bool IsNormalized() const;
167 
169  KY_INLINE Vec2f Get2d() const { return Vec2f(x, y); }
170 
176  KY_INLINE KyFloat32 GetNormalized2d(Vec2f& normalized) const;
177 
179  KY_INLINE bool IsZero() const { return x== 0.0f && y == 0.0f && z == 0.0f; }
180 
181 
182  // -------------------------- Static methods --------------------------------
183 
185  static KY_INLINE Vec3f Zero() { return Vec3f(0.0f, 0.0f, 0.0f); }
186 
188  static KY_INLINE Vec3f UnitX() { return Vec3f(1.0f, 0.0f, 0.0f); }
189 
191  static KY_INLINE Vec3f UnitY() { return Vec3f(0.0f, 1.0f, 0.0f); }
192 
194  static KY_INLINE Vec3f UnitZ() { return Vec3f(0.0f, 0.0f, 1.0f); }
195 
196 
197  // ---------------------------------- Public Data Members ----------------------------------
198 
200  KyFloat32 y;
201  KyFloat32 z;
202 };
203 
206 KY_INLINE void SwapEndianness(Endianness::Target e, Vec3f& self)
207 {
208  SwapEndianness(e, self.x);
209  SwapEndianness(e, self.y);
210  SwapEndianness(e, self.z);
211 }
213 
214 // ----------------------------------- global functions -----------------------------------
217 KY_INLINE Vec3f operator*(KyFloat32 s, const Vec3f& v) { return Vec3f(v.x * s, v.y * s, v.z * s); }
220 KY_INLINE KyFloat32 DotProduct(const Vec3f& v1, const Vec3f& v2) { return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z); }
221 
223 KY_INLINE KyFloat32 DotProduct2d(const Vec3f& v1, const Vec3f& v2) { return (v1.x * v2.x + v1.y * v2.y); }
224 
226 KY_INLINE KyFloat32 CrossProduct_x(const Vec3f& v1, const Vec3f& v2) { return v1.y * v2.z - v1.z * v2.y; }
229 KY_INLINE KyFloat32 CrossProduct_y(const Vec3f& v1, const Vec3f& v2) { return v1.z * v2.x - v1.x * v2.z; }
230 
232 KY_INLINE KyFloat32 CrossProduct_z(const Vec3f& v1, const Vec3f& v2) { return v1.x * v2.y - v1.y * v2.x; }
233 
235 KY_INLINE Vec3f CrossProduct(const Vec3f& v1, const Vec3f& v2) { return Vec3f(CrossProduct_x(v1, v2), CrossProduct_y(v1, v2), CrossProduct_z(v1, v2)); }
236 
238 KY_INLINE Vec3f GetRightDirection(const Vec3f& v) {
239  Vec3f right = CrossProduct(v, Vec3f::UnitZ());
240  right.Normalize();
241  return right;
242 }
243 
245 KY_INLINE KyFloat32 SquareDistance(const Vec3f& v1, const Vec3f& v2) { return (v2 - v1).GetSquareLength(); }
248 KY_INLINE KyFloat32 SquareDistance2d(const Vec3f& v1, const Vec3f& v2)
249 {
250  const KyFloat32 dx = v2.x - v1.x;
251  const KyFloat32 dy = v2.y - v1.y;
252  return dx * dx + dy * dy;
253 }
254 
256 KY_INLINE KyFloat32 Distance(const Vec3f& v1, const Vec3f& v2) { return Sqrtf(SquareDistance(v1, v2)); }
257 
259 KY_INLINE KyFloat32 Distance2d(const Vec3f& v1, const Vec3f& v2) { return Sqrtf(SquareDistance2d(v1, v2)); }
260 
262 KY_INLINE Vec3f GetDir(const Vec3f& A, const Vec3f& B)
263 {
264  Vec3f AB = B - A;
265  AB.Normalize();
266  return AB;
267 }
268 
270 KY_INLINE Vec2f GetDir2d(const Vec3f& A, const Vec3f& B)
271 {
272  Vec2f AB(B.x - A.x, B.y - A.y);
273  AB.Normalize();
274  return AB;
275 }
276 
277 // ----------------------------------- KY_INLINE implementation -----------------------------------
278 
279 KY_INLINE bool Vec3f::operator<(const Vec3f& v) const
280 {
281  if (x < v.x) return true;
282  if (x > v.x) return false;
283  if (y < v.y) return true;
284  if (y > v.y) return false;
285  return (z < v.z);
286 }
287 
288 KY_INLINE bool Vec3f::operator>(const Vec3f& v) const
289 {
290  if (x > v.x) return true;
291  if (x < v.x) return false;
292  if (y > v.y) return true;
293  if (y < v.y) return false;
294  return (z > v.z);
295 }
296 
297 KY_INLINE bool Vec3f::operator<=(const Vec3f& v) const { return !operator>(v); }
298 KY_INLINE bool Vec3f::operator>=(const Vec3f& v) const { return !operator<(v); }
300 KY_INLINE void Vec3f::Cross(const Vec3f& u, const Vec3f& v)
301 {
302  x = u.y * v.z - u.z * v.y;
303  y = u.z * v.x - u.x * v.z;
304  z = u.x * v.y - u.y * v.x;
305 }
306 
307 
309 {
310  const KyFloat32 length = GetLength();
311  if (length != 0.0f)
312  (*this) /= length;
313  return length;
314 }
315 
316 
317 KY_INLINE KyFloat32 Vec3f::GetNormalized(Vec3f& normalized) const
318 {
319  // don't call Vec3f::Normalize() to avoid LHS
320  const KyFloat32 length = GetLength();
321  if (length != 0.0f)
322  {
323  const KyFloat32 invLength = 1.f / length;
324  normalized.Set(x * invLength, y * invLength, z * invLength);
325  return length;
326  }
327  else
328  {
329  normalized.Set(0.f, 0.f, 0.f);
330  return 0.f;
331  }
332 }
333 
334 KY_INLINE bool Vec3f::IsNormalized() const
335 {
336  const KyFloat32 sqLength = GetSquareLength();
337  return (fabsf(sqLength - 1.0f) < 1e-6f);
338 }
339 
340 KY_INLINE KyFloat32 Vec3f::GetNormalized2d(Vec2f& normalized2d) const
341 {
342  // don't call Vec2f::Normalize() to avoid LHS
343  const KyFloat32 length = GetLength2d();
344  if (length != 0.0f)
345  {
346  const KyFloat32 invLength = 1.0f / length;
347  normalized2d.Set(x * invLength, y * invLength);
348  return length;
349  }
350  else
351  {
352  normalized2d.Set(0.0f, 0.0f);
353  return 0.0f;
354  }
355 }
356 
357 
358 template <class OSTREAM>
359 inline OSTREAM& operator<<(OSTREAM& os, const Vec3f& v)
360 {
361  os << "{" << v.x << ";" << v.y << ";" << v.z << "}";
362  return os;
363 }
364 
365 
366 } // namespace Kaim
367 
368 #endif
KyFloat32 GetSquareLength() const
Returns the square of the magnitude of the vector.
Definition: vec3f.h:162
KyFloat32 GetNormalized(Vec3f &normalized) const
Normalizes the vector, making it one unit in length without changing its orientation.
Definition: vec3f.h:346
Vec3f operator/(KyFloat32 d) const
Divides the X, Y and Z coordinates of this vector by the specified value.
Definition: vec3f.h:109
Vec2f GetDir2d(const Vec2f &v1, const Vec2f &v2)
Returns the normalized direction between v1 and v2.
Definition: vec2f.h:208
KyFloat32 CrossProduct(const Vec2f &v1, const Vec2f &v2)
Returns the magnitude on the Z axis of the cross product between this vector and v.
Definition: vec2f.h:190
KyFloat32 z
The size of the vector along the Z axis.
Definition: vec3f.h:229
Vec3f GetRightDirection(const Vec3f &v)
Returns the normalized horizontal vector on the right of v. If v is vertical, this return Vec3f::Zero...
Definition: vec3f.h:267
Vec3f & operator*=(KyFloat32 s)
Multiplies the X, Y and Z coordinates of this vector by the specified value.
Definition: vec3f.h:92
void Set(KyFloat32 _x, KyFloat32 _y, KyFloat32 _z)
Sets the coordinates.
Definition: vec3f.h:52
KyFloat32 Distance2d(const Vec3f &v1, const Vec3f &v2)
Returns the distance between v1 and v2, ignoring Z coordinates.
Definition: vec3f.h:288
KyFloat32 x
The size of the vector along the X axis.
Definition: vec2f.h:169
Vec3f & operator+=(const Vec3f &v)
Adds the X coordinate of v to the X coordinate of this vector, adds the Y coordinate of v to the Y co...
Definition: vec3f.h:99
void Set(KyFloat32 _x, KyFloat32 _y)
Sets the coordinates of the vector.
Definition: vec2f.h:47
bool operator<(const Vec3f &v) const
Returns true if the size of this vector along all three of the X, Y and Z axes is less than that of v...
Definition: vec3f.h:308
static Vec3f Zero()
Returns a vector of zero size: (0,0,0).
Definition: vec3f.h:209
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
Vec3f operator-() const
Negates the X, Y and Z coordinates of this vector, effectively flipping it around the origin...
Definition: vec3f.h:120
KyFloat32 CrossProduct_x(const Vec3f &v1, const Vec3f &v2)
Returns the X component of the cross product of v1 and v2.
Definition: vec3f.h:255
KyFloat32 & operator[](KyInt32 i)
Retrieves the size of the vector around one of its axes.
Definition: vec3f.h:73
Vec3f()
Creates a vector with coordinates (0,0,0).
Definition: vec3f.h:36
KyFloat32 y
The size of the vector along the Y axis.
Definition: vec3f.h:228
Vec3f operator+(const Vec3f &v) const
Adds the X coordinate of v to the X coordinate of this vector, adds the Y coordinate of v to the Y co...
Definition: vec3f.h:113
KyFloat32 SquareDistance2d(const Vec3f &v1, const Vec3f &v2)
Returns the square of the distance between v1 and v2, ignoring Z coordinates.
Definition: vec3f.h:277
KyFloat32 GetLength() const
Returns the magnitude of the vector.
Definition: vec3f.h:168
KyFloat32 DotProduct2d(const Vec3f &v1, const Vec3f &v2)
Returns the dot product of v1 and v2, ignoring Z coordinates.
Definition: vec3f.h:252
Vec2f Get2d() const
Returns a Vec2f that contains the X and Y coordinates of this vector. The Z coordinate is ignored...
Definition: vec3f.h:188
void Set(const KyFloat32 *coords)
Sets the coordinates.
Definition: vec3f.h:56
static Vec3f UnitZ()
Returns the normalized orientation of the Z axis.
Definition: vec3f.h:218
KyFloat32 CrossProduct_z(const Vec3f &v1, const Vec3f &v2)
Returns the Z component of the cross product of v1 and v2.
Definition: vec3f.h:261
KyFloat32 SquareDistance(const Vec2f &v1, const Vec2f &v2)
Returns the square of the distance between v1 and v2.
Definition: vec2f.h:194
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:35
KyFloat32 x
The size of the vector along the X axis.
Definition: vec3f.h:227
This class defines a two-dimensional vector whose coordinates are stored using floating-point numbers...
Definition: vec2f.h:24
Vec3f operator^(const Vec3f &v) const
Returns the cross product of this vector and v.
Definition: vec3f.h:126
void Cross(const Vec3f &u, const Vec3f &v)
Calculates the cross product ofu and v, and stores the result in this vector.
Definition: vec3f.h:329
KyFloat32 GetLength2d() const
Returns the magnitude of the vector on the plane of the (X,Y) axes.
Definition: vec3f.h:171
KyFloat32 Distance(const Vec2f &v1, const Vec2f &v2)
Returns the distance between v1 and v2.
Definition: vec2f.h:202
bool operator!=(const Vec3f &v) const
Returns true if this object contains at least one different coordinate from v.
Definition: vec3f.h:83
Definition: gamekitcrowddispersion.h:20
KyFloat32 DotProduct(const Vec2f &v1, const Vec2f &v2)
Returns the dot product of v1 and v2.
Definition: vec2f.h:187
KyFloat32 Normalize()
Normalizes the vector, making it one unit in length without changing its orientation.
Definition: vec3f.h:337
Vec2f operator*(KyFloat32 s, const Vec2f &v)
Multiplies the X and Y coordinates of v by s.
Definition: vec2f.h:184
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
bool IsZero() const
Returns true if the X, Y and Z coordinates of this vector are all 0.
Definition: vec3f.h:199
Vec3f operator*(KyFloat32 s) const
Multiplies the X, Y and Z coordinates of this vector by the specified value.
Definition: vec3f.h:106
Vec3f & operator/=(KyFloat32 d)
Divides the X, Y and Z coordinates of this vector by the specified value.
Definition: vec3f.h:95
Vec3f GetDir(const Vec3f &A, const Vec3f &B)
Get Direction.
Definition: vec3f.h:291
static Vec3f UnitY()
Returns the normalized orientation of the Y axis.
Definition: vec3f.h:215
Vec3f(const KyFloat32 *coords)
Creates a vector with the specified coordinates.
Definition: vec3f.h:43
static Vec3f UnitX()
Returns the normalized orientation of the X axis.
Definition: vec3f.h:212
KyFloat32 GetNormalized2d(Vec2f &normalized) const
Normalizes the (X,Y) coordinates of the vector, making it one unit in length without changing its ori...
Definition: vec3f.h:369
Vec3f & operator-=(const Vec3f &v)
Subtracts the X coordinate of v from the X coordinate of this vector, subtracts the Y coordinate of v...
Definition: vec3f.h:103
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43
This class defines a three-dimensional vector whose coordinates are stored using floating-point numbe...
Definition: vec3f.h:23
bool operator==(const Vec3f &v) const
Returns true if this object contains the same coordinates as v.
Definition: vec3f.h:80
KyFloat32 y
The size of the vector along the Y axis.
Definition: vec2f.h:170
KyFloat32 GetSquareLength2d() const
Returns the square of the magnitude of the vector on the plane of the (X,Y) axes. ...
Definition: vec3f.h:165
KyFloat32 CrossProduct_y(const Vec3f &v1, const Vec3f &v2)
Returns the Y component of the cross product of v1 and v2.
Definition: vec3f.h:258