aboutsummaryrefslogtreecommitdiff
path: root/svg-flatten/include/geom2d.hpp
blob: 34455cd8223c0ba185509c7a8ce0373b50c2adcb (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
/*
 * 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/>.
 */

#pragma once

#include <array>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <algorithm>

#include <clipper.hpp>

#include "svg_import_defs.h"

using namespace std;

namespace gerbolyze {

    typedef std::array<double, 2> d2p;
    typedef std::vector<d2p> Polygon;

    typedef std::array<int64_t, 2> i2p;
    typedef std::vector<i2p> Polygon_i;

    class xform2d {
        public:
            xform2d(double xx, double xy, double yx, double yy, double x0=0.0, double y0=0.0) :
                xx(xx), xy(xy), x0(x0), yx(yx), yy(yy), y0(y0) {}
            
            xform2d() : xform2d(1.0, 0.0, 0.0, 1.0) {}

            xform2d(const string &svg_transform) : xform2d() {
                if (svg_transform.empty())
                    return;

                string start("matrix(");
                if (svg_transform.substr(0, start.length()) != start)
                    return;
                if (svg_transform.back() != ')')
                    return;

                const string &foo = svg_transform.substr(start.length(), svg_transform.length());
                const string &bar = foo.substr(0, foo.length() - 1);

                istringstream xform(bar);

                double a, c, e,
                       b, d, f;
                xform >> a >> b >> c >> d >> e >> f;
                if (xform.fail())
                    return;

                xx=a, yx=b, xy=c, yy=d, x0=e, y0=f;
                //cerr << "xform loaded " << dbg_str() << endl;
            }

            xform2d &translate(double x, double y) {
                xform2d xf(1, 0, 0, 1, x, y);
                transform(xf);
                return *this;
            }

            xform2d &scale(double x, double y) {
                xform2d xf(x, 0, 0, y);
                transform(xf);
                return *this;
            }

            xform2d &rotate(double theta) {
                double s = sin(theta);
                double c = cos(theta);
                xform2d xf(c, -s, s, c);
                transform(xf);
                return *this;
            }

            xform2d &skew(double m) {
                xform2d xf(1, m, 0, 1);
                transform(xf);
                return *this;
            }

            xform2d &transform(const xform2d &other) {
                double n_xx = other.xx * xx + other.yx * xy;
                double n_yx = other.xx * yx + other.yx * yy;

                double n_xy = other.xy * xx + other.yy * xy;
                double n_yy = other.xy * yx + other.yy * yy;

                double n_x0 = other.x0 * xx + other.y0 * xy + x0;
                double n_y0 = other.x0 * yx + other.y0 * yy + y0;

                xx = n_xx;
                yx = n_yx;
                xy = n_xy;
                yy = n_yy;
                x0 = n_x0;
                y0 = n_y0;
                decomposed = false;

                return *this;
            };

            double doc2phys_dist(double dist_doc) {
                return dist_doc * sqrt(xx*xx + xy*xy);
            }

            double phys2doc_dist(double dist_doc) {
                return dist_doc / sqrt(xx*xx + xy*xy);
            }

            std::tuple<double, double, double, double> decompose() {
                /* FIXME unit tests, especially for degenerate cases! */
                if (decomposed) {
                    return {s_x, s_y, m, theta};
                }

                /* https://math.stackexchange.com/a/3521141 */
                /* https://stackoverflow.com/a/70381885 */
                /* xx xy x0
                 * yx yy y0 */
                s_x = sqrt(xx*xx + yx*yx);

                if (xx == 0 && yx == 0) {
                    theta = 0;
                } else {
                    theta = atan2(yx, xx);
                }

                double f = (xx*yy - xy*yx);

                if (f == 0) {
                    m = 0;
                } else {
                    m = (xx*xy + yy*yx) / f;
                }

                f = xx + m*yx;
                if (fabs(f) < 1e-12) {
                    f = m*xx - yx;
                    if (fabs(f) < 1e-12) {
                        s_y = 0;
                    } else {
                        s_y = xy*s_x / f;
                    }
                } else {
                    s_y = yy*s_x / f;
                }

                double b = sqrt(s_y*s_y + m*m);
                f_min = fmin(s_x, b);
                f_max = fmax(s_x, b);

                decomposed = true;
                return {s_x, s_y, m, theta};
            }

            bool doc2phys_skew_ok(double dist_doc, double rel_tol, double abs_tol) {
                decompose();

                if (f_min == 0) {
                    return false;
                }

                double imbalance = f_max / f_min - 1.0;
                //cerr << "  * skew check: " << dbg_str();
                //cerr << "    imbalance=" << imbalance << endl;
                //cerr << "    rel=" << (imbalance < rel_tol) << " abs=" << (imbalance*fabs(dist_doc) < abs_tol) << endl;
                return imbalance < rel_tol && imbalance*fabs(dist_doc) < abs_tol;
            }

            double doc2phys_min(double dist_doc) {
                decompose();
                return dist_doc * f_min;
            }

            double doc2phys_max(double dist_doc) {
                decompose();
                return dist_doc * f_max;
            }

            double phys2doc_min(double dist_doc) {
                decompose();

                if (f_min == 0)
                    return std::nan("9");

                return dist_doc / f_min;
            }

            double phys2doc_max(double dist_doc) {
                decompose();

                if (f_max == 0)
                    return std::nan("9");

                return dist_doc / f_max;
            }

            d2p doc2phys(const d2p p) {
                return d2p {
                    xx * p[0] + xy * p[1] + x0,
                    yx * p[0] + yy * p[1] + y0
                };
            }

            xform2d &invert(bool *success_out=nullptr) {
                /* From Cairo source */

                /* inv (A) = 1/det (A) * adj (A) */
                double det = xx*yy - yx*xy;

                if (det == 0 || !isfinite(det)) {
                    if (success_out)
                        *success_out = false;
                    *this = xform2d(); /* unity matrix */
                    return *this;
                }

                *this = xform2d(yy/det, -xy/det,
                        -yx/det, xx/det,
                        (xy*y0 - yy*x0)/det, (yx*x0 - xx*y0)/det);

                if (success_out)
                    *success_out = true;

                return *this;
            }

            /* Transform given clipper paths */
            void doc2phys_clipper(ClipperLib::Paths &paths) {
                for (auto &p : paths) {
                    doc2phys_clipper(p);
                }
            }

            void doc2phys_clipper(ClipperLib::Path &path) {
                std::transform(path.begin(), path.end(), path.begin(),
                        [this](ClipperLib::IntPoint p) -> ClipperLib::IntPoint {
                            d2p out(this->doc2phys(d2p{p.X / clipper_scale, p.Y / clipper_scale}));
                            return {
                                (ClipperLib::cInt)round(out[0] * clipper_scale),
                                (ClipperLib::cInt)round(out[1] * clipper_scale)
                            };
                        });
            }

            /* Transform given clipper paths */
            void phys2doc_clipper(ClipperLib::Paths &paths) {
                for (auto &p : paths) {
                    phys2doc_clipper(p);
                }
            }

            void phys2doc_clipper(ClipperLib::Path &path) {
                xform2d copy(*this);
                bool inverted = false;
                copy.invert(&inverted);
                if (!inverted) {
                    path.clear();
                    return;
                }

                std::transform(path.begin(), path.end(), path.begin(),
                        [&copy](ClipperLib::IntPoint p) -> ClipperLib::IntPoint {
                            d2p out(copy.doc2phys(d2p{p.X / clipper_scale, p.Y / clipper_scale}));
                            return {
                                (ClipperLib::cInt)round(out[0] * clipper_scale),
                                (ClipperLib::cInt)round(out[1] * clipper_scale)
                            };
                        });
            }

            void transform_polygon(Polygon &poly) {
                std::transform(poly.begin(), poly.end(), poly.begin(),
                        [this](d2p p) -> d2p {
                            return this->doc2phys(d2p{p[0], p[1]});
                        });
            }

            string dbg_str() {
                decompose();
                ostringstream os;
                os << "xform2d< " << setw(5);
                os << xx << ", " << xy << ", " << x0 << " / ";
                os << yx << ", " << yy << ", " << y0 << " / ";
                os << "θ=" << theta << ", m=" << m << " s=(" << s_x << ", " << s_y << ") | ";
                os << "f_min=" << f_min << ", f_max=" << f_max;
                os << " >";
                return os.str();
            }

        private:
            double xx, xy, x0,
                   yx, yy, y0;
            double theta, m, s_x, s_y;
            double f_min, f_max;
            bool decomposed = false;
    };
}