polygon_45_set_data.hpp 76.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
/*
  Copyright 2008 Intel Corporation
 
  Use, modification and distribution are subject to the Boost Software License,
  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  http://www.boost.org/LICENSE_1_0.txt).
*/
#ifndef BOOST_POLYGON_POLYGON_45_SET_DATA_HPP
#define BOOST_POLYGON_POLYGON_45_SET_DATA_HPP
#include "polygon_90_set_data.hpp"
#include "detail/boolean_op_45.hpp"
#include "detail/polygon_45_formation.hpp"
#include "detail/polygon_45_touch.hpp"
#include "detail/property_merge_45.hpp"
namespace boost { namespace polygon{

  enum RoundingOption { CLOSEST = 0, OVERSIZE = 1, UNDERSIZE = 2, SQRT2 = 3, SQRT1OVER2 = 4 };
  enum CornerOption { INTERSECTION = 0, ORTHOGONAL = 1, UNFILLED = 2 };

  template <typename ltype, typename rtype, int op_type>
  class polygon_45_set_view;
  
  struct polygon_45_set_concept {};

  template <typename Unit>
  class polygon_45_set_data {
  public:
    typedef typename polygon_45_formation<Unit>::Vertex45Compact Vertex45Compact;
    typedef std::vector<Vertex45Compact> Polygon45VertexData;

    typedef Unit coordinate_type;
    typedef Polygon45VertexData value_type;
    typedef typename value_type::const_iterator iterator_type;
    typedef polygon_45_set_data operator_arg_type;

    // default constructor
    inline polygon_45_set_data() : error_data_(), data_(), dirty_(false), unsorted_(false), is_manhattan_(true) {}

    // constructor from a geometry object
    template <typename geometry_type>
    inline polygon_45_set_data(const geometry_type& that) : error_data_(), data_(), dirty_(false), unsorted_(false), is_manhattan_(true) {
      insert(that);
    }

    // copy constructor
    inline polygon_45_set_data(const polygon_45_set_data& that) : 
      error_data_(that.error_data_), data_(that.data_), dirty_(that.dirty_), 
      unsorted_(that.unsorted_), is_manhattan_(that.is_manhattan_) {}

    template <typename ltype, typename rtype, int op_type>
    inline polygon_45_set_data(const polygon_45_set_view<ltype, rtype, op_type>& that) :
      error_data_(), data_(), dirty_(false), unsorted_(false), is_manhattan_(true) {
      (*this) = that.value();
    }

    // destructor
    inline ~polygon_45_set_data() {}

    // assignement operator
    inline polygon_45_set_data& operator=(const polygon_45_set_data& that) {
      if(this == &that) return *this;
      error_data_ = that.error_data_;
      data_ = that.data_;
      dirty_ = that.dirty_;
      unsorted_ = that.unsorted_;
      is_manhattan_ = that.is_manhattan_;
      return *this;
    }

    template <typename ltype, typename rtype, int op_type>
    inline polygon_45_set_data& operator=(const polygon_45_set_view<ltype, rtype, op_type>& that) {
      (*this) = that.value();
      return *this;
    }

    template <typename geometry_object>
    inline polygon_45_set_data& operator=(const geometry_object& geometry) {
      data_.clear();
      insert(geometry);
      return *this;
    }

    // insert iterator range
    inline void insert(iterator_type input_begin, iterator_type input_end, bool is_hole = false) {
      if(input_begin == input_end || (!data_.empty() && &(*input_begin) == &(*(data_.begin())))) return;
      dirty_ = true;
      unsorted_ = true;
      while(input_begin != input_end) {
        insert(*input_begin, is_hole);
        ++input_begin;
      }
    }

    // insert iterator range
    template <typename iT>
    inline void insert(iT input_begin, iT input_end, bool is_hole = false) {
      if(input_begin == input_end) return;
      dirty_ = true;
      unsorted_ = true;
      while(input_begin != input_end) {
        insert(*input_begin, is_hole);
        ++input_begin;
      }
    }

    inline void insert(const polygon_45_set_data& polygon_set, bool is_hole = false);
    template <typename coord_type>
    inline void insert(const polygon_45_set_data<coord_type>& polygon_set, bool is_hole = false);

    template <typename geometry_type>
    inline void insert(const geometry_type& geometry_object, bool is_hole = false) {
      insert_dispatch(geometry_object, is_hole, typename geometry_concept<geometry_type>::type());
    }

    inline void insert_clean(const Vertex45Compact& vertex_45, bool is_hole = false) {
      if(vertex_45.count.is_45()) is_manhattan_ = false;
      data_.push_back(vertex_45);
      if(is_hole) data_.back().count.invert();
    }

    inline void insert(const Vertex45Compact& vertex_45, bool is_hole = false) {
      dirty_ = true;
      unsorted_ = true;
      insert_clean(vertex_45, is_hole);
    }
    
    template <typename coordinate_type_2>
    inline void insert(const polygon_90_set_data<coordinate_type_2>& polygon_set, bool is_hole = false) {
      if(polygon_set.orient() == VERTICAL) {
        for(typename polygon_90_set_data<coordinate_type_2>::iterator_type itr = polygon_set.begin();
            itr != polygon_set.end(); ++itr) {
          Vertex45Compact vertex_45(point_data<Unit>((*itr).first, (*itr).second.first), 2, (*itr).second.second);
          vertex_45.count[1] = (*itr).second.second;
          if(is_hole) vertex_45.count[1] *= - 1;
          insert_clean(vertex_45, is_hole);
        }
      } else {
        for(typename polygon_90_set_data<coordinate_type_2>::iterator_type itr = polygon_set.begin();
            itr != polygon_set.end(); ++itr) {
          Vertex45Compact vertex_45(point_data<Unit>((*itr).second.first, (*itr).first), 2, (*itr).second.second);
          vertex_45.count[1] = (*itr).second.second;
          if(is_hole) vertex_45.count[1] *= - 1;
          insert_clean(vertex_45, is_hole);
        }
      }
      dirty_ = true;
      unsorted_ = true;
    }

    template <typename output_container>
    inline void get(output_container& output) const {
      get_dispatch(output, typename geometry_concept<typename output_container::value_type>::type());
    }

    inline bool has_error_data() const { return !error_data_.empty(); }
    inline std::size_t error_count() const { return error_data_.size() / 4; }
    inline void get_error_data(polygon_45_set_data& p) const {
      p.data_.insert(p.data_.end(), error_data_.begin(), error_data_.end());
    }

    // equivalence operator 
    inline bool operator==(const polygon_45_set_data& p) const {
      clean();
      p.clean();
      return data_ == p.data_;
    }

    // inequivalence operator 
    inline bool operator!=(const polygon_45_set_data& p) const {
      return !((*this) == p);
    }

    // get iterator to begin vertex data
    inline iterator_type begin() const {
      return data_.begin();
    }

    // get iterator to end vertex data
    inline iterator_type end() const {
      return data_.end();
    }

    const value_type& value() const {
      return data_;
    }

    // clear the contents of the polygon_45_set_data
    inline void clear() { data_.clear(); error_data_.clear(); dirty_ = unsorted_ = false; is_manhattan_ = true; }

    // find out if Polygon set is empty
    inline bool empty() const { return data_.empty(); }

    // get the Polygon set size in vertices
    inline std::size_t size() const { clean(); return data_.size(); }

    // get the current Polygon set capacity in vertices
    inline std::size_t capacity() const { return data_.capacity(); }

    // reserve size of polygon set in vertices
    inline void reserve(std::size_t size) { return data_.reserve(size); }

    // find out if Polygon set is sorted
    inline bool sorted() const { return !unsorted_; }

    // find out if Polygon set is clean
    inline bool dirty() const { return dirty_; }

    // find out if Polygon set is clean
    inline bool is_manhattan() const { return is_manhattan_; }

    bool clean() const;

    void sort() const{
      if(unsorted_) {
        gtlsort(data_.begin(), data_.end());
        unsorted_ = false;
      }
    }

    template <typename input_iterator_type>
    void set(input_iterator_type input_begin, input_iterator_type input_end) {
      data_.clear();
      insert(input_begin, input_end);
      dirty_ = true;
      unsorted_ = true;
    }

    void set_clean(const value_type& value) {
      data_ = value;
      dirty_ = false;
      unsorted_ = false;
    }

    void set(const value_type& value) {
      data_ = value; 
      dirty_ = true;
      unsorted_ = true;
    }

    // append to the container cT with polygons (holes will be fractured vertically)
    template <class cT>
    void get_polygons(cT& container) const {
      get_dispatch(container, polygon_45_concept());
    }
    
    // append to the container cT with PolygonWithHoles objects
    template <class cT>
    void get_polygons_with_holes(cT& container) const {
      get_dispatch(container, polygon_45_with_holes_concept());
    }
    
    // append to the container cT with polygons of three or four verticies
    // slicing orientation is vertical
    template <class cT>
    void get_trapezoids(cT& container) const {
      clean();
      typename polygon_45_formation<Unit>::Polygon45Tiling pf;
      //std::cout << "FORMING POLYGONS\n";
      pf.scan(container, data_.begin(), data_.end());
      //std::cout << "DONE FORMING POLYGONS\n";
    }

    // append to the container cT with polygons of three or four verticies
    template <class cT>
    void get_trapezoids(cT& container, orientation_2d slicing_orientation) const {
      if(slicing_orientation == VERTICAL) {
        get_trapezoids(container);
      } else {
        polygon_45_set_data<Unit> ps(*this);
        ps.transform(axis_transformation(axis_transformation::SWAP_XY));
        cT result;
        ps.get_trapezoids(result);
        for(typename cT::iterator itr = result.begin(); itr != result.end(); ++itr) {
          ::boost::polygon::transform(*itr, axis_transformation(axis_transformation::SWAP_XY));
        }
        container.insert(container.end(), result.begin(), result.end());
      }
    }

    // insert vertex sequence
    template <class iT>
    void insert_vertex_sequence(iT begin_vertex, iT end_vertex,
                                direction_1d winding, bool is_hole = false);

    // get the external boundary rectangle
    template <typename rectangle_type>
    bool extents(rectangle_type& rect) const;

    // snap verticies of set to even,even or odd,odd coordinates
    void snap() const;
    
    // |= &= += *= -= ^= binary operators
    polygon_45_set_data& operator|=(const polygon_45_set_data& b);
    polygon_45_set_data& operator&=(const polygon_45_set_data& b);
    polygon_45_set_data& operator+=(const polygon_45_set_data& b);
    polygon_45_set_data& operator*=(const polygon_45_set_data& b);
    polygon_45_set_data& operator-=(const polygon_45_set_data& b);
    polygon_45_set_data& operator^=(const polygon_45_set_data& b);

    // resizing operations
    polygon_45_set_data& operator+=(Unit delta);
    polygon_45_set_data& operator-=(Unit delta);
    
    // shrink the Polygon45Set by shrinking
    polygon_45_set_data& resize(coordinate_type resizing, RoundingOption rounding = CLOSEST,
                                CornerOption corner = INTERSECTION);

    // transform set
    template <typename transformation_type>
    polygon_45_set_data& transform(const transformation_type& tr);

    // scale set
    polygon_45_set_data& scale_up(typename coordinate_traits<Unit>::unsigned_area_type factor);
    polygon_45_set_data& scale_down(typename coordinate_traits<Unit>::unsigned_area_type factor);
    polygon_45_set_data& scale(double scaling);

    // self_intersect
    polygon_45_set_data& self_intersect() {
      sort();
      applyAdaptiveUnary_<1>(); //1 = AND
      dirty_ = false;
      return *this;
    }

    // self_xor
    polygon_45_set_data& self_xor() {
      sort();
      applyAdaptiveUnary_<3>(); //3 = XOR
      dirty_ = false;
      return *this;
    }

    // accumulate the bloated polygon
    template <typename geometry_type>
    polygon_45_set_data& insert_with_resize(const geometry_type& poly,
                                            coordinate_type resizing, RoundingOption rounding = CLOSEST,
                                            CornerOption corner = INTERSECTION,
                                            bool hole = false) {
      return insert_with_resize_dispatch(poly, resizing, rounding, corner, hole, typename geometry_concept<geometry_type>::type());
    }
    
  private:
    mutable value_type error_data_;
    mutable value_type data_;
    mutable bool dirty_;
    mutable bool unsorted_;
    mutable bool is_manhattan_;
  
  private:
    //functions
    template <typename output_container>
    void get_dispatch(output_container& output, polygon_45_concept tag) const {
      get_fracture(output, true, tag);
    }
    template <typename output_container>
    void get_dispatch(output_container& output, polygon_45_with_holes_concept tag) const {
      get_fracture(output, false, tag);
    }
    template <typename output_container>
    void get_dispatch(output_container& output, polygon_concept tag) const {
      get_fracture(output, true, tag);
    }
    template <typename output_container>
    void get_dispatch(output_container& output, polygon_with_holes_concept tag) const {
      get_fracture(output, false, tag);
    }
    template <typename output_container, typename concept_type>
    void get_fracture(output_container& container, bool fracture_holes, concept_type ) const {
      clean();
      typename polygon_45_formation<Unit>::Polygon45Formation pf(fracture_holes);
      //std::cout << "FORMING POLYGONS\n";
      pf.scan(container, data_.begin(), data_.end());
    }

    template <typename geometry_type>
    void insert_dispatch(const geometry_type& geometry_object, bool is_hole, undefined_concept) {
      insert(geometry_object.begin(), geometry_object.end(), is_hole);
    }
    template <typename geometry_type>
    void insert_dispatch(const geometry_type& geometry_object, bool is_hole, rectangle_concept tag); 
    template <typename geometry_type>
    void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_90_concept ) {
      insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
    }
    template <typename geometry_type>
    void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_90_with_holes_concept ) {
      insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
      for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
            begin_holes(geometry_object); itr != end_holes(geometry_object);
          ++itr) {
        insert_vertex_sequence(begin_points(*itr), end_points(*itr), winding(*itr), !is_hole);
      }
    }
    template <typename geometry_type>
    void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_45_concept ) {
      insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
    }
    template <typename geometry_type>
    void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_45_with_holes_concept ) {
      insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
      for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
            begin_holes(geometry_object); itr != end_holes(geometry_object);
          ++itr) {
        insert_vertex_sequence(begin_points(*itr), end_points(*itr), winding(*itr), !is_hole);
      }
    }
    template <typename geometry_type>
    void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_45_set_concept ) {
      polygon_45_set_data ps;
      assign(ps, geometry_object);
      insert(ps, is_hole);
    }
    template <typename geometry_type>
    void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_90_set_concept ) {
      std::list<polygon_90_data<coordinate_type> > pl;
      assign(pl, geometry_object);
      insert(pl.begin(), pl.end(), is_hole);
    }

    void insert_vertex_half_edge_45_pair(const point_data<Unit>& pt1, point_data<Unit>& pt2, 
                                         const point_data<Unit>& pt3, direction_1d wdir);

    template <typename geometry_type>
    polygon_45_set_data& insert_with_resize_dispatch(const geometry_type& poly,
                                                     coordinate_type resizing, RoundingOption rounding,
                                                     CornerOption corner, bool hole, polygon_45_concept tag);
    
    // accumulate the bloated polygon with holes
    template <typename geometry_type>
    polygon_45_set_data& insert_with_resize_dispatch(const geometry_type& poly,
                                                     coordinate_type resizing, RoundingOption rounding,
                                                     CornerOption corner, bool hole, polygon_45_with_holes_concept tag); 
    
    static void snap_vertex_45(Vertex45Compact& vertex);

  public:
    template <int op>
    void applyAdaptiveBoolean_(const polygon_45_set_data& rvalue) const;
    template <int op>
    void applyAdaptiveBoolean_(polygon_45_set_data& result, const polygon_45_set_data& rvalue) const;
    template <int op>
    void applyAdaptiveUnary_() const;
  };

  template <typename T>
  struct geometry_concept<polygon_45_set_data<T> > {
    typedef polygon_45_set_concept type;
  };
 
  template <typename iT, typename T>
  void scale_up_vertex_45_compact_range(iT beginr, iT endr, T factor) {
    for( ; beginr != endr; ++beginr) {
      scale_up((*beginr).pt, factor);
    }
  }
  template <typename iT, typename T>
  void scale_down_vertex_45_compact_range_blindly(iT beginr, iT endr, T factor) {
    for( ; beginr != endr; ++beginr) {
      scale_down((*beginr).pt, factor);
    }
  }

  template <typename Unit>
  inline std::pair<int, int> characterizeEdge45(const point_data<Unit>& pt1, const point_data<Unit>& pt2) {
    std::pair<int, int> retval(0, 1);
    if(pt1.x() == pt2.x()) {
      retval.first = 3;
      retval.second = -1;
      return retval;
    }
    //retval.second = pt1.x() < pt2.x() ? -1 : 1;
    retval.second = 1;
    if(pt1.y() == pt2.y()) {
      retval.first = 1;
    } else if(pt1.x() < pt2.x()) {
      if(pt1.y() < pt2.y()) {
        retval.first = 2;
      } else {
        retval.first = 0;
      }
    } else {
      if(pt1.y() < pt2.y()) {
        retval.first = 0;
      } else {
        retval.first = 2;
      }
    }
    return retval;
  }

  template <typename cT, typename pT>
  bool insert_vertex_half_edge_45_pair_into_vector(cT& output,
                                       const pT& pt1, pT& pt2, 
                                       const pT& pt3, 
                                       direction_1d wdir) {
    int multiplier = wdir == LOW ? -1 : 1;
    typename cT::value_type vertex(pt2, 0, 0);
    //std::cout << pt1 << " " << pt2 << " " << pt3 << std::endl;
    std::pair<int, int> check;
    check = characterizeEdge45(pt1, pt2); 
    //std::cout << "index " << check.first << " " << check.second * -multiplier << std::endl;
    vertex.count[check.first] += check.second * -multiplier;
    check = characterizeEdge45(pt2, pt3); 
    //std::cout << "index " << check.first << " " << check.second * multiplier << std::endl;
    vertex.count[check.first] += check.second * multiplier;
    output.push_back(vertex);
    return vertex.count.is_45();
  }

  template <typename Unit>
  inline void polygon_45_set_data<Unit>::insert_vertex_half_edge_45_pair(const point_data<Unit>& pt1, point_data<Unit>& pt2, 
                                                                         const point_data<Unit>& pt3, 
                                                                         direction_1d wdir) {
    if(insert_vertex_half_edge_45_pair_into_vector(data_, pt1, pt2, pt3, wdir)) is_manhattan_ = false;
  }

  template <typename Unit>
  template <class iT>
  inline void polygon_45_set_data<Unit>::insert_vertex_sequence(iT begin_vertex, iT end_vertex,
                                                                direction_1d winding, bool is_hole) {
    if(begin_vertex == end_vertex) return;
    if(is_hole) winding = winding.backward();
    iT itr = begin_vertex;
    if(itr == end_vertex) return;
    point_data<Unit> firstPt = *itr;
    ++itr;
    point_data<Unit> secondPt(firstPt);
    //skip any duplicate points
    do {
      if(itr == end_vertex) return;
      secondPt = *itr;
      ++itr;
    } while(secondPt == firstPt);
    point_data<Unit> prevPt = secondPt;
    point_data<Unit> prevPrevPt = firstPt;
    while(itr != end_vertex) {
      point_data<Unit> pt = *itr;
      //skip any duplicate points
      if(pt == prevPt) {
        ++itr;
        continue;
      }
      //operate on the three points
      insert_vertex_half_edge_45_pair(prevPrevPt, prevPt, pt, winding);
      prevPrevPt = prevPt;
      prevPt = pt;
      ++itr;
    }
    if(prevPt != firstPt) {
      insert_vertex_half_edge_45_pair(prevPrevPt, prevPt, firstPt, winding);
      insert_vertex_half_edge_45_pair(prevPt, firstPt, secondPt, winding);
    } else {
      insert_vertex_half_edge_45_pair(prevPrevPt, firstPt, secondPt, winding);
    }
    dirty_ = true;
    unsorted_ = true;
  }

  // insert polygon set
  template <typename Unit>
  inline void polygon_45_set_data<Unit>::insert(const polygon_45_set_data<Unit>& polygon_set, bool is_hole) {
    std::size_t count = data_.size();
    data_.insert(data_.end(), polygon_set.data_.begin(), polygon_set.data_.end());
    error_data_.insert(error_data_.end(), polygon_set.error_data_.begin(),
                       polygon_set.error_data_.end());
    if(is_hole) {
      for(std::size_t i = count; i < data_.size(); ++i) {
        data_[i].count = data_[i].count.invert();
      }
    }
    dirty_ = true;
    unsorted_ = true;
    if(polygon_set.is_manhattan_ == false) is_manhattan_ = false;
    return;
  }
  // insert polygon set
  template <typename Unit>
  template <typename coord_type>
  inline void polygon_45_set_data<Unit>::insert(const polygon_45_set_data<coord_type>& polygon_set, bool is_hole) {
    std::size_t count = data_.size();
    for(typename polygon_45_set_data<coord_type>::iterator_type itr = polygon_set.begin();
        itr != polygon_set.end(); ++itr) {
      const typename polygon_45_set_data<coord_type>::Vertex45Compact& v = *itr;
      typename polygon_45_set_data<Unit>::Vertex45Compact v2;
      v2.pt.x(static_cast<Unit>(v.pt.x()));
      v2.pt.y(static_cast<Unit>(v.pt.y()));
      v2.count = typename polygon_45_formation<Unit>::Vertex45Count(v.count[0], v.count[1], v.count[2], v.count[3]);
      data_.push_back(v2);
    }
    polygon_45_set_data<coord_type> tmp;
    polygon_set.get_error_data(tmp);
    for(typename polygon_45_set_data<coord_type>::iterator_type itr = tmp.begin();
        itr != tmp.end(); ++itr) {
      const typename polygon_45_set_data<coord_type>::Vertex45Compact& v = *itr;
      typename polygon_45_set_data<Unit>::Vertex45Compact v2;
      v2.pt.x(static_cast<Unit>(v.pt.x()));
      v2.pt.y(static_cast<Unit>(v.pt.y()));
      v2.count = typename polygon_45_formation<Unit>::Vertex45Count(v.count[0], v.count[1], v.count[2], v.count[3]);
      error_data_.push_back(v2);
    }
    if(is_hole) {
      for(std::size_t i = count; i < data_.size(); ++i) {
        data_[i].count = data_[i].count.invert();
      }
    }
    dirty_ = true;
    unsorted_ = true;
    if(polygon_set.is_manhattan() == false) is_manhattan_ = false;
    return;
  }

  template <typename cT, typename rT>
  void insert_rectangle_into_vector_45(cT& output, const rT& rect, bool is_hole) {
    point_data<typename rectangle_traits<rT>::coordinate_type> 
      llpt = ll(rect), lrpt = lr(rect), ulpt = ul(rect), urpt = ur(rect);
    direction_1d dir = COUNTERCLOCKWISE;
    if(is_hole) dir = CLOCKWISE;
    insert_vertex_half_edge_45_pair_into_vector(output, llpt, lrpt, urpt, dir);
    insert_vertex_half_edge_45_pair_into_vector(output, lrpt, urpt, ulpt, dir);
    insert_vertex_half_edge_45_pair_into_vector(output, urpt, ulpt, llpt, dir);
    insert_vertex_half_edge_45_pair_into_vector(output, ulpt, llpt, lrpt, dir);
  }

  template <typename Unit>
  template <typename geometry_type>
  inline void polygon_45_set_data<Unit>::insert_dispatch(const geometry_type& geometry_object, 
                                                         bool is_hole, rectangle_concept ) {
    dirty_ = true;
    unsorted_ = true;
    insert_rectangle_into_vector_45(data_, geometry_object, is_hole);
  }

  // get the external boundary rectangle
  template <typename Unit>
  template <typename rectangle_type>
  inline bool polygon_45_set_data<Unit>::extents(rectangle_type& rect) const{
    clean();
    if(empty()) {
      return false;
    }
    Unit low = (std::numeric_limits<Unit>::max)();
    Unit high = (std::numeric_limits<Unit>::min)();
    interval_data<Unit> xivl(low, high); 
    interval_data<Unit> yivl(low, high);
    for(typename value_type::const_iterator itr = data_.begin();
        itr != data_.end(); ++ itr) {
      if((*itr).pt.x() > xivl.get(HIGH))
        xivl.set(HIGH, (*itr).pt.x());
      if((*itr).pt.x() < xivl.get(LOW))
        xivl.set(LOW, (*itr).pt.x());
      if((*itr).pt.y() > yivl.get(HIGH))
        yivl.set(HIGH, (*itr).pt.y());
      if((*itr).pt.y() < yivl.get(LOW))
        yivl.set(LOW, (*itr).pt.y());
    }
    rect = construct<rectangle_type>(xivl, yivl);
    return true;
  }

  //this function snaps the vertex and two half edges
  //to be both even or both odd coordinate values if one of the edges is 45
  //and throws an excpetion if an edge is non-manhattan, non-45.
  template <typename Unit>
  inline void polygon_45_set_data<Unit>::snap_vertex_45(typename polygon_45_set_data<Unit>::Vertex45Compact& vertex) {
    bool plus45 = vertex.count[2] != 0;
    bool minus45 = vertex.count[0] != 0;
    if(plus45 || minus45) {
      if(abs(vertex.pt.x()) % 2 != abs(vertex.pt.y()) % 2) {
        if(vertex.count[1] != 0 ||
           (plus45 && minus45)) {
          //move right
          vertex.pt.x(vertex.pt.x() + 1);
        } else {
          //assert that vertex.count[3] != 0
          Unit modifier = plus45 ? -1 : 1;
          vertex.pt.y(vertex.pt.y() + modifier);
        }
      }
    }
  }

  template <typename Unit>
  inline void polygon_45_set_data<Unit>::snap() const {
    for(typename value_type::iterator itr = data_.begin();
        itr != data_.end(); ++itr) {
      snap_vertex_45(*itr);
    }
  }

  // |= &= += *= -= ^= binary operators
  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator|=(const polygon_45_set_data<Unit>& b) {
    insert(b);
    return *this;
  }
  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator&=(const polygon_45_set_data<Unit>& b) {
    //b.sort();
    //sort();
    applyAdaptiveBoolean_<1>(b);
    dirty_ = false;
    unsorted_ = false;
    return *this;
  }
  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator+=(const polygon_45_set_data<Unit>& b) {
    return (*this) |= b;
  }
  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator*=(const polygon_45_set_data<Unit>& b) {
    return (*this) &= b;
  }
  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator-=(const polygon_45_set_data<Unit>& b) {
    //b.sort();
    //sort();
    applyAdaptiveBoolean_<2>(b);   
    dirty_ = false;
    unsorted_ = false;
    return *this;
  }
  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator^=(const polygon_45_set_data<Unit>& b) {
    //b.sort();
    //sort();
    applyAdaptiveBoolean_<3>(b);   
    dirty_ = false;
    unsorted_ = false;
    return *this;
  }

  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator+=(Unit delta) {
    return resize(delta);
  }
  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator-=(Unit delta) {
    return (*this) += -delta;
  }

  template <typename Unit>
  inline polygon_45_set_data<Unit>& 
  polygon_45_set_data<Unit>::resize(Unit resizing, RoundingOption rounding, CornerOption corner) {
    if(resizing == 0) return *this;
    std::list<polygon_45_with_holes_data<Unit> > pl;
    get_polygons_with_holes(pl);
    clear();
    for(typename std::list<polygon_45_with_holes_data<Unit> >::iterator itr = pl.begin(); itr != pl.end(); ++itr) {
      insert_with_resize(*itr, resizing, rounding, corner);
    }
    clean();
    //perterb 45 edges to prevent non-integer intersection errors upon boolean op
    //snap();
    return *this;
  }

  //distance is assumed to be positive
  inline int roundClosest(double distance) {
    int f = (int)distance;
    if(distance - (double)f < 0.5) return f;
    return f+1;
  }

  //distance is assumed to be positive
  template <typename Unit>
  inline Unit roundWithOptions(double distance, RoundingOption rounding) {
    if(rounding == CLOSEST) {
      return roundClosest(distance);
    } else if(rounding == OVERSIZE) {
      return (Unit)distance + 1;
    } else { //UNDERSIZE
      return (Unit)distance;
    }
  }

  // 0 is east, 1 is northeast, 2 is north, 3 is northwest, 4 is west, 5 is southwest, 6 is south
  // 7 is southwest
  template <typename Unit>
  inline point_data<Unit> bloatVertexInDirWithOptions(const point_data<Unit>& point, unsigned int dir,
                                                      Unit bloating, RoundingOption rounding) {
    const double sqrt2 = 1.4142135623730950488016887242097;
    if(dir & 1) {
      Unit unitDistance = (Unit)bloating;
      if(rounding != SQRT2) {
        //45 degree bloating
        double distance = (double)bloating;
        distance /= sqrt2;  // multiply by 1/sqrt2
        unitDistance = roundWithOptions<Unit>(distance, rounding);
      }
      int xMultiplier = 1;
      int yMultiplier = 1;
      if(dir == 3 || dir == 5) xMultiplier = -1;
      if(dir == 5 || dir == 7) yMultiplier = -1;
      return point_data<Unit>(point.x()+xMultiplier*unitDistance,
                   point.y()+yMultiplier*unitDistance);
    } else {
      if(dir == 0)
        return point_data<Unit>(point.x()+bloating, point.y());
      if(dir == 2)
        return point_data<Unit>(point.x(), point.y()+bloating);
      if(dir == 4)
        return point_data<Unit>(point.x()-bloating, point.y());
      if(dir == 6)
        return point_data<Unit>(point.x(), point.y()-bloating);
      return point_data<Unit>();
    }
  }

  template <typename Unit>
  inline unsigned int getEdge45Direction(const point_data<Unit>& pt1, const point_data<Unit>& pt2) {
    if(pt1.x() == pt2.x()) {
      if(pt1.y() < pt2.y()) return 2;
      return 6;
    }
    if(pt1.y() == pt2.y()) {
      if(pt1.x() < pt2.x()) return 0;
      return 4;
    }
    if(pt2.y() > pt1.y()) {
      if(pt2.x() > pt1.x()) return 1;
      return 3;
    }
    if(pt2.x() > pt1.x()) return 7;
    return 5;
  }

  inline unsigned int getEdge45NormalDirection(unsigned int dir, int multiplier) {
    if(multiplier < 0)
      return (dir + 2) % 8;
    return (dir + 4 + 2) % 8;
  }

  template <typename Unit>
  inline point_data<Unit> getIntersectionPoint(const point_data<Unit>& pt1, unsigned int slope1,
                                               const point_data<Unit>& pt2, unsigned int slope2) {
    //the intention here is to use all integer arithmetic without causing overflow
    //turncation error or divide by zero error
    //I don't use floating point arithmetic because its precision may not be high enough
    //at the extremes of the integer range
    typedef typename coordinate_traits<Unit>::area_type LongUnit;
    const Unit rises[8] = {0, 1, 1, 1, 0, -1, -1, -1};
    const Unit runs[8] = {1, 1, 0, -1, -1, -1, 0, 1};
    LongUnit rise1 = rises[slope1];
    LongUnit rise2 = rises[slope2];
    LongUnit run1 = runs[slope1];
    LongUnit run2 = runs[slope2];
    LongUnit x1 = (LongUnit)pt1.x();
    LongUnit x2 = (LongUnit)pt2.x();
    LongUnit y1 = (LongUnit)pt1.y();
    LongUnit y2 = (LongUnit)pt2.y();
    Unit x = 0;
    Unit y = 0;
    if(run1 == 0) {
      x = pt1.x();
      y = (Unit)(((x1 - x2) * rise2) / run2) + pt2.y(); 
    } else if(run2 == 0) {
      x = pt2.x();
      y = (Unit)(((x2 - x1) * rise1) / run1) + pt1.y();
    } else {
      // y - y1 = (rise1/run1)(x - x1)
      // y - y2 = (rise2/run2)(x - x2)
      // y = (rise1/run1)(x - x1) + y1 = (rise2/run2)(x - x2) + y2
      // (rise1/run1 - rise2/run2)x = y2 - y1 + rise1/run1 x1 - rise2/run2 x2
      // x = (y2 - y1 + rise1/run1 x1 - rise2/run2 x2)/(rise1/run1 - rise2/run2)
      // x = (y2 - y1 + rise1/run1 x1 - rise2/run2 x2)(rise1 run2 - rise2 run1)/(run1 run2)
      x = (Unit)((y2 - y1 + ((rise1 * x1) / run1) - ((rise2 * x2) / run2)) * 
                 (run1 * run2) / (rise1 * run2 - rise2 * run1));
      if(rise1 == 0) {
        y = pt1.y();
      } else if(rise2 == 0) {
        y = pt2.y();
      } else {
        // y - y1 = (rise1/run1)(x - x1)
        // (run1/rise1)(y - y1) = x - x1
        // x = (run1/rise1)(y - y1) + x1 = (run2/rise2)(y - y2) + x2
        y = (Unit)((x2 - x1 + ((run1 * y1) / rise1) - ((run2 * y2) / rise2)) * 
                   (rise1 * rise2) / (run1 * rise2 - run2 * rise1));
      }
    }
    return point_data<Unit>(x, y);
  }

  template <typename Unit>
  inline
  void handleResizingEdge45_SQRT1OVER2(polygon_45_set_data<Unit>& sizingSet, point_data<Unit> first, 
                                       point_data<Unit> second, Unit resizing, CornerOption corner) {
    if(first.x() == second.x()) {
      sizingSet.insert(rectangle_data<Unit>(first.x() - resizing, first.y(), first.x() + resizing, second.y()));
      return;
    }
    if(first.y() == second.y()) {
      sizingSet.insert(rectangle_data<Unit>(first.x(), first.y() - resizing, second.x(), first.y() + resizing));
      return;
    }
    std::vector<point_data<Unit> > pts;
    Unit bloating = resizing < 0 ? -resizing : resizing;
    if(corner == UNFILLED) {
      //we have to round up
      bloating = bloating / 2 + bloating % 2 ; //round up
      if(second.x() < first.x()) std::swap(first, second);
      if(first.y() < second.y()) { //upward sloping
        pts.push_back(point_data<Unit>(first.x() + bloating, first.y() - bloating));
        pts.push_back(point_data<Unit>(first.x() - bloating, first.y() + bloating));
        pts.push_back(point_data<Unit>(second.x() - bloating, second.y() + bloating));
        pts.push_back(point_data<Unit>(second.x() + bloating, second.y() - bloating));
        sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), CLOCKWISE, false);
      } else { //downward sloping
        pts.push_back(point_data<Unit>(first.x() + bloating, first.y() + bloating));
        pts.push_back(point_data<Unit>(first.x() - bloating, first.y() - bloating));
        pts.push_back(point_data<Unit>(second.x() - bloating, second.y() - bloating));
        pts.push_back(point_data<Unit>(second.x() + bloating, second.y() + bloating));
        sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), COUNTERCLOCKWISE, false);
      }
      return;
    }
    if(second.x() < first.x()) std::swap(first, second);
    if(first.y() < second.y()) { //upward sloping
      pts.push_back(point_data<Unit>(first.x(), first.y() - bloating));
      pts.push_back(point_data<Unit>(first.x() - bloating, first.y()));
      pts.push_back(point_data<Unit>(second.x(), second.y() + bloating));
      pts.push_back(point_data<Unit>(second.x() + bloating, second.y()));
      sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), CLOCKWISE, false);
    } else { //downward sloping
      pts.push_back(point_data<Unit>(first.x() - bloating, first.y()));
      pts.push_back(point_data<Unit>(first.x(), first.y() + bloating));
      pts.push_back(point_data<Unit>(second.x() + bloating, second.y()));
      pts.push_back(point_data<Unit>(second.x(), second.y() - bloating));
      sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), CLOCKWISE, false);
    }
  }


  template <typename Unit>
  inline
  void handleResizingEdge45(polygon_45_set_data<Unit>& sizingSet, point_data<Unit> first, 
                            point_data<Unit> second, Unit resizing, RoundingOption rounding) {
    if(first.x() == second.x()) {
      sizingSet.insert(rectangle_data<int>(first.x() - resizing, first.y(), first.x() + resizing, second.y()));
      return;
    }
    if(first.y() == second.y()) {
      sizingSet.insert(rectangle_data<int>(first.x(), first.y() - resizing, second.x(), first.y() + resizing));
      return;
    }
    //edge is 45
    std::vector<point_data<Unit> > pts;
    Unit bloating = resizing < 0 ? -resizing : resizing;
    if(second.x() < first.x()) std::swap(first, second);
    if(first.y() < second.y()) {
      pts.push_back(bloatVertexInDirWithOptions(first, 3, bloating, rounding));
      pts.push_back(bloatVertexInDirWithOptions(first, 7, bloating, rounding));
      pts.push_back(bloatVertexInDirWithOptions(second, 7, bloating, rounding));
      pts.push_back(bloatVertexInDirWithOptions(second, 3, bloating, rounding));
      sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), HIGH, false);
    } else {
      pts.push_back(bloatVertexInDirWithOptions(first, 1, bloating, rounding));
      pts.push_back(bloatVertexInDirWithOptions(first, 5, bloating, rounding));
      pts.push_back(bloatVertexInDirWithOptions(second, 5, bloating, rounding));
      pts.push_back(bloatVertexInDirWithOptions(second, 1, bloating, rounding));
      sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), HIGH, false);
    }
  }

  template <typename Unit>
  inline point_data<Unit> bloatVertexInDirWithSQRT1OVER2(int edge1, int normal1, const point_data<Unit>& second, Unit bloating,
                                                         bool first) {
    orientation_2d orient = first ? HORIZONTAL : VERTICAL;
    orientation_2d orientp = orient.get_perpendicular();
    int multiplier = first ? 1 : -1;
    point_data<Unit> pt1(second);
    if(edge1 == 1) {
      if(normal1 == 3) {
        move(pt1, orient, -multiplier * bloating);
      } else {
        move(pt1, orientp, -multiplier * bloating);
      }
    } else if(edge1 == 3) {
      if(normal1 == 1) {
        move(pt1, orient, multiplier * bloating);
      } else {
        move(pt1, orientp, -multiplier * bloating);
      }
    } else if(edge1 == 5) {
      if(normal1 == 3) {
        move(pt1, orientp, multiplier * bloating);
      } else {
        move(pt1, orient, multiplier * bloating);
      }
    } else {
      if(normal1 == 5) {
        move(pt1, orient, -multiplier * bloating);
      } else {
        move(pt1, orientp, multiplier * bloating);
      }
    }
    return pt1;
  }

  template <typename Unit>
  inline
  void handleResizingVertex45(polygon_45_set_data<Unit>& sizingSet, const point_data<Unit>& first, 
                              const point_data<Unit>& second, const point_data<Unit>& third, Unit resizing, 
                              RoundingOption rounding, CornerOption corner, 
                              int multiplier) {
    unsigned int edge1 = getEdge45Direction(first, second);
    unsigned int edge2 = getEdge45Direction(second, third);
    unsigned int diffAngle;
    if(multiplier < 0) 
      diffAngle = (edge2 + 8 - edge1) % 8;
    else
      diffAngle = (edge1 + 8 - edge2) % 8;
    if(diffAngle < 4) {
      if(resizing > 0) return; //accute interior corner
      else multiplier *= -1; //make it appear to be an accute exterior angle
    }
    Unit bloating = abs(resizing);
    if(rounding == SQRT1OVER2) {
      if(edge1 % 2 && edge2 % 2) return; 
      if(corner == ORTHOGONAL && edge1 % 2 == 0 && edge2 % 2 == 0) {
        rectangle_data<Unit> insertion_rect;
        set_points(insertion_rect, second, second);
        bloat(insertion_rect, bloating);
        sizingSet.insert(insertion_rect);
      } else if(corner != ORTHOGONAL) {
        point_data<Unit> pt1(0, 0);
        point_data<Unit> pt2(0, 0);
        unsigned int normal1 = getEdge45NormalDirection(edge1, multiplier);
        unsigned int normal2 = getEdge45NormalDirection(edge2, multiplier);
        if(edge1 % 2) {
          pt1 = bloatVertexInDirWithSQRT1OVER2(edge1, normal1, second, bloating, true);
        } else {
          pt1 = bloatVertexInDirWithOptions(second, normal1, bloating, UNDERSIZE);
        }
        if(edge2 % 2) {
          pt2 = bloatVertexInDirWithSQRT1OVER2(edge2, normal2, second, bloating, false);
        } else {
          pt2 = bloatVertexInDirWithOptions(second, normal2, bloating, UNDERSIZE);
        }
        std::vector<point_data<Unit> > pts;
        pts.push_back(pt1);
        pts.push_back(second);
        pts.push_back(pt2);
        pts.push_back(getIntersectionPoint(pt1, edge1, pt2, edge2));
        polygon_45_data<Unit> poly(pts.begin(), pts.end());
        sizingSet.insert(poly);
      } else {
        //ORTHOGONAL of a 45 degree corner
        int normal = 0;
        if(edge1 % 2) {
          normal = getEdge45NormalDirection(edge2, multiplier);
        } else {
          normal = getEdge45NormalDirection(edge1, multiplier);
        }
        rectangle_data<Unit> insertion_rect;
        point_data<Unit> edgePoint = bloatVertexInDirWithOptions(second, normal, bloating, UNDERSIZE);
        set_points(insertion_rect, second, edgePoint);
        if(normal == 0 || normal == 4)
          bloat(insertion_rect, VERTICAL, bloating);
        else
          bloat(insertion_rect, HORIZONTAL, bloating);
        sizingSet.insert(insertion_rect);
      }
      return;
    }
    unsigned int normal1 = getEdge45NormalDirection(edge1, multiplier);
    unsigned int normal2 = getEdge45NormalDirection(edge2, multiplier);
    point_data<Unit> edgePoint1 = bloatVertexInDirWithOptions(second, normal1, bloating, rounding);
    point_data<Unit> edgePoint2 = bloatVertexInDirWithOptions(second, normal2, bloating, rounding);
    //if the change in angle is 135 degrees it is an accute exterior corner
    if((edge1+ multiplier * 3) % 8 == edge2) {
      if(corner == ORTHOGONAL) {
        rectangle_data<Unit> insertion_rect;
        set_points(insertion_rect, edgePoint1, edgePoint2);
        sizingSet.insert(insertion_rect);
        return;
      }
    } 
    std::vector<point_data<Unit> > pts;
    pts.push_back(edgePoint1);
    pts.push_back(second);
    pts.push_back(edgePoint2);
    pts.push_back(getIntersectionPoint(edgePoint1, edge1, edgePoint2, edge2));
    polygon_45_data<Unit> poly(pts.begin(), pts.end());
    sizingSet.insert(poly);
  }

  template <typename Unit>
  template <typename geometry_type>
  inline polygon_45_set_data<Unit>& 
  polygon_45_set_data<Unit>::insert_with_resize_dispatch(const geometry_type& poly,
                                                         coordinate_type resizing, 
                                                         RoundingOption rounding,
                                                         CornerOption corner,
                                                         bool hole, polygon_45_concept ) {
    direction_1d wdir = winding(poly);
    int multiplier = wdir == LOW ? -1 : 1;
    if(hole) resizing *= -1; 
    typedef typename polygon_45_data<Unit>::iterator_type piterator;
    piterator first, second, third, end, real_end;
    real_end = end_points(poly);
    third = begin_points(poly);
    first = third;
    if(first == real_end) return *this;
    ++third;
    if(third == real_end) return *this;
    second = end = third;
    ++third;
    if(third == real_end) return *this;
    polygon_45_set_data<Unit> sizingSet;
    //insert minkofski shapes on edges and corners
    do {
      if(rounding != SQRT1OVER2) {
        handleResizingEdge45(sizingSet, *first, *second, resizing, rounding);
      } else {
        handleResizingEdge45_SQRT1OVER2(sizingSet, *first, *second, resizing, corner);
      }        
      if(corner != UNFILLED) 
        handleResizingVertex45(sizingSet, *first, *second, *third, resizing, rounding, corner, multiplier);
      first = second;
      second = third;
      ++third;
      if(third == real_end) {
        third = begin_points(poly);
        if(*second == *third) {
          ++third; //skip first point if it is duplicate of last point
        }
      }
    } while(second != end);
    //sizingSet.snap();
    polygon_45_set_data<Unit> tmp;
    //insert original shape
    tmp.insert_dispatch(poly, false, polygon_45_concept());
    if(resizing < 0) tmp -= sizingSet;
    else tmp += sizingSet;
    tmp.clean();
    insert(tmp, hole);
    dirty_ = true;
    unsorted_ = true;
    return (*this);
  }

  // accumulate the bloated polygon with holes
  template <typename Unit>
  template <typename geometry_type>
  inline polygon_45_set_data<Unit>&
  polygon_45_set_data<Unit>::insert_with_resize_dispatch(const geometry_type& poly,
                                                         coordinate_type resizing, 
                                                         RoundingOption rounding,
                                                         CornerOption corner, 
                                                         bool hole, polygon_45_with_holes_concept ) {
    insert_with_resize_dispatch(poly, resizing, rounding, corner, hole, polygon_45_concept());
    for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
          begin_holes(poly); itr != end_holes(poly);
        ++itr) {
      insert_with_resize_dispatch(*itr, resizing, rounding, corner, !hole, polygon_45_concept());
    }
    return *this;
  }

  // transform set
  template <typename Unit>
  template <typename transformation_type>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::transform(const transformation_type& tr){
    clean();
    std::vector<polygon_45_with_holes_data<Unit> > polys;
    get(polys);
    for(typename std::vector<polygon_45_with_holes_data<Unit> >::iterator itr = polys.begin();
        itr != polys.end(); ++itr) {
      ::boost::polygon::transform(*itr, tr);
    }
    clear();
    insert(polys.begin(), polys.end());
    dirty_ = true; 
    unsorted_ = true;
    return *this;
  }
    
  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::scale_up(typename coordinate_traits<Unit>::unsigned_area_type factor) {
    scale_up_vertex_45_compact_range(data_.begin(), data_.end(), factor);
    return *this;
  }

  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::scale_down(typename coordinate_traits<Unit>::unsigned_area_type factor) {
    clean();
    std::vector<polygon_45_with_holes_data<Unit> > polys;
    get_polygons_with_holes(polys);
    for(typename std::vector<polygon_45_with_holes_data<Unit> >::iterator itr = polys.begin();
        itr != polys.end(); ++itr) {
      ::boost::polygon::scale_down(*itr, factor);
    }
    clear();
    insert(polys.begin(), polys.end());
    dirty_ = true; 
    unsorted_ = true;
    return *this;
  }

  template <typename Unit>
  inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::scale(double factor) {
    clean();
    std::vector<polygon_45_with_holes_data<Unit> > polys;
    get_polygons_with_holes(polys);
    for(typename std::vector<polygon_45_with_holes_data<Unit> >::iterator itr = polys.begin();
        itr != polys.end(); ++itr) {
      ::boost::polygon::scale(*itr, factor);
    }
    clear();
    insert(polys.begin(), polys.end());
    dirty_ = true; 
    unsorted_ = true;
    return *this;
  }

  template <typename Unit>
  inline bool polygon_45_set_data<Unit>::clean() const {
    if(unsorted_) sort();
    if(dirty_) {
      applyAdaptiveUnary_<0>();
      dirty_ = false;
    }
    return true;
  }

  template <typename Unit>
  template <int op>
  inline void polygon_45_set_data<Unit>::applyAdaptiveBoolean_(const polygon_45_set_data<Unit>& rvalue) const {
    polygon_45_set_data<Unit> tmp;
    applyAdaptiveBoolean_<op>(tmp, rvalue);
    data_.swap(tmp.data_); //swapping vectors should be constant time operation
    error_data_.swap(tmp.error_data_);
    is_manhattan_ = tmp.is_manhattan_;
    unsorted_ = false;
    dirty_ = false;
  }

  template <typename Unit2, int op>
  bool applyBoolean45OpOnVectors(std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& result_data,
                                 std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& lvalue_data,
                                 std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& rvalue_data
                                 ) {
    bool result_is_manhattan_ = true;
    typename boolean_op_45<Unit2>::template Scan45<typename boolean_op_45<Unit2>::Count2,
      typename boolean_op_45<Unit2>::template boolean_op_45_output_functor<op> > scan45;
    std::vector<typename boolean_op_45<Unit2>::Vertex45> eventOut;
    typedef std::pair<typename boolean_op_45<Unit2>::Point, 
      typename boolean_op_45<Unit2>::template Scan45CountT<typename boolean_op_45<Unit2>::Count2> > Scan45Vertex;
    std::vector<Scan45Vertex> eventIn;
    typedef std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact> value_type;
    typename value_type::const_iterator iter1 = lvalue_data.begin();
    typename value_type::const_iterator iter2 = rvalue_data.begin();
    typename value_type::const_iterator end1 = lvalue_data.end();
    typename value_type::const_iterator end2 = rvalue_data.end();
    const Unit2 UnitMax = (std::numeric_limits<Unit2>::max)();
    Unit2 x = UnitMax;
    while(iter1 != end1 || iter2 != end2) {
      Unit2 currentX = UnitMax;
      if(iter1 != end1) currentX = iter1->pt.x();
      if(iter2 != end2) currentX = (std::min)(currentX, iter2->pt.x());
      if(currentX != x) {
        //std::cout << "SCAN " << currentX << "\n";
        //scan event
        scan45.scan(eventOut, eventIn.begin(), eventIn.end());
        gtlsort(eventOut.begin(), eventOut.end());
        std::size_t ptCount = 0;
        for(std::size_t i = 0; i < eventOut.size(); ++i) {
          if(!result_data.empty() &&
             result_data.back().pt == eventOut[i].pt) {
            result_data.back().count += eventOut[i];
            ++ptCount;
          } else {
            if(!result_data.empty()) { 
              if(result_data.back().count.is_45()) {
                result_is_manhattan_ = false;
              }
              if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
                result_data.pop_back();
              }
            }
            result_data.push_back(eventOut[i]);
            ptCount = 1;
          }
        }
        if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
          result_data.pop_back();
        }
        eventOut.clear();
        eventIn.clear();
        x = currentX;
      }
      //std::cout << "get next\n";
      if(iter2 != end2 && (iter1 == end1 || iter2->pt.x() < iter1->pt.x() || 
                           (iter2->pt.x() == iter1->pt.x() &&
                            iter2->pt.y() < iter1->pt.y()) )) {
        //std::cout << "case1 next\n";
        eventIn.push_back(Scan45Vertex
                          (iter2->pt, 
                           typename polygon_45_formation<Unit2>::
                           Scan45Count(typename polygon_45_formation<Unit2>::Count2(0, iter2->count[0]),
                                       typename polygon_45_formation<Unit2>::Count2(0, iter2->count[1]),
                                       typename polygon_45_formation<Unit2>::Count2(0, iter2->count[2]),
                                       typename polygon_45_formation<Unit2>::Count2(0, iter2->count[3]))));
        ++iter2;
      } else if(iter1 != end1 && (iter2 == end2 || iter1->pt.x() < iter2->pt.x() || 
                                  (iter1->pt.x() == iter2->pt.x() &&
                                   iter1->pt.y() < iter2->pt.y()) )) {
        //std::cout << "case2 next\n";
        eventIn.push_back(Scan45Vertex
                          (iter1->pt, 
                           typename polygon_45_formation<Unit2>::
                           Scan45Count(
                                       typename polygon_45_formation<Unit2>::Count2(iter1->count[0], 0),
                                       typename polygon_45_formation<Unit2>::Count2(iter1->count[1], 0),
                                       typename polygon_45_formation<Unit2>::Count2(iter1->count[2], 0),
                                       typename polygon_45_formation<Unit2>::Count2(iter1->count[3], 0))));
        ++iter1;
      } else {
        //std::cout << "case3 next\n";
        eventIn.push_back(Scan45Vertex
                          (iter2->pt, 
                           typename polygon_45_formation<Unit2>::
                           Scan45Count(typename polygon_45_formation<Unit2>::Count2(iter1->count[0], 
                                                                                    iter2->count[0]),
                                       typename polygon_45_formation<Unit2>::Count2(iter1->count[1], 
                                                                                    iter2->count[1]),
                                       typename polygon_45_formation<Unit2>::Count2(iter1->count[2], 
                                                                                    iter2->count[2]),
                                       typename polygon_45_formation<Unit2>::Count2(iter1->count[3], 
                                                                                    iter2->count[3]))));
        ++iter1;
        ++iter2;
      }
    }
    scan45.scan(eventOut, eventIn.begin(), eventIn.end());
    gtlsort(eventOut.begin(), eventOut.end());

    std::size_t ptCount = 0;
    for(std::size_t i = 0; i < eventOut.size(); ++i) {
      if(!result_data.empty() &&
         result_data.back().pt == eventOut[i].pt) {
        result_data.back().count += eventOut[i];
        ++ptCount;
      } else {
        if(!result_data.empty()) { 
          if(result_data.back().count.is_45()) {
            result_is_manhattan_ = false;
          }
          if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
            result_data.pop_back();
          }
        }
        result_data.push_back(eventOut[i]);
        ptCount = 1;
      }
    }
    if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
      result_data.pop_back();
    }
    if(!result_data.empty() &&
       result_data.back().count.is_45()) {
      result_is_manhattan_ = false;
    }
    return result_is_manhattan_;
  }

  template <typename Unit2, int op>
  bool applyUnary45OpOnVectors(std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& result_data,
                                 std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& lvalue_data ) {
    bool result_is_manhattan_ = true;
    typename boolean_op_45<Unit2>::template Scan45<typename boolean_op_45<Unit2>::Count1,
      typename boolean_op_45<Unit2>::template unary_op_45_output_functor<op> > scan45;
    std::vector<typename boolean_op_45<Unit2>::Vertex45> eventOut;
    typedef typename boolean_op_45<Unit2>::template Scan45CountT<typename boolean_op_45<Unit2>::Count1> Scan45Count;
    typedef std::pair<typename boolean_op_45<Unit2>::Point, Scan45Count> Scan45Vertex;
    std::vector<Scan45Vertex> eventIn;
    typedef std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact> value_type;
    typename value_type::const_iterator iter1 = lvalue_data.begin();
    typename value_type::const_iterator end1 = lvalue_data.end();
    const Unit2 UnitMax = (std::numeric_limits<Unit2>::max)();
    Unit2 x = UnitMax;
    while(iter1 != end1) {
      Unit2 currentX = iter1->pt.x();
      if(currentX != x) {
        //std::cout << "SCAN " << currentX << "\n";
        //scan event
        scan45.scan(eventOut, eventIn.begin(), eventIn.end());
        gtlsort(eventOut.begin(), eventOut.end());
        std::size_t ptCount = 0;
        for(std::size_t i = 0; i < eventOut.size(); ++i) {
          if(!result_data.empty() &&
             result_data.back().pt == eventOut[i].pt) {
            result_data.back().count += eventOut[i];
            ++ptCount;
          } else {
            if(!result_data.empty()) { 
              if(result_data.back().count.is_45()) {
                result_is_manhattan_ = false;
              }
              if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
                result_data.pop_back();
              }
            }
            result_data.push_back(eventOut[i]);
            ptCount = 1;
          }
        }
        if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
          result_data.pop_back();
        }
        eventOut.clear();
        eventIn.clear();
        x = currentX;
      }
      //std::cout << "get next\n";
      eventIn.push_back(Scan45Vertex
                        (iter1->pt, 
                         Scan45Count( typename boolean_op_45<Unit2>::Count1(iter1->count[0]),
                                      typename boolean_op_45<Unit2>::Count1(iter1->count[1]),
                                      typename boolean_op_45<Unit2>::Count1(iter1->count[2]),
                                      typename boolean_op_45<Unit2>::Count1(iter1->count[3]))));
      ++iter1;
    }
    scan45.scan(eventOut, eventIn.begin(), eventIn.end());
    gtlsort(eventOut.begin(), eventOut.end());

    std::size_t ptCount = 0;
    for(std::size_t i = 0; i < eventOut.size(); ++i) {
      if(!result_data.empty() &&
         result_data.back().pt == eventOut[i].pt) {
        result_data.back().count += eventOut[i];
        ++ptCount;
      } else {
        if(!result_data.empty()) { 
          if(result_data.back().count.is_45()) {
            result_is_manhattan_ = false;
          }
          if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
            result_data.pop_back();
          }
        }
        result_data.push_back(eventOut[i]);
        ptCount = 1;
      }
    }
    if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
      result_data.pop_back();
    }
    if(!result_data.empty() &&
       result_data.back().count.is_45()) {
      result_is_manhattan_ = false;
    }
    return result_is_manhattan_;
  }

  template <typename cT, typename iT> 
  void get_error_rects_shell(cT& posE, cT& negE, iT beginr, iT endr) {
    typedef typename iT::value_type Point;
    typedef typename point_traits<Point>::coordinate_type Unit;
    typedef typename coordinate_traits<Unit>::area_type area_type;
    Point pt1, pt2, pt3;
    bool i1 = true;
    bool i2 = true;
    bool not_done = beginr != endr;
    bool next_to_last = false;
    bool last = false;
    Point first, second;
    while(not_done) {
      if(last) {
        last = false;
        not_done = false;
        pt3 = second;
      } else if(next_to_last) {
        next_to_last = false;
        last = true;
        pt3 = first;
      } else if(i1) {
        const Point& pt = *beginr;
        first = pt1 = pt;
        i1 = false;
        i2 = true;
        ++beginr;
        if(beginr == endr) return; //too few points
        continue;
      } else if (i2) {
        const Point& pt = *beginr;
        second = pt2 = pt;
        i2 = false;
        ++beginr;
        if(beginr == endr) return; //too few points
        continue;
      } else {
        const Point& pt = *beginr;
        pt3 = pt;
        ++beginr;
        if(beginr == endr) { 
          next_to_last = true;
          //skip last point equal to first
          continue;
        }
      }
      if(local_abs(x(pt2)) % 2) { //y % 2 should also be odd
        //is corner concave or convex?
        Point pts[] = {pt1, pt2, pt3};
        area_type ar = point_sequence_area<Point*, area_type>(pts, pts+3);
        direction_1d dir = ar < 0 ? COUNTERCLOCKWISE : CLOCKWISE;
        //std::cout << pt1 << " " << pt2 << " " << pt3 << " " << ar << std::endl;
        if(dir == CLOCKWISE) {
          posE.push_back(rectangle_data<typename Point::coordinate_type>
                         (x(pt2) - 1, y(pt2) - 1, x(pt2) + 1, y(pt2) + 1));
          
        } else {
          negE.push_back(rectangle_data<typename Point::coordinate_type>
                         (x(pt2) - 1, y(pt2) - 1, x(pt2) + 1, y(pt2) + 1));
        }
      }
      pt1 = pt2;
      pt2 = pt3;
    }
  }
    
  template <typename cT, typename pT> 
  void get_error_rects(cT& posE, cT& negE, const pT& p) {
    get_error_rects_shell(posE, negE, p.begin(), p.end());
    for(typename pT::iterator_holes_type iHb = p.begin_holes();
        iHb != p.end_holes(); ++iHb) {
      get_error_rects_shell(posE, negE, iHb->begin(), iHb->end());
    }
  }

  template <typename Unit>
  template <int op>
  inline void polygon_45_set_data<Unit>::applyAdaptiveBoolean_(polygon_45_set_data<Unit>& result,
                                                               const polygon_45_set_data<Unit>& rvalue) const {
    result.clear();
    result.error_data_ = error_data_;
    result.error_data_.insert(result.error_data_.end(), rvalue.error_data_.begin(),
                              rvalue.error_data_.end());
    if(is_manhattan() && rvalue.is_manhattan()) {
      //convert each into polygon_90_set data and call boolean operations
      polygon_90_set_data<Unit> l90sd(VERTICAL), r90sd(VERTICAL), output(VERTICAL);
      for(typename value_type::const_iterator itr = data_.begin(); itr != data_.end(); ++itr) {
        if((*itr).count[3] == 0) continue; //skip all non vertical edges
        l90sd.insert(std::make_pair((*itr).pt.x(), std::make_pair<Unit, int>((*itr).pt.y(), (*itr).count[3])), false, VERTICAL);
      }
      for(typename value_type::const_iterator itr = rvalue.data_.begin(); itr != rvalue.data_.end(); ++itr) {
        if((*itr).count[3] == 0) continue; //skip all non vertical edges
        r90sd.insert(std::make_pair((*itr).pt.x(), std::make_pair<Unit, int>((*itr).pt.y(), (*itr).count[3])), false, VERTICAL);
      }
      l90sd.sort();
      r90sd.sort();
#ifdef BOOST_POLYGON_MSVC
#pragma warning (disable: 4127)
#endif
      if(op == 0) {
        output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
                                    r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryOr>()); 
      } else if (op == 1) {
        output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
                                    r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryAnd>()); 
      } else if (op == 2) {
        output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
                                    r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryNot>()); 
      } else if (op == 3) {
        output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
                                    r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryXor>()); 
      }
#ifdef BOOST_POLYGON_MSVC
#pragma warning (default: 4127)
#endif
      result.data_.clear();
      result.insert(output);
      result.is_manhattan_ = true;
      result.dirty_ = false;
      result.unsorted_ = false;
    } else {
      sort();
      rvalue.sort();
      try {
        result.is_manhattan_ = applyBoolean45OpOnVectors<Unit, op>(result.data_, data_, rvalue.data_);
      } catch (std::string str) {
        std::string msg = "GTL 45 Boolean error, precision insufficient to represent edge intersection coordinate value.";
        if(str == msg) {
          result.clear();
          typedef typename coordinate_traits<Unit>::manhattan_area_type Unit2;
          typedef typename polygon_45_formation<Unit2>::Vertex45Compact Vertex45Compact2;
          typedef std::vector<Vertex45Compact2> Data2;
          Data2 rvalue_data, lvalue_data, result_data;
          rvalue_data.reserve(rvalue.data_.size());
          lvalue_data.reserve(data_.size());
          for(std::size_t i = 0 ; i < data_.size(); ++i) {
            const Vertex45Compact& vi = data_[i];
            Vertex45Compact2 ci; 
            ci.pt = point_data<Unit2>(x(vi.pt), y(vi.pt));
            ci.count = typename polygon_45_formation<Unit2>::Vertex45Count
              ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
            lvalue_data.push_back(ci);
          }
          for(std::size_t i = 0 ; i < rvalue.data_.size(); ++i) {
            const Vertex45Compact& vi = rvalue.data_[i];
            Vertex45Compact2 ci;
            ci.pt = (point_data<Unit2>(x(vi.pt), y(vi.pt)));
            ci.count = typename polygon_45_formation<Unit2>::Vertex45Count
              ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
            rvalue_data.push_back(ci);
          }
          scale_up_vertex_45_compact_range(lvalue_data.begin(), lvalue_data.end(), 2);
          scale_up_vertex_45_compact_range(rvalue_data.begin(), rvalue_data.end(), 2);
          bool result_is_manhattan = applyBoolean45OpOnVectors<Unit2, op>(result_data,
                                                                          lvalue_data,
                                                                          rvalue_data );
          if(!result_is_manhattan) {
            typename polygon_45_formation<Unit2>::Polygon45Formation pf(false);
            //std::cout << "FORMING POLYGONS\n";
            std::vector<polygon_45_with_holes_data<Unit2> > container;
            pf.scan(container, result_data.begin(), result_data.end());
            Data2 error_data_out;
            std::vector<rectangle_data<Unit2> > pos_error_rects;
            std::vector<rectangle_data<Unit2> > neg_error_rects;
            for(std::size_t i = 0; i < container.size(); ++i) {
              get_error_rects(pos_error_rects, neg_error_rects, container[i]);
            }
            for(std::size_t i = 0; i < pos_error_rects.size(); ++i) {
              insert_rectangle_into_vector_45(result_data, pos_error_rects[i], false);
              insert_rectangle_into_vector_45(error_data_out, pos_error_rects[i], false);
            }
            for(std::size_t i = 0; i < neg_error_rects.size(); ++i) {
              insert_rectangle_into_vector_45(result_data, neg_error_rects[i], true);
              insert_rectangle_into_vector_45(error_data_out, neg_error_rects[i], false);
            }
            scale_down_vertex_45_compact_range_blindly(error_data_out.begin(), error_data_out.end(), 2);
            for(std::size_t i = 0 ; i < error_data_out.size(); ++i) {
              const Vertex45Compact2& vi = error_data_out[i];
              Vertex45Compact ci;
              ci.pt.x(static_cast<Unit>(x(vi.pt)));
              ci.pt.y(static_cast<Unit>(y(vi.pt)));
              ci.count = typename polygon_45_formation<Unit>::Vertex45Count
              ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
              result.error_data_.push_back(ci);
            }
            Data2 new_result_data;
            gtlsort(result_data.begin(), result_data.end());
            applyUnary45OpOnVectors<Unit2, 0>(new_result_data, result_data); //OR operation
            result_data.swap(new_result_data);
          }
          scale_down_vertex_45_compact_range_blindly(result_data.begin(), result_data.end(), 2);
          //result.data_.reserve(result_data.size());
          for(std::size_t i = 0 ; i < result_data.size(); ++i) {
            const Vertex45Compact2& vi = result_data[i];
            Vertex45Compact ci;
            ci.pt.x(static_cast<Unit>(x(vi.pt)));
            ci.pt.y(static_cast<Unit>(y(vi.pt)));
            ci.count = typename polygon_45_formation<Unit>::Vertex45Count
              ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
            result.data_.push_back(ci);
          }
          result.is_manhattan_ = result_is_manhattan;
          result.dirty_ = false;
          result.unsorted_ = false;
        } else { throw str; }
      }
      //std::cout << "DONE SCANNING\n";
    }
  }

  template <typename Unit>
  template <int op>
  inline void polygon_45_set_data<Unit>::applyAdaptiveUnary_() const {
    polygon_45_set_data<Unit> result;
    result.error_data_ = error_data_;
    if(is_manhattan()) {
      //convert each into polygon_90_set data and call boolean operations
      polygon_90_set_data<Unit> l90sd(VERTICAL);
      for(typename value_type::const_iterator itr = data_.begin(); itr != data_.end(); ++itr) {
        if((*itr).count[3] == 0) continue; //skip all non vertical edges
        l90sd.insert(std::make_pair((*itr).pt.x(), std::make_pair<Unit, int>((*itr).pt.y(), (*itr).count[3])), false, VERTICAL);
      }
      l90sd.sort();
#ifdef BOOST_POLYGON_MSVC
#pragma warning (disable: 4127)
#endif
      if(op == 0) {
        l90sd.clean();
      } else if (op == 1) {
        l90sd.self_intersect();
      } else if (op == 3) {
        l90sd.self_xor();
      }
#ifdef BOOST_POLYGON_MSVC
#pragma warning (default: 4127)
#endif
      result.data_.clear();
      result.insert(l90sd);
      result.is_manhattan_ = true;
      result.dirty_ = false;
      result.unsorted_ = false;
    } else {
      sort();
      try {
        result.is_manhattan_ = applyUnary45OpOnVectors<Unit, op>(result.data_, data_);
      } catch (std::string str) {
        std::string msg = "GTL 45 Boolean error, precision insufficient to represent edge intersection coordinate value.";
        if(str == msg) {
          result.clear();
          typedef typename coordinate_traits<Unit>::manhattan_area_type Unit2;
          typedef typename polygon_45_formation<Unit2>::Vertex45Compact Vertex45Compact2;
          typedef std::vector<Vertex45Compact2> Data2;
          Data2 lvalue_data, result_data;
          lvalue_data.reserve(data_.size());
          for(std::size_t i = 0 ; i < data_.size(); ++i) {
            const Vertex45Compact& vi = data_[i];
            Vertex45Compact2 ci; 
            ci.pt.x(static_cast<Unit>(x(vi.pt)));
            ci.pt.y(static_cast<Unit>(y(vi.pt)));
            ci.count = typename polygon_45_formation<Unit2>::Vertex45Count
              ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
            lvalue_data.push_back(ci);
          }
          scale_up_vertex_45_compact_range(lvalue_data.begin(), lvalue_data.end(), 2);
          bool result_is_manhattan = applyUnary45OpOnVectors<Unit2, op>(result_data,
                                                                        lvalue_data );
          if(!result_is_manhattan) {
            typename polygon_45_formation<Unit2>::Polygon45Formation pf(false);
            //std::cout << "FORMING POLYGONS\n";
            std::vector<polygon_45_with_holes_data<Unit2> > container;
            pf.scan(container, result_data.begin(), result_data.end());
            Data2 error_data_out;
            std::vector<rectangle_data<Unit2> > pos_error_rects;
            std::vector<rectangle_data<Unit2> > neg_error_rects;
            for(std::size_t i = 0; i < container.size(); ++i) {
              get_error_rects(pos_error_rects, neg_error_rects, container[i]);
            }
            for(std::size_t i = 0; i < pos_error_rects.size(); ++i) {
              insert_rectangle_into_vector_45(result_data, pos_error_rects[i], false);
              insert_rectangle_into_vector_45(error_data_out, pos_error_rects[i], false);
            }
            for(std::size_t i = 0; i < neg_error_rects.size(); ++i) {
              insert_rectangle_into_vector_45(result_data, neg_error_rects[i], true);
              insert_rectangle_into_vector_45(error_data_out, neg_error_rects[i], false);
            }
            scale_down_vertex_45_compact_range_blindly(error_data_out.begin(), error_data_out.end(), 2);
            for(std::size_t i = 0 ; i < error_data_out.size(); ++i) {
              const Vertex45Compact2& vi = error_data_out[i];
              Vertex45Compact ci;
              ci.pt.x(static_cast<Unit>(x(vi.pt)));
              ci.pt.y(static_cast<Unit>(y(vi.pt)));
              ci.count = typename polygon_45_formation<Unit>::Vertex45Count
              ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
              result.error_data_.push_back(ci);
            }
            Data2 new_result_data;
            gtlsort(result_data.begin(), result_data.end());
            applyUnary45OpOnVectors<Unit2, 0>(new_result_data, result_data); //OR operation
            result_data.swap(new_result_data);
          }
          scale_down_vertex_45_compact_range_blindly(result_data.begin(), result_data.end(), 2);
          //result.data_.reserve(result_data.size());
          for(std::size_t i = 0 ; i < result_data.size(); ++i) {
            const Vertex45Compact2& vi = result_data[i];
            Vertex45Compact ci;
            ci.pt.x(static_cast<Unit>(x(vi.pt)));
            ci.pt.y(static_cast<Unit>(y(vi.pt)));
            ci.count = typename polygon_45_formation<Unit>::Vertex45Count
              ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
            result.data_.push_back(ci);
          }
          result.is_manhattan_ = result_is_manhattan;
          result.dirty_ = false;
          result.unsorted_ = false;
        } else { throw str; }
      }
      //std::cout << "DONE SCANNING\n";
    }
    data_.swap(result.data_);
    error_data_.swap(result.error_data_);
    dirty_ = result.dirty_;
    unsorted_ = result.unsorted_;
    is_manhattan_ = result.is_manhattan_;
  }

  template <typename coordinate_type, typename property_type>
  class property_merge_45 {
  private:
    typedef typename coordinate_traits<coordinate_type>::manhattan_area_type big_coord;
    typedef typename polygon_45_property_merge<big_coord, property_type>::MergeSetData tsd;
    tsd tsd_;
  public:
    inline property_merge_45() : tsd_() {}
    inline property_merge_45(const property_merge_45& that) : tsd_(that.tsd_) {}
    inline property_merge_45& operator=(const property_merge_45& that) { 
      tsd_ = that.tsd_; 
      return *this;
    }
    
    inline void insert(const polygon_45_set_data<coordinate_type>& ps, property_type property) {
      ps.clean();
      polygon_45_property_merge<big_coord, property_type>::populateMergeSetData(tsd_, ps.begin(), ps.end(), property);
    }
    template <class GeoObjT>
    inline void insert(const GeoObjT& geoObj, property_type property) {
      polygon_45_set_data<coordinate_type> ps;
      ps.insert(geoObj);
      insert(ps, property);
    }

    //merge properties of input geometries and store the resulting geometries of regions
    //with unique sets of merged properties to polygons sets in a map keyed by sets of properties
    // T = std::map<std::set<property_type>, polygon_45_set_data<coordiante_type> > or
    // T = std::map<std::vector<property_type>, polygon_45_set_data<coordiante_type> >
    template <class result_type>
    inline void merge(result_type& result) {
      typedef typename result_type::key_type keytype;
      typedef std::map<keytype, polygon_45_set_data<big_coord> > bigtype;
      bigtype result_big;
      polygon_45_property_merge<big_coord, property_type>::performMerge(result_big, tsd_);
      std::vector<polygon_45_with_holes_data<big_coord> > polys;
      std::vector<rectangle_data<big_coord> > pos_error_rects;
      std::vector<rectangle_data<big_coord> > neg_error_rects;
      for(typename std::map<keytype, polygon_45_set_data<big_coord> >::iterator itr = result_big.begin();
          itr != result_big.end(); ++itr) {
        polys.clear();
        (*itr).second.get(polys);
        for(std::size_t i = 0; i < polys.size(); ++i) {
          get_error_rects(pos_error_rects, neg_error_rects, polys[i]);
        }
        (*itr).second += pos_error_rects;
        (*itr).second -= neg_error_rects;
        (*itr).second.scale_down(2);
        result[(*itr).first].insert((*itr).second);
      }
    }
  };

  //ConnectivityExtraction computes the graph of connectivity between rectangle, polygon and
  //polygon set graph nodes where an edge is created whenever the geometry in two nodes overlap
  template <typename coordinate_type>
  class connectivity_extraction_45 {
  private:
    typedef typename coordinate_traits<coordinate_type>::manhattan_area_type big_coord;
    typedef typename polygon_45_touch<big_coord>::TouchSetData tsd;
    tsd tsd_;
    unsigned int nodeCount_;
  public:
    inline connectivity_extraction_45() : tsd_(), nodeCount_(0) {}
    inline connectivity_extraction_45(const connectivity_extraction_45& that) : tsd_(that.tsd_),
                                                                          nodeCount_(that.nodeCount_) {}
    inline connectivity_extraction_45& operator=(const connectivity_extraction_45& that) { 
      tsd_ = that.tsd_; 
      nodeCount_ = that.nodeCount_; {}
      return *this;
    }
    
    //insert a polygon set graph node, the value returned is the id of the graph node
    inline unsigned int insert(const polygon_45_set_data<coordinate_type>& ps) {
      ps.clean();
      polygon_45_touch<big_coord>::populateTouchSetData(tsd_, ps.begin(), ps.end(), nodeCount_);
      return nodeCount_++;
    }
    template <class GeoObjT>
    inline unsigned int insert(const GeoObjT& geoObj) {
      polygon_45_set_data<coordinate_type> ps;
      ps.insert(geoObj);
      return insert(ps);
    }
    
    //extract connectivity and store the edges in the graph
    //graph must be indexable by graph node id and the indexed value must be a std::set of
    //graph node id
    template <class GraphT>
    inline void extract(GraphT& graph) {
      polygon_45_touch<big_coord>::performTouch(graph, tsd_);
    }
  };
}
}
#endif