1 /* 2 * Copyright (c) 2007 Eric Jordan 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 19 #ifndef B2_POLYGON_H 20 #define B2_POLYGON_H 21 22 #include "b2Triangle.h" 23 #include "stdio.h" 24 #include <string.h> 25 #include <limits.h> 26 namespace b2ConvexDecomp { 27 28 static bool B2_POLYGON_REPORT_ERRORS = false; 29 30 class b2Polygon; 31 32 int32 remainder(int32 x, int32 modulus); 33 int32 TriangulatePolygon(float32* xv, float32* yv, int32 vNum, b2Triangle* results); 34 bool IsEar(int32 i, float32* xv, float32* yv, int32 xvLength); //Not for external use 35 int32 PolygonizeTriangles(b2Triangle* triangulated, int32 triangulatedLength, b2Polygon* polys, int32 polysLength); 36 int32 DecomposeConvex(b2Polygon* p, b2Polygon* results, int32 maxPolys); 37 //void DecomposeConvexAndAddTo(b2Polygon* p, b2Body* bd, b2FixtureDef* prototype); 38 39 void ReversePolygon(float32* x, float32* y, int n); 40 41 b2Polygon TraceEdge(b2Polygon* p); //For use with self-intersecting polygons, finds outline 42 43 class b2Polygon { 44 45 public: 46 const static int32 maxVerticesPerPolygon = b2_maxPolygonVertices; 47 48 float32* x; //vertex arrays 49 float32* y; 50 int32 nVertices; 51 52 float32 area; 53 bool areaIsSet; 54 55 b2Polygon(float32* _x, float32* _y, int32 nVert); 56 b2Polygon(b2Vec2* v, int32 nVert); 57 b2Polygon(); 58 ~b2Polygon(); 59 60 float32 GetArea(); 61 62 void MergeParallelEdges(float32 tolerance); 63 b2Vec2* GetVertexVecs(); 64 b2Polygon(b2Triangle& t); 65 void Set(const b2Polygon& p); 66 bool IsConvex(); 67 bool IsCCW(); 68 bool IsUsable(bool printError); 69 bool IsUsable(); 70 bool IsSimple(); 71 // void AddTo(b2FixtureDef& pd); 72 73 b2Polygon* Add(b2Triangle& t); 74 print()75 void print(){ 76 printFormatted(); 77 // for (int32 i=0; i<nVertices; ++i){ 78 // printf("i: %d, x:%f, y:%f\n",i,x[i],y[i]); 79 // } 80 } 81 printFormatted()82 void printFormatted(){ 83 printf("float xv[] = {"); 84 for (int32 i=0; i<nVertices; ++i){ 85 printf("%ff,",x[i]); 86 } 87 printf("};\nfloat yv[] = {"); 88 for (int32 i=0; i<nVertices; ++i){ 89 printf("%ff,",y[i]); 90 } 91 printf("};\n"); 92 } 93 b2Polygon(const b2Polygon & p)94 b2Polygon(const b2Polygon& p){ 95 nVertices = p.nVertices; 96 area = p.area; 97 areaIsSet = p.areaIsSet; 98 x = new float32[nVertices]; 99 y = new float32[nVertices]; 100 memcpy(x, p.x, nVertices * sizeof(float32)); 101 memcpy(y, p.y, nVertices * sizeof(float32)); 102 } 103 104 105 }; 106 107 const int32 MAX_CONNECTED = 32; 108 const float32 COLLAPSE_DIST_SQR = CMP_EPSILON*CMP_EPSILON;//0.1f;//1000*CMP_EPSILON*1000*CMP_EPSILON; 109 110 class b2PolyNode{ 111 public: 112 b2Vec2 position; 113 b2PolyNode* connected[MAX_CONNECTED]; 114 int32 nConnected; 115 bool visited; 116 117 b2PolyNode(b2Vec2& pos); 118 b2PolyNode(); 119 void AddConnection(b2PolyNode& toMe); 120 void RemoveConnection(b2PolyNode& fromMe); 121 void RemoveConnectionByIndex(int32 index); 122 bool IsConnectedTo(b2PolyNode& me); 123 b2PolyNode* GetRightestConnection(b2PolyNode* incoming); 124 b2PolyNode* GetRightestConnection(b2Vec2& incomingDir); 125 }; 126 127 128 b2Polygon ConvexHull(b2Vec2* v, int nVert); 129 b2Polygon ConvexHull(float32* cloudX, float32* cloudY, int32 nVert); 130 } 131 #endif 132