| | 134 | |
| | 135 | |
| | 136 | //quad tree based label collission detector so labels dont appear within a given distance |
| | 137 | class label_collision_detector4 : boost::noncopyable |
| | 138 | { |
| | 139 | struct label |
| | 140 | { |
| | 141 | label(Envelope<double> const& b) : box(b) {} |
| | 142 | label(Envelope<double> const& b, std::wstring const& t) : box(b), text(t) {} |
| | 143 | |
| | 144 | Envelope<double> box; |
| | 145 | std::wstring text; |
| | 146 | }; |
| | 147 | |
| | 148 | typedef quad_tree< label > tree_t; |
| | 149 | tree_t tree_; |
| | 150 | public: |
| | 151 | |
| | 152 | explicit label_collision_detector4(Envelope<double> const& extent) |
| | 153 | : tree_(extent) {} |
| | 154 | |
| | 155 | bool has_placement(Envelope<double> const& box) |
| | 156 | { |
| | 157 | tree_t::query_iterator itr = tree_.query_in_box(box); |
| | 158 | tree_t::query_iterator end = tree_.query_end(); |
| | 159 | |
| | 160 | for ( ;itr != end; ++itr) |
| | 161 | { |
| | 162 | if (itr->box.intersects(box)) |
| | 163 | { |
| | 164 | return false; |
| | 165 | } |
| | 166 | } |
| | 167 | |
| | 168 | return true; |
| | 169 | } |
| | 170 | |
| | 171 | bool has_placement(Envelope<double> const& box, std::wstring const& text, double distance) |
| | 172 | { |
| | 173 | Envelope<double> bigger_box(box.minx() - distance, box.miny() - distance, box.maxx() + distance, box.maxy() + distance); |
| | 174 | |
| | 175 | tree_t::query_iterator itr = tree_.query_in_box(bigger_box); |
| | 176 | tree_t::query_iterator end = tree_.query_end(); |
| | 177 | |
| | 178 | for ( ;itr != end; ++itr) |
| | 179 | { |
| | 180 | if (itr->box.intersects(box) || (text == itr->text && itr->box.intersects(bigger_box))) |
| | 181 | { |
| | 182 | return false; |
| | 183 | } |
| | 184 | } |
| | 185 | |
| | 186 | return true; |
| | 187 | } |
| | 188 | |
| | 189 | |
| | 190 | void insert(Envelope<double> const& box) |
| | 191 | { |
| | 192 | tree_.insert(label(box), box); |
| | 193 | } |
| | 194 | |
| | 195 | void insert(Envelope<double> const& box, std::wstring const& text) |
| | 196 | { |
| | 197 | tree_.insert(label(box, text), box); |
| | 198 | } |
| | 199 | |
| | 200 | void clear() |
| | 201 | { |
| | 202 | tree_.clear(); |
| | 203 | } |
| | 204 | }; |