Scribus
Open source desktop publishing at your fingertips
crossing.h
1 #ifndef __GEOM_CROSSING_H
2 #define __GEOM_CROSSING_H
3 
4 #include <vector>
5 #include "rect.h"
6 #include "sweep.h"
7 
8 namespace Geom {
9 
10 //Crossing between one or two paths
11 struct Crossing {
12  bool dir; //True: along a, a becomes outside.
13  double ta, tb; //time on a and b of crossing
14  unsigned a, b; //storage of indices
15  Crossing() : dir(false), ta(0), tb(1), a(0), b(1) {}
16  Crossing(double t_a, double t_b, bool direction) : dir(direction), ta(t_a), tb(t_b), a(0), b(1) {}
17  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) {}
18  bool operator==(const Crossing & other) const { return a == other.a && b == other.b && dir == other.dir && ta == other.ta && tb == other.tb; }
19  bool operator!=(const Crossing & other) const { return !(*this == other); }
20 
21  unsigned getOther(unsigned cur) const { return a == cur ? b : a; }
22  double getTime(unsigned cur) const { return a == cur ? ta : tb; }
23  double getOtherTime(unsigned cur) const { return a == cur ? tb : ta; }
24  bool onIx(unsigned ix) const { return a == ix || b == ix; }
25 };
26 
27 
28 struct Edge {
29  unsigned node, path;
30  double time;
31  bool reverse;
32  Edge(unsigned p, double t, bool r) : path(p), time(t), reverse(r) {}
33  bool operator==(Edge const &other) const { return other.path == path && other.time == time && other.reverse == reverse; }
34 };
35 
36 struct CrossingNode {
37  std::vector<Edge> edges;
38  CrossingNode() : edges(std::vector<Edge>()) {}
39  explicit CrossingNode(std::vector<Edge> es) : edges(es) {}
40  void add_edge(Edge const &e) {
41  if(std::find(edges.begin(), edges.end(), e) == edges.end())
42  edges.push_back(e);
43  }
44  double time_on(unsigned p) {
45  for(unsigned i = 0; i < edges.size(); i++)
46  if(edges[i].path == p) return edges[i].time;
47  std::cout << "CrossingNode time_on failed\n";
48  return 0;
49  }
50 };
51 
52 typedef std::vector<Crossing> Crossings;
53 
54 typedef std::vector<CrossingNode> CrossingGraph;
55 
56 struct TimeOrder {
57  bool operator()(Edge a, Edge b) {
58  return a.time < b.time;
59  }
60 };
61 
62 class Path;
63 CrossingGraph create_crossing_graph(std::vector<Path> const &p, Crossings const &crs);
64 
65 /*inline bool are_near(Crossing a, Crossing b) {
66  return are_near(a.ta, b.ta) && are_near(a.tb, b.tb);
67 }
68 
69 struct NearF { bool operator()(Crossing a, Crossing b) { return are_near(a, b); } };
70 */
71 
72 struct CrossingOrder {
73  unsigned ix;
74  CrossingOrder(unsigned i) : ix(i) {}
75  bool operator()(Crossing a, Crossing b) {
76  return (ix == a.a ? a.ta : a.tb) <
77  (ix == b.a ? b.ta : b.tb);
78  }
79 };
80 
81 
82 typedef std::vector<Crossings> CrossingSet;
83 
84 template<typename C>
85 std::vector<Rect> bounds(C const &a) {
86  std::vector<Rect> rs;
87  for(unsigned i = 0; i < a.size(); i++) rs.push_back(a[i].boundsFast());
88  return rs;
89 }
90 
91 inline void sort_crossings(Crossings &cr, unsigned ix) { std::sort(cr.begin(), cr.end(), CrossingOrder(ix)); }
92 
93 template<typename T>
94 struct Crosser {
95  virtual ~Crosser() {}
96  virtual Crossings crossings(T const &a, T const &b) { return crossings(std::vector<T>(1,a), std::vector<T>(1,b))[0]; }
97  virtual CrossingSet crossings(std::vector<T> const &a, std::vector<T> const &b) {
98  CrossingSet results(a.size() + b.size(), Crossings());
99 
100  std::vector<std::vector<unsigned> > cull = sweep_bounds(bounds(a), bounds(b));
101  for(unsigned i = 0; i < cull.size(); i++) {
102  for(unsigned jx = 0; jx < cull[i].size(); jx++) {
103  unsigned j = cull[i][jx];
104  unsigned jc = j + a.size();
105  Crossings cr = crossings(a[i], b[j]);
106  for(unsigned k = 0; k < cr.size(); k++) { cr[k].a = i; cr[k].b = jc; }
107 
108  //Sort & add A-sorted crossings
109  sort_crossings(cr, i);
110  Crossings n(results[i].size() + cr.size());
111  std::merge(results[i].begin(), results[i].end(), cr.begin(), cr.end(), n.begin(), CrossingOrder(i));
112  results[i] = n;
113 
114  //Sort & add B-sorted crossings
115  sort_crossings(cr, jc);
116  n.resize(results[jc].size() + cr.size());
117  std::merge(results[jc].begin(), results[jc].end(), cr.begin(), cr.end(), n.begin(), CrossingOrder(jc));
118  results[jc] = n;
119  }
120  }
121  return results;
122  }
123 };
124 void merge_crossings(Crossings &a, Crossings &b, unsigned i);
125 void offset_crossings(Crossings &cr, double a, double b);
126 
127 Crossings reverse_ta(Crossings const &cr, std::vector<double> max);
128 Crossings reverse_tb(Crossings const &cr, unsigned split, std::vector<double> max);
129 CrossingSet reverse_ta(CrossingSet const &cr, unsigned split, std::vector<double> max);
130 CrossingSet reverse_tb(CrossingSet const &cr, unsigned split, std::vector<double> max);
131 
132 void clean(Crossings &cr_a, Crossings &cr_b);
133 
134 }
135 
136 #endif
137 /*
138  Local Variables:
139  mode:c++
140  c-file-style:"stroustrup"
141  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
142  indent-tabs-mode:nil
143  fill-column:99
144  End:
145 */
146 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
Definition: angle.h:38
Definition: path.h:419
Definition: crossing.h:94
Definition: crossing.h:36
Definition: crossing.h:56
Definition: crossing.h:28
Definition: crossing.h:72
Definition: crossing.h:11