Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / LinearMath / btGrahamScan2dConvexHull.h
1 /*\r
2 Bullet Continuous Collision Detection and Physics Library\r
3 Copyright (c) 2011 Advanced Micro Devices, Inc.  http://bulletphysics.org\r
4 \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
10 \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
14 */\r
15 \r
16 \r
17 #ifndef GRAHAM_SCAN_2D_CONVEX_HULL_H\r
18 #define GRAHAM_SCAN_2D_CONVEX_HULL_H\r
19 \r
20 \r
21 #include "btVector3.h"\r
22 #include "btAlignedObjectArray.h"\r
23 \r
24 struct GrahamVector3 : public btVector3\r
25 {\r
26         GrahamVector3(const btVector3& org, int orgIndex)\r
27                 :btVector3(org),\r
28                         m_orgIndex(orgIndex)\r
29         {\r
30         }\r
31         btScalar        m_angle;\r
32         int m_orgIndex;\r
33 };\r
34 \r
35 \r
36 struct btAngleCompareFunc {\r
37         btVector3 m_anchor;\r
38         btAngleCompareFunc(const btVector3& anchor)\r
39         : m_anchor(anchor) \r
40         {\r
41         }\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
45                 else\r
46                 {\r
47                         btScalar al = (a-m_anchor).length2();\r
48                         btScalar bl = (b-m_anchor).length2();\r
49                         if (al != bl)\r
50                                 return  al < bl;\r
51                         else\r
52                         {\r
53                                 return a.m_orgIndex < b.m_orgIndex;\r
54                         }\r
55                 }\r
56         }\r
57 };\r
58 \r
59 inline void GrahamScanConvexHull2D(btAlignedObjectArray<GrahamVector3>& originalPoints, btAlignedObjectArray<GrahamVector3>& hull, const btVector3& normalAxis)\r
60 {\r
61         btVector3 axis0,axis1;\r
62         btPlaneSpace1(normalAxis,axis0,axis1);\r
63         \r
64 \r
65         if (originalPoints.size()<=1)\r
66         {\r
67                 for (int i=0;i<originalPoints.size();i++)\r
68                         hull.push_back(originalPoints[0]);\r
69                 return;\r
70         }\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
73         {\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
78                 if (projL < projR)\r
79                 {\r
80                         originalPoints.swap(0,i);\r
81                 }\r
82         }\r
83 \r
84         //also precompute angles\r
85         originalPoints[0].m_angle = -1e30f;\r
86         for (int i=1;i<originalPoints.size();i++)\r
87         {\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
91         }\r
92 \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
96 \r
97         int i;\r
98         for (i = 0; i<2; i++) \r
99                 hull.push_back(originalPoints[i]);\r
100 \r
101         //step 3: keep all 'convex' points and discard concave points (using back tracking)\r
102         for (; i != originalPoints.size(); i++) \r
103         {\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
109                         if (!isConvex)\r
110                                 hull.pop_back();\r
111                         else \r
112                                 hull.push_back(originalPoints[i]);\r
113                 }\r
114         }\r
115 }\r
116 \r
117 #endif //GRAHAM_SCAN_2D_CONVEX_HULL_H\r