summaryrefslogtreecommitdiffstats
path: root/src/ui/tools/booleans-subitems.cpp
blob: 29309f83a81e39d217241dadbeb182796b9595f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * SubItem controls each fractured piece and links it to its original items.
 *
 *//*
 * Authors:
 *   Martin Owens
 *   PBS
 *
 * Copyright (C) 2022 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <numeric>
#include <utility>
#include <random>

#include <boost/range/adaptor/reversed.hpp>

#include "booleans-subitems.h"
#include "helper/geom-pathstroke.h"
#include "livarot/LivarotDefs.h"
#include "livarot/Shape.h"
#include "object/sp-shape.h"
#include "object/sp-text.h"
#include "object/sp-use.h"
#include "object/sp-image.h"
#include "path/path-boolop.h"
#include "style.h"

namespace Inkscape {

// Todo: (Wishlist) Remove this function when no longer necessary to remove boolops artifacts.
static Geom::PathVector clean_pathvector(Geom::PathVector &&pathv)
{
    Geom::PathVector result;

    for (auto &path : pathv) {
        if (path.closed() && !is_path_empty(path)) {
            result.push_back(std::move(path));
        }
    }

    return result;
}

/**
 * Union operator, merges two subitems when requested by the user
 * The left hand side will retain priority for the resulting style
 * so you should be mindful of how you merge these shapes.
 */
SubItem &SubItem::operator+=(SubItem const &other)
{
    _paths = sp_pathvector_boolop(_paths, other._paths, bool_op_union, fill_nonZero, fill_nonZero, true);
    sp_flatten(_paths, fill_nonZero);
    _paths = clean_pathvector(std::move(_paths));
    return *this;
}

using ExtractPathvectorsResult = std::vector<std::pair<Geom::PathVector, SPStyle*>>;

static void extract_pathvectors_recursive(SPItem *item, ExtractPathvectorsResult &result, Geom::Affine const &transform)
{
    if (is<SPGroup>(item)) {
        for (auto &child : item->children | boost::adaptors::reversed) {
            if (auto child_item = cast<SPItem>(&child)) {
                extract_pathvectors_recursive(child_item, result, child_item->transform * transform);
            }
        }
    } else if (auto img = cast<SPImage>(item)) {
        result.emplace_back(img->get_curve()->get_pathvector() * transform, item->style);
    } else if (auto shape = cast<SPShape>(item)) {
        if (auto curve = shape->curve()) {
            result.emplace_back(curve->get_pathvector() * transform, item->style);
        }
    } else if (auto text = cast<SPText>(item)) {
        result.emplace_back(text->getNormalizedBpath().get_pathvector() * transform, item->style);
    } else if (auto use = cast<SPUse>(item)) {
        if (use->child) {
            extract_pathvectors_recursive(use->child, result, use->child->transform * Geom::Translate(use->x.computed, use->y.computed) * transform);
        }
    }
}

// Return all pathvectors found within an item, along with their styles, sorted top-to-bottom.
static ExtractPathvectorsResult extract_pathvectors(SPItem *item)
{
    ExtractPathvectorsResult result;
    extract_pathvectors_recursive(item, result, item->i2dt_affine());
    return result;
}

static FillRule sp_to_livarot(SPWindRule fillrule)
{
    return fillrule == SP_WIND_RULE_NONZERO ? fill_nonZero : fill_oddEven;
}

static double diameter(Geom::PathVector const &path)
{
    auto rect = path.boundsExact();
    if (!rect) {
        return 1;
    }
    return std::hypot(rect->width(), rect->height());
}

// Cut the given pathvector along the lines into several smaller pathvectors.
static std::vector<Geom::PathVector> improved_cut(Geom::PathVector const &pathv, Geom::PathVector const &lines)
{
    Path patha;
    patha.LoadPathVector(pathv);
    patha.ConvertWithBackData(diameter(pathv) * 1e-3);

    Path pathb;
    pathb.LoadPathVector(lines);
    pathb.ConvertWithBackData(diameter(lines) * 1e-3);

    Shape shapea;
    {
        Shape tmp;
        patha.Fill(&tmp, 0);
        shapea.ConvertToShape(&tmp);
    }

    Shape shapeb;
    {
        Shape tmp;
        bool isline = pathb.pts.size() == 2 && pathb.pts[0].isMoveTo && !pathb.pts[1].isMoveTo;
        pathb.Fill(&tmp, 1, false, isline);
        shapeb.ConvertToShape(&tmp, fill_justDont);
    }

    Shape shape;
    shape.Booleen(&shapeb, &shapea, bool_op_cut, 1);

    Path path;
    int num_nesting = 0;
    int *nesting = nullptr;
    int *conts = nullptr;
    {
        path.SetBackData(false);
        Path *paths[2] = { &patha, &pathb };
        shape.ConvertToFormeNested(&path, 2, paths, 1, num_nesting, nesting, conts);
    }

    int num_paths;
    auto paths = path.SubPathsWithNesting(num_paths, false, num_nesting, nesting, conts);

    std::vector<Geom::PathVector> result;

    for (int i = 0; i < num_paths; i++) {
        result.emplace_back(paths[i]->MakePathVector());
    }

    g_free(paths);
    g_free(conts);
    g_free(nesting);

    return result;
}

/**
 * Take a list of items and fracture into a list of SubItems ready for
 * use inside the booleans interactive tool.
 */
WorkItems SubItem::build_mosaic(std::vector<SPItem*> &&items)
{
    // Sort so that topmost items come first.
    std::sort(items.begin(), items.end(), [] (auto a, auto b) {
        return sp_object_compare_position_bool(b, a);
    });

    // Extract all individual pathvectors within the collection of items,
    // keeping track of their associated item and style, again sorted topmost-first.
    using AugmentedItem = std::tuple<Geom::PathVector, SPItem*, SPStyle*>;
    std::vector<AugmentedItem> augmented;

    for (auto item : items) {
        // Get the correctly-transformed pathvectors, together with their corresponding styles.
        auto extracted = extract_pathvectors(item);

        // Append to the list of augmented items.
        for (auto &[pathv, style] : extracted) {
            augmented.emplace_back(std::move(pathv), item, style);
        }
    }

    // Compute a slightly expanded bounding box, collect together all lines, and cut the former by the latter.
    Geom::OptRect bounds;
    Geom::PathVector lines;

    for (auto &[pathv, item, style] : augmented) {
        bounds |= pathv.boundsExact();
        for (auto &path : pathv) {
            lines.push_back(path);
        }
    }

    if (!bounds) {
        return {};
    }

    constexpr double expansion = 10.0;
    bounds->expandBy(expansion);

    auto bounds_pathv = Geom::PathVector(Geom::Path(*bounds));
    auto pieces = improved_cut(bounds_pathv, lines);

    // Construct the SubItems, attempting to guess the corresponding augmented item for each piece.
    WorkItems result;

    auto gen = std::default_random_engine(std::random_device()());
    auto ranf = [&] { return std::uniform_real_distribution()(gen); };
    auto randpt = [&] { return Geom::Point(ranf(), ranf()); };

    for (auto &piece : pieces) {
        // Skip the big enclosing piece that is touching the outer boundary.
        if (auto rect = piece.boundsExact()) {
            if (   Geom::are_near(rect->top(), bounds->top(), expansion / 2)
                || Geom::are_near(rect->bottom(), bounds->bottom(), expansion / 2)
                || Geom::are_near(rect->left(), bounds->left(), expansion / 2)
                || Geom::are_near(rect->right(), bounds->right(), expansion / 2))
            {
                continue;
            }
        }

        // Remove junk paths that are open and/or tiny.
        for (auto it = piece.begin(); it != piece.end(); ) {
            if (!it->closed() || is_path_empty(*it)) {
                it = piece.erase(it);
            } else {
                ++it;
            }
        }

        // Skip empty pathvectors.
        if (piece.empty()) {
            continue;
        }

        // Determine the corresponding augmented item.
        // Fixme: (Wishlist) This is done unreliably and hackily, but livarot/2geom seemingly offer no alternative.
        std::unordered_map<AugmentedItem*, int> hits;

        auto rect = piece.boundsExact();

        auto add_hit = [&] (Geom::Point const &pt) {
            // Find an augmented item containing the point.
            for (auto &aug : augmented) {
                auto &[pathv, item, style] = aug;
                auto fill_rule = style->fill_rule.computed;
                auto winding = pathv.winding(pt);
                if (fill_rule == SP_WIND_RULE_NONZERO ? winding : winding % 2) {
                    hits[&aug]++;
                    return;
                }
            }

            // If none exists, register a background hit.
            hits[nullptr]++;
        };

        for (int total_hits = 0, patience = 1000; total_hits < 20 && patience > 0; patience--) {
            // Attempt to generate a point strictly inside the piece.
            auto pt = rect->min() + randpt() * rect->dimensions();
            if (piece.winding(pt)) {
                add_hit(pt);
                total_hits++;
            }
        }

        // Pick the augmented item with the most hits.
        AugmentedItem *found = nullptr;
        int max_hits = 0;

        for (auto &[a, h] : hits) {
            if (h > max_hits) {
                max_hits = h;
                found = a;
            }
        }

        // Add the SubItem.
        auto item = found ? std::get<1>(*found) : nullptr;
        auto style = found ? std::get<2>(*found) : nullptr;
        result.emplace_back(std::make_shared<SubItem>(std::move(piece), item, style));
    }

    return result;
}

/**
 * Take a list of items and flatten into a list of SubItems.
 */
WorkItems SubItem::build_flatten(std::vector<SPItem*> &&items)
{
    // Sort so that topmost items come first.
    std::sort(items.begin(), items.end(), [] (auto a, auto b) {
        return sp_object_compare_position_bool(b, a);
    });

    WorkItems result;
    Geom::PathVector unioned;

    for (auto item : items) {
        // Get the correctly-transformed pathvectors, together with their corresponding styles.
        auto extracted = extract_pathvectors(item);

        for (auto &[pathv, style] : extracted) {
            // Remove lines.
            for (auto it = pathv.begin(); it != pathv.end(); ) {
                if (!it->closed()) {
                    it = pathv.erase(it);
                } else {
                    ++it;
                }
            }

            // Skip pathvectors that are just lines.
            if (pathv.empty()) {
                continue;
            }

            // Flatten the remaining pathvector according to its fill rule.
            auto fillrule = style->fill_rule.computed;
            sp_flatten(pathv, sp_to_livarot(fillrule));

            // Remove the union so far from the shape, then add the shape to the union so far.
            Geom::PathVector uniq;

            if (unioned.empty()) {
                uniq = pathv;
                unioned = std::move(pathv);
            } else {
                uniq = sp_pathvector_boolop(unioned, pathv, bool_op_diff, fill_nonZero, fill_nonZero, true);
                unioned = sp_pathvector_boolop(unioned, pathv, bool_op_union, fill_nonZero, fill_nonZero, true);
            }

            // Add the new SubItem.
            result.emplace_back(std::make_shared<SubItem>(std::move(uniq), item, style));
        }
    }

    return result;
}

/**
 * Return true if this subitem contains the give point.
 */
bool SubItem::contains(Geom::Point const &pt) const
{
    return _paths.winding(pt) % 2 != 0;
}

} // namespace Inkscape