2 Bullet Continuous Collision Detection and Physics Library
\r
3 Copyright (c) 2011 Advanced Micro Devices, Inc. http://bulletphysics.org
\r
5 This software is provided 'as-is', without any express or implied warranty.
\r
6 In no event will the authors be held liable for any damages arising from the use of this software.
\r
7 Permission is granted to anyone to use this software for any purpose,
\r
8 including commercial applications, and to alter it and redistribute it freely,
\r
9 subject to the following restrictions:
\r
11 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.
\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
\r
13 3. This notice may not be removed or altered from any source distribution.
\r
17 #ifndef GRAHAM_SCAN_2D_CONVEX_HULL_H
\r
18 #define GRAHAM_SCAN_2D_CONVEX_HULL_H
\r
21 #include "btVector3.h"
\r
22 #include "btAlignedObjectArray.h"
\r
24 struct GrahamVector3 : public btVector3
\r
26 GrahamVector3(const btVector3& org, int orgIndex)
\r
28 m_orgIndex(orgIndex)
\r
36 struct btAngleCompareFunc {
\r
38 btAngleCompareFunc(const btVector3& anchor)
\r
42 bool operator()(const GrahamVector3& a, const GrahamVector3& b) const {
\r
43 if (a.m_angle != b.m_angle)
\r
44 return a.m_angle < b.m_angle;
\r
47 btScalar al = (a-m_anchor).length2();
\r
48 btScalar bl = (b-m_anchor).length2();
\r
53 return a.m_orgIndex < b.m_orgIndex;
\r
59 inline void GrahamScanConvexHull2D(btAlignedObjectArray<GrahamVector3>& originalPoints, btAlignedObjectArray<GrahamVector3>& hull, const btVector3& normalAxis)
\r
61 btVector3 axis0,axis1;
\r
62 btPlaneSpace1(normalAxis,axis0,axis1);
\r
65 if (originalPoints.size()<=1)
\r
67 for (int i=0;i<originalPoints.size();i++)
\r
68 hull.push_back(originalPoints[0]);
\r
71 //step1 : find anchor point with smallest projection on axis0 and move it to first location
\r
72 for (int i=0;i<originalPoints.size();i++)
\r
74 // const btVector3& left = originalPoints[i];
\r
75 // const btVector3& right = originalPoints[0];
\r
76 btScalar projL = originalPoints[i].dot(axis0);
\r
77 btScalar projR = originalPoints[0].dot(axis0);
\r
80 originalPoints.swap(0,i);
\r
84 //also precompute angles
\r
85 originalPoints[0].m_angle = -1e30f;
\r
86 for (int i=1;i<originalPoints.size();i++)
\r
88 btVector3 xvec = axis0;
\r
89 btVector3 ar = originalPoints[i]-originalPoints[0];
\r
90 originalPoints[i].m_angle = btCross(xvec, ar).dot(normalAxis) / ar.length();
\r
93 //step 2: sort all points, based on 'angle' with this anchor
\r
94 btAngleCompareFunc comp(originalPoints[0]);
\r
95 originalPoints.quickSortInternal(comp,1,originalPoints.size()-1);
\r
98 for (i = 0; i<2; i++)
\r
99 hull.push_back(originalPoints[i]);
\r
101 //step 3: keep all 'convex' points and discard concave points (using back tracking)
\r
102 for (; i != originalPoints.size(); i++)
\r
104 bool isConvex = false;
\r
105 while (!isConvex&& hull.size()>1) {
\r
106 btVector3& a = hull[hull.size()-2];
\r
107 btVector3& b = hull[hull.size()-1];
\r
108 isConvex = btCross(a-b,a-originalPoints[i]).dot(normalAxis)> 0;
\r
112 hull.push_back(originalPoints[i]);
\r
117 #endif //GRAHAM_SCAN_2D_CONVEX_HULL_H
\r