Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
unclump.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
6/* Authors:
7 * bulia byak
8 * Jon A. Cruz <jon@joncruz.org>
9 * Abhishek Sharma
10 *
11 * Copyright (C) 2005 Authors
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 */
14
15#include "unclump.h"
16
17#include <2geom/transforms.h>
18#include <algorithm>
19#include <map>
20
21#include "object/sp-item.h"
22
23class Unclump
24{
25public:
26 double dist(SPItem *item1, SPItem *item2);
27 double average(SPItem *item, std::list<SPItem *> &others);
28 SPItem *closest(SPItem *item, std::list<SPItem *> &others);
29 SPItem *farthest(SPItem *item, std::list<SPItem *> &others);
30 std::vector<SPItem *> unclump_remove_behind(SPItem *item, SPItem *closest, std::list<SPItem *> &rest);
31 void push(SPItem *from, SPItem *what, double dist);
32 void pull(SPItem *to, SPItem *what, double dist);
33
34private:
35 Geom::Point unclump_center(SPItem *item);
36 Geom::Point unclump_wh(SPItem *item);
37
38 // Taking bbox of an item is an expensive operation, and we need to do it many times, so here we
39 // cache the centers, widths, and heights of items
40
41 std::map<const gchar *, Geom::Point> c_cache;
42 std::map<const gchar *, Geom::Point> wh_cache;
43};
44
48Geom::Point Unclump::unclump_center(SPItem *item)
49{
50 std::map<const gchar *, Geom::Point>::iterator i = c_cache.find(item->getId());
51 if (i != c_cache.end()) {
52 return i->second;
53 }
54
56 if (r) {
57 Geom::Point const c = r->midpoint();
58 c_cache[item->getId()] = c;
59 return c;
60 } else {
61 // FIXME
62 return Geom::Point(0, 0);
63 }
64}
65
66Geom::Point Unclump::unclump_wh(SPItem *item)
67{
68 Geom::Point wh;
69 std::map<const gchar *, Geom::Point>::iterator i = wh_cache.find(item->getId());
70 if (i != wh_cache.end()) {
71 wh = i->second;
72 } else {
74 if (r) {
75 wh = r->dimensions();
76 wh_cache[item->getId()] = wh;
77 } else {
78 wh = Geom::Point(0, 0);
79 }
80 }
81
82 return wh;
83}
84
90double Unclump::dist(SPItem *item1, SPItem *item2)
91{
92 Geom::Point c1 = unclump_center(item1);
93 Geom::Point c2 = unclump_center(item2);
94
95 Geom::Point wh1 = unclump_wh(item1);
96 Geom::Point wh2 = unclump_wh(item2);
97
98 // angle from each item's center to the other's, unsqueezed by its w/h, normalized to 0..pi/2
99 double a1 = atan2((c2 - c1)[Geom::Y], (c2 - c1)[Geom::X] * wh1[Geom::Y] / wh1[Geom::X]);
100 a1 = fabs(a1);
101 if (a1 > M_PI / 2)
102 a1 = M_PI - a1;
103
104 double a2 = atan2((c1 - c2)[Geom::Y], (c1 - c2)[Geom::X] * wh2[Geom::Y] / wh2[Geom::X]);
105 a2 = fabs(a2);
106 if (a2 > M_PI / 2)
107 a2 = M_PI - a2;
108
109 // get the radius of each item for the given angle
110 double r1 = 0.5 * (wh1[Geom::X] + (wh1[Geom::Y] - wh1[Geom::X]) * (a1 / (M_PI / 2)));
111 double r2 = 0.5 * (wh2[Geom::X] + (wh2[Geom::Y] - wh2[Geom::X]) * (a2 / (M_PI / 2)));
112
113 // dist between centers minus angle-adjusted radii
114 double dist_r = (Geom::L2(c2 - c1) - r1 - r2);
115
116 double stretch1 = wh1[Geom::Y] / wh1[Geom::X];
117 double stretch2 = wh2[Geom::Y] / wh2[Geom::X];
118
119 if ((stretch1 > 1.5 || stretch1 < 0.66) && (stretch2 > 1.5 || stretch2 < 0.66)) {
120 std::vector<double> dists;
121 dists.push_back(dist_r);
122
123 // If both objects are not circle-like, find dists between four corners
124 std::vector<Geom::Point> c1_points(2);
125 {
126 double y_closest;
127 if (c2[Geom::Y] > c1[Geom::Y] + wh1[Geom::Y] / 2) {
128 y_closest = c1[Geom::Y] + wh1[Geom::Y] / 2;
129 } else if (c2[Geom::Y] < c1[Geom::Y] - wh1[Geom::Y] / 2) {
130 y_closest = c1[Geom::Y] - wh1[Geom::Y] / 2;
131 } else {
132 y_closest = c2[Geom::Y];
133 }
134 c1_points[0] = Geom::Point(c1[Geom::X], y_closest);
135 double x_closest;
136 if (c2[Geom::X] > c1[Geom::X] + wh1[Geom::X] / 2) {
137 x_closest = c1[Geom::X] + wh1[Geom::X] / 2;
138 } else if (c2[Geom::X] < c1[Geom::X] - wh1[Geom::X] / 2) {
139 x_closest = c1[Geom::X] - wh1[Geom::X] / 2;
140 } else {
141 x_closest = c2[Geom::X];
142 }
143 c1_points[1] = Geom::Point(x_closest, c1[Geom::Y]);
144 }
145
146 std::vector<Geom::Point> c2_points(2);
147 {
148 double y_closest;
149 if (c1[Geom::Y] > c2[Geom::Y] + wh2[Geom::Y] / 2) {
150 y_closest = c2[Geom::Y] + wh2[Geom::Y] / 2;
151 } else if (c1[Geom::Y] < c2[Geom::Y] - wh2[Geom::Y] / 2) {
152 y_closest = c2[Geom::Y] - wh2[Geom::Y] / 2;
153 } else {
154 y_closest = c1[Geom::Y];
155 }
156 c2_points[0] = Geom::Point(c2[Geom::X], y_closest);
157 double x_closest;
158 if (c1[Geom::X] > c2[Geom::X] + wh2[Geom::X] / 2) {
159 x_closest = c2[Geom::X] + wh2[Geom::X] / 2;
160 } else if (c1[Geom::X] < c2[Geom::X] - wh2[Geom::X] / 2) {
161 x_closest = c2[Geom::X] - wh2[Geom::X] / 2;
162 } else {
163 x_closest = c1[Geom::X];
164 }
165 c2_points[1] = Geom::Point(x_closest, c2[Geom::Y]);
166 }
167
168 for (int i = 0; i < 2; i++) {
169 for (int j = 0; j < 2; j++) {
170 dists.push_back(Geom::L2(c1_points[i] - c2_points[j]));
171 }
172 }
173
174 // return the minimum of all dists
175 return *std::min_element(dists.begin(), dists.end());
176 } else {
177 return dist_r;
178 }
179}
180
184double Unclump::average(SPItem *item, std::list<SPItem *> &others)
185{
186 int n = 0;
187 double sum = 0;
188 for (SPItem *other : others) {
189 if (other == item)
190 continue;
191
192 n++;
193 sum += dist(item, other);
194 }
195
196 if (n != 0)
197 return sum / n;
198 else
199 return 0;
200}
201
205SPItem *Unclump::closest(SPItem *item, std::list<SPItem *> &others)
206{
207 double min = HUGE_VAL;
208 SPItem *closest = nullptr;
209
210 for (SPItem *other : others) {
211 if (other == item)
212 continue;
213
214 double dist = this->dist(item, other);
215 if (dist < min && fabs(dist) < 1e6) {
216 min = dist;
217 closest = other;
218 }
219 }
220
221 return closest;
222}
223
227SPItem *Unclump::farthest(SPItem *item, std::list<SPItem *> &others)
228{
229 double max = -HUGE_VAL;
230 SPItem *farthest = nullptr;
231
232 for (SPItem *other : others) {
233 if (other == item)
234 continue;
235
236 double dist = this->dist(item, other);
237 if (dist > max && fabs(dist) < 1e6) {
238 max = dist;
239 farthest = other;
240 }
241 }
242
243 return farthest;
244}
245
251std::vector<SPItem *> Unclump::unclump_remove_behind(SPItem *item, SPItem *closest, std::list<SPItem *> &rest)
252{
253 Geom::Point it = unclump_center(item);
254 Geom::Point p1 = unclump_center(closest);
255
256 // perpendicular through closest to the direction to item:
257 Geom::Point perp = Geom::rot90(it - p1);
258 Geom::Point p2 = p1 + perp;
259
260 // get the standard Ax + By + C = 0 form for p1-p2:
261 double A = p1[Geom::Y] - p2[Geom::Y];
262 double B = p2[Geom::X] - p1[Geom::X];
263 double C = p2[Geom::Y] * p1[Geom::X] - p1[Geom::Y] * p2[Geom::X];
264
265 // substitute the item into it:
266 double val_item = A * it[Geom::X] + B * it[Geom::Y] + C;
267
268 std::vector<SPItem *> out;
269 for (SPItem *other : rest) {
270 if (other == item)
271 continue;
272
273 Geom::Point o = unclump_center(other);
274 double val_other = A * o[Geom::X] + B * o[Geom::Y] + C;
275
276 if (val_item * val_other <= 1e-6) {
277 // different signs, which means item and other are on the different sides of p1-p2 line; skip
278 } else {
279 out.push_back(other);
280 }
281 }
282
283 return out;
284}
285
289void Unclump::push(SPItem *from, SPItem *what, double dist)
290{
291 Geom::Point it = unclump_center(what);
292 Geom::Point p = unclump_center(from);
293 Geom::Point by = dist * Geom::unit_vector(-(p - it));
294
295 Geom::Affine move = Geom::Translate(by);
296
297 std::map<const gchar *, Geom::Point>::iterator i = c_cache.find(what->getId());
298 if (i != c_cache.end()) {
299 i->second *= move;
300 }
301
302 // g_print ("push %s at %g,%g from %g,%g by %g,%g, dist %g\n", what->getId(), it[Geom::X],it[Geom::Y],
303 // p[Geom::X],p[Geom::Y], by[Geom::X],by[Geom::Y], dist);
304
305 what->set_i2d_affine(what->i2dt_affine() * move);
306 what->doWriteTransform(what->transform);
307}
308
312void Unclump::pull(SPItem *to, SPItem *what, double dist)
313{
314 Geom::Point it = unclump_center(what);
315 Geom::Point p = unclump_center(to);
316 Geom::Point by = dist * Geom::unit_vector(p - it);
317
318 Geom::Affine move = Geom::Translate(by);
319
320 std::map<const gchar *, Geom::Point>::iterator i = c_cache.find(what->getId());
321 if (i != c_cache.end()) {
322 i->second *= move;
323 }
324
325 // g_print ("pull %s at %g,%g to %g,%g by %g,%g, dist %g\n", what->getId(), it[Geom::X],it[Geom::Y],
326 // p[Geom::X],p[Geom::Y], by[Geom::X],by[Geom::Y], dist);
327
328 what->set_i2d_affine(what->i2dt_affine() * move);
329 what->doWriteTransform(what->transform);
330}
331
337void unclump(std::vector<SPItem *> &items)
338{
339 Unclump unclump;
340
341 for (SPItem *item : items) { // for each original/clone x:
342 std::list<SPItem *> nei;
343
344 std::list<SPItem *> rest;
345 for (size_t i = 0; i < items.size(); i++) {
346 rest.push_front(items[items.size() - i - 1]);
347 }
348 rest.remove(item);
349
350 while (!rest.empty()) {
351 SPItem *closest = unclump.closest(item, rest);
352 if (closest) {
353 nei.push_front(closest);
354 rest.remove(closest);
355 std::vector<SPItem *> new_rest = unclump.unclump_remove_behind(item, closest, rest);
356 rest.clear();
357 for (size_t i = 0; i < new_rest.size(); i++) {
358 rest.push_front(new_rest[new_rest.size() - i - 1]);
359 }
360 } else {
361 break;
362 }
363 }
364
365 if ((nei.size()) >= 2) {
366 double ave = unclump.average(item, nei);
367
368 SPItem *closest = unclump.closest(item, nei);
369 SPItem *farthest = unclump.farthest(item, nei);
370
371 double dist_closest = unclump.dist(closest, item);
372 double dist_farthest = unclump.dist(farthest, item);
373
374 // g_print ("NEI %d for item %s closest %s at %g farthest %s at %g ave %g\n", g_slist_length(nei),
375 // item->getId(), closest->getId(), dist_closest, farthest->getId(), dist_farthest, ave);
376
377 if (fabs(ave) < 1e6 && fabs(dist_closest) < 1e6 && fabs(dist_farthest) < 1e6) { // otherwise the items are
378 // bogus
379 // increase these coefficients to make unclumping more aggressive and less stable
380 // the pull coefficient is a bit bigger to counteract the long-term expansion trend
381 unclump.push(closest, item, 0.3 * (ave - dist_closest));
382 unclump.pull(farthest, item, 0.35 * (dist_farthest - ave));
383 }
384 }
385 }
386}
387
388/*
389 Local Variables:
390 mode:c++
391 c-file-style:"stroustrup"
392 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
393 indent-tabs-mode:nil
394 fill-column:99
395 End:
396*/
397// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
3x3 matrix representing an affine transformation.
Definition affine.h:70
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
void set_i2d_affine(Geom::Affine const &transform)
Definition sp-item.cpp:1835
Geom::Affine i2dt_affine() const
Returns the transformation from item to desktop coords.
Definition sp-item.cpp:1829
Geom::Affine transform
Definition sp-item.h:138
Geom::OptRect desktopVisualBounds() const
Get item's visual bbox in desktop coordinate system.
Definition sp-item.cpp:1057
void doWriteTransform(Geom::Affine const &transform, Geom::Affine const *adv=nullptr, bool compensate=true)
Set a new transform on an object.
Definition sp-item.cpp:1666
char const * getId() const
Returns the objects current ID string.
double c[8][4]
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
SPItem * item
double dist(const Point &a, const Point &b)
Definition geometry.cpp:310
MultiDegree< n > max(MultiDegree< n > const &p, MultiDegree< n > const &q)
Returns the maximal degree appearing in the two arguments for each variables.
Definition sbasisN.h:158
double atan2(Point const &p)
SBasis L2(D2< SBasis > const &a, unsigned k)
Definition d2-sbasis.cpp:42
Piecewise< SBasis > min(SBasis const &f, SBasis const &g)
Return the more negative of the two functions pointwise.
Point unit_vector(Point const &a)
D2< T > rot90(D2< T > const &a)
Definition d2.h:397
int n
Definition spiro.cpp:57
GList * items
Some things pertinent to all visible shapes: SPItem, SPItemView, SPItemCtx.
double sum(const double alpha[16], const double &x, const double &y)
Affine transformation classes.
void unclump(std::vector< SPItem * > &items)
Unclumps the items in items, reducing local unevenness in their distribution.
Definition unclump.cpp:337
Unclumping objects.