Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
removeoverlap.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5/*
6 * Authors:
7 * Tim Dwyer <tgdwyer@gmail.com>
8 * Abhishek Sharma
9 *
10 * Copyright (C) 2005 Authors
11 *
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 */
14
15#include <utility>
16
17#include <2geom/transforms.h>
18
19#include "removeoverlap.h"
20
21#include "libvpsc/rectangle.h"
22
23#include "object/sp-item.h"
25
26
27using vpsc::Rectangle;
28
29namespace {
30
31struct Record {
32 SPItem * item;
34 Rectangle * vspc_rect;
35
36 Record() : item(nullptr), vspc_rect(nullptr) {}
37 Record(SPItem * i, Geom::Point m, Rectangle * r)
38 : item(i), midpoint(m), vspc_rect(r) {}
39};
40
41}
42
48void removeoverlap(std::vector<SPItem*> const & items, double const xGap, double const yGap) {
49 std::vector<SPItem*> selected = items;
50 std::vector<Record> records;
51 std::vector<Rectangle*> rs;
52
53 Geom::Point const gap(xGap, yGap);
54 for (SPItem * item: selected) {
55 using Geom::X; using Geom::Y;
57 if (item_box) {
58 Geom::Point min(item_box->min() - .5 * gap);
59 Geom::Point max(item_box->max() + .5 * gap);
60 // A negative gap is allowed, but will lead to problems when the gap is larger than
61 // the bounding box (in either X or Y direction, or both); min will have become max
62 // now, which cannot be handled by Rectangle() which is called below. And how will
63 // removeRectangleOverlap handle such a case?
64 // That's why we will enforce some boundaries on min and max here:
65 if (max[X] < min[X]) {
66 min[X] = max[X] = (min[X] + max[X]) / 2.;
67 }
68 if (max[Y] < min[Y]) {
69 min[Y] = max[Y] = (min[Y] + max[Y]) / 2.;
70 }
71 Rectangle * vspc_rect = new Rectangle(min[X], max[X], min[Y], max[Y]);
72 records.emplace_back(item, item_box->midpoint(), vspc_rect);
73 rs.push_back(vspc_rect);
74 }
75 }
76 if (!rs.empty()) {
78 }
79 for (Record & rec: records) {
80 Geom::Point const curr = rec.midpoint;
81 Geom::Point const dest(rec.vspc_rect->getCentreX(), rec.vspc_rect->getCentreY());
82 rec.item->move_rel(Geom::Translate(dest - curr));
83 delete rec.vspc_rect;
84 }
85}
Axis-aligned rectangle that can be empty.
Definition rect.h:203
Two-dimensional point that doubles as a vector.
Definition point.h:66
Translation by a vector.
Definition transforms.h:115
Base class for visual SVG elements.
Definition sp-item.h:109
Geom::OptRect desktopVisualBounds() const
Get item's visual bbox in desktop coordinate system.
Definition sp-item.cpp:1057
A rectangle represents a fixed-size shape in the diagram that may be moved to prevent overlaps and sa...
Definition rectangle.h:78
vector< vpsc::Rectangle * > rs
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
SPItem * item
void removeoverlaps(vpsc::Rectangles &rs, bool bothaxes)
Point midpoint(Point a, Point b)
void removeoverlap(std::vector< SPItem * > const &items, double const xGap, double const yGap)
Takes a list of inkscape items and moves them as little as possible such that rectangular bounding bo...
Remove overlaps function.
GList * items
TODO: insert short description here.
Some things pertinent to all visible shapes: SPItem, SPItemView, SPItemCtx.
Affine transformation classes.