291f693826358c9728ec0148851a081da21fc65d
1 /**
2 * \file
3 * \brief \todo brief description
4 *
5 * Authors:
6 * ? <?@?.?>
7 *
8 * Copyright ?-? authors
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it either under the terms of the GNU Lesser General Public
12 * License version 2.1 as published by the Free Software Foundation
13 * (the "LGPL") or, at your option, under the terms of the Mozilla
14 * Public License Version 1.1 (the "MPL"). If you do not alter this
15 * notice, a recipient may use your version of this file under either
16 * the MPL or the LGPL.
17 *
18 * You should have received a copy of the LGPL along with this library
19 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * You should have received a copy of the MPL along with this library
22 * in the file COPYING-MPL-1.1
23 *
24 * The contents of this file are subject to the Mozilla Public License
25 * Version 1.1 (the "License"); you may not use this file except in
26 * compliance with the License. You may obtain a copy of the License at
27 * http://www.mozilla.org/MPL/
28 *
29 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31 * the specific language governing rights and limitations.
32 *
33 */
35 #ifndef __GEOM_CROSSING_H
36 #define __GEOM_CROSSING_H
38 #include <vector>
39 #include <2geom/rect.h>
40 #include <2geom/sweep.h>
42 namespace Geom {
44 //Crossing between one or two paths
45 struct Crossing {
46 bool dir; //True: along a, a becomes outside.
47 double ta, tb; //time on a and b of crossing
48 unsigned a, b; //storage of indices
49 Crossing() : dir(false), ta(0), tb(1), a(0), b(1) {}
50 Crossing(double t_a, double t_b, bool direction) : dir(direction), ta(t_a), tb(t_b), a(0), b(1) {}
51 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) {}
52 bool operator==(const Crossing & other) const { return a == other.a && b == other.b && dir == other.dir && ta == other.ta && tb == other.tb; }
53 bool operator!=(const Crossing & other) const { return !(*this == other); }
55 unsigned getOther(unsigned cur) const { return a == cur ? b : a; }
56 double getTime(unsigned cur) const { return a == cur ? ta : tb; }
57 double getOtherTime(unsigned cur) const { return a == cur ? tb : ta; }
58 bool onIx(unsigned ix) const { return a == ix || b == ix; }
59 };
62 /*
63 struct Edge {
64 unsigned node, path;
65 double time;
66 bool reverse;
67 Edge(unsigned p, double t, bool r) : path(p), time(t), reverse(r) {}
68 bool operator==(Edge const &other) const { return other.path == path && other.time == time && other.reverse == reverse; }
69 };
71 struct CrossingNode {
72 std::vector<Edge> edges;
73 CrossingNode() : edges(std::vector<Edge>()) {}
74 explicit CrossingNode(std::vector<Edge> es) : edges(es) {}
75 void add_edge(Edge const &e) {
76 if(std::find(edges.begin(), edges.end(), e) == edges.end())
77 edges.push_back(e);
78 }
79 double time_on(unsigned p) {
80 for(unsigned i = 0; i < edges.size(); i++)
81 if(edges[i].path == p) return edges[i].time;
82 std::cout << "CrossingNode time_on failed\n";
83 return 0;
84 }
85 };
88 typedef std::vector<CrossingNode> CrossingGraph;
90 struct TimeOrder {
91 bool operator()(Edge a, Edge b) {
92 return a.time < b.time;
93 }
94 };
96 class Path;
97 CrossingGraph create_crossing_graph(std::vector<Path> const &p, Crossings const &crs);
98 */
100 /*inline bool are_near(Crossing a, Crossing b) {
101 return are_near(a.ta, b.ta) && are_near(a.tb, b.tb);
102 }
104 struct NearF { bool operator()(Crossing a, Crossing b) { return are_near(a, b); } };
105 */
107 struct CrossingOrder {
108 unsigned ix;
109 CrossingOrder(unsigned i) : ix(i) {}
110 bool operator()(Crossing a, Crossing b) {
111 return (ix == a.a ? a.ta : a.tb) <
112 (ix == b.a ? b.ta : b.tb);
113 }
114 };
116 typedef std::vector<Crossing> Crossings;
118 typedef std::vector<Crossings> CrossingSet;
120 template<typename C>
121 std::vector<Rect> bounds(C const &a) {
122 std::vector<Rect> rs;
123 for(unsigned i = 0; i < a.size(); i++) rs.push_back(a[i].boundsFast());
124 return rs;
125 }
127 inline void sort_crossings(Crossings &cr, unsigned ix) { std::sort(cr.begin(), cr.end(), CrossingOrder(ix)); }
129 template<typename T>
130 struct Crosser {
131 virtual ~Crosser() {}
132 virtual Crossings crossings(T const &a, T const &b) { return crossings(std::vector<T>(1,a), std::vector<T>(1,b))[0]; }
133 virtual CrossingSet crossings(std::vector<T> const &a, std::vector<T> const &b) {
134 CrossingSet results(a.size() + b.size(), Crossings());
136 std::vector<std::vector<unsigned> > cull = sweep_bounds(bounds(a), bounds(b));
137 for(unsigned i = 0; i < cull.size(); i++) {
138 for(unsigned jx = 0; jx < cull[i].size(); jx++) {
139 unsigned j = cull[i][jx];
140 unsigned jc = j + a.size();
141 Crossings cr = crossings(a[i], b[j]);
142 for(unsigned k = 0; k < cr.size(); k++) { cr[k].a = i; cr[k].b = jc; }
144 //Sort & add A-sorted crossings
145 sort_crossings(cr, i);
146 Crossings n(results[i].size() + cr.size());
147 std::merge(results[i].begin(), results[i].end(), cr.begin(), cr.end(), n.begin(), CrossingOrder(i));
148 results[i] = n;
150 //Sort & add B-sorted crossings
151 sort_crossings(cr, jc);
152 n.resize(results[jc].size() + cr.size());
153 std::merge(results[jc].begin(), results[jc].end(), cr.begin(), cr.end(), n.begin(), CrossingOrder(jc));
154 results[jc] = n;
155 }
156 }
157 return results;
158 }
159 };
160 void merge_crossings(Crossings &a, Crossings &b, unsigned i);
161 void offset_crossings(Crossings &cr, double a, double b);
163 Crossings reverse_ta(Crossings const &cr, std::vector<double> max);
164 Crossings reverse_tb(Crossings const &cr, unsigned split, std::vector<double> max);
165 CrossingSet reverse_ta(CrossingSet const &cr, unsigned split, std::vector<double> max);
166 CrossingSet reverse_tb(CrossingSet const &cr, unsigned split, std::vector<double> max);
168 void clean(Crossings &cr_a, Crossings &cr_b);
170 }
172 #endif
173 /*
174 Local Variables:
175 mode:c++
176 c-file-style:"stroustrup"
177 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
178 indent-tabs-mode:nil
179 fill-column:99
180 End:
181 */
182 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :