boolean_op.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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_BOOLEAN_OP_HPP
  8. #define BOOST_POLYGON_BOOLEAN_OP_HPP
  9. namespace boost { namespace polygon{
  10. namespace boolean_op {
  11. //BooleanOp is the generic boolean operation scanline algorithm that provides
  12. //all the simple boolean set operations on manhattan data. By templatizing
  13. //the intersection count of the input and algorithm internals it is extensible
  14. //to multi-layer scans, properties and other advanced scanline operations above
  15. //and beyond simple booleans.
  16. //T must cast to int
  17. template <class T, typename Unit>
  18. class BooleanOp {
  19. public:
  20. typedef std::map<Unit, T> ScanData;
  21. typedef std::pair<Unit, T> ElementType;
  22. protected:
  23. ScanData scanData_;
  24. typename ScanData::iterator nextItr_;
  25. T nullT_;
  26. public:
  27. inline BooleanOp () : scanData_(), nextItr_(), nullT_() { nextItr_ = scanData_.end(); nullT_ = 0; }
  28. inline BooleanOp (T nullT) : scanData_(), nextItr_(), nullT_(nullT) { nextItr_ = scanData_.end(); }
  29. inline BooleanOp (const BooleanOp& that) : scanData_(that.scanData_), nextItr_(),
  30. nullT_(that.nullT_) { nextItr_ = scanData_.begin(); }
  31. inline BooleanOp& operator=(const BooleanOp& that);
  32. //moves scanline forward
  33. inline void advanceScan() { nextItr_ = scanData_.begin(); }
  34. //proceses the given interval and T data
  35. //appends output edges to cT
  36. template <class cT>
  37. inline void processInterval(cT& outputContainer, interval_data<Unit> ivl, T deltaCount);
  38. private:
  39. inline typename ScanData::iterator lookup_(Unit pos){
  40. if(nextItr_ != scanData_.end() && nextItr_->first >= pos) {
  41. return nextItr_;
  42. }
  43. return nextItr_ = scanData_.lower_bound(pos);
  44. }
  45. inline typename ScanData::iterator insert_(Unit pos, T count){
  46. return nextItr_ = scanData_.insert(nextItr_, ElementType(pos, count));
  47. }
  48. template <class cT>
  49. inline void evaluateInterval_(cT& outputContainer, interval_data<Unit> ivl, T beforeCount, T afterCount);
  50. };
  51. class BinaryAnd {
  52. public:
  53. inline BinaryAnd() {}
  54. inline bool operator()(int a, int b) { return (a > 0) & (b > 0); }
  55. };
  56. class BinaryOr {
  57. public:
  58. inline BinaryOr() {}
  59. inline bool operator()(int a, int b) { return (a > 0) | (b > 0); }
  60. };
  61. class BinaryNot {
  62. public:
  63. inline BinaryNot() {}
  64. inline bool operator()(int a, int b) { return (a > 0) & !(b > 0); }
  65. };
  66. class BinaryXor {
  67. public:
  68. inline BinaryXor() {}
  69. inline bool operator()(int a, int b) { return (a > 0) ^ (b > 0); }
  70. };
  71. //BinaryCount is an array of two deltaCounts coming from two different layers
  72. //of scan event data. It is the merged count of the two suitable for consumption
  73. //as the template argument of the BooleanOp algorithm because BinaryCount casts to int.
  74. //T is a binary functor object that evaluates the array of counts and returns a logical
  75. //result of some operation on those values.
  76. //BinaryCount supports many of the operators that work with int, particularly the
  77. //binary operators, but cannot support less than or increment.
  78. template <class T>
  79. class BinaryCount {
  80. public:
  81. inline BinaryCount()
  82. #ifndef BOOST_POLYGON_MSVC
  83. : counts_()
  84. #endif
  85. { counts_[0] = counts_[1] = 0; }
  86. // constructs from two integers
  87. inline BinaryCount(int countL, int countR)
  88. #ifndef BOOST_POLYGON_MSVC
  89. : counts_()
  90. #endif
  91. { counts_[0] = countL, counts_[1] = countR; }
  92. inline BinaryCount& operator=(int count) { counts_[0] = count, counts_[1] = count; return *this; }
  93. inline BinaryCount& operator=(const BinaryCount& that);
  94. inline BinaryCount(const BinaryCount& that)
  95. #ifndef BOOST_POLYGON_MSVC
  96. : counts_()
  97. #endif
  98. { *this = that; }
  99. inline bool operator==(const BinaryCount& that) const;
  100. inline bool operator!=(const BinaryCount& that) const { return !((*this) == that);}
  101. inline BinaryCount& operator+=(const BinaryCount& that);
  102. inline BinaryCount& operator-=(const BinaryCount& that);
  103. inline BinaryCount operator+(const BinaryCount& that) const;
  104. inline BinaryCount operator-(const BinaryCount& that) const;
  105. inline BinaryCount operator-() const;
  106. inline int& operator[](bool index) { return counts_[index]; }
  107. //cast to int operator evaluates data using T binary functor
  108. inline operator int() const { return T()(counts_[0], counts_[1]); }
  109. private:
  110. int counts_[2];
  111. };
  112. class UnaryCount {
  113. public:
  114. inline UnaryCount() : count_(0) {}
  115. // constructs from two integers
  116. inline explicit UnaryCount(int count) : count_(count) {}
  117. inline UnaryCount& operator=(int count) { count_ = count; return *this; }
  118. inline UnaryCount& operator=(const UnaryCount& that) { count_ = that.count_; return *this; }
  119. inline UnaryCount(const UnaryCount& that) : count_(that.count_) {}
  120. inline bool operator==(const UnaryCount& that) const { return count_ == that.count_; }
  121. inline bool operator!=(const UnaryCount& that) const { return !((*this) == that);}
  122. inline UnaryCount& operator+=(const UnaryCount& that) { count_ += that.count_; return *this; }
  123. inline UnaryCount& operator-=(const UnaryCount& that) { count_ -= that.count_; return *this; }
  124. inline UnaryCount operator+(const UnaryCount& that) const { UnaryCount tmp(*this); tmp += that; return tmp; }
  125. inline UnaryCount operator-(const UnaryCount& that) const { UnaryCount tmp(*this); tmp -= that; return tmp; }
  126. inline UnaryCount operator-() const { UnaryCount tmp; return tmp - *this; }
  127. //cast to int operator evaluates data using T binary functor
  128. inline operator int() const { return count_ % 2; }
  129. private:
  130. int count_;
  131. };
  132. template <class T, typename Unit>
  133. inline BooleanOp<T, Unit>& BooleanOp<T, Unit>::operator=(const BooleanOp& that) {
  134. scanData_ = that.scanData_;
  135. nextItr_ = scanData_.begin();
  136. nullT_ = that.nullT_;
  137. return *this;
  138. }
  139. //appends output edges to cT
  140. template <class T, typename Unit>
  141. template <class cT>
  142. inline void BooleanOp<T, Unit>::processInterval(cT& outputContainer, interval_data<Unit> ivl, T deltaCount) {
  143. typename ScanData::iterator lowItr = lookup_(ivl.low());
  144. typename ScanData::iterator highItr = lookup_(ivl.high());
  145. //add interval to scan data if it is past the end
  146. if(lowItr == scanData_.end()) {
  147. lowItr = insert_(ivl.low(), deltaCount);
  148. highItr = insert_(ivl.high(), nullT_);
  149. evaluateInterval_(outputContainer, ivl, nullT_, deltaCount);
  150. return;
  151. }
  152. //ensure that highItr points to the end of the ivl
  153. if(highItr == scanData_.end() || (*highItr).first > ivl.high()) {
  154. T value = nullT_;
  155. if(highItr != scanData_.begin()) {
  156. --highItr;
  157. value = highItr->second;
  158. }
  159. nextItr_ = highItr;
  160. highItr = insert_(ivl.high(), value);
  161. }
  162. //split the low interval if needed
  163. if(lowItr->first > ivl.low()) {
  164. if(lowItr != scanData_.begin()) {
  165. --lowItr;
  166. nextItr_ = lowItr;
  167. lowItr = insert_(ivl.low(), lowItr->second);
  168. } else {
  169. nextItr_ = lowItr;
  170. lowItr = insert_(ivl.low(), nullT_);
  171. }
  172. }
  173. //process scan data intersecting interval
  174. for(typename ScanData::iterator itr = lowItr; itr != highItr; ){
  175. T beforeCount = itr->second;
  176. T afterCount = itr->second += deltaCount;
  177. Unit low = itr->first;
  178. ++itr;
  179. Unit high = itr->first;
  180. evaluateInterval_(outputContainer, interval_data<Unit>(low, high), beforeCount, afterCount);
  181. }
  182. //merge the bottom interval with the one below if they have the same count
  183. if(lowItr != scanData_.begin()){
  184. typename ScanData::iterator belowLowItr = lowItr;
  185. --belowLowItr;
  186. if(belowLowItr->second == lowItr->second) {
  187. scanData_.erase(lowItr);
  188. }
  189. }
  190. //merge the top interval with the one above if they have the same count
  191. if(highItr != scanData_.begin()) {
  192. typename ScanData::iterator beforeHighItr = highItr;
  193. --beforeHighItr;
  194. if(beforeHighItr->second == highItr->second) {
  195. scanData_.erase(highItr);
  196. highItr = beforeHighItr;
  197. ++highItr;
  198. }
  199. }
  200. nextItr_ = highItr;
  201. }
  202. template <class T, typename Unit>
  203. template <class cT>
  204. inline void BooleanOp<T, Unit>::evaluateInterval_(cT& outputContainer, interval_data<Unit> ivl,
  205. T beforeCount, T afterCount) {
  206. bool before = (int)beforeCount > 0;
  207. bool after = (int)afterCount > 0;
  208. int value = (!before & after) - (before & !after);
  209. if(value) {
  210. outputContainer.insert(outputContainer.end(), std::pair<interval_data<Unit>, int>(ivl, value));
  211. }
  212. }
  213. template <class T>
  214. inline BinaryCount<T>& BinaryCount<T>::operator=(const BinaryCount<T>& that) {
  215. counts_[0] = that.counts_[0];
  216. counts_[1] = that.counts_[1];
  217. return *this;
  218. }
  219. template <class T>
  220. inline bool BinaryCount<T>::operator==(const BinaryCount<T>& that) const {
  221. return counts_[0] == that.counts_[0] &&
  222. counts_[1] == that.counts_[1];
  223. }
  224. template <class T>
  225. inline BinaryCount<T>& BinaryCount<T>::operator+=(const BinaryCount<T>& that) {
  226. counts_[0] += that.counts_[0];
  227. counts_[1] += that.counts_[1];
  228. return *this;
  229. }
  230. template <class T>
  231. inline BinaryCount<T>& BinaryCount<T>::operator-=(const BinaryCount<T>& that) {
  232. counts_[0] += that.counts_[0];
  233. counts_[1] += that.counts_[1];
  234. return *this;
  235. }
  236. template <class T>
  237. inline BinaryCount<T> BinaryCount<T>::operator+(const BinaryCount<T>& that) const {
  238. BinaryCount retVal(*this);
  239. retVal += that;
  240. return retVal;
  241. }
  242. template <class T>
  243. inline BinaryCount<T> BinaryCount<T>::operator-(const BinaryCount<T>& that) const {
  244. BinaryCount retVal(*this);
  245. retVal -= that;
  246. return retVal;
  247. }
  248. template <class T>
  249. inline BinaryCount<T> BinaryCount<T>::operator-() const {
  250. return BinaryCount<T>() - *this;
  251. }
  252. template <class T, typename Unit, typename iterator_type_1, typename iterator_type_2>
  253. inline void applyBooleanBinaryOp(std::vector<std::pair<Unit, std::pair<Unit, int> > >& output,
  254. //const std::vector<std::pair<Unit, std::pair<Unit, int> > >& input1,
  255. //const std::vector<std::pair<Unit, std::pair<Unit, int> > >& input2,
  256. iterator_type_1 itr1, iterator_type_1 itr1_end,
  257. iterator_type_2 itr2, iterator_type_2 itr2_end,
  258. T defaultCount) {
  259. BooleanOp<T, Unit> boolean(defaultCount);
  260. //typename std::vector<std::pair<Unit, std::pair<Unit, int> > >::const_iterator itr1 = input1.begin();
  261. //typename std::vector<std::pair<Unit, std::pair<Unit, int> > >::const_iterator itr2 = input2.begin();
  262. std::vector<std::pair<interval_data<Unit>, int> > container;
  263. //output.reserve((std::max)(input1.size(), input2.size()));
  264. //consider eliminating dependecy on limits with bool flag for initial state
  265. Unit UnitMax = (std::numeric_limits<Unit>::max)();
  266. Unit prevCoord = UnitMax;
  267. Unit prevPosition = UnitMax;
  268. T count(defaultCount);
  269. //define the starting point
  270. if(itr1 != itr1_end) {
  271. prevCoord = (*itr1).first;
  272. prevPosition = (*itr1).second.first;
  273. count[0] += (*itr1).second.second;
  274. }
  275. if(itr2 != itr2_end) {
  276. if((*itr2).first < prevCoord ||
  277. ((*itr2).first == prevCoord && (*itr2).second.first < prevPosition)) {
  278. prevCoord = (*itr2).first;
  279. prevPosition = (*itr2).second.first;
  280. count = defaultCount;
  281. count[1] += (*itr2).second.second;
  282. ++itr2;
  283. } else if((*itr2).first == prevCoord && (*itr2).second.first == prevPosition) {
  284. count[1] += (*itr2).second.second;
  285. ++itr2;
  286. if(itr1 != itr1_end) ++itr1;
  287. } else {
  288. if(itr1 != itr1_end) ++itr1;
  289. }
  290. } else {
  291. if(itr1 != itr1_end) ++itr1;
  292. }
  293. while(itr1 != itr1_end || itr2 != itr2_end) {
  294. Unit curCoord = UnitMax;
  295. Unit curPosition = UnitMax;
  296. T curCount(defaultCount);
  297. if(itr1 != itr1_end) {
  298. curCoord = (*itr1).first;
  299. curPosition = (*itr1).second.first;
  300. curCount[0] += (*itr1).second.second;
  301. }
  302. if(itr2 != itr2_end) {
  303. if((*itr2).first < curCoord ||
  304. ((*itr2).first == curCoord && (*itr2).second.first < curPosition)) {
  305. curCoord = (*itr2).first;
  306. curPosition = (*itr2).second.first;
  307. curCount = defaultCount;
  308. curCount[1] += (*itr2).second.second;
  309. ++itr2;
  310. } else if((*itr2).first == curCoord && (*itr2).second.first == curPosition) {
  311. curCount[1] += (*itr2).second.second;
  312. ++itr2;
  313. if(itr1 != itr1_end) ++itr1;
  314. } else {
  315. if(itr1 != itr1_end) ++itr1;
  316. }
  317. } else {
  318. ++itr1;
  319. }
  320. if(prevCoord != curCoord) {
  321. boolean.advanceScan();
  322. prevCoord = curCoord;
  323. prevPosition = curPosition;
  324. count = curCount;
  325. continue;
  326. }
  327. if(curPosition != prevPosition && count != defaultCount) {
  328. interval_data<Unit> ivl(prevPosition, curPosition);
  329. container.clear();
  330. boolean.processInterval(container, ivl, count);
  331. for(std::size_t i = 0; i < container.size(); ++i) {
  332. std::pair<interval_data<Unit>, int>& element = container[i];
  333. if(!output.empty() && output.back().first == prevCoord &&
  334. output.back().second.first == element.first.low() &&
  335. output.back().second.second == element.second * -1) {
  336. output.pop_back();
  337. } else {
  338. output.push_back(std::pair<Unit, std::pair<Unit, int> >(prevCoord, std::pair<Unit, int>(element.first.low(),
  339. element.second)));
  340. }
  341. output.push_back(std::pair<Unit, std::pair<Unit, int> >(prevCoord, std::pair<Unit, int>(element.first.high(),
  342. element.second * -1)));
  343. }
  344. }
  345. prevPosition = curPosition;
  346. count += curCount;
  347. }
  348. }
  349. template <class T, typename Unit>
  350. inline void applyBooleanBinaryOp(std::vector<std::pair<Unit, std::pair<Unit, int> > >& inputOutput,
  351. const std::vector<std::pair<Unit, std::pair<Unit, int> > >& input2,
  352. T defaultCount) {
  353. std::vector<std::pair<Unit, std::pair<Unit, int> > > output;
  354. applyBooleanBinaryOp(output, inputOutput, input2, defaultCount);
  355. if(output.size() < inputOutput.size() / 2) {
  356. inputOutput = std::vector<std::pair<Unit, std::pair<Unit, int> > >();
  357. } else {
  358. inputOutput.clear();
  359. }
  360. inputOutput.insert(inputOutput.end(), output.begin(), output.end());
  361. }
  362. template <typename count_type = int>
  363. struct default_arg_workaround {
  364. template <typename Unit>
  365. static inline void applyBooleanOr(std::vector<std::pair<Unit, std::pair<Unit, int> > >& input) {
  366. BooleanOp<count_type, Unit> booleanOr;
  367. std::vector<std::pair<interval_data<Unit>, int> > container;
  368. std::vector<std::pair<Unit, std::pair<Unit, int> > > output;
  369. output.reserve(input.size());
  370. //consider eliminating dependecy on limits with bool flag for initial state
  371. Unit UnitMax = (std::numeric_limits<Unit>::max)();
  372. Unit prevPos = UnitMax;
  373. Unit prevY = UnitMax;
  374. int count = 0;
  375. for(typename std::vector<std::pair<Unit, std::pair<Unit, int> > >::iterator itr = input.begin();
  376. itr != input.end(); ++itr) {
  377. Unit pos = (*itr).first;
  378. Unit y = (*itr).second.first;
  379. if(pos != prevPos) {
  380. booleanOr.advanceScan();
  381. prevPos = pos;
  382. prevY = y;
  383. count = (*itr).second.second;
  384. continue;
  385. }
  386. if(y != prevY && count != 0) {
  387. interval_data<Unit> ivl(prevY, y);
  388. container.clear();
  389. booleanOr.processInterval(container, ivl, count_type(count));
  390. for(std::size_t i = 0; i < container.size(); ++i) {
  391. std::pair<interval_data<Unit>, int>& element = container[i];
  392. if(!output.empty() && output.back().first == prevPos &&
  393. output.back().second.first == element.first.low() &&
  394. output.back().second.second == element.second * -1) {
  395. output.pop_back();
  396. } else {
  397. output.push_back(std::pair<Unit, std::pair<Unit, int> >(prevPos, std::pair<Unit, int>(element.first.low(),
  398. element.second)));
  399. }
  400. output.push_back(std::pair<Unit, std::pair<Unit, int> >(prevPos, std::pair<Unit, int>(element.first.high(),
  401. element.second * -1)));
  402. }
  403. }
  404. prevY = y;
  405. count += (*itr).second.second;
  406. }
  407. if(output.size() < input.size() / 2) {
  408. input = std::vector<std::pair<Unit, std::pair<Unit, int> > >();
  409. } else {
  410. input.clear();
  411. }
  412. input.insert(input.end(), output.begin(), output.end());
  413. }
  414. };
  415. }
  416. }
  417. }
  418. #endif