b2Polygon.h 3.7 KB

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