Advertisement
Guest User

Untitled

a guest
Apr 15th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  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. void b2PolygonShape::Set(const b2Vec2* vertices, int32 count)
  20. {
  21.     b2Assert(3 <= count && count <= b2_maxPolygonVertices);
  22.     if (count < 3)
  23.     {
  24.         SetAsBox(1.0f, 1.0f);
  25.         return;
  26.     }
  27.    
  28.     int32 n = b2Min(count, b2_maxPolygonVertices);
  29.  
  30.     // Perform welding and copy vertices into local buffer.
  31.     b2Vec2 ps[b2_maxPolygonVertices];
  32.     int32 tempCount = 0;
  33.     for (int32 i = 0; i < n; ++i)
  34.     {
  35.         b2Vec2 v = vertices[i];
  36.  
  37.         bool unique = true;
  38.         for (int32 j = 0; j < tempCount; ++j)
  39.         {
  40.             if (b2DistanceSquared(v, ps[j]) < 0.5f * b2_linearSlop)
  41.             {
  42.                 unique = false;
  43.                 break;
  44.             }
  45.         }
  46.  
  47.         if (unique)
  48.         {
  49.             ps[tempCount++] = v;
  50.         }
  51.     }
  52.  
  53.     n = tempCount;
  54.     if (n < 3)
  55.     {
  56.         // Polygon is degenerate.
  57.         b2Assert(false);
  58.         SetAsBox(1.0f, 1.0f);
  59.         return;
  60.     }
  61.  
  62.     // Create the convex hull using the Gift wrapping algorithm
  63.     // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm
  64.  
  65.     // Find the right most point on the hull
  66.     int32 i0 = 0;
  67.     float32 x0 = ps[0].x;
  68.     for (int32 i = 1; i < n; ++i)
  69.     {
  70.         float32 x = ps[i].x;
  71.         if (x > x0 || (x == x0 && ps[i].y < ps[i0].y))
  72.         {
  73.             i0 = i;
  74.             x0 = x;
  75.         }
  76.     }
  77.  
  78.     int32 hull[b2_maxPolygonVertices];
  79.     int32 m = 0;
  80.     int32 ih = i0;
  81.  
  82.     for (;;)
  83.     {
  84.         hull[m] = ih;
  85.  
  86.         int32 ie = 0;
  87.         for (int32 j = 1; j < n; ++j)
  88.         {
  89.             if (ie == ih)
  90.             {
  91.                 ie = j;
  92.                 continue;
  93.             }
  94.  
  95.             b2Vec2 r = ps[ie] - ps[hull[m]];
  96.             b2Vec2 v = ps[j] - ps[hull[m]];
  97.             float32 c = b2Cross(r, v);
  98.             if (c < 0.0f)
  99.             {
  100.                 ie = j;
  101.             }
  102.  
  103.             // Collinearity check
  104.             if (c == 0.0f && v.LengthSquared() > r.LengthSquared())
  105.             {
  106.                 ie = j;
  107.             }
  108.         }
  109.  
  110.         ++m;
  111.         ih = ie;
  112.  
  113.         if (ie == i0)
  114.         {
  115.             break;
  116.         }
  117.     }
  118.    
  119.     m_count = m;
  120.  
  121.     // Copy vertices.
  122.     for (int32 i = 0; i < m; ++i)
  123.     {
  124.         m_vertices[i] = ps[hull[i]];
  125.     }
  126.  
  127.     // Compute normals. Ensure the edges have non-zero length.
  128.     for (int32 i = 0; i < m; ++i)
  129.     {
  130.         int32 i1 = i;
  131.         int32 i2 = i + 1 < m ? i + 1 : 0;
  132.         b2Vec2 edge = m_vertices[i2] - m_vertices[i1];
  133.         b2Assert(edge.LengthSquared() > b2_epsilon * b2_epsilon);
  134.         m_normals[i] = b2Cross(edge, 1.0f);
  135.         m_normals[i].Normalize();
  136.     }
  137.  
  138.     // Compute the polygon centroid.
  139.     m_centroid = ComputeCentroid(m_vertices, m);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement