Commits

Joseph Marshall committed b06aa9c

Better handle when the generator sends dangling edges.

Comments (0)

Files changed (1)

     srand(0);
     vector<ivec2> points;
     for (int i=0; i<num_points; i++) {
-        points.push_back(ivec2(rand()%width, rand()%height));
+        ivec2 p(rand()%width, rand()%height);
+
+        // A really slow way of making sure all our points are unique.
+        bool found = true;
+        while (found) {
+            found = false;
+            for (auto &p_ : points) {
+                if (p == p_) {
+                    p = ivec2(rand()%width, rand()%height);
+                    found = true;
+                    break;
+                }
+            }
+        }
+
+        points.push_back(p);
     }
 
-    for (int i=0; i<100; i++) {
+    for (int i=0; i<10; i++) {
         if (i != 0) {
             for (int i=0; i<num_points; i++) {
                 points[i] = sites[i].center();
         ivec2 first = tmp_.xy;
         site->verts.push_back(first);
         ivec2 last = tmp_.zw;
-        site->verts.push_back(last);
+        if (first != last)
+            site->verts.push_back(last);
+
 
         vsite.edges.pop_back();
 
         // Convert edges to a single loop of vertices.
         bool reversed = false;
+        int md = 1;
         while (vsite.edges.size() > 0) {
             bool found = false;
             int idx = 0;
             for (auto &e : vsite.edges) {
-                int md = 1;
-                ivec2 d = ivec2(e.xy)-last;
+                ivec2 exy = e.xy;
+                ivec2 ezw = e.zw;
+
+                ivec2 d = exy-last;
                 if (abs(d.x) < md && abs(d.y) < md) {
-                    if (reversed)
-                        site->verts.emplace_front(e.zw);
-                    else
-                        site->verts.emplace_back(e.zw);
-                    last = e.zw;
+                    if (reversed) {
+                        if (ezw != site->verts.front())
+                            site->verts.emplace_front(ezw);
+                    } else {
+                        if (ezw != site->verts.back())
+                            site->verts.emplace_back(ezw);
+                    }
+                    last = ezw;
                     found = true;
                     break;
                 }
 
-                d = ivec2(e.zw)-last;
+                d = ezw-last;
                 if (abs(d.x) < md && abs(d.y) < md) {
-                    if (reversed)
-                        site->verts.emplace_front(e.xy);
-                    else
-                        site->verts.emplace_back(e.xy);
-                    last = e.xy;
+                    if (reversed) {
+                        if (exy != site->verts.front())
+                            site->verts.emplace_front(exy);
+                    } else {
+                        if (exy != site->verts.back())
+                            site->verts.emplace_back(exy);
+                    }
+                    last = exy;
                     found = true;
                     break;
                 }
             if (found) { 
                 vsite.edges.erase(vsite.edges.begin()+idx);
             } else {
-                if (reversed) break;
+                if (reversed) {
+                    // TODO we shouldn't get dangling edges like this!
+                    // Here we try to recover from it and snap to the next
+                    // closest edge point.
+                    md ++;
+                    continue;
+                }
                 last = first;
                 reversed = true;
             }
+            md = 1;
         }
         if (site->verts.front() == site->verts.back()) {
             site->verts.pop_back();