DetourCommon.h 18 KB

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