DetourCommon.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Copyright (c) 2009 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. //////////////////////////////////////////////////////////////////////////////////////////
  21. template<class T> inline void swap(T& a, T& b) { T t = a; a = b; b = t; }
  22. template<class T> inline T min(T a, T b) { return a < b ? a : b; }
  23. template<class T> inline T max(T a, T b) { return a > b ? a : b; }
  24. template<class T> inline T abs(T a) { return a < 0 ? -a : a; }
  25. template<class T> inline T sqr(T a) { return a*a; }
  26. template<class T> inline T clamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
  27. inline void vcross(float* dest, const float* v1, const float* v2)
  28. {
  29. dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
  30. dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
  31. dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
  32. }
  33. inline float vdot(const float* v1, const float* v2)
  34. {
  35. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  36. }
  37. inline void vmad(float* dest, const float* v1, const float* v2, const float s)
  38. {
  39. dest[0] = v1[0]+v2[0]*s;
  40. dest[1] = v1[1]+v2[1]*s;
  41. dest[2] = v1[2]+v2[2]*s;
  42. }
  43. inline void vadd(float* dest, const float* v1, const float* v2)
  44. {
  45. dest[0] = v1[0]+v2[0];
  46. dest[1] = v1[1]+v2[1];
  47. dest[2] = v1[2]+v2[2];
  48. }
  49. inline void vsub(float* dest, const float* v1, const float* v2)
  50. {
  51. dest[0] = v1[0]-v2[0];
  52. dest[1] = v1[1]-v2[1];
  53. dest[2] = v1[2]-v2[2];
  54. }
  55. inline void vmin(float* mn, const float* v)
  56. {
  57. mn[0] = min(mn[0], v[0]);
  58. mn[1] = min(mn[1], v[1]);
  59. mn[2] = min(mn[2], v[2]);
  60. }
  61. inline void vmax(float* mx, const float* v)
  62. {
  63. mx[0] = max(mx[0], v[0]);
  64. mx[1] = max(mx[1], v[1]);
  65. mx[2] = max(mx[2], v[2]);
  66. }
  67. inline void vcopy(float* dest, const float* a)
  68. {
  69. dest[0] = a[0];
  70. dest[1] = a[1];
  71. dest[2] = a[2];
  72. }
  73. inline float vdist(const float* v1, const float* v2)
  74. {
  75. float dx = v2[0] - v1[0];
  76. float dy = v2[1] - v1[1];
  77. float dz = v2[2] - v1[2];
  78. return sqrtf(dx*dx + dy*dy + dz*dz);
  79. }
  80. inline float vdistSqr(const float* v1, const float* v2)
  81. {
  82. float dx = v2[0] - v1[0];
  83. float dy = v2[1] - v1[1];
  84. float dz = v2[2] - v1[2];
  85. return dx*dx + dy*dy + dz*dz;
  86. }
  87. inline void vnormalize(float* v)
  88. {
  89. float d = 1.0f / sqrtf(sqr(v[0]) + sqr(v[1]) + sqr(v[2]));
  90. v[0] *= d;
  91. v[1] *= d;
  92. v[2] *= d;
  93. }
  94. inline bool vequal(const float* p0, const float* p1)
  95. {
  96. static const float thr = sqr(1.0f/16384.0f);
  97. const float d = vdistSqr(p0, p1);
  98. return d < thr;
  99. }
  100. inline int nextPow2(int v)
  101. {
  102. v--;
  103. v |= v >> 1;
  104. v |= v >> 2;
  105. v |= v >> 4;
  106. v |= v >> 8;
  107. v |= v >> 16;
  108. v++;
  109. return v;
  110. }
  111. inline float vdot2D(const float* u, const float* v)
  112. {
  113. return u[0]*v[0] + u[2]*v[2];
  114. }
  115. inline float vperp2D(const float* u, const float* v)
  116. {
  117. return u[2]*v[0] - u[0]*v[2];
  118. }
  119. inline float triArea2D(const float* a, const float* b, const float* c)
  120. {
  121. return ((b[0]*a[2] - a[0]*b[2]) + (c[0]*b[2] - b[0]*c[2]) + (a[0]*c[2] - c[0]*a[2])) * 0.5f;
  122. }
  123. inline bool checkOverlapBox(const unsigned short amin[3], const unsigned short amax[3],
  124. const unsigned short bmin[3], const unsigned short bmax[3])
  125. {
  126. bool overlap = true;
  127. overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
  128. overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
  129. overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
  130. return overlap;
  131. }
  132. void closestPtPointTriangle(float* closest, const float* p,
  133. const float* a, const float* b, const float* c);
  134. bool closestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h);
  135. bool intersectSegmentPoly2D(const float* p0, const float* p1,
  136. const float* verts, int nverts,
  137. float& tmin, float& tmax,
  138. int& segMin, int& segMax);
  139. float distancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t);
  140. void calcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts);
  141. #endif // DETOURCOMMON_H