DetourCommon.h 18 KB

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