gwnavruntime/math/vec2i.h Source File

vec2i.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: NOBODY
9 #ifndef Navigation_Vec2i_H
10 #define Navigation_Vec2i_H
11 
12 
16 #include <math.h>
17 
18 namespace Kaim
19 {
20 
21 
25 class Vec2i
26 {
27  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
28 
29 public:
30  // ---------------------------------- Creation & Initialization ----------------------------------
31 
33  Vec2i() : x(0), y(0) {}
34 
38  Vec2i(KyInt32 _x, KyInt32 _y) : x(_x), y(_y) {}
39 
43  explicit Vec2i(KyInt32* coords) { Set(coords); }
44 
48  KY_INLINE void Set(KyInt32 _x, KyInt32 _y) { x = _x; y = _y; }
49 
53  KY_INLINE void Set(KyInt32* coords) { x = coords[0]; y = coords[1]; }
54 
55 
56  // -------------------------- Operators --------------------------------
57 
60  KY_INLINE KyInt32 operator[](KyInt32 idx) const { return (&x)[idx]; }
61 
64  KY_INLINE KyInt32& operator[](KyInt32 idx) { return (&x)[idx]; }
65 
67  KY_INLINE bool operator==(const Vec2i& v) const { return ((x ^ v.x) | (y ^ v.y)) == 0/*x == v.x && y == v.y*/; }
68 
70  KY_INLINE bool operator!=(const Vec2i& v) const { return !operator==(v);/*x != v.x || y != v.y;*/ }
71 
73  KY_INLINE bool operator<(const Vec2i& v) const { return (x != v.x) ? x < v.x : y < v.y; }
74 
76  KY_INLINE bool operator<=(const Vec2i& v) const { return (x != v.x) ? x < v.x : y <= v.y; }
77 
79  KY_INLINE bool operator>(const Vec2i& v) const { return (x != v.x) ? x > v.x : y > v.y; }
80 
82  KY_INLINE bool operator>=(const Vec2i& v) const { return (x != v.x) ? x < v.x : y >= v.y; }
83 
85  KY_INLINE Vec2i& operator*=(KyInt32 s) { x *= s; y *= s; return *this; }
86 
88  KY_INLINE Vec2i& operator/=(KyInt32 d) { x /= d; y /= d; return *this; }
89 
91  KY_INLINE Vec2i& operator+=(KyInt32 t) { x += t; y += t; return *this; }
92 
94  KY_INLINE Vec2i& operator-=(KyInt32 t) { x -= t; y -= t; return *this; }
95 
98  KY_INLINE Vec2i& operator+=(const Vec2i& v) { x += v.x; y += v.y; return *this; }
99 
102  KY_INLINE Vec2i& operator-=(const Vec2i& v) { x -= v.x; y -= v.y; return *this; }
103 
105  KY_INLINE Vec2i operator*(KyInt32 s) const { return Vec2i(x * s , y * s ); }
106 
108  KY_INLINE Vec2i operator/(KyInt32 d) const { return Vec2i(x / d , y / d ); }
109 
112  KY_INLINE Vec2i operator+(const Vec2i& v) const { return Vec2i(x + v.x, y + v.y); }
113 
116  KY_INLINE Vec2i operator-(const Vec2i& v) const { return Vec2i(x - v.x, y - v.y); }
117 
119  KY_INLINE Vec2i operator-() const { return Vec2i(-x , -y ); }
120 
122  KY_INLINE KyInt32 operator*(const Vec2i& v) const { return x * v.x + y * v.y; } // DOT PROD
123 
125  KY_INLINE KyInt32 operator^(const Vec2i& v) const { return CrossProd(v); } // CROSS PROD 2D aka PERP PROD
127  // -------------------------- Main interface --------------------------------
128 
130  KY_INLINE KyInt32 CrossProd(const Vec2i& v) const { return x * v.y - y * v.x; } // CROSS PROD 2D aka PERP PROD
131 
138  KY_INLINE KyInt32 Orient2d(const Vec2i& A, const Vec2i& B) const { return (A.x - x) * (B.y - y) - (B.x - x) * (A.y - y); } // > 0 if (MA,MB) is CCW
139 
141  KY_INLINE Vec2i PerpCCW() const{return Vec2i(y, -x);}
142 
144  KY_INLINE Vec2i PerpCW() const{return Vec2i(-y, x);}
145 
148  KY_INLINE bool IsOnLeftSide(const Vec2i& A, const Vec2i& B) const { return Orient2d(A, B) >= 0; }
149 
152  KY_INLINE bool IsStrictlyOnLeftSide(const Vec2i& A, const Vec2i& B) const { return Orient2d(A, B) > 0; }
153 
155  KY_INLINE KyInt32 GetSquareLength() const { return x * x + y * y; }
156 
157 
158  // -------------------------- Neighbors --------------------------------
159 
161  KY_INLINE Vec2i NeighborEast() const { return Vec2i(x + 1, y ); }
164  KY_INLINE void NeighborEast(Vec2i& neighbor) const { neighbor.Set(x + 1, y ); }
165 
167  KY_INLINE Vec2i NeighborWest() const { return Vec2i(x - 1, y ); }
168 
170  KY_INLINE void NeighborWest(Vec2i& neighbor) const { neighbor.Set(x - 1, y ); }
171 
173  KY_INLINE Vec2i NeighborNorth() const { return Vec2i(x , y + 1); }
174 
176  KY_INLINE void NeighborNorth(Vec2i& neighbor) const { neighbor.Set(x , y + 1); }
177 
179  KY_INLINE Vec2i NeighborSouth() const { return Vec2i(x , y - 1); }
180 
182  KY_INLINE void NeighborSouth(Vec2i& neighbor) const { neighbor.Set(x , y - 1); }
183 
185  KY_INLINE Vec2i Neighbor(CardinalDir dir) const
186  {
187  Vec2i returnCellPos;
188  Neighbor(dir, returnCellPos);
189  return returnCellPos;
190  }
193  KY_INLINE void Neighbor(CardinalDir dir, Vec2i& neighbor) const
194  {
195  static const Vec2i s_cardinalDirToNeighbor[4] =
196  {
197  /*CardinalDir_EAST*/Vec2i(1, 0),
198  /*CardinalDir_NORTH*/Vec2i(0, 1),
199  /*CardinalDir_WEST*/Vec2i(-1, 0),
200  /*CardinalDir_SOUTh*/Vec2i(0, -1)
201  };
202 
203  neighbor.x = x + s_cardinalDirToNeighbor[dir].x;
204  neighbor.y = y + s_cardinalDirToNeighbor[dir].y;
205  }
206 
207 
210  KY_INLINE Vec2i NeighborNorthEast() const { return Vec2i(x + 1, y + 1); }
211 
214  KY_INLINE Vec2i NeighborNorthWest() const { return Vec2i(x - 1, y + 1); }
215 
218  KY_INLINE Vec2i NeighborSouthEast() const { return Vec2i(x + 1, y - 1); }
219 
222  KY_INLINE Vec2i NeighborSouthWest() const { return Vec2i(x - 1, y - 1); }
223 
224 
225  // -------------------------- Triangle Inclusion Checkers --------------------------------
226 
230  KY_INLINE bool IsInsideTriangle(const Vec2i& A, const Vec2i& B, const Vec2i& C) const;
231 
236  KY_INLINE bool IsInsideNotColinearTriangle(const Vec2i& A, const Vec2i& B, const Vec2i& C) const;
237 
240  KY_INLINE bool IsStrictlyInsideTriangle(const Vec2i& A, const Vec2i& B, const Vec2i& C) const;
241 
242  // -------------------------- Static methods --------------------------------
243 
245  static KY_INLINE Vec2i UnitX() { return Vec2i(1, 0); }
246 
248  static KY_INLINE Vec2i UnitY() { return Vec2i(0, 1); }
249 
250 
251  // ---------------------------------- Public Data Members ----------------------------------
252 
253  KyInt32 x;
254  KyInt32 y;
255 };
256 
257 inline Vec2i operator * (KyInt32 lhs, const Vec2i& rhs) { return rhs * lhs; }
258 
261 KY_INLINE void SwapEndianness(Kaim::Endianness::Target e, Vec2i& self)
262 {
263  SwapEndianness(e, self.x);
264  SwapEndianness(e, self.y);
265 }
266 
267 KY_INLINE KyInt32 SquareDistance(const Vec2i& v1, const Vec2i& v2)
268 {
269  const KyInt32 dx = v2.x - v1.x;
270  const KyInt32 dy = v2.y - v1.y;
271  return dx * dx + dy * dy;
272 }
273 
274 KY_INLINE bool Vec2i::IsInsideTriangle(const Vec2i& A, const Vec2i& B, const Vec2i& C) const
275 {
276  if (A.Orient2d(B, C) == 0) // flat triangle
277  {
278  if (A.x == B.x) // vertical
279  {
280  // not vertical
281  if (y < A.y && y < B.y && y < C.y)
282  return false; // M is below of ABC
283  if (y > A.y && y > B.y && y > C.y)
284  return false; // M is above of ABC
285  return true; // M is on ABC
286  }
287  else // not vertical
288  {
289  if (x < A.x && x < B.x && x < C.x)
290  return false; // M is "left" of ABC
291  if (x > A.x && x > B.x && x > C.x)
292  return false; // M is "right" of ABC
293  return true; // M is on ABC
294  }
295  }
296 
297  return IsInsideNotColinearTriangle(A, B, C);
298 }
299 
300 
301 KY_INLINE bool Vec2i::IsInsideNotColinearTriangle(const Vec2i& A, const Vec2i& B, const Vec2i& C) const
302 {
303  return IsOnLeftSide(A, B) && IsOnLeftSide(B, C) && IsOnLeftSide(C, A);
304 }
305 
306 
307 KY_INLINE bool Vec2i::IsStrictlyInsideTriangle(const Vec2i& A, const Vec2i& B, const Vec2i& C) const
308 {
309  return IsStrictlyOnLeftSide(A, B) && IsStrictlyOnLeftSide(B, C) && IsStrictlyOnLeftSide(C, A);
310 }
311 
313 KY_INLINE KyInt32 DotProduct(const Vec2i& v1, const Vec2i& v2) { return v1 * v2; }
314 
316 KY_INLINE KyInt32 CrossProduct(const Vec2i& v1, const Vec2i& v2) { return v1.CrossProd(v2); }
317 
318 
319 template <typename T> KY_INLINE T CrossProductT(const Vec2i& v1, const Vec2i& v2) { return (T)(v1.x) * (T)(v2.y) - (T)(v1.y) * (T)(v2.x); }
320 template <typename T> KY_INLINE T DotProductT(const Vec2i& v1, const Vec2i& v2) { return (T)(v1.x) * (T)(v2.x) + (T)(v1.y) * (T)(v2.y); }
321 
322 template <class OSTREAM>
323 inline OSTREAM& operator<<(OSTREAM& os, const Kaim::Vec2i& v)
324 {
325  os << "{" << v.x << ";" << v.y << "}";
326  return os;
327 }
328 
329 } // namespace Kaim
330 
332 
333 #endif
334 
bool operator<(const Vec2i &v) const
order by x first: (1,9)<(2,8).>
Definition: vec2i.h:83
Vec2i operator*(KyInt32 s) const
Multiplies both the X and Y coordinates of the vector by the specified value.
Definition: vec2i.h:115
Vec2i & operator/=(KyInt32 d)
Divides both the X and Y coordinates of this vector by the specified value.
Definition: vec2i.h:98
Vec2i PerpCW() const
Rotates this vector 90 degrees clockwise (negating the X coordinate).
Definition: vec2i.h:158
Vec2i PerpCCW() const
Rotates this vector 90 degrees counter-clockwise (negating the Y coordinate).
Definition: vec2i.h:155
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
KyInt32 operator^(const Vec2i &v) const
Returns the magnitude on the Z axis of the cross product between this vector and v.
Definition: vec2i.h:135
Vec2i NeighborEast() const
Returns a vector that identifies the next grid cell to the East: i.e. in the positive direction of th...
Definition: vec2i.h:179
bool IsInsideNotColinearTriangle(const Vec2i &A, const Vec2i &B, const Vec2i &C) const
Returns true if this vector is inside the clockwise triangle formed by theA,B and C parameters...
Definition: vec2i.h:331
Vec2i & operator-=(KyInt32 t)
Subtractst from both the X and Y coordinates of this vector.
Definition: vec2i.h:104
Vec2i NeighborNorthWest() const
Returns a vector that identifies the next grid cell to the North-West: i.e.
Definition: vec2i.h:232
Vec2i NeighborSouthWest() const
Returns a vector that identifies the next grid cell to the South-West: i.e.
Definition: vec2i.h:240
bool IsStrictlyInsideTriangle(const Vec2i &A, const Vec2i &B, const Vec2i &C) const
Returns true if this vector is inside the clockwise triangle formed by theA,B and C parameters...
Definition: vec2i.h:337
bool operator!=(const Vec2i &v) const
Returns true if this object contains at least one different coordinate from v.
Definition: vec2i.h:80
Vec2i NeighborSouth() const
Returns a vector that identifies the next grid cell to the South: i.e. in the negative direction of t...
Definition: vec2i.h:197
Vec2i NeighborWest() const
Returns a vector that identifies the next grid cell to the West: i.e. in the negative direction of th...
Definition: vec2i.h:185
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
void Set(KyInt32 _x, KyInt32 _y)
Sets the coordinates of the vector to match the specified values.
Definition: vec2i.h:54
bool operator==(const Vec2i &v) const
Returns true if this object contains the same coordinates as v.
Definition: vec2i.h:77
Vec2i Neighbor(CardinalDir dir) const
Returns a vector that identifies the next grid cell in the direction of the specified CardinalDir...
Definition: vec2i.h:203
KyInt32 x
The size of the vector along the X axis.
Definition: vec2i.h:283
KyUInt32 CardinalDir
Defines a type that refers to one of the cardinal points on the compass:
Definition: cardinaldir.h:23
static Vec2i UnitY()
Returns the normalized orientation of the Y axis.
Definition: vec2i.h:274
bool operator>=(const Vec2i &v) const
order by x first: (2,8)>=(1,9).
Definition: vec2i.h:92
KyInt32 CrossProd(const Vec2i &v) const
Returns the magnitude on the Z axis of the cross product between this vector and v.
Definition: vec2i.h:144
bool IsStrictlyOnLeftSide(const Vec2i &A, const Vec2i &B) const
Returns true if this vector is on the left side when moving from A to B, but not if this vector is on...
Definition: vec2i.h:166
Vec2i operator/(KyInt32 d) const
Divides both the X and Y coordinates of the vector by the specified value.
Definition: vec2i.h:118
KyInt32 y
The size of the vector along the Y axis.
Definition: vec2i.h:284
KyInt32 operator[](KyInt32 idx) const
Retrieves the size of the vector around one of its axes.
Definition: vec2i.h:70
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
bool IsOnLeftSide(const Vec2i &A, const Vec2i &B) const
Returns true if this vector is on the left side when moving from A to B, or if this vector is on the ...
Definition: vec2i.h:162
static Vec2i UnitX()
Returns the normalized orientation of the X axis.
Definition: vec2i.h:271
bool IsInsideTriangle(const Vec2i &A, const Vec2i &B, const Vec2i &C) const
Returns true if this vector is inside the clockwise triangle formed by theA,B and C parameters...
Definition: vec2i.h:304
This class defines a two-dimensional vector whose coordinates are stored using 32-bit integers...
Definition: vec2i.h:26
Definition: gamekitcrowddispersion.h:20
KyFloat32 DotProduct(const Vec2f &v1, const Vec2f &v2)
Returns the dot product of v1 and v2.
Definition: vec2f.h:187
Vec2i & operator+=(KyInt32 t)
Addst to both the X and Y coordinates of this vector.
Definition: vec2i.h:101
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
KyInt32 GetSquareLength() const
Returns the square of the magnitude of the vector.
Definition: vec2i.h:169
Vec2i operator+(const Vec2i &v) const
Adds the X coordinate of v to the X coordinate of this vector, and adds the Y coordinate of v to the ...
Definition: vec2i.h:122
Vec2i operator-() const
Negates the X and Y coordinates of this vector, effectively flipping it around the origin...
Definition: vec2i.h:129
bool operator>(const Vec2i &v) const
order by x first: (2,8)>(1,9).
Definition: vec2i.h:89
Vec2i & operator*=(KyInt32 s)
Multiplies both the X and Y coordinates of this vector by the specified value.
Definition: vec2i.h:95
Vec2i()
Creates a vector with coordinates (0,0).
Definition: vec2i.h:39
KyInt32 Orient2d(const Vec2i &A, const Vec2i &B) const
Indicates whether or not the polyline formed by (A to B to this vector ) outlines a triangle in count...
Definition: vec2i.h:152
Vec2i NeighborSouthEast() const
Returns a vector that identifies the next grid cell to the South-East: i.e.
Definition: vec2i.h:236
Vec2i NeighborNorthEast() const
Returns a vector that identifies the next grid cell to the North-East: i.e.
Definition: vec2i.h:228
bool operator<=(const Vec2i &v) const
order by x first: (1,9)<=(2,8).>
Definition: vec2i.h:86
Vec2i NeighborNorth() const
Returns a vector that identifies the next grid cell to the North: i.e. in the positive direction of t...
Definition: vec2i.h:191