polygon_diff.cpp
Go to the documentation of this file.
1 // difference between polygons
2 
3 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
4 #include <CGAL/Polygon_2.h>
5 #include <CGAL/Polygon_with_holes_2.h>
6 #include <CGAL/Boolean_set_operations_2.h>
7 
8 #include "global.h"
9 
10 using namespace std;
11 
12 // exact kernel
13 typedef CGAL::Exact_predicates_exact_constructions_kernel EK;
15 typedef CGAL::Polygon_2<EK> EPolygon_2;
16 typedef CGAL::Polygon_with_holes_2<EK> EPolygon_with_holes_2;
17 typedef std::list<EPolygon_with_holes_2> EPwh_list_2;
18 
19 // main
20 int main(int argc, char* argv[])
21 {
22 /*
23  if (argc == 5)
24  {
25  //cerr<<"program "<<argv[0]<<endl;
26  //cerr<<"domainFilePath "<<argv[1]<<endl;
27  //cerr<<"holeFilePath "<<argv[2]<<endl;
28  //cerr<<"resultFilePath "<<argv[3]<<endl;
29  //cerr<<"communicateFile "<<argv[4]<<endl;
30  domainFilePath = argv[1];
31  holeFilePath = argv[2];
32  resultFilePath = argv[3];
33  communicateFile = argv[4];
34  }
35 //*/
36  cerr<<"diff between polygons...";
37  // set up
38  ifstream ifs(communicateFile.c_str());
39  int process_num = 0;
40  ifs >> process_num;
41  ifs.clear();
42  ifs.close();
43  // load polygons
44  EPolygon_with_holes_2 eDomain;
45  ifs.open(domainFilePath.c_str());
46  ifs >> eDomain;
47  ifs.clear();
48  ifs.close();
49  EPolygon_2 eHole;
50  ifs.open(holeFilePath.c_str());
51  ifs >> eHole;
52  ifs.clear();
53  ifs.close();
54  // add noise or not. avoid numerical error.
55  if (process_num == -1)
56  {
57  // noise domain outer boundary
58  {
59  srand((unsigned)time(NULL));
60  vector<EPoint_2> temp;
61  for (int i = 0; i < eDomain.outer_boundary().size(); i++)
62  temp.push_back(EPoint_2(eDomain.outer_boundary()[i].x() + (double)rand() / RAND_MAX / 10, eDomain.outer_boundary()[i].y() + (double)rand() / RAND_MAX / 10));
63  eDomain.outer_boundary() = EPolygon_2(temp.begin(), temp.end());
64  }
65  // noise hole
66  {
67  srand((unsigned)time(NULL));
68  vector<EPoint_2> temp;
69  for (int i = 0; i < eHole.size(); i++)
70  temp.push_back(EPoint_2(eHole[i].x() + (double)rand() / RAND_MAX / 10, eHole[i].y() + (double)rand() / RAND_MAX / 10));
71  eHole = EPolygon_2(temp.begin(), temp.end());
72  }
73  }
74  // compute difference
75  EPwh_list_2 eList;
76  CGAL::difference(eDomain, eHole, back_inserter(eList));
77  // result
78  if (eList.size() == 0)
79  {
80  ofstream ofs(communicateFile.c_str());
81  ofs << 2 << endl;
82  ofs.clear();
83  ofs.close();
84  return -1;
85  }
86  // get domain result
88  if (eList.size() != 1) // if domain become not continious
89  {
90  auto j = eList.begin();
91  CGAL::Lazy_exact_nt<CGAL::Quotient<CGAL::Gmpq> > max_area = 0;
92  for (auto i = eList.begin(); i != eList.end(); i++)
93  {
94  if ((i->outer_boundary().area())*(i->outer_boundary().area()) > max_area*max_area)
95  {
96  max_area = i->outer_boundary().area();
97  j = i;
98  }
99  }
100  ePass = EPolygon_with_holes_2(*j);
101  }
102  else // domain continious
103  {
104  ePass = EPolygon_with_holes_2(*eList.begin());
105  }
106  // save result and feedback
107  ofstream ofs(communicateFile.c_str());
108  ofs << 1 << endl;
109  ofs.clear();
110  ofs.close();
111  ofs.open(resultFilePath.c_str());
112  ofs << ePass;
113  ofs.clear();
114  ofs.close();
115  // end.
116  cerr << "done." << endl;
117  return 0;
118 }
K::Point_2 Point_2
Definition: global.h:32
std::list< EPolygon_with_holes_2 > EPwh_list_2
EK::Point_2 EPoint_2
CGAL::Exact_predicates_exact_constructions_kernel EK
CGAL::Polygon_2< EK > EPolygon_2
int main(int argc, char *argv[])
const std::string domainFilePath
Definition: global.h:5
const std::string communicateFile
Definition: global.h:8
CGAL::Polygon_with_holes_2< EK > EPolygon_with_holes_2
const std::string holeFilePath
Definition: global.h:6
const std::string resultFilePath
Definition: global.h:7


co_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:00:47