gwnavruntime/math/gridutils.h Source File

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