1 #ifndef __GEOM_CROSSING_H
2 #define __GEOM_CROSSING_H
4 #include <vector>
5 #include <set>
6 #include "rect.h"
7 #include "sweep.h"
8 namespace Geom {
10 struct Crossing {
11 bool dir; //True: along a, a becomes outside.
12 double ta, tb; //time on a and b of crossing
13 unsigned a, b; //storage of indices
14 Crossing() : dir(false), ta(0), tb(1), a(0), b(1) {}
15 Crossing(double t_a, double t_b, bool direction) : dir(direction), ta(t_a), tb(t_b), a(0), b(1) {}
16 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) {}
17 bool operator==(const Crossing & other) const { return a == other.a && b == other.b && dir == other.dir && ta == other.ta && tb == other.tb; }
18 bool operator!=(const Crossing & other) const { return !(*this == other); }
19 unsigned getOther(unsigned cur) const { return a == cur ? b : a; }
20 double getTime(unsigned cur) const { return a == cur ? ta : tb; }
21 double getOtherTime(unsigned cur) const { return a == cur ? tb : ta; }
22 bool onIx(unsigned ix) const { return a == ix || b == ix; }
23 };
26 /*inline bool near(Crossing a, Crossing b) {
27 return near(a.ta, b.ta) && near(a.tb, b.tb);
28 }
30 struct NearF { bool operator()(Crossing a, Crossing b) { return near(a, b); } };
31 */
33 struct CrossingOrder {
34 unsigned ix;
35 CrossingOrder(unsigned i) : ix(i) {}
36 bool operator()(Crossing a, Crossing b) {
37 return (ix == a.a ? a.ta : a.tb) <
38 (ix == b.a ? b.ta : b.tb);
39 }
40 };
42 typedef std::vector<Crossing> Crossings;
43 typedef std::vector<Crossings> CrossingSet;
45 template<typename C>
46 std::vector<Rect> bounds(C const &a) {
47 std::vector<Rect> rs;
48 for(unsigned i = 0; i < a.size(); i++) rs.push_back(a[i].boundsFast());
49 return rs;
50 }
52 inline void sort_crossings(Crossings &cr, unsigned ix) { std::sort(cr.begin(), cr.end(), CrossingOrder(ix)); }
54 template<typename T>
55 struct Crosser {
56 virtual ~Crosser() {}
57 virtual Crossings crossings(T const &a, T const &b) { return crossings(std::vector<T>(1,a), std::vector<T>(1,b))[0]; }
58 virtual CrossingSet crossings(std::vector<T> const &a, std::vector<T> const &b) {
59 CrossingSet results(a.size() + b.size(), Crossings());
61 std::vector<std::vector<unsigned> > cull = sweep_bounds(bounds(a), bounds(b));
62 for(unsigned i = 0; i < cull.size(); i++) {
63 for(unsigned jx = 0; jx < cull[i].size(); jx++) {
64 unsigned j = cull[i][jx];
65 unsigned jc = j + a.size();
66 Crossings cr = crossings(a[i], b[j]);
67 for(unsigned k = 0; k < cr.size(); k++) { cr[k].a = i; cr[k].b = jc; }
69 //Sort & add A-sorted crossings
70 sort_crossings(cr, i);
71 Crossings n(results[i].size() + cr.size());
72 std::merge(results[i].begin(), results[i].end(), cr.begin(), cr.end(), n.begin(), CrossingOrder(i));
73 results[i] = n;
75 //Sort & add B-sorted crossings
76 sort_crossings(cr, jc);
77 n.resize(results[jc].size() + cr.size());
78 std::merge(results[jc].begin(), results[jc].end(), cr.begin(), cr.end(), n.begin(), CrossingOrder(jc));
79 results[jc] = n;
80 }
81 }
82 return results;
83 }
84 };
85 void merge_crossings(Crossings &a, Crossings &b, unsigned i);
86 void offset_crossings(Crossings &cr, double a, double b);
88 Crossings reverse_ta(Crossings const &cr, std::vector<double> max);
89 Crossings reverse_tb(Crossings const &cr, unsigned split, std::vector<double> max);
90 CrossingSet reverse_ta(CrossingSet const &cr, unsigned split, std::vector<double> max);
91 CrossingSet reverse_tb(CrossingSet const &cr, unsigned split, std::vector<double> max);
93 void clean(Crossings &cr_a, Crossings &cr_b);
95 }
97 #endif
98 /*
99 Local Variables:
100 mode:c++
101 c-file-style:"stroustrup"
102 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
103 indent-tabs-mode:nil
104 fill-column:99
105 End:
106 */
107 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :