| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* Abstract curve type - implementation of default methods | ||
| 2 | * | ||
| 3 | * Authors: | ||
| 4 | * MenTaLguY <mental@rydia.net> | ||
| 5 | * Marco Cecchetti <mrcekets at gmail.com> | ||
| 6 | * Krzysztof Kosiński <tweenk.pl@gmail.com> | ||
| 7 | * Rafał Siejakowski <rs@rs-math.net> | ||
| 8 | * | ||
| 9 | * Copyright 2007-2009 Authors | ||
| 10 | * | ||
| 11 | * This library is free software; you can redistribute it and/or | ||
| 12 | * modify it either under the terms of the GNU Lesser General Public | ||
| 13 | * License version 2.1 as published by the Free Software Foundation | ||
| 14 | * (the "LGPL") or, at your option, under the terms of the Mozilla | ||
| 15 | * Public License Version 1.1 (the "MPL"). If you do not alter this | ||
| 16 | * notice, a recipient may use your version of this file under either | ||
| 17 | * the MPL or the LGPL. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the LGPL along with this library | ||
| 20 | * in the file COPYING-LGPL-2.1; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 22 | * You should have received a copy of the MPL along with this library | ||
| 23 | * in the file COPYING-MPL-1.1 | ||
| 24 | * | ||
| 25 | * The contents of this file are subject to the Mozilla Public License | ||
| 26 | * Version 1.1 (the "License"); you may not use this file except in | ||
| 27 | * compliance with the License. You may obtain a copy of the License at | ||
| 28 | * http://www.mozilla.org/MPL/ | ||
| 29 | * | ||
| 30 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY | ||
| 31 | * OF ANY KIND, either express or implied. See the LGPL or the MPL for | ||
| 32 | * the specific language governing rights and limitations. | ||
| 33 | */ | ||
| 34 | |||
| 35 | #include <2geom/curve.h> | ||
| 36 | #include <2geom/exception.h> | ||
| 37 | #include <2geom/nearest-time.h> | ||
| 38 | #include <2geom/sbasis-geometric.h> | ||
| 39 | #include <2geom/sbasis-to-bezier.h> | ||
| 40 | #include <2geom/ord.h> | ||
| 41 | #include <2geom/path-sink.h> | ||
| 42 | |||
| 43 | namespace Geom | ||
| 44 | { | ||
| 45 | |||
| 46 | ✗ | Coord Curve::nearestTime(Point const& p, Coord a, Coord b) const | |
| 47 | { | ||
| 48 | ✗ | return nearest_time(p, toSBasis(), a, b); | |
| 49 | } | ||
| 50 | |||
| 51 | ✗ | std::vector<Coord> Curve::allNearestTimes(Point const& p, Coord from, Coord to) const | |
| 52 | { | ||
| 53 | ✗ | return all_nearest_times(p, toSBasis(), from, to); | |
| 54 | } | ||
| 55 | |||
| 56 | ✗ | Coord Curve::length(Coord tolerance) const | |
| 57 | { | ||
| 58 | ✗ | return ::Geom::length(toSBasis(), tolerance); | |
| 59 | } | ||
| 60 | |||
| 61 | 4 | int Curve::winding(Point const &p) const | |
| 62 | { | ||
| 63 | try { | ||
| 64 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | std::vector<Coord> ts = roots(p[Y], Y); |
| 65 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if(ts.empty()) return 0; |
| 66 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | std::sort(ts.begin(), ts.end()); |
| 67 | |||
| 68 | // skip endpoint roots when they are local maxima on the Y axis | ||
| 69 | // this follows the convention used in other winding routines, | ||
| 70 | // i.e. that the bottommost coordinate is not part of the shape | ||
| 71 |
1/2✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | bool ignore_0 = unitTangentAt(0)[Y] <= 0; |
| 72 |
1/2✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | bool ignore_1 = unitTangentAt(1)[Y] >= 0; |
| 73 | |||
| 74 | 4 | int wind = 0; | |
| 75 |
2/2✓ Branch 7 taken 8 times.
✓ Branch 8 taken 4 times.
|
12 | for (double t : ts) { |
| 76 | //std::cout << t << std::endl; | ||
| 77 |
2/8✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
8 | if ((t == 0 && ignore_0) || (t == 1 && ignore_1)) continue; |
| 78 |
3/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 4 times.
|
8 | if (valueAt(t, X) > p[X]) { // root is ray intersection |
| 79 |
1/2✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | Point tangent = unitTangentAt(t); |
| 80 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (tangent[Y] > 0) { |
| 81 | // at the point of intersection, curve goes in +Y direction, | ||
| 82 | // so it winds in the direction of positive angles | ||
| 83 | ✗ | ++wind; | |
| 84 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | } else if (tangent[Y] < 0) { |
| 85 | ✗ | --wind; | |
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | 4 | return wind; | |
| 90 |
0/2✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4 | } catch (InfiniteSolutions const &e) { |
| 91 | // this means we encountered a line segment exactly coincident with the point | ||
| 92 | // skip, since this will be taken care of by endpoint roots in other segments | ||
| 93 | ✗ | return 0; | |
| 94 | ✗ | } | |
| 95 | } | ||
| 96 | |||
| 97 | ✗ | std::vector<CurveIntersection> Curve::intersect(Curve const &/*other*/, Coord /*eps*/) const | |
| 98 | { | ||
| 99 | // TODO: approximate as Bezier | ||
| 100 | ✗ | THROW_NOTIMPLEMENTED(); | |
| 101 | } | ||
| 102 | |||
| 103 | 68 | std::vector<CurveIntersection> Curve::intersectSelf(Coord eps) const | |
| 104 | { | ||
| 105 | /// Represents a sub-arc of the curve. | ||
| 106 | struct Subcurve | ||
| 107 | { | ||
| 108 | std::unique_ptr<Curve> curve; | ||
| 109 | Interval parameter_range; | ||
| 110 | |||
| 111 | 65 | Subcurve(Curve *piece, Coord from, Coord to) | |
| 112 | 65 | : curve{piece} | |
| 113 |
1/2✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
|
65 | , parameter_range{from, to} |
| 114 | 65 | {} | |
| 115 | }; | ||
| 116 | |||
| 117 | /// A closure to split the curve into portions at the prescribed split points. | ||
| 118 | 68 | auto const split_into_subcurves = [this] (std::vector<Coord> const &splits) { | |
| 119 | 32 | std::vector<Subcurve> result; | |
| 120 |
1/2✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
|
32 | result.reserve(splits.size() + 1); |
| 121 | 32 | Coord previous = 0; | |
| 122 |
2/2✓ Branch 8 taken 48 times.
✓ Branch 9 taken 32 times.
|
80 | for (Coord split : splits) { |
| 123 | // Use global EPSILON since we're operating on normalized curve times. | ||
| 124 |
4/4✓ Branch 0 taken 39 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 33 times.
|
48 | if (split < EPSILON || split > 1.0 - EPSILON) { |
| 125 | 15 | continue; | |
| 126 | } | ||
| 127 |
2/4✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 33 times.
✗ Branch 6 not taken.
|
33 | result.emplace_back(portion(previous, split), previous, split); |
| 128 | 33 | previous = split; | |
| 129 | } | ||
| 130 |
2/4✓ Branch 3 taken 32 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 32 times.
✗ Branch 7 not taken.
|
32 | result.emplace_back(portion(previous, 1.0), previous, 1.0); |
| 131 | 64 | return result; | |
| 132 | ✗ | }; | |
| 133 | |||
| 134 | /// A closure to find pairwise intersections between the passed subcurves. | ||
| 135 | 68 | auto const pairwise_intersect = [=](std::vector<Subcurve> const &subcurves) { | |
| 136 | 32 | std::vector<CurveIntersection> result; | |
| 137 |
2/2✓ Branch 1 taken 65 times.
✓ Branch 2 taken 32 times.
|
97 | for (unsigned i = 0; i < subcurves.size(); i++) { |
| 138 |
2/2✓ Branch 1 taken 42 times.
✓ Branch 2 taken 65 times.
|
107 | for (unsigned j = i + 1; j < subcurves.size(); j++) { |
| 139 |
1/2✓ Branch 6 taken 42 times.
✗ Branch 7 not taken.
|
42 | auto const xings = subcurves[i].curve->intersect(*subcurves[j].curve, eps); |
| 140 |
2/2✓ Branch 7 taken 40 times.
✓ Branch 8 taken 42 times.
|
82 | for (auto const &xing : xings) { |
| 141 | // To avoid duplicate intersections, skip values at exactly 1. | ||
| 142 |
3/4✓ Branch 0 taken 17 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
|
40 | if (xing.first == 1. || xing.second == 1.) { |
| 143 | 23 | continue; | |
| 144 | } | ||
| 145 |
1/2✓ Branch 3 taken 17 times.
✗ Branch 4 not taken.
|
17 | Coord const ti = subcurves[i].parameter_range.valueAt(xing.first); |
| 146 |
1/2✓ Branch 3 taken 17 times.
✗ Branch 4 not taken.
|
17 | Coord const tj = subcurves[j].parameter_range.valueAt(xing.second); |
| 147 |
1/2✓ Branch 3 taken 17 times.
✗ Branch 4 not taken.
|
17 | result.emplace_back(ti, tj, xing.point()); |
| 148 | } | ||
| 149 | 42 | } | |
| 150 | } | ||
| 151 |
1/2✓ Branch 3 taken 32 times.
✗ Branch 4 not taken.
|
32 | std::sort(result.begin(), result.end()); |
| 152 | 32 | return result; | |
| 153 | ✗ | }; | |
| 154 | |||
| 155 | // Monotonic segments cannot have self-intersections. Thus, we can split | ||
| 156 | // the curve at critical points of the X or Y coordinate and intersect | ||
| 157 | // the portions. However, there's the risk that a juncture between two | ||
| 158 | // adjacent portions is mistaken for an intersection due to numerical errors. | ||
| 159 | // Hence, we run the algorithm for both the X and Y coordinates and only | ||
| 160 | // keep the intersections that show up in both intersection lists. | ||
| 161 | |||
| 162 | // Find the critical points of both coordinates. | ||
| 163 |
1/2✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
|
68 | std::unique_ptr<Curve> deriv{derivative()}; |
| 164 |
1/2✓ Branch 3 taken 68 times.
✗ Branch 4 not taken.
|
68 | auto const crits_x = deriv->roots(0, X); |
| 165 |
1/2✓ Branch 3 taken 68 times.
✗ Branch 4 not taken.
|
68 | auto const crits_y = deriv->roots(0, Y); |
| 166 |
6/6✓ Branch 1 taken 17 times.
✓ Branch 2 taken 51 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 52 times.
✓ Branch 7 taken 16 times.
|
68 | if (crits_x.empty() || crits_y.empty()) { |
| 167 | 52 | return {}; | |
| 168 | } | ||
| 169 | |||
| 170 | // Split into pieces in two ways and find self-intersections. | ||
| 171 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | auto const pieces_x = split_into_subcurves(crits_x); |
| 172 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | auto const pieces_y = split_into_subcurves(crits_y); |
| 173 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | auto const crossings_from_x = pairwise_intersect(pieces_x); |
| 174 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | auto const crossings_from_y = pairwise_intersect(pieces_y); |
| 175 |
6/6✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 4 times.
|
16 | if (crossings_from_x.empty() || crossings_from_y.empty()) { |
| 176 | 12 | return {}; | |
| 177 | } | ||
| 178 | |||
| 179 | // Filter the results, only keeping self-intersections found by both approaches. | ||
| 180 | 4 | std::vector<CurveIntersection> result; | |
| 181 | 4 | unsigned index_y = 0; | |
| 182 |
2/2✓ Branch 7 taken 4 times.
✓ Branch 8 taken 4 times.
|
8 | for (auto &&candidate_x : crossings_from_x) { |
| 183 | // Find a crossing corresponding to this one in the y-method collection. | ||
| 184 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | while (index_y != crossings_from_y.size()) { |
| 185 | 4 | auto const gap = crossings_from_y[index_y].first - candidate_x.first; | |
| 186 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
|
4 | if (std::abs(gap) < EPSILON) { |
| 187 | // We found the matching intersection! | ||
| 188 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | result.emplace_back(candidate_x); |
| 189 | 2 | index_y++; | |
| 190 | 2 | break; | |
| 191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | } else if (gap < 0.0) { |
| 192 | ✗ | index_y++; | |
| 193 | } else { | ||
| 194 | 2 | break; | |
| 195 | } | ||
| 196 | } | ||
| 197 | } | ||
| 198 | 4 | return result; | |
| 199 | 68 | } | |
| 200 | |||
| 201 | 173 | Point Curve::unitTangentAt(Coord t, unsigned n) const | |
| 202 | { | ||
| 203 |
1/2✓ Branch 2 taken 173 times.
✗ Branch 3 not taken.
|
173 | std::vector<Point> derivs = pointAndDerivatives(t, n); |
| 204 |
2/2✓ Branch 1 taken 176 times.
✓ Branch 2 taken 1 times.
|
177 | for (unsigned deriv_n = 1; deriv_n < derivs.size(); deriv_n++) { |
| 205 | 176 | Coord length = derivs[deriv_n].length(); | |
| 206 |
2/2✓ Branch 1 taken 172 times.
✓ Branch 2 taken 4 times.
|
176 | if ( ! are_near(length, 0) ) { |
| 207 | // length of derivative is non-zero, so return unit vector | ||
| 208 | 172 | return derivs[deriv_n] / length; | |
| 209 | } | ||
| 210 | } | ||
| 211 | 1 | return Point (0,0); | |
| 212 | 173 | }; | |
| 213 | |||
| 214 | ✗ | void Curve::feed(PathSink &sink, bool moveto_initial) const | |
| 215 | { | ||
| 216 | ✗ | std::vector<Point> pts; | |
| 217 | ✗ | sbasis_to_bezier(pts, toSBasis(), 2); //TODO: use something better! | |
| 218 | ✗ | if (moveto_initial) { | |
| 219 | ✗ | sink.moveTo(initialPoint()); | |
| 220 | } | ||
| 221 | ✗ | sink.curveTo(pts[0], pts[1], pts[2]); | |
| 222 | ✗ | } | |
| 223 | |||
| 224 | } // namespace Geom | ||
| 225 | |||
| 226 | /* | ||
| 227 | Local Variables: | ||
| 228 | mode:c++ | ||
| 229 | c-file-style:"stroustrup" | ||
| 230 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) | ||
| 231 | indent-tabs-mode:nil | ||
| 232 | fill-column:99 | ||
| 233 | End: | ||
| 234 | */ | ||
| 235 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : | ||
| 236 |