DetourCommon.h 18 KB

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