| 1 | // |
| 2 | // Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 3 | // |
| 4 | // This software is provided 'as-is', without any express or implied |
| 5 | // warranty. In no event will the authors be held liable for any damages |
| 6 | // arising from the use of this software. |
| 7 | // Permission is granted to anyone to use this software for any purpose, |
| 8 | // including commercial applications, and to alter it and redistribute it |
| 9 | // freely, subject to the following restrictions: |
| 10 | // 1. The origin of this software must not be misrepresented; you must not |
| 11 | // claim that you wrote the original software. If you use this software |
| 12 | // in a product, an acknowledgment in the product documentation would be |
| 13 | // appreciated but is not required. |
| 14 | // 2. Altered source versions must be plainly marked as such, and must not be |
| 15 | // misrepresented as being the original software. |
| 16 | // 3. This notice may not be removed or altered from any source distribution. |
| 17 | // |
| 18 | |
| 19 | #ifndef DETOURCOMMON_H |
| 20 | #define DETOURCOMMON_H |
| 21 | |
| 22 | #include "DetourMath.h" |
| 23 | #include <stddef.h> |
| 24 | |
| 25 | /** |
| 26 | @defgroup detour Detour |
| 27 | |
| 28 | Members in this module are used to create, manipulate, and query navigation |
| 29 | meshes. |
| 30 | |
| 31 | @note This is a summary list of members. Use the index or search |
| 32 | feature to find minor members. |
| 33 | */ |
| 34 | |
| 35 | /// @name General helper functions |
| 36 | /// @{ |
| 37 | |
| 38 | /// Used to ignore a function parameter. VS complains about unused parameters |
| 39 | /// and this silences the warning. |
| 40 | template<class T> void dtIgnoreUnused(const T&) { } |
| 41 | |
| 42 | /// Swaps the values of the two parameters. |
| 43 | /// @param[in,out] a Value A |
| 44 | /// @param[in,out] b Value B |
| 45 | template<class T> inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; } |
| 46 | |
| 47 | /// Returns the minimum of two values. |
| 48 | /// @param[in] a Value A |
| 49 | /// @param[in] b Value B |
| 50 | /// @return The minimum of the two values. |
| 51 | template<class T> inline T dtMin(T a, T b) { return a < b ? a : b; } |
| 52 | |
| 53 | /// Returns the maximum of two values. |
| 54 | /// @param[in] a Value A |
| 55 | /// @param[in] b Value B |
| 56 | /// @return The maximum of the two values. |
| 57 | template<class T> inline T dtMax(T a, T b) { return a > b ? a : b; } |
| 58 | |
| 59 | /// Returns the absolute value. |
| 60 | /// @param[in] a The value. |
| 61 | /// @return The absolute value of the specified value. |
| 62 | template<class T> inline T dtAbs(T a) { return a < 0 ? -a : a; } |
| 63 | |
| 64 | /// Returns the square of the value. |
| 65 | /// @param[in] a The value. |
| 66 | /// @return The square of the value. |
| 67 | template<class T> inline T dtSqr(T a) { return a*a; } |
| 68 | |
| 69 | /// Clamps the value to the specified range. |
| 70 | /// @param[in] v The value to clamp. |
| 71 | /// @param[in] mn The minimum permitted return value. |
| 72 | /// @param[in] mx The maximum permitted return value. |
| 73 | /// @return The value, clamped to the specified range. |
| 74 | template<class T> inline T dtClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); } |
| 75 | |
| 76 | /// @} |
| 77 | /// @name Vector helper functions. |
| 78 | /// @{ |
| 79 | |
| 80 | /// Derives the cross product of two vectors. (@p v1 x @p v2) |
| 81 | /// @param[out] dest The cross product. [(x, y, z)] |
| 82 | /// @param[in] v1 A Vector [(x, y, z)] |
| 83 | /// @param[in] v2 A vector [(x, y, z)] |
| 84 | inline void dtVcross(float* dest, const float* v1, const float* v2) |
| 85 | { |
| 86 | dest[0] = v1[1]*v2[2] - v1[2]*v2[1]; |
| 87 | dest[1] = v1[2]*v2[0] - v1[0]*v2[2]; |
| 88 | dest[2] = v1[0]*v2[1] - v1[1]*v2[0]; |
| 89 | } |
| 90 | |
| 91 | /// Derives the dot product of two vectors. (@p v1 . @p v2) |
| 92 | /// @param[in] v1 A Vector [(x, y, z)] |
| 93 | /// @param[in] v2 A vector [(x, y, z)] |
| 94 | /// @return The dot product. |
| 95 | inline float dtVdot(const float* v1, const float* v2) |
| 96 | { |
| 97 | return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]; |
| 98 | } |
| 99 | |
| 100 | /// Performs a scaled vector addition. (@p v1 + (@p v2 * @p s)) |
| 101 | /// @param[out] dest The result vector. [(x, y, z)] |
| 102 | /// @param[in] v1 The base vector. [(x, y, z)] |
| 103 | /// @param[in] v2 The vector to scale and add to @p v1. [(x, y, z)] |
| 104 | /// @param[in] s The amount to scale @p v2 by before adding to @p v1. |
| 105 | inline void dtVmad(float* dest, const float* v1, const float* v2, const float s) |
| 106 | { |
| 107 | dest[0] = v1[0]+v2[0]*s; |
| 108 | dest[1] = v1[1]+v2[1]*s; |
| 109 | dest[2] = v1[2]+v2[2]*s; |
| 110 | } |
| 111 | |
| 112 | /// Performs a linear interpolation between two vectors. (@p v1 toward @p v2) |
| 113 | /// @param[out] dest The result vector. [(x, y, x)] |
| 114 | /// @param[in] v1 The starting vector. |
| 115 | /// @param[in] v2 The destination vector. |
| 116 | /// @param[in] t The interpolation factor. [Limits: 0 <= value <= 1.0] |
| 117 | inline void dtVlerp(float* dest, const float* v1, const float* v2, const float t) |
| 118 | { |
| 119 | dest[0] = v1[0]+(v2[0]-v1[0])*t; |
| 120 | dest[1] = v1[1]+(v2[1]-v1[1])*t; |
| 121 | dest[2] = v1[2]+(v2[2]-v1[2])*t; |
| 122 | } |
| 123 | |
| 124 | /// Performs a vector addition. (@p v1 + @p v2) |
| 125 | /// @param[out] dest The result vector. [(x, y, z)] |
| 126 | /// @param[in] v1 The base vector. [(x, y, z)] |
| 127 | /// @param[in] v2 The vector to add to @p v1. [(x, y, z)] |
| 128 | inline void dtVadd(float* dest, const float* v1, const float* v2) |
| 129 | { |
| 130 | dest[0] = v1[0]+v2[0]; |
| 131 | dest[1] = v1[1]+v2[1]; |
| 132 | dest[2] = v1[2]+v2[2]; |
| 133 | } |
| 134 | |
| 135 | /// Performs a vector subtraction. (@p v1 - @p v2) |
| 136 | /// @param[out] dest The result vector. [(x, y, z)] |
| 137 | /// @param[in] v1 The base vector. [(x, y, z)] |
| 138 | /// @param[in] v2 The vector to subtract from @p v1. [(x, y, z)] |
| 139 | inline void dtVsub(float* dest, const float* v1, const float* v2) |
| 140 | { |
| 141 | dest[0] = v1[0]-v2[0]; |
| 142 | dest[1] = v1[1]-v2[1]; |
| 143 | dest[2] = v1[2]-v2[2]; |
| 144 | } |
| 145 | |
| 146 | /// Scales the vector by the specified value. (@p v * @p t) |
| 147 | /// @param[out] dest The result vector. [(x, y, z)] |
| 148 | /// @param[in] v The vector to scale. [(x, y, z)] |
| 149 | /// @param[in] t The scaling factor. |
| 150 | inline void dtVscale(float* dest, const float* v, const float t) |
| 151 | { |
| 152 | dest[0] = v[0]*t; |
| 153 | dest[1] = v[1]*t; |
| 154 | dest[2] = v[2]*t; |
| 155 | } |
| 156 | |
| 157 | /// Selects the minimum value of each element from the specified vectors. |
| 158 | /// @param[in,out] mn A vector. (Will be updated with the result.) [(x, y, z)] |
| 159 | /// @param[in] v A vector. [(x, y, z)] |
| 160 | inline void dtVmin(float* mn, const float* v) |
| 161 | { |
| 162 | mn[0] = dtMin(mn[0], v[0]); |
| 163 | mn[1] = dtMin(mn[1], v[1]); |
| 164 | mn[2] = dtMin(mn[2], v[2]); |
| 165 | } |
| 166 | |
| 167 | /// Selects the maximum value of each element from the specified vectors. |
| 168 | /// @param[in,out] mx A vector. (Will be updated with the result.) [(x, y, z)] |
| 169 | /// @param[in] v A vector. [(x, y, z)] |
| 170 | inline void dtVmax(float* mx, const float* v) |
| 171 | { |
| 172 | mx[0] = dtMax(mx[0], v[0]); |
| 173 | mx[1] = dtMax(mx[1], v[1]); |
| 174 | mx[2] = dtMax(mx[2], v[2]); |
| 175 | } |
| 176 | |
| 177 | /// Sets the vector elements to the specified values. |
| 178 | /// @param[out] dest The result vector. [(x, y, z)] |
| 179 | /// @param[in] x The x-value of the vector. |
| 180 | /// @param[in] y The y-value of the vector. |
| 181 | /// @param[in] z The z-value of the vector. |
| 182 | inline void dtVset(float* dest, const float x, const float y, const float z) |
| 183 | { |
| 184 | dest[0] = x; dest[1] = y; dest[2] = z; |
| 185 | } |
| 186 | |
| 187 | /// Performs a vector copy. |
| 188 | /// @param[out] dest The result. [(x, y, z)] |
| 189 | /// @param[in] a The vector to copy. [(x, y, z)] |
| 190 | inline void dtVcopy(float* dest, const float* a) |
| 191 | { |
| 192 | dest[0] = a[0]; |
| 193 | dest[1] = a[1]; |
| 194 | dest[2] = a[2]; |
| 195 | } |
| 196 | |
| 197 | /// Derives the scalar length of the vector. |
| 198 | /// @param[in] v The vector. [(x, y, z)] |
| 199 | /// @return The scalar length of the vector. |
| 200 | inline float dtVlen(const float* v) |
| 201 | { |
| 202 | return dtMathSqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); |
| 203 | } |
| 204 | |
| 205 | /// Derives the square of the scalar length of the vector. (len * len) |
| 206 | /// @param[in] v The vector. [(x, y, z)] |
| 207 | /// @return The square of the scalar length of the vector. |
| 208 | inline float dtVlenSqr(const float* v) |
| 209 | { |
| 210 | return v[0]*v[0] + v[1]*v[1] + v[2]*v[2]; |
| 211 | } |
| 212 | |
| 213 | /// Returns the distance between two points. |
| 214 | /// @param[in] v1 A point. [(x, y, z)] |
| 215 | /// @param[in] v2 A point. [(x, y, z)] |
| 216 | /// @return The distance between the two points. |
| 217 | inline float dtVdist(const float* v1, const float* v2) |
| 218 | { |
| 219 | const float dx = v2[0] - v1[0]; |
| 220 | const float dy = v2[1] - v1[1]; |
| 221 | const float dz = v2[2] - v1[2]; |
| 222 | return dtMathSqrtf(dx*dx + dy*dy + dz*dz); |
| 223 | } |
| 224 | |
| 225 | /// Returns the square of the distance between two points. |
| 226 | /// @param[in] v1 A point. [(x, y, z)] |
| 227 | /// @param[in] v2 A point. [(x, y, z)] |
| 228 | /// @return The square of the distance between the two points. |
| 229 | inline float dtVdistSqr(const float* v1, const float* v2) |
| 230 | { |
| 231 | const float dx = v2[0] - v1[0]; |
| 232 | const float dy = v2[1] - v1[1]; |
| 233 | const float dz = v2[2] - v1[2]; |
| 234 | return dx*dx + dy*dy + dz*dz; |
| 235 | } |
| 236 | |
| 237 | /// Derives the distance between the specified points on the xz-plane. |
| 238 | /// @param[in] v1 A point. [(x, y, z)] |
| 239 | /// @param[in] v2 A point. [(x, y, z)] |
| 240 | /// @return The distance between the point on the xz-plane. |
| 241 | /// |
| 242 | /// The vectors are projected onto the xz-plane, so the y-values are ignored. |
| 243 | inline float dtVdist2D(const float* v1, const float* v2) |
| 244 | { |
| 245 | const float dx = v2[0] - v1[0]; |
| 246 | const float dz = v2[2] - v1[2]; |
| 247 | return dtMathSqrtf(dx*dx + dz*dz); |
| 248 | } |
| 249 | |
| 250 | /// Derives the square of the distance between the specified points on the xz-plane. |
| 251 | /// @param[in] v1 A point. [(x, y, z)] |
| 252 | /// @param[in] v2 A point. [(x, y, z)] |
| 253 | /// @return The square of the distance between the point on the xz-plane. |
| 254 | inline float dtVdist2DSqr(const float* v1, const float* v2) |
| 255 | { |
| 256 | const float dx = v2[0] - v1[0]; |
| 257 | const float dz = v2[2] - v1[2]; |
| 258 | return dx*dx + dz*dz; |
| 259 | } |
| 260 | |
| 261 | /// Normalizes the vector. |
| 262 | /// @param[in,out] v The vector to normalize. [(x, y, z)] |
| 263 | inline void dtVnormalize(float* v) |
| 264 | { |
| 265 | float d = 1.0f / dtMathSqrtf(dtSqr(v[0]) + dtSqr(v[1]) + dtSqr(v[2])); |
| 266 | v[0] *= d; |
| 267 | v[1] *= d; |
| 268 | v[2] *= d; |
| 269 | } |
| 270 | |
| 271 | /// Performs a 'sloppy' colocation check of the specified points. |
| 272 | /// @param[in] p0 A point. [(x, y, z)] |
| 273 | /// @param[in] p1 A point. [(x, y, z)] |
| 274 | /// @return True if the points are considered to be at the same location. |
| 275 | /// |
| 276 | /// Basically, this function will return true if the specified points are |
| 277 | /// close enough to eachother to be considered colocated. |
| 278 | inline bool dtVequal(const float* p0, const float* p1) |
| 279 | { |
| 280 | static const float thr = dtSqr(1.0f/16384.0f); |
| 281 | const float d = dtVdistSqr(p0, p1); |
| 282 | return d < thr; |
| 283 | } |
| 284 | |
| 285 | /// Checks that the specified vector's components are all finite. |
| 286 | /// @param[in] v A point. [(x, y, z)] |
| 287 | /// @return True if all of the point's components are finite, i.e. not NaN |
| 288 | /// or any of the infinities. |
| 289 | inline bool dtVisfinite(const float* v) |
| 290 | { |
| 291 | bool result = |
| 292 | dtMathIsfinite(v[0]) && |
| 293 | dtMathIsfinite(v[1]) && |
| 294 | dtMathIsfinite(v[2]); |
| 295 | |
| 296 | return result; |
| 297 | } |
| 298 | |
| 299 | /// Checks that the specified vector's 2D components are finite. |
| 300 | /// @param[in] v A point. [(x, y, z)] |
| 301 | inline bool dtVisfinite2D(const float* v) |
| 302 | { |
| 303 | bool result = dtMathIsfinite(v[0]) && dtMathIsfinite(v[2]); |
| 304 | return result; |
| 305 | } |
| 306 | |
| 307 | /// Derives the dot product of two vectors on the xz-plane. (@p u . @p v) |
| 308 | /// @param[in] u A vector [(x, y, z)] |
| 309 | /// @param[in] v A vector [(x, y, z)] |
| 310 | /// @return The dot product on the xz-plane. |
| 311 | /// |
| 312 | /// The vectors are projected onto the xz-plane, so the y-values are ignored. |
| 313 | inline float dtVdot2D(const float* u, const float* v) |
| 314 | { |
| 315 | return u[0]*v[0] + u[2]*v[2]; |
| 316 | } |
| 317 | |
| 318 | /// Derives the xz-plane 2D perp product of the two vectors. (uz*vx - ux*vz) |
| 319 | /// @param[in] u The LHV vector [(x, y, z)] |
| 320 | /// @param[in] v The RHV vector [(x, y, z)] |
| 321 | /// @return The perp dot product on the xz-plane. |
| 322 | /// |
| 323 | /// The vectors are projected onto the xz-plane, so the y-values are ignored. |
| 324 | inline float dtVperp2D(const float* u, const float* v) |
| 325 | { |
| 326 | return u[2]*v[0] - u[0]*v[2]; |
| 327 | } |
| 328 | |
| 329 | /// @} |
| 330 | /// @name Computational geometry helper functions. |
| 331 | /// @{ |
| 332 | |
| 333 | /// Derives the signed xz-plane area of the triangle ABC, or the relationship of line AB to point C. |
| 334 | /// @param[in] a Vertex A. [(x, y, z)] |
| 335 | /// @param[in] b Vertex B. [(x, y, z)] |
| 336 | /// @param[in] c Vertex C. [(x, y, z)] |
| 337 | /// @return The signed xz-plane area of the triangle. |
| 338 | inline float dtTriArea2D(const float* a, const float* b, const float* c) |
| 339 | { |
| 340 | const float abx = b[0] - a[0]; |
| 341 | const float abz = b[2] - a[2]; |
| 342 | const float acx = c[0] - a[0]; |
| 343 | const float acz = c[2] - a[2]; |
| 344 | return acx*abz - abx*acz; |
| 345 | } |
| 346 | |
| 347 | /// Determines if two axis-aligned bounding boxes overlap. |
| 348 | /// @param[in] amin Minimum bounds of box A. [(x, y, z)] |
| 349 | /// @param[in] amax Maximum bounds of box A. [(x, y, z)] |
| 350 | /// @param[in] bmin Minimum bounds of box B. [(x, y, z)] |
| 351 | /// @param[in] bmax Maximum bounds of box B. [(x, y, z)] |
| 352 | /// @return True if the two AABB's overlap. |
| 353 | /// @see dtOverlapBounds |
| 354 | inline bool dtOverlapQuantBounds(const unsigned short amin[3], const unsigned short amax[3], |
| 355 | const unsigned short bmin[3], const unsigned short bmax[3]) |
| 356 | { |
| 357 | bool overlap = true; |
| 358 | overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap; |
| 359 | overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap; |
| 360 | overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap; |
| 361 | return overlap; |
| 362 | } |
| 363 | |
| 364 | /// Determines if two axis-aligned bounding boxes overlap. |
| 365 | /// @param[in] amin Minimum bounds of box A. [(x, y, z)] |
| 366 | /// @param[in] amax Maximum bounds of box A. [(x, y, z)] |
| 367 | /// @param[in] bmin Minimum bounds of box B. [(x, y, z)] |
| 368 | /// @param[in] bmax Maximum bounds of box B. [(x, y, z)] |
| 369 | /// @return True if the two AABB's overlap. |
| 370 | /// @see dtOverlapQuantBounds |
| 371 | inline bool dtOverlapBounds(const float* amin, const float* amax, |
| 372 | const float* bmin, const float* bmax) |
| 373 | { |
| 374 | bool overlap = true; |
| 375 | overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap; |
| 376 | overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap; |
| 377 | overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap; |
| 378 | return overlap; |
| 379 | } |
| 380 | |
| 381 | /// Derives the closest point on a triangle from the specified reference point. |
| 382 | /// @param[out] closest The closest point on the triangle. |
| 383 | /// @param[in] p The reference point from which to test. [(x, y, z)] |
| 384 | /// @param[in] a Vertex A of triangle ABC. [(x, y, z)] |
| 385 | /// @param[in] b Vertex B of triangle ABC. [(x, y, z)] |
| 386 | /// @param[in] c Vertex C of triangle ABC. [(x, y, z)] |
| 387 | void dtClosestPtPointTriangle(float* closest, const float* p, |
| 388 | const float* a, const float* b, const float* c); |
| 389 | |
| 390 | /// Derives the y-axis height of the closest point on the triangle from the specified reference point. |
| 391 | /// @param[in] p The reference point from which to test. [(x, y, z)] |
| 392 | /// @param[in] a Vertex A of triangle ABC. [(x, y, z)] |
| 393 | /// @param[in] b Vertex B of triangle ABC. [(x, y, z)] |
| 394 | /// @param[in] c Vertex C of triangle ABC. [(x, y, z)] |
| 395 | /// @param[out] h The resulting height. |
| 396 | bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h); |
| 397 | |
| 398 | bool dtIntersectSegmentPoly2D(const float* p0, const float* p1, |
| 399 | const float* verts, int nverts, |
| 400 | float& tmin, float& tmax, |
| 401 | int& segMin, int& segMax); |
| 402 | |
| 403 | bool dtIntersectSegSeg2D(const float* ap, const float* aq, |
| 404 | const float* bp, const float* bq, |
| 405 | float& s, float& t); |
| 406 | |
| 407 | /// Determines if the specified point is inside the convex polygon on the xz-plane. |
| 408 | /// @param[in] pt The point to check. [(x, y, z)] |
| 409 | /// @param[in] verts The polygon vertices. [(x, y, z) * @p nverts] |
| 410 | /// @param[in] nverts The number of vertices. [Limit: >= 3] |
| 411 | /// @return True if the point is inside the polygon. |
| 412 | bool dtPointInPolygon(const float* pt, const float* verts, const int nverts); |
| 413 | |
| 414 | bool dtDistancePtPolyEdgesSqr(const float* pt, const float* verts, const int nverts, |
| 415 | float* ed, float* et); |
| 416 | |
| 417 | float dtDistancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t); |
| 418 | |
| 419 | /// Derives the centroid of a convex polygon. |
| 420 | /// @param[out] tc The centroid of the polgyon. [(x, y, z)] |
| 421 | /// @param[in] idx The polygon indices. [(vertIndex) * @p nidx] |
| 422 | /// @param[in] nidx The number of indices in the polygon. [Limit: >= 3] |
| 423 | /// @param[in] verts The polygon vertices. [(x, y, z) * vertCount] |
| 424 | void dtCalcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts); |
| 425 | |
| 426 | /// Determines if the two convex polygons overlap on the xz-plane. |
| 427 | /// @param[in] polya Polygon A vertices. [(x, y, z) * @p npolya] |
| 428 | /// @param[in] npolya The number of vertices in polygon A. |
| 429 | /// @param[in] polyb Polygon B vertices. [(x, y, z) * @p npolyb] |
| 430 | /// @param[in] npolyb The number of vertices in polygon B. |
| 431 | /// @return True if the two polygons overlap. |
| 432 | bool dtOverlapPolyPoly2D(const float* polya, const int npolya, |
| 433 | const float* polyb, const int npolyb); |
| 434 | |
| 435 | /// @} |
| 436 | /// @name Miscellanious functions. |
| 437 | /// @{ |
| 438 | |
| 439 | inline unsigned int dtNextPow2(unsigned int v) |
| 440 | { |
| 441 | v--; |
| 442 | v |= v >> 1; |
| 443 | v |= v >> 2; |
| 444 | v |= v >> 4; |
| 445 | v |= v >> 8; |
| 446 | v |= v >> 16; |
| 447 | v++; |
| 448 | return v; |
| 449 | } |
| 450 | |
| 451 | inline unsigned int dtIlog2(unsigned int v) |
| 452 | { |
| 453 | unsigned int r; |
| 454 | unsigned int shift; |
| 455 | r = (v > 0xffff) << 4; v >>= r; |
| 456 | shift = (v > 0xff) << 3; v >>= shift; r |= shift; |
| 457 | shift = (v > 0xf) << 2; v >>= shift; r |= shift; |
| 458 | shift = (v > 0x3) << 1; v >>= shift; r |= shift; |
| 459 | r |= (v >> 1); |
| 460 | return r; |
| 461 | } |
| 462 | |
| 463 | inline int dtAlign4(int x) { return (x+3) & ~3; } |
| 464 | |
| 465 | inline int dtOppositeTile(int side) { return (side+4) & 0x7; } |
| 466 | |
| 467 | inline void dtSwapByte(unsigned char* a, unsigned char* b) |
| 468 | { |
| 469 | unsigned char tmp = *a; |
| 470 | *a = *b; |
| 471 | *b = tmp; |
| 472 | } |
| 473 | |
| 474 | inline void dtSwapEndian(unsigned short* v) |
| 475 | { |
| 476 | unsigned char* x = (unsigned char*)v; |
| 477 | dtSwapByte(x+0, x+1); |
| 478 | } |
| 479 | |
| 480 | inline void dtSwapEndian(short* v) |
| 481 | { |
| 482 | unsigned char* x = (unsigned char*)v; |
| 483 | dtSwapByte(x+0, x+1); |
| 484 | } |
| 485 | |
| 486 | inline void dtSwapEndian(unsigned int* v) |
| 487 | { |
| 488 | unsigned char* x = (unsigned char*)v; |
| 489 | dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2); |
| 490 | } |
| 491 | |
| 492 | inline void dtSwapEndian(int* v) |
| 493 | { |
| 494 | unsigned char* x = (unsigned char*)v; |
| 495 | dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2); |
| 496 | } |
| 497 | |
| 498 | inline void dtSwapEndian(float* v) |
| 499 | { |
| 500 | unsigned char* x = (unsigned char*)v; |
| 501 | dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2); |
| 502 | } |
| 503 | |
| 504 | void dtRandomPointInConvexPoly(const float* pts, const int npts, float* areas, |
| 505 | const float s, const float t, float* out); |
| 506 | |
| 507 | template<typename TypeToRetrieveAs> |
| 508 | TypeToRetrieveAs* dtGetThenAdvanceBufferPointer(const unsigned char*& buffer, const size_t distanceToAdvance) |
| 509 | { |
| 510 | TypeToRetrieveAs* returnPointer = reinterpret_cast<TypeToRetrieveAs*>(buffer); |
| 511 | buffer += distanceToAdvance; |
| 512 | return returnPointer; |
| 513 | } |
| 514 | |
| 515 | template<typename TypeToRetrieveAs> |
| 516 | TypeToRetrieveAs* dtGetThenAdvanceBufferPointer(unsigned char*& buffer, const size_t distanceToAdvance) |
| 517 | { |
| 518 | TypeToRetrieveAs* returnPointer = reinterpret_cast<TypeToRetrieveAs*>(buffer); |
| 519 | buffer += distanceToAdvance; |
| 520 | return returnPointer; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /// @} |
| 525 | |
| 526 | #endif // DETOURCOMMON_H |
| 527 | |
| 528 | /////////////////////////////////////////////////////////////////////////// |
| 529 | |
| 530 | // This section contains detailed documentation for members that don't have |
| 531 | // a source file. It reduces clutter in the main section of the header. |
| 532 | |
| 533 | /** |
| 534 | |
| 535 | @fn float dtTriArea2D(const float* a, const float* b, const float* c) |
| 536 | @par |
| 537 | |
| 538 | The vertices are projected onto the xz-plane, so the y-values are ignored. |
| 539 | |
| 540 | This is a low cost function than can be used for various purposes. Its main purpose |
| 541 | is for point/line relationship testing. |
| 542 | |
| 543 | In all cases: A value of zero indicates that all vertices are collinear or represent the same point. |
| 544 | (On the xz-plane.) |
| 545 | |
| 546 | When used for point/line relationship tests, AB usually represents a line against which |
| 547 | the C point is to be tested. In this case: |
| 548 | |
| 549 | A positive value indicates that point C is to the left of line AB, looking from A toward B.<br/> |
| 550 | A negative value indicates that point C is to the right of lineAB, looking from A toward B. |
| 551 | |
| 552 | When used for evaluating a triangle: |
| 553 | |
| 554 | The absolute value of the return value is two times the area of the triangle when it is |
| 555 | projected onto the xz-plane. |
| 556 | |
| 557 | A positive return value indicates: |
| 558 | |
| 559 | <ul> |
| 560 | <li>The vertices are wrapped in the normal Detour wrap direction.</li> |
| 561 | <li>The triangle's 3D face normal is in the general up direction.</li> |
| 562 | </ul> |
| 563 | |
| 564 | A negative return value indicates: |
| 565 | |
| 566 | <ul> |
| 567 | <li>The vertices are reverse wrapped. (Wrapped opposite the normal Detour wrap direction.)</li> |
| 568 | <li>The triangle's 3D face normal is in the general down direction.</li> |
| 569 | </ul> |
| 570 | |
| 571 | */ |