summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/include/2geom/pathvector.h
blob: 578166337328d46699442abe1c903b54bb85f337 (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
/** @file
 * @brief PathVector - a sequence of subpaths
 *//*
 * Authors:
 *   Johan Engelen <j.b.c.engelen@alumnus.utwente.nl>
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 * 
 * Copyright 2008-2014 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.
 */

#ifndef LIB2GEOM_SEEN_PATHVECTOR_H
#define LIB2GEOM_SEEN_PATHVECTOR_H

#include <optional>
#include <boost/concept/requires.hpp>
#include <boost/range/algorithm/equal.hpp>
#include <2geom/forward.h>
#include <2geom/path.h>
#include <2geom/transforms.h>

namespace Geom {

/** @brief Generalized time value in the path vector.
 *
 * This class exists because mapping the range of multiple curves onto the same interval
 * as the curve index, we lose some precision. For instance, a path with 16 curves will
 * have 4 bits less precision than a path with 1 curve. If you need high precision results
 * in long paths, use this class and related methods instead of the standard methods
 * pointAt(), nearestTime() and so on.
 * 
 * @ingroup Paths */
struct PathVectorTime
    : public PathTime
    , boost::totally_ordered<PathVectorTime>
{
    size_type path_index; ///< Index of the path in the vector

    PathVectorTime() : PathTime(0, 0), path_index(0) {}
    PathVectorTime(size_type _i, size_type _c, Coord _t)
        : PathTime(_c, _t), path_index(_i) {}
    PathVectorTime(size_type _i, PathTime const &pos)
        : PathTime(pos), path_index(_i) {}

    bool operator<(PathVectorTime const &other) const {
        if (path_index < other.path_index) return true;
        if (path_index == other.path_index) {
            return static_cast<PathTime const &>(*this) < static_cast<PathTime const &>(other);
        }
        return false;
    }
    bool operator==(PathVectorTime const &other) const {
        return path_index == other.path_index
            && static_cast<PathTime const &>(*this) == static_cast<PathTime const &>(other);
    }

    PathTime const &asPathTime() const {
        return *static_cast<PathTime const *>(this);
    }
};

inline std::ostream &operator<<(std::ostream &os, PathVectorTime const &pvt) {
    os << pvt.path_index << ": " << pvt.asPathTime();
    return os;
}

typedef Intersection<PathVectorTime> PathVectorIntersection;
typedef PathVectorIntersection PVIntersection; ///< Alias to save typing

template <>
struct ShapeTraits<PathVector> {
    typedef PathVectorTime TimeType;
    //typedef PathVectorInterval IntervalType;
    typedef PathVector AffineClosureType;
    typedef PathVectorIntersection IntersectionType;
};

/** @brief Sequence of subpaths.
 *
 * This class corresponds to the SVG notion of a path:
 * a sequence of any number of open or closed contiguous subpaths.
 * Unlike Path, this class is closed under boolean operations.
 *
 * If you want to represent an arbitrary shape, this is the best class to use.
 * Shapes with a boundary that is composed of only a single contiguous
 * component can be represented with Path instead.
 *
 * @ingroup Paths
 */
class PathVector
    : MultipliableNoncommutative< PathVector, Affine
    , MultipliableNoncommutative< PathVector, Translate
    , MultipliableNoncommutative< PathVector, Scale
    , MultipliableNoncommutative< PathVector, Rotate
    , MultipliableNoncommutative< PathVector, HShear
    , MultipliableNoncommutative< PathVector, VShear
    , MultipliableNoncommutative< PathVector, Zoom
    , boost::equality_comparable< PathVector
      > > > > > > > >
{
    typedef std::vector<Path> Sequence;
public:
    typedef PathVectorTime Position;
    typedef Sequence::iterator iterator;
    typedef Sequence::const_iterator const_iterator;
    typedef Sequence::size_type size_type;
    typedef Path value_type;
    typedef Path &reference;
    typedef Path const &const_reference;
    typedef Path *pointer;
    typedef std::ptrdiff_t difference_type;

    PathVector() {}
    PathVector(Path const &p)
        : _data(1, p)
    {}
    template <typename InputIter>
    PathVector(InputIter first, InputIter last)
        : _data(first, last)
    {}

    /// Check whether the vector contains any paths.
    bool empty() const { return _data.empty(); }
    /// Get the number of paths in the vector.
    size_type size() const { return _data.size(); }
    /// Get the total number of curves in the vector.
    size_type curveCount() const;

    iterator begin() { return _data.begin(); }
    iterator end() { return _data.end(); }
    const_iterator begin() const { return _data.begin(); }
    const_iterator end() const { return _data.end(); }
    Path &operator[](size_type index) {
        return _data[index];
    }
    Path const &operator[](size_type index) const {
        return _data[index];
    }
    Path &at(size_type index) {
        return _data.at(index);
    }
    Path const &at(size_type index) const {
        return _data.at(index);
    }
    Path &front() { return _data.front(); }
    Path const &front() const { return _data.front(); }
    Path &back() { return _data.back(); }
    Path const &back() const { return _data.back(); }
    /// Append a path at the end.
    void push_back(Path const &path) {
        _data.push_back(path);
    }
    /// Remove the last path.
    void pop_back() {
        _data.pop_back();
    }
    iterator insert(iterator pos, Path const &p) {
        return _data.insert(pos, p);
    }
    template <typename InputIter>
    void insert(iterator out, InputIter first, InputIter last) {
        _data.insert(out, first, last);
    }
    /// Remove a path from the vector.
    iterator erase(iterator i) {
        return _data.erase(i);
    }
    /// Remove a range of paths from the vector.
    iterator erase(iterator first, iterator last) {
        return _data.erase(first, last);
    }
    /// Remove all paths from the vector.
    void clear() { _data.clear(); }
    /** @brief Change the number of paths.
     * If the vector size increases, it is passed with paths that contain only
     * a degenerate closing segment at (0,0). */
    void resize(size_type n) { _data.resize(n); }
    /** @brief Reverse the direction of paths in the vector.
     * @param reverse_paths If this is true, the order of paths is reversed as well;
     *                      otherwise each path is reversed, but their order in the
     *                      PathVector stays the same */
    void reverse(bool reverse_paths = true);
    /** @brief Get a new vector with reversed direction of paths.
     * @param reverse_paths If this is true, the order of paths is reversed as well;
     *                      otherwise each path is reversed, but their order in the
     *                      PathVector stays the same */
    PathVector reversed(bool reverse_paths = true) const;

    /// Get the range of allowed time values.
    Interval timeRange() const {
        Interval ret(0, curveCount()); return ret;
    }
    /** @brief Get the first point in the first path of the vector.
     * This method will throw an exception if the vector doesn't contain any paths. */
    Point initialPoint() const {
        return _data.front().initialPoint();
    }
    /** @brief Get the last point in the last path of the vector.
     * This method will throw an exception if the vector doesn't contain any paths. */
    Point finalPoint() const {
        return _data.back().finalPoint();
    }
    Path &pathAt(Coord t, Coord *rest = NULL);
    Path const &pathAt(Coord t, Coord *rest = NULL) const;
    Curve const &curveAt(Coord t, Coord *rest = NULL) const;
    Coord valueAt(Coord t, Dim2 d) const;
    Point pointAt(Coord t) const;

    Path &pathAt(PathVectorTime const &pos) {
        return const_cast<Path &>(static_cast<PathVector const*>(this)->pathAt(pos));
    }
    Path const &pathAt(PathVectorTime const &pos) const {
        return at(pos.path_index);
    }
    Curve const &curveAt(PathVectorTime const &pos) const {
        return at(pos.path_index).at(pos.curve_index);
    }
    Point pointAt(PathVectorTime const &pos) const {
        return at(pos.path_index).at(pos.curve_index).pointAt(pos.t);
    }
    Coord valueAt(PathVectorTime const &pos, Dim2 d) const {
        return at(pos.path_index).at(pos.curve_index).valueAt(pos.t, d);
    }

    OptRect boundsFast() const;
    OptRect boundsExact() const;

    template <typename T>
    BOOST_CONCEPT_REQUIRES(((TransformConcept<T>)), (PathVector &))
    operator*=(T const &t) {
        if (empty()) return *this;
        for (auto & i : *this) {
            i *= t;
        }
        return *this;
    }

    bool operator==(PathVector const &other) const {
        return boost::range::equal(_data, other._data);
    }

    void snapEnds(Coord precision = EPSILON);

    std::vector<PVIntersection> intersect(PathVector const &other, Coord precision = EPSILON) const;

    /** @brief Determine the winding number at the specified point.
     * This is simply the sum of winding numbers for constituent paths. */
    int winding(Point const &p) const;

    std::optional<PathVectorTime> nearestTime(Point const &p, Coord *dist = NULL) const;
    std::vector<PathVectorTime> allNearestTimes(Point const &p, Coord *dist = NULL) const;

    std::vector<Point> nodes() const;

private:
    PathVectorTime _factorTime(Coord t) const;

    Sequence _data;
};

inline OptRect bounds_fast(PathVector const &pv) { return pv.boundsFast(); }
inline OptRect bounds_exact(PathVector const &pv) { return pv.boundsExact(); }

std::ostream &operator<<(std::ostream &out, PathVector const &pv);

} // end namespace Geom

#endif // LIB2GEOM_SEEN_PATHVECTOR_H

/*
  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 :