btGrahamScan2dConvexHull.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2011 Advanced Micro Devices, Inc. http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef GRAHAM_SCAN_2D_CONVEX_HULL_H
  14. #define GRAHAM_SCAN_2D_CONVEX_HULL_H
  15. #include "btVector3.h"
  16. #include "btAlignedObjectArray.h"
  17. struct GrahamVector3 : public btVector3
  18. {
  19. GrahamVector3(const btVector3& org, int orgIndex)
  20. : btVector3(org),
  21. m_orgIndex(orgIndex)
  22. {
  23. }
  24. btScalar m_angle;
  25. int m_orgIndex;
  26. };
  27. struct btAngleCompareFunc
  28. {
  29. btVector3 m_anchor;
  30. btAngleCompareFunc(const btVector3& anchor)
  31. : m_anchor(anchor)
  32. {
  33. }
  34. bool operator()(const GrahamVector3& a, const GrahamVector3& b) const
  35. {
  36. if (a.m_angle != b.m_angle)
  37. return a.m_angle < b.m_angle;
  38. else
  39. {
  40. btScalar al = (a - m_anchor).length2();
  41. btScalar bl = (b - m_anchor).length2();
  42. if (al != bl)
  43. return al < bl;
  44. else
  45. {
  46. return a.m_orgIndex < b.m_orgIndex;
  47. }
  48. }
  49. }
  50. };
  51. inline void GrahamScanConvexHull2D(btAlignedObjectArray<GrahamVector3>& originalPoints, btAlignedObjectArray<GrahamVector3>& hull, const btVector3& normalAxis)
  52. {
  53. btVector3 axis0, axis1;
  54. btPlaneSpace1(normalAxis, axis0, axis1);
  55. if (originalPoints.size() <= 1)
  56. {
  57. for (int i = 0; i < originalPoints.size(); i++)
  58. hull.push_back(originalPoints[0]);
  59. return;
  60. }
  61. //step1 : find anchor point with smallest projection on axis0 and move it to first location
  62. for (int i = 0; i < originalPoints.size(); i++)
  63. {
  64. // const btVector3& left = originalPoints[i];
  65. // const btVector3& right = originalPoints[0];
  66. btScalar projL = originalPoints[i].dot(axis0);
  67. btScalar projR = originalPoints[0].dot(axis0);
  68. if (projL < projR)
  69. {
  70. originalPoints.swap(0, i);
  71. }
  72. }
  73. //also precompute angles
  74. originalPoints[0].m_angle = -1e30f;
  75. for (int i = 1; i < originalPoints.size(); i++)
  76. {
  77. btVector3 ar = originalPoints[i] - originalPoints[0];
  78. btScalar ar1 = axis1.dot(ar);
  79. btScalar ar0 = axis0.dot(ar);
  80. if (ar1 * ar1 + ar0 * ar0 < FLT_EPSILON)
  81. {
  82. originalPoints[i].m_angle = 0.0f;
  83. }
  84. else
  85. {
  86. originalPoints[i].m_angle = btAtan2Fast(ar1, ar0);
  87. }
  88. }
  89. //step 2: sort all points, based on 'angle' with this anchor
  90. btAngleCompareFunc comp(originalPoints[0]);
  91. originalPoints.quickSortInternal(comp, 1, originalPoints.size() - 1);
  92. int i;
  93. for (i = 0; i < 2; i++)
  94. hull.push_back(originalPoints[i]);
  95. //step 3: keep all 'convex' points and discard concave points (using back tracking)
  96. for (; i != originalPoints.size(); i++)
  97. {
  98. bool isConvex = false;
  99. while (!isConvex && hull.size() > 1)
  100. {
  101. btVector3& a = hull[hull.size() - 2];
  102. btVector3& b = hull[hull.size() - 1];
  103. isConvex = btCross(a - b, a - originalPoints[i]).dot(normalAxis) > 0;
  104. if (!isConvex)
  105. hull.pop_back();
  106. else
  107. hull.push_back(originalPoints[i]);
  108. }
  109. if (hull.size() == 1)
  110. {
  111. hull.push_back(originalPoints[i]);
  112. }
  113. }
  114. }
  115. #endif //GRAHAM_SCAN_2D_CONVEX_HULL_H