author | truebrain |
Thu, 12 Jun 2008 18:03:50 +0000 | |
branch | noai |
changeset 10938 | df6235dd2b7a |
parent 10355 | ee4b5f7a5bf2 |
permissions | -rw-r--r-- |
9723 | 1 |
/* $Id$ */ |
2 |
||
3 |
/** @file direction_func.h Different functions related to conversions between directions. */ |
|
4 |
||
5 |
#ifndef DIRECTION_FUNC_H |
|
6 |
#define DIRECTION_FUNC_H |
|
7 |
||
8 |
#include "direction_type.h" |
|
9 |
||
10 |
/** |
|
11 |
* Return the reverse of a direction |
|
12 |
* |
|
13 |
* @param d The direction to get the reverse from |
|
14 |
* @return The reverse Direction |
|
15 |
*/ |
|
16 |
static inline Direction ReverseDir(Direction d) |
|
17 |
{ |
|
18 |
return (Direction)(4 ^ d); |
|
19 |
} |
|
20 |
||
21 |
||
22 |
/** |
|
23 |
* Calculate the difference between to directions |
|
24 |
* |
|
25 |
* @param d0 The first direction as the base |
|
26 |
* @param d1 The second direction as the offset from the base |
|
27 |
* @return The difference how the second directions drifts of the first one. |
|
28 |
*/ |
|
29 |
static inline DirDiff DirDifference(Direction d0, Direction d1) |
|
30 |
{ |
|
31 |
return (DirDiff)((d0 + 8 - d1) % 8); |
|
32 |
} |
|
33 |
||
34 |
/** |
|
35 |
* Applies two differences together |
|
36 |
* |
|
37 |
* This function adds two differences together and return the resulting |
|
38 |
* difference. So adding two DIRDIFF_REVERSE together results in the |
|
39 |
* DIRDIFF_SAME difference. |
|
40 |
* |
|
41 |
* @param d The first difference |
|
42 |
* @param delta The second difference to add on |
|
43 |
* @return The resulting difference |
|
44 |
*/ |
|
45 |
static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta) |
|
46 |
{ |
|
47 |
return (DirDiff)((d + delta) % 8); |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* Change a direction by a given difference |
|
52 |
* |
|
53 |
* This functions returns a new direction of the given direction |
|
54 |
* which is rotated by the given difference. |
|
55 |
* |
|
56 |
* @param d The direction to get a new direction from |
|
57 |
* @param delta The offset/drift applied to the direction |
|
58 |
* @return The new direction |
|
59 |
*/ |
|
60 |
static inline Direction ChangeDir(Direction d, DirDiff delta) |
|
61 |
{ |
|
62 |
return (Direction)((d + delta) % 8); |
|
63 |
} |
|
64 |
||
65 |
||
66 |
/** |
|
67 |
* Returns the reverse direction of the given DiagDirection |
|
68 |
* |
|
69 |
* @param d The DiagDirection to get the reverse from |
|
70 |
* @return The reverse direction |
|
71 |
*/ |
|
72 |
static inline DiagDirection ReverseDiagDir(DiagDirection d) |
|
73 |
{ |
|
74 |
return (DiagDirection)(2 ^ d); |
|
75 |
} |
|
76 |
||
77 |
||
78 |
/** |
|
79 |
* Applies a difference on a DiagDirection |
|
80 |
* |
|
81 |
* This function applies a difference on a DiagDirection and returns |
|
82 |
* the new DiagDirection. |
|
83 |
* |
|
84 |
* @param d The DiagDirection |
|
85 |
* @param delta The difference to applie on |
|
86 |
* @return The new direction which was calculated |
|
87 |
*/ |
|
88 |
static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta) |
|
89 |
{ |
|
90 |
return (DiagDirection)((d + delta) % 4); |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Convert a Direction to a DiagDirection. |
|
95 |
* |
|
96 |
* This function can be used to convert the 8-way Direction to |
|
97 |
* the 4-way DiagDirection. If the direction cannot be mapped its |
|
98 |
* "rounded clockwise". So DIR_N becomes DIAGDIR_NE. |
|
99 |
* |
|
100 |
* @param dir The direction to convert |
|
101 |
* @return The resulting DiagDirection, maybe "rounded clockwise". |
|
102 |
*/ |
|
103 |
static inline DiagDirection DirToDiagDir(Direction dir) |
|
104 |
{ |
|
105 |
return (DiagDirection)(dir >> 1); |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Convert a DiagDirection to a Direction. |
|
110 |
* |
|
111 |
* This function can be used to convert the 4-way DiagDirection |
|
112 |
* to the 8-way Direction. As 4-way are less than 8-way not all |
|
113 |
* possible directions can be calculated. |
|
114 |
* |
|
115 |
* @param dir The direction to convert |
|
116 |
* @return The resulting Direction |
|
117 |
*/ |
|
118 |
static inline Direction DiagDirToDir(DiagDirection dir) |
|
119 |
{ |
|
120 |
return (Direction)(dir * 2 + 1); |
|
121 |
} |
|
122 |
||
123 |
||
124 |
/** |
|
125 |
* Select the other axis as provided. |
|
126 |
* |
|
127 |
* This is basically the not-operator for the axis. |
|
128 |
* |
|
129 |
* @param a The given axis |
|
130 |
* @return The other axis |
|
131 |
*/ |
|
132 |
static inline Axis OtherAxis(Axis a) |
|
133 |
{ |
|
134 |
return (Axis)(a ^ 1); |
|
135 |
} |
|
136 |
||
137 |
||
138 |
/** |
|
139 |
* Convert a DiagDirection to the axis. |
|
140 |
* |
|
141 |
* This function returns the axis which belongs to the given |
|
142 |
* DiagDirection. The axis X belongs to the DiagDirection |
|
143 |
* north-east and south-west. |
|
144 |
* |
|
145 |
* @param d The DiagDirection |
|
146 |
* @return The axis which belongs to the direction |
|
147 |
*/ |
|
148 |
static inline Axis DiagDirToAxis(DiagDirection d) |
|
149 |
{ |
|
150 |
return (Axis)(d & 1); |
|
151 |
} |
|
152 |
||
153 |
||
154 |
/** |
|
155 |
* Converts an Axis to a DiagDirection |
|
156 |
* |
|
157 |
* This function returns the DiagDirection which |
|
158 |
* belongs to the axis. As 2 directions are mapped to an axis |
|
159 |
* this function returns the one which points to south, |
|
160 |
* either south-west (on X axis) or south-east (on Y axis) |
|
161 |
* |
|
162 |
* @param a The axis |
|
163 |
* @return The direction pointed to south |
|
164 |
*/ |
|
165 |
static inline DiagDirection AxisToDiagDir(Axis a) |
|
166 |
{ |
|
167 |
return (DiagDirection)(2 - a); |
|
168 |
} |
|
169 |
||
170 |
/** |
|
10355
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
171 |
* Converts an Axis to a Direction |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
172 |
* |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
173 |
* This function returns the Direction which |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
174 |
* belongs to the axis. As 2 directions are mapped to an axis |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
175 |
* this function returns the one which points to south, |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
176 |
* either south-west (on X axis) or south-east (on Y axis) |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
177 |
* |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
178 |
* @param a The axis |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
179 |
* @return The direction pointed to south |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
180 |
*/ |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
181 |
static inline Direction AxisToDirection(Axis a) |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
182 |
{ |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
183 |
return (Direction)(5 - 2 * a); |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
184 |
} |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
185 |
|
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
9723
diff
changeset
|
186 |
/** |
9723 | 187 |
* Convert an axis and a flag for north/south into a DiagDirection |
188 |
* @param xy axis to convert |
|
189 |
* @param ns north -> 0, south -> 1 |
|
190 |
* @return the desired DiagDirection |
|
191 |
*/ |
|
192 |
static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns) |
|
193 |
{ |
|
194 |
return (DiagDirection)(xy * 3 ^ ns * 2); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Checks if an interger value is a valid DiagDirection |
|
199 |
* |
|
200 |
* @param d The value to check |
|
201 |
* @return True if the value belongs to a DiagDirection, else false |
|
202 |
*/ |
|
203 |
static inline bool IsValidDiagDirection(DiagDirection d) |
|
204 |
{ |
|
205 |
return d < DIAGDIR_END; |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Checks if an integer value is a valid Direction |
|
210 |
* |
|
211 |
* @param d The value to check |
|
212 |
* @return True if the value belongs to a Direction, else false |
|
213 |
*/ |
|
214 |
static inline bool IsValidDirection(Direction d) |
|
215 |
{ |
|
216 |
return d < DIR_END; |
|
217 |
} |
|
218 |
||
219 |
/** |
|
220 |
* Checks if an integer value is a valid Axis |
|
221 |
* |
|
222 |
* @param d The value to check |
|
223 |
* @return True if the value belongs to an Axis, else false |
|
224 |
*/ |
|
225 |
static inline bool IsValidAxis(Axis d) |
|
226 |
{ |
|
227 |
return d < AXIS_END; |
|
228 |
} |
|
229 |
||
230 |
#endif /* DIRECTION_H */ |