summaryrefslogtreecommitdiffstats
path: root/src/2geom/curve.cpp
blob: f79edb3afc6ec0a1b8213c0a999c07b3a7a5f598 (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
/* Abstract curve type - implementation of default methods
 *
 * Authors:
 *   MenTaLguY <mental@rydia.net>
 *   Marco Cecchetti <mrcekets at gmail.com>
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 *   Rafał Siejakowski <rs@rs-math.net>
 * 
 * Copyright 2007-2009 Authors
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */

#include <2geom/curve.h>
#include <2geom/exception.h>
#include <2geom/nearest-time.h>
#include <2geom/sbasis-geometric.h>
#include <2geom/sbasis-to-bezier.h>
#include <2geom/ord.h>
#include <2geom/path-sink.h>

namespace Geom 
{

Coord Curve::nearestTime(Point const& p, Coord a, Coord b) const
{
    return nearest_time(p, toSBasis(), a, b);
}

std::vector<Coord> Curve::allNearestTimes(Point const& p, Coord from, Coord to) const
{
    return all_nearest_times(p, toSBasis(), from, to);
}

Coord Curve::length(Coord tolerance) const
{
    return ::Geom::length(toSBasis(), tolerance);
}

int Curve::winding(Point const &p) const
{
    try {
        std::vector<Coord> ts = roots(p[Y], Y);
        if(ts.empty()) return 0;
        std::sort(ts.begin(), ts.end());

        // skip endpoint roots when they are local maxima on the Y axis
        // this follows the convention used in other winding routines,
        // i.e. that the bottommost coordinate is not part of the shape
        bool ignore_0 = unitTangentAt(0)[Y] <= 0;
        bool ignore_1 = unitTangentAt(1)[Y] >= 0;

        int wind = 0;
        for (double t : ts) {
            //std::cout << t << std::endl;
            if ((t == 0 && ignore_0) || (t == 1 && ignore_1)) continue;
            if (valueAt(t, X) > p[X]) { // root is ray intersection
                Point tangent = unitTangentAt(t);
                if (tangent[Y] > 0) {
                    // at the point of intersection, curve goes in +Y direction,
                    // so it winds in the direction of positive angles
                    ++wind;
                } else if (tangent[Y] < 0) {
                    --wind;
                }
            }
        }
        return wind;
    } catch (InfiniteSolutions const &e) {
        // this means we encountered a line segment exactly coincident with the point
        // skip, since this will be taken care of by endpoint roots in other segments
        return 0;
    }
}

std::vector<CurveIntersection> Curve::intersect(Curve const &/*other*/, Coord /*eps*/) const
{
    // TODO: approximate as Bezier
    THROW_NOTIMPLEMENTED();
}

std::vector<CurveIntersection> Curve::intersectSelf(Coord eps) const
{
    /// Represents a sub-arc of the curve.
    struct Subcurve
    {
        std::unique_ptr<Curve> curve;
        Interval parameter_range;

        Subcurve(Curve *piece, Coord from, Coord to)
            : curve{piece}
            , parameter_range{from, to}
        {}
    };

    /// A closure to split the curve into portions at the prescribed split points.
    auto const split_into_subcurves = [=](std::vector<Coord> const &splits) {
        std::vector<Subcurve> result;
        result.reserve(splits.size() + 1);
        Coord previous = 0;
        for (Coord split : splits) {
            // Use global EPSILON since we're operating on normalized curve times.
            if (split < EPSILON || split > 1.0 - EPSILON) {
                continue;
            }
            result.emplace_back(portion(previous, split), previous, split);
            previous = split;
        }
        result.emplace_back(portion(previous, 1.0), previous, 1.0);
        return result;
    };

    /// A closure to find pairwise intersections between the passed subcurves.
    auto const pairwise_intersect = [=](std::vector<Subcurve> const &subcurves) {
        std::vector<CurveIntersection> result;
        for (unsigned i = 0; i < subcurves.size(); i++) {
            for (unsigned j = i + 1; j < subcurves.size(); j++) {
                auto const xings = subcurves[i].curve->intersect(*subcurves[j].curve, eps);
                for (auto const &xing : xings) {
                    // To avoid duplicate intersections, skip values at exactly 1.
                    if (xing.first == 1. || xing.second == 1.) {
                        continue;
                    }
                    Coord const ti = subcurves[i].parameter_range.valueAt(xing.first);
                    Coord const tj = subcurves[j].parameter_range.valueAt(xing.second);
                    result.emplace_back(ti, tj, xing.point());
                }
            }
        }
        std::sort(result.begin(), result.end());
        return result;
    };

    // Monotonic segments cannot have self-intersections. Thus, we can split
    // the curve at critical points of the X or Y coordinate and intersect
    // the portions. However, there's the risk that a juncture between two
    // adjacent portions is mistaken for an intersection due to numerical errors.
    // Hence, we run the algorithm for both the X and Y coordinates and only
    // keep the intersections that show up in both intersection lists.

    // Find the critical points of both coordinates.
    std::unique_ptr<Curve> deriv{derivative()};
    auto const crits_x = deriv->roots(0, X);
    auto const crits_y = deriv->roots(0, Y);
    if (crits_x.empty() || crits_y.empty()) {
        return {};
    }

    // Split into pieces in two ways and find self-intersections.
    auto const pieces_x = split_into_subcurves(crits_x);
    auto const pieces_y = split_into_subcurves(crits_y);
    auto const crossings_from_x = pairwise_intersect(pieces_x);
    auto const crossings_from_y = pairwise_intersect(pieces_y);
    if (crossings_from_x.empty() || crossings_from_y.empty()) {
        return {};
    }

    // Filter the results, only keeping self-intersections found by both approaches.
    std::vector<CurveIntersection> result;
    unsigned index_y = 0;
    for (auto &&candidate_x : crossings_from_x) {
        // Find a crossing corresponding to this one in the y-method collection.
        while (index_y != crossings_from_y.size()) {
            auto const gap = crossings_from_y[index_y].first - candidate_x.first;
            if (std::abs(gap) < EPSILON) {
                // We found the matching intersection!
                result.emplace_back(candidate_x);
                index_y++;
                break;
            } else if (gap < 0.0) {
                index_y++;
            } else {
                break;
            }
        }
    }
    return result;
}

Point Curve::unitTangentAt(Coord t, unsigned n) const
{
    std::vector<Point> derivs = pointAndDerivatives(t, n);
    for (unsigned deriv_n = 1; deriv_n < derivs.size(); deriv_n++) {
        Coord length = derivs[deriv_n].length();
        if ( ! are_near(length, 0) ) {
            // length of derivative is non-zero, so return unit vector
            return derivs[deriv_n] / length;
        }
    }
    return Point (0,0);
};

void Curve::feed(PathSink &sink, bool moveto_initial) const
{
    std::vector<Point> pts;
    sbasis_to_bezier(pts, toSBasis(), 2); //TODO: use something better!
    if (moveto_initial) {
        sink.moveTo(initialPoint());
    }
    sink.curveTo(pts[0], pts[1], pts[2]);
}

} // namespace Geom

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :