aboutsummaryrefslogtreecommitdiff
path: root/svg-flatten/src/out_flattener.cpp
blob: 0eae854c1f6fc6af8425231b6fedf6c2e599a42b (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
/*
 * This file is part of gerbolyze, a vector image preprocessing toolchain 
 * Copyright (C) 2021 Jan Sebastian Götte <gerbolyze@jaseg.de>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <gerbolyze.hpp>
#include <svg_import_defs.h>
#include <svg_geom.h>
#include "polylinecombine.hpp"

using namespace gerbolyze;
using namespace std;

static void polygon_to_cavc (const Polygon &in, cavc::Polyline<double> &out) {
    for (auto &p : in) {
        out.addVertex(p[0], p[1], 0);
    }
    out.isClosed() = true; /* sic! */
}

static void cavc_to_polygon (const cavc::Polyline<double> &in, Polygon &out) {
    for (auto &p : in.vertexes()) {
        out.emplace_back(d2p{p.x(), p.y()});
    }
}

namespace gerbolyze {
    class Flattener_D {
    public:
        vector<cavc::Polyline<double>> dark_polys;
        vector<cavc::Polyline<double>> clear_polys;

        void add_dark_polygon(const Polygon &in) {
            polygon_to_cavc(in, dark_polys.emplace_back());
        }

        void add_clear_polygon(const Polygon &in) {
            polygon_to_cavc(in, clear_polys.emplace_back());
        }
    };
}

Flattener::Flattener(PolygonSink &sink) : m_sink(sink) {
    d = new Flattener_D();
}

Flattener::~Flattener() {
    delete d;
}

void Flattener::header(d2p origin, d2p size) {
    m_sink.header(origin, size);
}

void Flattener::render_out_clear_polys() {
    for (auto &sub : d->clear_polys) {
        vector<cavc::Polyline<double>> new_dark_polys;
        new_dark_polys.reserve(d->dark_polys.size());

        for (cavc::Polyline<double> cavc_in : d->dark_polys) {
            auto res = cavc::combinePolylines(cavc_in, sub, cavc::PlineCombineMode::Exclude);

            if (res.subtracted.size() == 0) {
                for (auto &rem : res.remaining) {
                    new_dark_polys.push_back(std::move(rem));
                }

            } else { /* custom one-hole deholing code */
                assert (res.remaining.size() == 1);
                assert (res.subtracted.size() == 1);

                auto &rem = res.remaining[0];
                auto &sub = res.subtracted[0];
                auto bbox = getExtents(rem);

                cavc::Polyline<double> quad;
                quad.addVertex(bbox.xMin, bbox.yMin, 0);
                if (sub.vertexes()[0].x() < sub.vertexes()[1].x()) {
                    quad.addVertex(sub.vertexes()[0]);
                    quad.addVertex(sub.vertexes()[1]);
                } else {
                    quad.addVertex(sub.vertexes()[1]);
                    quad.addVertex(sub.vertexes()[0]);
                }
                quad.addVertex(bbox.xMax, bbox.yMin, 0);
                quad.isClosed() = true; /* sic! */

                auto res2 = cavc::combinePolylines(rem, quad, cavc::PlineCombineMode::Exclude);
                assert (res2.subtracted.size() == 0);

                for (auto &rem : res2.remaining) {
                    auto res3 = cavc::combinePolylines(rem, sub, cavc::PlineCombineMode::Exclude);
                    assert (res3.subtracted.size() == 0);
                    for (auto &p : res3.remaining) {
                        new_dark_polys.push_back(std::move(p));
                    }
                }

                auto res4 = cavc::combinePolylines(rem, quad, cavc::PlineCombineMode::Intersect);
                assert (res4.subtracted.size() == 0);

                for (auto &rem : res4.remaining) {
                    auto res5 = cavc::combinePolylines(rem, sub, cavc::PlineCombineMode::Exclude);
                    assert (res5.subtracted.size() == 0);
                    for (auto &p : res5.remaining) {
                        new_dark_polys.push_back(std::move(p));
                    }
                }
            }
        }

        d->dark_polys = std::move(new_dark_polys);
    }
    d->clear_polys.clear();
}

Flattener &Flattener::operator<<(GerberPolarityToken pol) {
    if (m_current_polarity != pol) {
        m_current_polarity = pol;

        if (pol == GRB_POL_DARK) {
            render_out_clear_polys();
        }
    }

    return *this;
}

Flattener &Flattener::operator<<(const LayerNameToken &layer_name) {
    flush_polys_to_sink();
    m_sink << layer_name;
    cerr << "Flattener forwarding layer name to sink: \"" << layer_name.m_name << "\"" << endl;

    return *this;
}

Flattener &Flattener::operator<<(const DrillToken &tok) {
    m_sink << tok;
    return *this;
}

Flattener &Flattener::operator<<(const ApertureToken &tok) {
    m_sink << tok;
    return *this;
}

Flattener &Flattener::operator<<(const Polygon &poly) {
    if (m_current_polarity == GRB_POL_DARK) {
        d->add_dark_polygon(poly);

    } else { /* clear */
        d->add_clear_polygon(poly);
        render_out_clear_polys();
    }

    return *this;
}

void Flattener::flush_polys_to_sink() {
    *this << GRB_POL_DARK; /* force render */
    m_sink << GRB_POL_DARK;

    for (auto &poly : d->dark_polys) {
        Polygon poly_out;
        for (auto &p : poly.vertexes()) {
            poly_out.emplace_back(d2p{p.x(), p.y()});
        }
        m_sink << poly_out;
    }

    d->clear_polys.clear();
    d->dark_polys.clear();
}

void Flattener::footer() {
    flush_polys_to_sink();
    m_sink.footer();
}