gwnavruntime/math/gridutils.h Source File

gridutils.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 GwNavGen_SquarePartitionUtils_H
10 #define GwNavGen_SquarePartitionUtils_H
11 
13 
14 
15 namespace Kaim
16 {
17 
18 
19 class GridUtils // class with static functions only
20 {
21 public:
22  // ----------------------------- SQUARE FROM COORD -----------------------------
23  //
24  // ----|---------------|---------------|---------------|---------------|----
25  // Coord -200 -100 0 100 200
26  // LowSquare ] -2 ] -1 ] 0 ] 1 ]
27  // HighSquare [ -2 [ -1 [ 0 [ 1 [
28 
29  static KyInt32 LowSquare(KyInt32 squareSize, KyInt32 coord)
30  {
31  KyInt32 is_strict_pos = -(-coord >> 31); // = coord > 0 ? 1 : 0;
32  return (coord - is_strict_pos) / squareSize - 1 + is_strict_pos;
33  }
34 
35  static KyInt32 HighSquare(KyInt32 squareSize, KyInt32 coord)
36  {
37  KyInt32 is_strict_neg = -(coord >> 31); // = coord < 0 ? 1 : 0;
38  return (coord + is_strict_neg) / squareSize - is_strict_neg;
39  }
40 
41  static KyInt64 HighSquare64(KyInt64 squareSize, KyInt64 coord)
42  {
43  KyInt64 is_strict_neg = -(coord >> 63); // = coord < 0 ? 1 : 0;
44  return (coord + is_strict_neg) / squareSize - is_strict_neg;
45  }
46 
47  static void LowSquare(KyInt32 squareSize, const Vec2i& pos, Vec2i& squarePos)
48  {
49  squarePos.x = LowSquare(squareSize, pos.x);
50  squarePos.y = LowSquare(squareSize, pos.y);
51  }
52 
53  static void HighSquare(KyInt32 squareSize, const Vec2i& pos, Vec2i& squarePos)
54  {
55  squarePos.x = HighSquare(squareSize, pos.x);
56  squarePos.y = HighSquare(squareSize, pos.y);
57  }
58 
59  static void LowSquare(KyInt32 squareSize, const Vec3i& pos, Vec2i& squarePos)
60  {
61  squarePos.x = LowSquare(squareSize, pos.x);
62  squarePos.y = LowSquare(squareSize, pos.y);
63  }
64 
65  static void HighSquare(KyInt32 squareSize, const Vec3i& pos, Vec2i& squarePos)
66  {
67  squarePos.x = HighSquare(squareSize, pos.x);
68  squarePos.y = HighSquare(squareSize, pos.y);
69  }
70 
71  // Get the squareBox that overlaps the coordBox (INCLUDING the coordBox BOUNDARY)
72  static void SquareBox(KyInt32 squareSize, const Box2i& coordBox, Box2i& squareBox)
73  {
74  LowSquare(squareSize, coordBox.m_min, squareBox.m_min);
75  HighSquare(squareSize, coordBox.m_max, squareBox.m_max);
76  squareBox.UpdateCountXY();
77  }
78 
79  // Get the squareBox that overlaps the coordBox (EXCLUDING the coordBox BOUNDARY)
80  static void SquareBoxStrict(KyInt32 squareSize, const Box2i& coordBox, Box2i& squareBox)
81  {
82  HighSquare(squareSize, coordBox.m_min, squareBox.m_min);
83  LowSquare(squareSize, coordBox.m_max, squareBox.m_max);
84  squareBox.UpdateCountXY();
85  }
86 
87  // ----------------------------- COORD FROM SQUARE -----------------------------
88  //
89  static KyInt32 MinCoord(KyInt32 squareSize, KyInt32 squareCoord)
90  {
91  return squareCoord * squareSize;
92  }
93 
94  static KyInt32 MaxCoord(KyInt32 squareSize, KyInt32 squareCoord)
95  {
96  return (squareCoord + 1) * squareSize;
97  }
98 
99  static void MinCoordPos(KyInt32 squareSize, const Vec2i& squarePos, Vec2i& coordPos)
100  {
101  coordPos.x = MinCoord(squareSize, squarePos.x);
102  coordPos.y = MinCoord(squareSize, squarePos.y);
103  }
104 
105  static void MaxCoordPos(KyInt32 squareSize, const Vec2i& squarePos, Vec2i& coordPos)
106  {
107  coordPos.x = MaxCoord(squareSize, squarePos.x);
108  coordPos.y = MaxCoord(squareSize, squarePos.y);
109  }
110 
111  static void GetCoordBox(KyInt32 squareSize, const Box2i& squareBox, Box2i& coordBox)
112  {
113  MinCoordPos(squareSize, squareBox.m_min, coordBox.m_min);
114  MaxCoordPos(squareSize, squareBox.m_max, coordBox.m_max);
115  coordBox.UpdateCountXY();
116  }
117 
118  // ----------------------------- SMALL SQUARE FROM BIG SQUARE -----------------------------
119  //
120  // SmallSquare | -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
121  // BigSquare | -2 | -1 | 0 | 1 | 2 |
122  //
123  static KyInt32 MinSmallSquareCoord(KyInt32 nbSmallsOnBigSide, KyInt32 bigCoord)
124  {
125  return bigCoord * nbSmallsOnBigSide;
126  }
127 
128  static KyInt32 MaxSmallSquareCoord(KyInt32 nbSmallsOnBigSide, KyInt32 bigCoord)
129  {
130  return (bigCoord + 1) * nbSmallsOnBigSide - 1;
131  }
132 
133  static void MinSmallSquarePos(KyInt32 nbSmallsOnBigSide, const Vec2i& bigPos, Vec2i& smallPos)
134  {
135  smallPos.x = MinSmallSquareCoord(nbSmallsOnBigSide, bigPos.x);
136  smallPos.y = MinSmallSquareCoord(nbSmallsOnBigSide, bigPos.y);
137  }
138 
139  static void MaxSmallSquarePos(KyInt32 nbSmallsOnBigSide, const Vec2i& bigPos, Vec2i& smallPos)
140  {
141  smallPos.x = MaxSmallSquareCoord(nbSmallsOnBigSide, bigPos.x);
142  smallPos.y = MaxSmallSquareCoord(nbSmallsOnBigSide, bigPos.y);
143  }
144 
145  static void SmallSquareBox(KyInt32 nbSmallsOnBigSide, const Box2i& bigBox, Box2i& smallBox)
146  {
147  MinSmallSquarePos(nbSmallsOnBigSide, bigBox.m_min, smallBox.m_min);
148  MaxSmallSquarePos(nbSmallsOnBigSide, bigBox.m_max, smallBox.m_max);
149  smallBox.UpdateCountXY();
150  }
151 
152  static void SmallSquareBox(KyInt32 nbSmallsOnBigSide, const Vec2i& bigPos, Box2i& smallBox)
153  {
154  MinSmallSquarePos(nbSmallsOnBigSide, bigPos, smallBox.m_min);
155  MaxSmallSquarePos(nbSmallsOnBigSide, bigPos, smallBox.m_max);
156  smallBox.UpdateCountXY();
157  }
158 
159  // ----------------------------- BIG SQUARE FROM SMALL SQUARE -----------------------------
160  //
161  // SmallSquare | -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
162  // BigSquare | -2 | -1 | 0 | 1 | 2 |
163  //
164  static KyInt32 BigSquareCoord(KyInt32 nbSmallsOnBigSide, KyInt32 smallCoord)
165  {
166  KyInt32 is_strict_neg = -(smallCoord >> 31); // = smallCoord < 0 ? 1 : 0;
167  return (smallCoord + is_strict_neg) / nbSmallsOnBigSide - is_strict_neg;
168  // ((-1 + 1) / 3 - 1) = ( 0 / 3 - 1) = -1
169  // ((-2 + 1) / 3 - 1) = (-1 / 3 - 1) = -1
170  // ((-3 + 1) / 3 - 1) = (-2 / 3 - 1) = -1
171  // ((-4 + 1) / 3 - 1) = (-3 / 3 - 1) = -2
172  }
173 
174  static void BigSquarePos(KyInt32 nbSmallsOnBigSide, const Vec2i& smallPos, Vec2i& bigPos)
175  {
176  bigPos.x = BigSquareCoord(nbSmallsOnBigSide, smallPos.x);
177  bigPos.y = BigSquareCoord(nbSmallsOnBigSide, smallPos.y);
178  }
179 
180  static void BigSquareBox(KyInt32 nbSmallsOnBigSide, const Box2i& smallBox, Box2i& bigBox)
181  {
182  BigSquarePos(nbSmallsOnBigSide, smallBox.m_min, bigBox.m_min);
183  BigSquarePos(nbSmallsOnBigSide, smallBox.m_max, bigBox.m_max);
184  bigBox.UpdateCountXY();
185  }
186 };
187 
188 
189 }
190 
191 
192 #endif
193 
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
Definition: gamekitcrowddispersion.h:20
__int64 KyInt64
Type used internally to represent a 64-bit integer.
Definition: types.h:37