Advertisement
Guest User

Untitled

a guest
May 4th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.73 KB | None | 0 0
  1.     // change the list of boxes so that the coverage is the same but none overlaps
  2.     // also filters out empty/invalid boxes
  3.     // TODO: something better than O(n^2)
  4.     void removeOverlappingAreas()
  5.     {
  6.         box2i[] filtered;
  7.  
  8.         for(int i = 0; i < cast(int)(boxes.length); ++i)
  9.         {
  10.             box2i A = boxes[i];
  11.  
  12.             assert(A.isSorted());
  13.  
  14.             // empty boxes aren't kept
  15.             if (A.volume() <= 0)
  16.                 continue;
  17.  
  18.             bool foundIntersection = false;
  19.  
  20.             // test A against all other rectangles, if it pass, it is pushed
  21.             for(int j = i + 1; j < cast(int)(boxes.length); ++j)
  22.             {
  23.                 box2i B = boxes[j];
  24.  
  25.                 box2i C = A.intersection(B);
  26.                 bool doesIntersect =  C.isSorted() && C.volume() != 0;
  27.  
  28.                 if (doesIntersect)
  29.                 {
  30.                     // case 1: A contains B, B is simply removed, no intersection considered
  31.                     if (A.contains(B))
  32.                         continue;
  33.  
  34.                     foundIntersection = true; // A will not be pushed as is
  35.  
  36.                     if (B.contains(A))
  37.                         break; // nothing from A is kept
  38.                     else
  39.                     {
  40.                         // Make 4 boxes that are A without C (C is contained in A)
  41.                         // Some may be empty though since C  and touch 2 edges
  42.  
  43.                         // General case
  44.                         // +---------+               +---------+
  45.                         // |    A    |               |    D    |
  46.                         // |  +---+  |   After split +--+---+--+
  47.                         // |  | C |  |        =>     | E|   |F |   At least two of D, E, F or G are empty
  48.                         // |  +---+  |               +--+---+--+
  49.                         // |         |               |    G    |
  50.                         // +---------+               +---------+
  51.  
  52.                         D = box2i(A.min.x, A.min.y, A.max.x, C.min.y);
  53.                         E = box2i(A.min.x, C.min.y, C.min.x, C.max.y);
  54.                         F = box2i(C.min.x, C.min.y, C.max.x, C.max.y);
  55.                         G = box2i(A.min.x, C.max.y, A.max.x, A.max.y);
  56.  
  57.                         if (D.volume() > 0)
  58.                             boxes ~= D;
  59.                         if (E.volume() > 0)
  60.                             boxes ~= D;
  61.                         if (F.volume() > 0)
  62.                             boxes ~= D;
  63.                         if (G.volume() > 0)
  64.                             boxes ~= D;
  65.                     }
  66.                 }
  67.             }
  68.  
  69.             if (!foundIntersection)
  70.                 filtered ~= A;
  71.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement