polygon_90_touch.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. Copyright 2008 Intel Corporation
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. */
  7. #ifndef BOOST_POLYGON_POLYGON_90_TOUCH_HPP
  8. #define BOOST_POLYGON_POLYGON_90_TOUCH_HPP
  9. namespace boost { namespace polygon{
  10. template <typename Unit>
  11. struct touch_90_operation {
  12. typedef interval_data<Unit> Interval;
  13. class TouchScanEvent {
  14. private:
  15. typedef std::map<Unit, std::set<int> > EventData;
  16. EventData eventData_;
  17. public:
  18. // The TouchScanEvent::iterator is a lazy algorithm that accumulates
  19. // polygon ids in a set as it is incremented through the
  20. // scan event data structure.
  21. // The iterator provides a forward iterator semantic only.
  22. class iterator {
  23. private:
  24. typename EventData::const_iterator itr_;
  25. std::pair<Interval, std::set<int> > ivlIds_;
  26. bool incremented_;
  27. public:
  28. inline iterator() : itr_(), ivlIds_(), incremented_(false) {}
  29. inline iterator(typename EventData::const_iterator itr,
  30. Unit prevPos, Unit curPos, const std::set<int>& ivlIds) : itr_(itr), ivlIds_(), incremented_(false) {
  31. ivlIds_.second = ivlIds;
  32. ivlIds_.first = Interval(prevPos, curPos);
  33. }
  34. inline iterator(const iterator& that) : itr_(), ivlIds_(), incremented_(false) { (*this) = that; }
  35. inline iterator& operator=(const iterator& that) {
  36. itr_ = that.itr_;
  37. ivlIds_.first = that.ivlIds_.first;
  38. ivlIds_.second = that.ivlIds_.second;
  39. incremented_ = that.incremented_;
  40. return *this;
  41. }
  42. inline bool operator==(const iterator& that) { return itr_ == that.itr_; }
  43. inline bool operator!=(const iterator& that) { return itr_ != that.itr_; }
  44. inline iterator& operator++() {
  45. //std::cout << "increment\n";
  46. //std::cout << "state\n";
  47. //for(std::set<int>::iterator itr = ivlIds_.second.begin(); itr != ivlIds_.second.end(); ++itr) {
  48. // std::cout << (*itr) << " ";
  49. //} std::cout << std::endl;
  50. //std::cout << "update\n";
  51. for(std::set<int>::const_iterator itr = (*itr_).second.begin();
  52. itr != (*itr_).second.end(); ++itr) {
  53. //std::cout << (*itr) << " ";
  54. std::set<int>::iterator lb = ivlIds_.second.find(*itr);
  55. if(lb != ivlIds_.second.end()) {
  56. ivlIds_.second.erase(lb);
  57. } else {
  58. ivlIds_.second.insert(*itr);
  59. }
  60. }
  61. //std::cout << std::endl;
  62. //std::cout << "new state\n";
  63. //for(std::set<int>::iterator itr = ivlIds_.second.begin(); itr != ivlIds_.second.end(); ++itr) {
  64. // std::cout << (*itr) << " ";
  65. //} std::cout << std::endl;
  66. ++itr_;
  67. //ivlIds_.first = Interval(ivlIds_.first.get(HIGH), itr_->first);
  68. incremented_ = true;
  69. return *this;
  70. }
  71. inline const iterator operator++(int){
  72. iterator tmpItr(*this);
  73. ++(*this);
  74. return tmpItr;
  75. }
  76. inline std::pair<Interval, std::set<int> >& operator*() {
  77. if(incremented_) ivlIds_.first = Interval(ivlIds_.first.get(HIGH), itr_->first);
  78. incremented_ = false;
  79. if(ivlIds_.second.empty())(++(*this));
  80. if(incremented_) ivlIds_.first = Interval(ivlIds_.first.get(HIGH), itr_->first);
  81. incremented_ = false;
  82. return ivlIds_; }
  83. };
  84. inline TouchScanEvent() : eventData_() {}
  85. template<class iT>
  86. inline TouchScanEvent(iT begin, iT end) : eventData_() {
  87. for( ; begin != end; ++begin){
  88. insert(*begin);
  89. }
  90. }
  91. inline TouchScanEvent(const TouchScanEvent& that) : eventData_(that.eventData_) {}
  92. inline TouchScanEvent& operator=(const TouchScanEvent& that){
  93. eventData_ = that.eventData_;
  94. return *this;
  95. }
  96. //Insert an interval polygon id into the EventData
  97. inline void insert(const std::pair<Interval, int>& intervalId){
  98. insert(intervalId.first.low(), intervalId.second);
  99. insert(intervalId.first.high(), intervalId.second);
  100. }
  101. //Insert an position and polygon id into EventData
  102. inline void insert(Unit pos, int id) {
  103. typename EventData::iterator lb = eventData_.lower_bound(pos);
  104. if(lb != eventData_.end() && lb->first == pos) {
  105. std::set<int>& mr (lb->second);
  106. std::set<int>::iterator mri = mr.find(id);
  107. if(mri == mr.end()) {
  108. mr.insert(id);
  109. } else {
  110. mr.erase(id);
  111. }
  112. } else {
  113. lb = eventData_.insert(lb, std::pair<Unit, std::set<int> >(pos, std::set<int>()));
  114. (*lb).second.insert(id);
  115. }
  116. }
  117. //merge this scan event with that by inserting its data
  118. inline void insert(const TouchScanEvent& that){
  119. typename EventData::const_iterator itr;
  120. for(itr = that.eventData_.begin(); itr != that.eventData_.end(); ++itr) {
  121. eventData_[(*itr).first].insert(itr->second.begin(), itr->second.end());
  122. }
  123. }
  124. //Get the begin iterator over event data
  125. inline iterator begin() const {
  126. //std::cout << "begin\n";
  127. if(eventData_.empty()) return end();
  128. typename EventData::const_iterator itr = eventData_.begin();
  129. Unit pos = itr->first;
  130. const std::set<int>& idr = itr->second;
  131. ++itr;
  132. return iterator(itr, pos, itr->first, idr);
  133. }
  134. //Get the end iterator over event data
  135. inline iterator end() const { return iterator(eventData_.end(), 0, 0, std::set<int>()); }
  136. inline void clear() { eventData_.clear(); }
  137. inline Interval extents() const {
  138. if(eventData_.empty()) return Interval();
  139. return Interval((*(eventData_.begin())).first, (*(eventData_.rbegin())).first);
  140. }
  141. };
  142. //declaration of a map of scan events by coordinate value used to store all the
  143. //polygon data for a single layer input into the scanline algorithm
  144. typedef std::pair<std::map<Unit, TouchScanEvent>, std::map<Unit, TouchScanEvent> > TouchSetData;
  145. class TouchOp {
  146. public:
  147. typedef std::map<Unit, std::set<int> > ScanData;
  148. typedef std::pair<Unit, std::set<int> > ElementType;
  149. protected:
  150. ScanData scanData_;
  151. typename ScanData::iterator nextItr_;
  152. public:
  153. inline TouchOp () : scanData_(), nextItr_() { nextItr_ = scanData_.end(); }
  154. inline TouchOp (const TouchOp& that) : scanData_(that.scanData_), nextItr_() { nextItr_ = scanData_.begin(); }
  155. inline TouchOp& operator=(const TouchOp& that);
  156. //moves scanline forward
  157. inline void advanceScan() { nextItr_ = scanData_.begin(); }
  158. //proceses the given interval and std::set<int> data
  159. //the output data structre is a graph, the indicies in the vector correspond to graph nodes,
  160. //the integers in the set are vector indicies and are the nodes with which that node shares an edge
  161. template <typename graphT>
  162. inline void processInterval(graphT& outputContainer, Interval ivl, const std::set<int>& ids, bool leadingEdge) {
  163. //print();
  164. typename ScanData::iterator lowItr = lookup_(ivl.low());
  165. typename ScanData::iterator highItr = lookup_(ivl.high());
  166. //std::cout << "Interval: " << ivl << std::endl;
  167. //for(std::set<int>::const_iterator itr = ids.begin(); itr != ids.end(); ++itr)
  168. // std::cout << (*itr) << " ";
  169. //std::cout << std::endl;
  170. //add interval to scan data if it is past the end
  171. if(lowItr == scanData_.end()) {
  172. //std::cout << "case0" << std::endl;
  173. lowItr = insert_(ivl.low(), ids);
  174. evaluateBorder_(outputContainer, ids, ids);
  175. highItr = insert_(ivl.high(), std::set<int>());
  176. return;
  177. }
  178. //ensure that highItr points to the end of the ivl
  179. if(highItr == scanData_.end() || (*highItr).first > ivl.high()) {
  180. //std::cout << "case1" << std::endl;
  181. //std::cout << highItr->first << std::endl;
  182. std::set<int> value = std::set<int>();
  183. if(highItr != scanData_.begin()) {
  184. --highItr;
  185. //std::cout << highItr->first << std::endl;
  186. //std::cout << "high set size " << highItr->second.size() << std::endl;
  187. value = highItr->second;
  188. }
  189. nextItr_ = highItr;
  190. highItr = insert_(ivl.high(), value);
  191. } else {
  192. //evaluate border with next higher interval
  193. //std::cout << "case1a" << std::endl;
  194. if(leadingEdge)evaluateBorder_(outputContainer, highItr->second, ids);
  195. }
  196. //split the low interval if needed
  197. if(lowItr->first > ivl.low()) {
  198. //std::cout << "case2" << std::endl;
  199. if(lowItr != scanData_.begin()) {
  200. //std::cout << "case3" << std::endl;
  201. --lowItr;
  202. nextItr_ = lowItr;
  203. //std::cout << lowItr->first << " " << lowItr->second.size() << std::endl;
  204. lowItr = insert_(ivl.low(), lowItr->second);
  205. } else {
  206. //std::cout << "case4" << std::endl;
  207. nextItr_ = lowItr;
  208. lowItr = insert_(ivl.low(), std::set<int>());
  209. }
  210. } else {
  211. //evaluate border with next higher interval
  212. //std::cout << "case2a" << std::endl;
  213. typename ScanData::iterator nextLowerItr = lowItr;
  214. if(leadingEdge && nextLowerItr != scanData_.begin()){
  215. --nextLowerItr;
  216. evaluateBorder_(outputContainer, nextLowerItr->second, ids);
  217. }
  218. }
  219. //std::cout << "low: " << lowItr->first << " high: " << highItr->first << std::endl;
  220. //print();
  221. //process scan data intersecting interval
  222. for(typename ScanData::iterator itr = lowItr; itr != highItr; ){
  223. //std::cout << "case5" << std::endl;
  224. //std::cout << itr->first << std::endl;
  225. std::set<int>& beforeIds = itr->second;
  226. ++itr;
  227. evaluateInterval_(outputContainer, beforeIds, ids, leadingEdge);
  228. }
  229. //print();
  230. //merge the bottom interval with the one below if they have the same count
  231. if(lowItr != scanData_.begin()){
  232. //std::cout << "case6" << std::endl;
  233. typename ScanData::iterator belowLowItr = lowItr;
  234. --belowLowItr;
  235. if(belowLowItr->second == lowItr->second) {
  236. //std::cout << "case7" << std::endl;
  237. scanData_.erase(lowItr);
  238. }
  239. }
  240. //merge the top interval with the one above if they have the same count
  241. if(highItr != scanData_.begin()) {
  242. //std::cout << "case8" << std::endl;
  243. typename ScanData::iterator beforeHighItr = highItr;
  244. --beforeHighItr;
  245. if(beforeHighItr->second == highItr->second) {
  246. //std::cout << "case9" << std::endl;
  247. scanData_.erase(highItr);
  248. highItr = beforeHighItr;
  249. ++highItr;
  250. }
  251. }
  252. //print();
  253. nextItr_ = highItr;
  254. }
  255. // inline void print() const {
  256. // for(typename ScanData::const_iterator itr = scanData_.begin(); itr != scanData_.end(); ++itr) {
  257. // std::cout << itr->first << ": ";
  258. // for(std::set<int>::const_iterator sitr = itr->second.begin();
  259. // sitr != itr->second.end(); ++sitr){
  260. // std::cout << *sitr << " ";
  261. // }
  262. // std::cout << std::endl;
  263. // }
  264. // }
  265. private:
  266. inline typename ScanData::iterator lookup_(Unit pos){
  267. if(nextItr_ != scanData_.end() && nextItr_->first >= pos) {
  268. return nextItr_;
  269. }
  270. return nextItr_ = scanData_.lower_bound(pos);
  271. }
  272. inline typename ScanData::iterator insert_(Unit pos, const std::set<int>& ids){
  273. //std::cout << "inserting " << ids.size() << " ids at: " << pos << std::endl;
  274. return nextItr_ = scanData_.insert(nextItr_, std::pair<Unit, std::set<int> >(pos, ids));
  275. }
  276. template <typename graphT>
  277. inline void evaluateInterval_(graphT& outputContainer, std::set<int>& ids,
  278. const std::set<int>& changingIds, bool leadingEdge) {
  279. for(std::set<int>::const_iterator ciditr = changingIds.begin(); ciditr != changingIds.end(); ++ciditr){
  280. //std::cout << "evaluateInterval " << (*ciditr) << std::endl;
  281. evaluateId_(outputContainer, ids, *ciditr, leadingEdge);
  282. }
  283. }
  284. template <typename graphT>
  285. inline void evaluateBorder_(graphT& outputContainer, const std::set<int>& ids, const std::set<int>& changingIds) {
  286. for(std::set<int>::const_iterator ciditr = changingIds.begin(); ciditr != changingIds.end(); ++ciditr){
  287. //std::cout << "evaluateBorder " << (*ciditr) << std::endl;
  288. evaluateBorderId_(outputContainer, ids, *ciditr);
  289. }
  290. }
  291. template <typename graphT>
  292. inline void evaluateBorderId_(graphT& outputContainer, const std::set<int>& ids, int changingId) {
  293. for(std::set<int>::const_iterator scanItr = ids.begin(); scanItr != ids.end(); ++scanItr) {
  294. //std::cout << "create edge: " << changingId << " " << *scanItr << std::endl;
  295. if(changingId != *scanItr){
  296. outputContainer[changingId].insert(*scanItr);
  297. outputContainer[*scanItr].insert(changingId);
  298. }
  299. }
  300. }
  301. template <typename graphT>
  302. inline void evaluateId_(graphT& outputContainer, std::set<int>& ids, int changingId, bool leadingEdge) {
  303. //std::cout << "changingId: " << changingId << std::endl;
  304. //for( std::set<int>::iterator itr = ids.begin(); itr != ids.end(); ++itr){
  305. // std::cout << *itr << " ";
  306. //}std::cout << std::endl;
  307. std::set<int>::iterator lb = ids.lower_bound(changingId);
  308. if(lb == ids.end() || (*lb) != changingId) {
  309. if(leadingEdge) {
  310. //std::cout << "insert\n";
  311. //insert and add to output
  312. for(std::set<int>::iterator scanItr = ids.begin(); scanItr != ids.end(); ++scanItr) {
  313. //std::cout << "create edge: " << changingId << " " << *scanItr << std::endl;
  314. if(changingId != *scanItr){
  315. outputContainer[changingId].insert(*scanItr);
  316. outputContainer[*scanItr].insert(changingId);
  317. }
  318. }
  319. ids.insert(changingId);
  320. }
  321. } else {
  322. if(!leadingEdge){
  323. //std::cout << "erase\n";
  324. ids.erase(lb);
  325. }
  326. }
  327. }
  328. };
  329. template <typename graphT>
  330. static inline void processEvent(graphT& outputContainer, TouchOp& op, const TouchScanEvent& data, bool leadingEdge) {
  331. for(typename TouchScanEvent::iterator itr = data.begin(); itr != data.end(); ++itr) {
  332. //std::cout << "processInterval" << std::endl;
  333. op.processInterval(outputContainer, (*itr).first, (*itr).second, leadingEdge);
  334. }
  335. }
  336. template <typename graphT>
  337. static inline void performTouch(graphT& outputContainer, const TouchSetData& data) {
  338. typename std::map<Unit, TouchScanEvent>::const_iterator leftItr = data.first.begin();
  339. typename std::map<Unit, TouchScanEvent>::const_iterator rightItr = data.second.begin();
  340. typename std::map<Unit, TouchScanEvent>::const_iterator leftEnd = data.first.end();
  341. typename std::map<Unit, TouchScanEvent>::const_iterator rightEnd = data.second.end();
  342. TouchOp op;
  343. while(leftItr != leftEnd || rightItr != rightEnd) {
  344. //std::cout << "loop" << std::endl;
  345. op.advanceScan();
  346. //rightItr cannont be at end if leftItr is not at end
  347. if(leftItr != leftEnd && rightItr != rightEnd &&
  348. leftItr->first <= rightItr->first) {
  349. //std::cout << "case1" << std::endl;
  350. //std::cout << leftItr ->first << std::endl;
  351. processEvent(outputContainer, op, leftItr->second, true);
  352. ++leftItr;
  353. } else {
  354. //std::cout << "case2" << std::endl;
  355. //std::cout << rightItr ->first << std::endl;
  356. processEvent(outputContainer, op, rightItr->second, false);
  357. ++rightItr;
  358. }
  359. }
  360. }
  361. template <class iT>
  362. static inline void populateTouchSetData(TouchSetData& data, iT beginData, iT endData, int id) {
  363. Unit prevPos = ((std::numeric_limits<Unit>::max)());
  364. Unit prevY = prevPos;
  365. int count = 0;
  366. for(iT itr = beginData; itr != endData; ++itr) {
  367. Unit pos = (*itr).first;
  368. if(pos != prevPos) {
  369. prevPos = pos;
  370. prevY = (*itr).second.first;
  371. count = (*itr).second.second;
  372. continue;
  373. }
  374. Unit y = (*itr).second.first;
  375. if(count != 0 && y != prevY) {
  376. std::pair<Interval, int> element(Interval(prevY, y), id);
  377. if(count > 0) {
  378. data.first[pos].insert(element);
  379. } else {
  380. data.second[pos].insert(element);
  381. }
  382. }
  383. prevY = y;
  384. count += (*itr).second.second;
  385. }
  386. }
  387. static inline void populateTouchSetData(TouchSetData& data, const std::vector<std::pair<Unit, std::pair<Unit, int> > >& inputData, int id) {
  388. populateTouchSetData(data, inputData.begin(), inputData.end(), id);
  389. }
  390. };
  391. }
  392. }
  393. #endif