| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file | ||
| 3 | * @brief Structure representing the intersection of two curves | ||
| 4 | *//* | ||
| 5 | * Authors: | ||
| 6 | * Michael Sloan <mgsloan@gmail.com> | ||
| 7 | * Marco Cecchetti <mrcekets at gmail.com> | ||
| 8 | * | ||
| 9 | * Copyright 2006-2008 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 | |||
| 36 | #ifndef LIB2GEOM_SEEN_CROSSING_H | ||
| 37 | #define LIB2GEOM_SEEN_CROSSING_H | ||
| 38 | |||
| 39 | #include <vector> | ||
| 40 | #include <2geom/rect.h> | ||
| 41 | #include <2geom/sweep-bounds.h> | ||
| 42 | #include <optional> | ||
| 43 | #include <2geom/pathvector.h> | ||
| 44 | |||
| 45 | namespace Geom { | ||
| 46 | |||
| 47 | //Crossing between one or two paths | ||
| 48 | struct Crossing { | ||
| 49 | bool dir; //True: along a, a becomes outside. | ||
| 50 | double ta, tb; //time on a and b of crossing | ||
| 51 | unsigned a, b; //storage of indices | ||
| 52 | ✗ | Crossing() : dir(false), ta(0), tb(1), a(0), b(1) {} | |
| 53 | ✗ | Crossing(double t_a, double t_b, bool direction) : dir(direction), ta(t_a), tb(t_b), a(0), b(1) {} | |
| 54 | ✗ | Crossing(double t_a, double t_b, unsigned ai, unsigned bi, bool direction) : dir(direction), ta(t_a), tb(t_b), a(ai), b(bi) {} | |
| 55 | bool operator==(const Crossing & other) const { return a == other.a && b == other.b && dir == other.dir && ta == other.ta && tb == other.tb; } | ||
| 56 | bool operator!=(const Crossing & other) const { return !(*this == other); } | ||
| 57 | |||
| 58 | ✗ | unsigned getOther(unsigned cur) const { return a == cur ? b : a; } | |
| 59 | ✗ | double getTime(unsigned cur) const { return a == cur ? ta : tb; } | |
| 60 | ✗ | double getOtherTime(unsigned cur) const { return a == cur ? tb : ta; } | |
| 61 | ✗ | bool onIx(unsigned ix) const { return a == ix || b == ix; } | |
| 62 | }; | ||
| 63 | |||
| 64 | typedef std::optional<Crossing> OptCrossing; | ||
| 65 | |||
| 66 | |||
| 67 | /* | ||
| 68 | struct Edge { | ||
| 69 | unsigned node, path; | ||
| 70 | double time; | ||
| 71 | bool reverse; | ||
| 72 | Edge(unsigned p, double t, bool r) : path(p), time(t), reverse(r) {} | ||
| 73 | bool operator==(Edge const &other) const { return other.path == path && other.time == time && other.reverse == reverse; } | ||
| 74 | }; | ||
| 75 | |||
| 76 | struct CrossingNode { | ||
| 77 | std::vector<Edge> edges; | ||
| 78 | CrossingNode() : edges(std::vector<Edge>()) {} | ||
| 79 | explicit CrossingNode(std::vector<Edge> es) : edges(es) {} | ||
| 80 | void add_edge(Edge const &e) { | ||
| 81 | if(std::find(edges.begin(), edges.end(), e) == edges.end()) | ||
| 82 | edges.push_back(e); | ||
| 83 | } | ||
| 84 | double time_on(unsigned p) { | ||
| 85 | for(unsigned i = 0; i < edges.size(); i++) | ||
| 86 | if(edges[i].path == p) return edges[i].time; | ||
| 87 | std::cout << "CrossingNode time_on failed\n"; | ||
| 88 | return 0; | ||
| 89 | } | ||
| 90 | }; | ||
| 91 | |||
| 92 | |||
| 93 | typedef std::vector<CrossingNode> CrossingGraph; | ||
| 94 | |||
| 95 | struct TimeOrder { | ||
| 96 | bool operator()(Edge a, Edge b) { | ||
| 97 | return a.time < b.time; | ||
| 98 | } | ||
| 99 | }; | ||
| 100 | |||
| 101 | class Path; | ||
| 102 | CrossingGraph create_crossing_graph(PathVector const &p, Crossings const &crs); | ||
| 103 | */ | ||
| 104 | |||
| 105 | /*inline bool are_near(Crossing a, Crossing b) { | ||
| 106 | return are_near(a.ta, b.ta) && are_near(a.tb, b.tb); | ||
| 107 | } | ||
| 108 | |||
| 109 | struct NearF { bool operator()(Crossing a, Crossing b) { return are_near(a, b); } }; | ||
| 110 | */ | ||
| 111 | |||
| 112 | struct CrossingOrder { | ||
| 113 | unsigned ix; | ||
| 114 | bool rev; | ||
| 115 | ✗ | CrossingOrder(unsigned i, bool r = false) : ix(i), rev(r) {} | |
| 116 | ✗ | bool operator()(Crossing a, Crossing b) { | |
| 117 | ✗ | if(rev) | |
| 118 | ✗ | return (ix == a.a ? a.ta : a.tb) < | |
| 119 | ✗ | (ix == b.a ? b.ta : b.tb); | |
| 120 | else | ||
| 121 | ✗ | return (ix == a.a ? a.ta : a.tb) > | |
| 122 | ✗ | (ix == b.a ? b.ta : b.tb); | |
| 123 | } | ||
| 124 | }; | ||
| 125 | |||
| 126 | typedef std::vector<Crossing> Crossings; | ||
| 127 | |||
| 128 | typedef std::vector<Crossings> CrossingSet; | ||
| 129 | |||
| 130 | template<typename C> | ||
| 131 | ✗ | std::vector<Rect> bounds(C const &a) { | |
| 132 | ✗ | std::vector<Rect> rs; | |
| 133 | ✗ | for (unsigned i = 0; i < a.size(); i++) { | |
| 134 | ✗ | OptRect bb = a[i].boundsFast(); | |
| 135 | ✗ | if (bb) { | |
| 136 | ✗ | rs.push_back(*bb); | |
| 137 | } | ||
| 138 | } | ||
| 139 | ✗ | return rs; | |
| 140 | ✗ | } | |
| 141 | // provide specific method for Paths because paths can be closed or open. Path::size() is named somewhat wrong... | ||
| 142 | std::vector<Rect> bounds(Path const &a); | ||
| 143 | |||
| 144 | ✗ | inline void sort_crossings(Crossings &cr, unsigned ix) { std::sort(cr.begin(), cr.end(), CrossingOrder(ix)); } | |
| 145 | |||
| 146 | template <typename T> | ||
| 147 | struct CrossingTraits { | ||
| 148 | typedef std::vector<T> VectorT; | ||
| 149 | static inline VectorT init(T const &x) { return VectorT(1, x); } | ||
| 150 | }; | ||
| 151 | template <> | ||
| 152 | struct CrossingTraits<Path> { | ||
| 153 | typedef PathVector VectorT; | ||
| 154 | ✗ | static inline VectorT vector_one(Path const &x) { return VectorT(x); } | |
| 155 | }; | ||
| 156 | |||
| 157 | template<typename T> | ||
| 158 | struct Crosser { | ||
| 159 | typedef typename CrossingTraits<T>::VectorT VectorT; | ||
| 160 | ✗ | virtual ~Crosser() {} | |
| 161 | ✗ | virtual Crossings crossings(T const &a, T const &b) { | |
| 162 | ✗ | return crossings(CrossingTraits<T>::vector_one(a), CrossingTraits<T>::vector_one(b))[0]; } | |
| 163 | ✗ | virtual CrossingSet crossings(VectorT const &a, VectorT const &b) { | |
| 164 | ✗ | CrossingSet results(a.size() + b.size(), Crossings()); | |
| 165 | |||
| 166 | ✗ | std::vector<std::vector<unsigned> > cull = sweep_bounds(bounds(a), bounds(b)); | |
| 167 | ✗ | for(unsigned i = 0; i < cull.size(); i++) { | |
| 168 | ✗ | for(unsigned jx = 0; jx < cull[i].size(); jx++) { | |
| 169 | ✗ | unsigned j = cull[i][jx]; | |
| 170 | ✗ | unsigned jc = j + a.size(); | |
| 171 | ✗ | Crossings cr = crossings(a[i], b[j]); | |
| 172 | ✗ | for(auto & k : cr) { k.a = i; k.b = jc; } | |
| 173 | |||
| 174 | //Sort & add A-sorted crossings | ||
| 175 | ✗ | sort_crossings(cr, i); | |
| 176 | ✗ | Crossings n(results[i].size() + cr.size()); | |
| 177 | ✗ | std::merge(results[i].begin(), results[i].end(), cr.begin(), cr.end(), n.begin(), CrossingOrder(i)); | |
| 178 | ✗ | results[i] = n; | |
| 179 | |||
| 180 | //Sort & add B-sorted crossings | ||
| 181 | ✗ | sort_crossings(cr, jc); | |
| 182 | ✗ | n.resize(results[jc].size() + cr.size()); | |
| 183 | ✗ | std::merge(results[jc].begin(), results[jc].end(), cr.begin(), cr.end(), n.begin(), CrossingOrder(jc)); | |
| 184 | ✗ | results[jc] = n; | |
| 185 | } | ||
| 186 | } | ||
| 187 | ✗ | return results; | |
| 188 | ✗ | } | |
| 189 | }; | ||
| 190 | void merge_crossings(Crossings &a, Crossings &b, unsigned i); | ||
| 191 | void offset_crossings(Crossings &cr, double a, double b); | ||
| 192 | |||
| 193 | Crossings reverse_ta(Crossings const &cr, std::vector<double> max); | ||
| 194 | Crossings reverse_tb(Crossings const &cr, unsigned split, std::vector<double> max); | ||
| 195 | CrossingSet reverse_ta(CrossingSet const &cr, unsigned split, std::vector<double> max); | ||
| 196 | CrossingSet reverse_tb(CrossingSet const &cr, unsigned split, std::vector<double> max); | ||
| 197 | |||
| 198 | void clean(Crossings &cr_a, Crossings &cr_b); | ||
| 199 | void delete_duplicates(Crossings &crs); | ||
| 200 | |||
| 201 | } // end namespace Geom | ||
| 202 | |||
| 203 | #endif | ||
| 204 | /* | ||
| 205 | Local Variables: | ||
| 206 | mode:c++ | ||
| 207 | c-file-style:"stroustrup" | ||
| 208 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) | ||
| 209 | indent-tabs-mode:nil | ||
| 210 | fill-column:99 | ||
| 211 | End: | ||
| 212 | */ | ||
| 213 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : | ||
| 214 |