Chris@16
|
1 /*
|
Chris@16
|
2 Copyright 2008 Intel Corporation
|
Chris@16
|
3
|
Chris@16
|
4 Use, modification and distribution are subject to the Boost Software License,
|
Chris@16
|
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 http://www.boost.org/LICENSE_1_0.txt).
|
Chris@16
|
7 */
|
Chris@16
|
8 #ifndef BOOST_POLYGON_PROPERTY_MERGE_45_HPP
|
Chris@16
|
9 #define BOOST_POLYGON_PROPERTY_MERGE_45_HPP
|
Chris@16
|
10 namespace boost { namespace polygon{
|
Chris@16
|
11
|
Chris@16
|
12 template <typename Unit, typename property_type>
|
Chris@16
|
13 struct polygon_45_property_merge {
|
Chris@16
|
14
|
Chris@16
|
15 typedef point_data<Unit> Point;
|
Chris@16
|
16 typedef typename coordinate_traits<Unit>::manhattan_area_type LongUnit;
|
Chris@16
|
17
|
Chris@16
|
18 template <typename property_map>
|
Chris@16
|
19 static inline void merge_property_maps(property_map& mp, const property_map& mp2, bool subtract = false) {
|
Chris@16
|
20 polygon_45_touch<Unit>::merge_property_maps(mp, mp2, subtract);
|
Chris@16
|
21 }
|
Chris@16
|
22
|
Chris@16
|
23 class CountMerge {
|
Chris@16
|
24 public:
|
Chris@16
|
25 inline CountMerge() : counts() {}
|
Chris@16
|
26 //inline CountMerge(int count) { counts[0] = counts[1] = count; }
|
Chris@16
|
27 //inline CountMerge(int count1, int count2) { counts[0] = count1; counts[1] = count2; }
|
Chris@16
|
28 inline CountMerge(const CountMerge& count) : counts(count.counts) {}
|
Chris@16
|
29 inline bool operator==(const CountMerge& count) const { return counts == count.counts; }
|
Chris@16
|
30 inline bool operator!=(const CountMerge& count) const { return !((*this) == count); }
|
Chris@16
|
31 //inline CountMerge& operator=(int count) { counts[0] = counts[1] = count; return *this; }
|
Chris@16
|
32 inline CountMerge& operator=(const CountMerge& count) { counts = count.counts; return *this; }
|
Chris@16
|
33 inline int& operator[](property_type index) {
|
Chris@16
|
34 std::vector<std::pair<int, int> >::iterator itr = lower_bound(counts.begin(), counts.end(), std::make_pair(index, int(0)));
|
Chris@16
|
35 if(itr != counts.end() && itr->first == index) {
|
Chris@16
|
36 return itr->second;
|
Chris@16
|
37 }
|
Chris@16
|
38 itr = counts.insert(itr, std::make_pair(index, int(0)));
|
Chris@16
|
39 return itr->second;
|
Chris@16
|
40 }
|
Chris@16
|
41 // inline int operator[](int index) const {
|
Chris@16
|
42 // std::vector<std::pair<int, int> >::const_iterator itr = counts.begin();
|
Chris@16
|
43 // for( ; itr != counts.end() && itr->first <= index; ++itr) {
|
Chris@16
|
44 // if(itr->first == index) {
|
Chris@16
|
45 // return itr->second;
|
Chris@16
|
46 // }
|
Chris@16
|
47 // }
|
Chris@16
|
48 // return 0;
|
Chris@16
|
49 // }
|
Chris@16
|
50 inline CountMerge& operator+=(const CountMerge& count){
|
Chris@16
|
51 merge_property_maps(counts, count.counts, false);
|
Chris@16
|
52 return *this;
|
Chris@16
|
53 }
|
Chris@16
|
54 inline CountMerge& operator-=(const CountMerge& count){
|
Chris@16
|
55 merge_property_maps(counts, count.counts, true);
|
Chris@16
|
56 return *this;
|
Chris@16
|
57 }
|
Chris@16
|
58 inline CountMerge operator+(const CountMerge& count) const {
|
Chris@16
|
59 return CountMerge(*this)+=count;
|
Chris@16
|
60 }
|
Chris@16
|
61 inline CountMerge operator-(const CountMerge& count) const {
|
Chris@16
|
62 return CountMerge(*this)-=count;
|
Chris@16
|
63 }
|
Chris@16
|
64 inline CountMerge invert() const {
|
Chris@16
|
65 CountMerge retval;
|
Chris@16
|
66 retval -= *this;
|
Chris@16
|
67 return retval;
|
Chris@16
|
68 }
|
Chris@16
|
69 std::vector<std::pair<property_type, int> > counts;
|
Chris@16
|
70 };
|
Chris@16
|
71
|
Chris@16
|
72 //output is a std::map<std::set<property_type>, polygon_45_set_data<Unit> >
|
Chris@16
|
73 struct merge_45_output_functor {
|
Chris@16
|
74 template <typename cT>
|
Chris@16
|
75 void operator()(cT& output, const CountMerge& count1, const CountMerge& count2,
|
Chris@16
|
76 const Point& pt, int rise, direction_1d end) {
|
Chris@16
|
77 typedef typename cT::key_type keytype;
|
Chris@16
|
78 keytype left;
|
Chris@16
|
79 keytype right;
|
Chris@16
|
80 int edgeType = end == LOW ? -1 : 1;
|
Chris@16
|
81 for(typename std::vector<std::pair<property_type, int> >::const_iterator itr = count1.counts.begin();
|
Chris@16
|
82 itr != count1.counts.end(); ++itr) {
|
Chris@16
|
83 left.insert(left.end(), (*itr).first);
|
Chris@16
|
84 }
|
Chris@16
|
85 for(typename std::vector<std::pair<property_type, int> >::const_iterator itr = count2.counts.begin();
|
Chris@16
|
86 itr != count2.counts.end(); ++itr) {
|
Chris@16
|
87 right.insert(right.end(), (*itr).first);
|
Chris@16
|
88 }
|
Chris@16
|
89 if(left == right) return;
|
Chris@16
|
90 if(!left.empty()) {
|
Chris@16
|
91 //std::cout << pt.x() << " " << pt.y() << " " << rise << " " << edgeType << std::endl;
|
Chris@16
|
92 output[left].insert_clean(typename boolean_op_45<Unit>::Vertex45(pt, rise, -edgeType));
|
Chris@16
|
93 }
|
Chris@16
|
94 if(!right.empty()) {
|
Chris@16
|
95 //std::cout << pt.x() << " " << pt.y() << " " << rise << " " << -edgeType << std::endl;
|
Chris@16
|
96 output[right].insert_clean(typename boolean_op_45<Unit>::Vertex45(pt, rise, edgeType));
|
Chris@16
|
97 }
|
Chris@16
|
98 }
|
Chris@16
|
99 };
|
Chris@16
|
100
|
Chris@16
|
101 typedef typename std::pair<Point,
|
Chris@16
|
102 typename boolean_op_45<Unit>::template Scan45CountT<CountMerge> > Vertex45Compact;
|
Chris@16
|
103 typedef std::vector<Vertex45Compact> MergeSetData;
|
Chris@16
|
104
|
Chris@16
|
105 struct lessVertex45Compact {
|
Chris@16
|
106 bool operator()(const Vertex45Compact& l, const Vertex45Compact& r) {
|
Chris@16
|
107 return l.first < r.first;
|
Chris@16
|
108 }
|
Chris@16
|
109 };
|
Chris@16
|
110
|
Chris@16
|
111 template <typename output_type>
|
Chris@16
|
112 static void performMerge(output_type& result, MergeSetData& tsd) {
|
Chris@16
|
113
|
Chris@16
|
114 polygon_sort(tsd.begin(), tsd.end(), lessVertex45Compact());
|
Chris@16
|
115 typedef std::vector<std::pair<Point, typename boolean_op_45<Unit>::template Scan45CountT<CountMerge> > > TSD;
|
Chris@16
|
116 TSD tsd_;
|
Chris@16
|
117 tsd_.reserve(tsd.size());
|
Chris@16
|
118 for(typename MergeSetData::iterator itr = tsd.begin(); itr != tsd.end(); ) {
|
Chris@16
|
119 typename MergeSetData::iterator itr2 = itr;
|
Chris@16
|
120 ++itr2;
|
Chris@16
|
121 for(; itr2 != tsd.end() && itr2->first == itr->first; ++itr2) {
|
Chris@16
|
122 (itr->second) += (itr2->second); //accumulate
|
Chris@16
|
123 }
|
Chris@16
|
124 tsd_.push_back(std::make_pair(itr->first, itr->second));
|
Chris@16
|
125 itr = itr2;
|
Chris@16
|
126 }
|
Chris@16
|
127 typename boolean_op_45<Unit>::template Scan45<CountMerge, merge_45_output_functor> scanline;
|
Chris@16
|
128 for(typename TSD::iterator itr = tsd_.begin(); itr != tsd_.end(); ) {
|
Chris@16
|
129 typename TSD::iterator itr2 = itr;
|
Chris@16
|
130 ++itr2;
|
Chris@16
|
131 while(itr2 != tsd_.end() && itr2->first.x() == itr->first.x()) {
|
Chris@16
|
132 ++itr2;
|
Chris@16
|
133 }
|
Chris@16
|
134 scanline.scan(result, itr, itr2);
|
Chris@16
|
135 itr = itr2;
|
Chris@16
|
136 }
|
Chris@16
|
137 }
|
Chris@16
|
138
|
Chris@16
|
139 template <typename iT>
|
Chris@16
|
140 static void populateMergeSetData(MergeSetData& tsd, iT begin, iT end, property_type property) {
|
Chris@16
|
141 for( ; begin != end; ++begin) {
|
Chris@16
|
142 Vertex45Compact vertex;
|
Chris@16
|
143 vertex.first = typename Vertex45Compact::first_type(begin->pt.x() * 2, begin->pt.y() * 2);
|
Chris@16
|
144 tsd.push_back(vertex);
|
Chris@16
|
145 for(unsigned int i = 0; i < 4; ++i) {
|
Chris@16
|
146 if(begin->count[i]) {
|
Chris@16
|
147 tsd.back().second[i][property] += begin->count[i];
|
Chris@16
|
148 }
|
Chris@16
|
149 }
|
Chris@16
|
150 }
|
Chris@16
|
151 }
|
Chris@16
|
152
|
Chris@16
|
153 };
|
Chris@16
|
154
|
Chris@16
|
155
|
Chris@16
|
156
|
Chris@16
|
157 }
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@16
|
160 #endif
|