Code

Avoid crash by uninitialized perspectives.
[inkscape.git] / src / removeoverlap / removeoverlap.cpp
index c640fb407ad308185562dcd1074715a6e55b60b7..975b4becb988e0ae7ec29412fa367e5630757bad 100644 (file)
 #include "sp-item-transform.h"
 #include "libvpsc/generate-constraints.h"
 #include "libvpsc/remove_rectangle_overlap.h"
+#include <utility>
 
 using vpsc::Rectangle;
 
+namespace {
+       struct Record {
+               SPItem *item;
+               Geom::Point midpoint;
+               Rectangle *vspc_rect;
+
+               Record() {}
+               Record(SPItem *i, Geom::Point m, Rectangle *r)
+               : item(i), midpoint(m), vspc_rect(r) {}
+       };
+}
+
 /**
 * Takes a list of inkscape items and moves them as little as possible
 * such that rectangular bounding boxes are separated by at least xGap
 * horizontally and yGap vertically
 */
 void removeoverlap(GSList const *const items, double const xGap, double const yGap) {
-       if(!items) {
-               return;
-       }
-
        using Inkscape::Util::GSListConstIterator;
        std::list<SPItem *> selected;
        selected.insert<GSListConstIterator<SPItem *> >(selected.end(), items, NULL);
-       if (selected.empty()) return;
-       int n=selected.size();
-
-       //Check 2 or more selected objects
-       if (n < 2) return;
+       std::vector<Record> records;
+       std::vector<Rectangle *> rs;
 
-       Rectangle **rs = new Rectangle*[n];
-       int i=0;
-
-       NR::Point const gap(xGap, yGap);
+       Geom::Point const gap(xGap, yGap);
        for (std::list<SPItem *>::iterator it(selected.begin());
                it != selected.end();
                ++it)
        {
-               using NR::X; using NR::Y;
-               NR::Rect const item_box(sp_item_bbox_desktop(*it));
-               
-               /* The current algorithm requires widths & heights to be strictly positive. */
-               NR::Point min(item_box.min());
-               NR::Point max(item_box.max());
-               for (unsigned d = 0; d < 2; ++d) {
-                       double const minsize = 1; // arbitrary positive number
-                       if (max[d] - min[d] + gap[d] < minsize) {
-                               double const mid = .5 * (min[d] + max[d]);
-                               min[d] = mid - .5*minsize;
-                               max[d] = mid + .5*minsize;
-                       } else {
-                               min[d] -= .5*gap[d];
-                               max[d] += .5*gap[d];
+               using Geom::X; using Geom::Y;
+               Geom::OptRect item_box(sp_item_bbox_desktop(*it));
+               if (item_box) {
+                       Geom::Point min(item_box->min() - .5*gap);
+                       Geom::Point max(item_box->max() + .5*gap);
+                       // A negative gap is allowed, but will lead to problems when the gap is larger than
+                       // the bounding box (in either X or Y direction, or both); min will have become max
+                       // now, which cannot be handled by Rectangle() which is called below. And how will
+                       // removeRectangleOverlap handle such a case?
+                       // That's why we will enforce some boundaries on min and max here:
+                       if (max[X] < min[X]) {
+                               min[X] = max[X] = (min[X] + max[X])/2;
+                       }
+                       if (max[Y] < min[Y]) {
+                               min[Y] = max[Y] = (min[Y] + max[Y])/2;
                        }
+                       Rectangle *vspc_rect = new Rectangle(min[X], max[X], min[Y], max[Y]);
+                       records.push_back(Record(*it, item_box->midpoint(), vspc_rect));
+                       rs.push_back(vspc_rect);
                }
-               rs[i++] = new Rectangle(min[X], max[X],
-                                       min[Y], max[Y]);
        }
-       removeRectangleOverlap(n, rs, 0.0, 0.0);
-       i=0;
-       for (std::list<SPItem *>::iterator it(selected.begin());
-               it != selected.end();
-               ++it)
+       if (!rs.empty()) {
+               removeRectangleOverlap(rs.size(), &rs[0], 0.0, 0.0);
+       }
+       for ( std::vector<Record>::iterator it = records.begin();
+             it != records.end();
+             ++it )
        {
-               NR::Rect const item_box(sp_item_bbox_desktop(*it));
-               Rectangle *r = rs[i++];
-               NR::Point const curr(item_box.midpoint());
-               NR::Point const dest(r->getCentreX(),
-                                    r->getCentreY());
-               sp_item_move_rel(*it, NR::translate(dest - curr));
-               delete r;
+               Geom::Point const curr = it->midpoint;
+               Geom::Point const dest(it->vspc_rect->getCentreX(),
+                                    it->vspc_rect->getCentreY());
+               sp_item_move_rel(it->item, Geom::Translate(dest - curr));
+               delete it->vspc_rect;
        }
-       delete [] rs;
 }