-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoop.cc
126 lines (122 loc) · 2.63 KB
/
Loop.cc
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
#include "Loop.hh"
#include <iostream>
#include <cmath>
#include "Configurations.hh"
using namespace std;
bool Loop::isCompleted() const {
return completed;
}
void Loop::add(double x, double y) {
add(new Vec2d(x, y));
}
void Loop::add(Vec2d* p) {
add(*p);
}
void Loop::add(Vec2d& p) {
if (head.front().equal(p)) { //Loop complete
completed = true;
midX = (minX + maxX) / 2;
midY = (minY + maxY) / 2;
return;
}
if (minX > p.x)
minX = p.x;
if (maxX < p.x)
maxX = p.x;
if (minY > p.y)
minY = p.y;
if (maxY < p.y)
maxY = p.y;
head.push_back(p);
}
int Loop::size() const {
return head.size();
}
const Vec2d& Loop::at(int i) const{
return head.at(i % head.size());
}
const Vec2d& Loop::front() const {
return head.front();
}
const Vec2d& Loop::back() const {
return head.back();
}
Vec2d& Loop::at(int i) {
return head.at(i % head.size());
}
Vec2d& Loop::front() {
return head.front();
}
Vec2d& Loop::back() {
return head.back();
}
double Loop::getminX() const {
return minX;
}
double Loop::getmaxX() const {
return maxX;
}
double Loop::getminY() const {
return minY;
}
double Loop::getmaxY() const {
return maxY;
}
double Loop::getXsum() const {
return xsum;
}
double Loop::getYsum() const {
return ysum;
}
void Loop::loopSum() {
for (const auto& v : head) {
xsum += v.x;
ysum += v.y;
}
}
void Loop::optimize() {
if (head.size() < 4)
return;
Vec2d p1(head.back()); //position at -1, 0, 1
Vec2d p2(head.front());
Vec2d p3(head.at(1));
if (fabs((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y)) < thr)
head.erase(head.begin());
for (int i = 0; i < head.size() - 2; i++) { //position form 0, 1, 2 to end-2, end-1, end
p1 = head.at(i);
p2 = head.at(i + 1);
p3 = head.at(i + 2);
if (fabs((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y)) < thr) { //in same line
head.erase(head.begin() + (i + 1));
i--;
}
}
p1 = head.at(head.size() - 2); //position at -2, -1, 0
p2 = head.back();
p3 = head.front();
if (fabs((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y)) < thr)
head.pop_back();
for(auto& lp: subLoops)
lp.optimize();
}
bool Loop::checkSubLoop(Loop& lp){
if(lp.getminX() > minX && lp.getmaxX() < maxX && lp.getminY() > minY && lp.getmaxY() < maxY){
subLoops.push_back(lp);
for(auto& l: lp.subLoops){
subLoops.push_back(l);
}
lp.subLoops.clear();
return true;
}
return false;
}
void Loop::print() const {
if (head.size() == 0) {
cout << "null\n";
return;
}
cout << head.at(0);
for (int i = 1; i < head.size(); i++) {
cout << " --> " << head.at(i);
}
}