This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
focus.cpp
157 lines (138 loc) · 3.79 KB
/
focus.cpp
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
/**
* focus.cpp
*
* @date Nov 4, 2015
* @author Ryan Peach
* @version v0.1
*/
#include "focus.hpp"
//#define TEST
int Fp::findInnerBorder(vector<cnt> cnts, double angleTol, double distRatio) {
#ifdef TEST
cout << "Running Fp::findInnerBorder" << endl;
#endif
for (int x = cnts.size()-1; x > 0; x--) {
if (isSquare(cnts[x], angleTol, distRatio)) {return x;}
}
return -1;
}
Fp::Fp(vector<cnt> conts, cnt cont, Point cent, int d, int s)
: contours(conts), contour(cont), center(cent), depth(d), shape(s)
{}
Fp::Fp() {
center = Point(0,0);
depth = 0;
shape = 0;
}
Fp::Fp(vector<cnt> conts, double angleTol, double distRatio) : contours(conts)
{
#ifdef TEST
cout << "Initializing Fp..." << endl;
#endif
center = centroid(contours);
depth = Fp::findInnerBorder(contours,angleTol,distRatio);
if (depth!=-1) {
contour = contours[depth];
shape = contour.size();
}
else {
contour = cnt();
shape = 0;
}
}
Fp::Fp(vector<cnt> conts) {Fp(conts, 10.0, 5.0);}
Fp Fp::copy() {
return Fp(contours, contour, center, depth, shape);
}
bool Fp::isValid() {return shape > 0;}
bool Fp::operator==(Fp newFp) {
#ifdef TEST
cout << "Running Fp::operator==..." << endl;
#endif
bool test1 = (contours == newFp.contours);
bool test2 = (contour == newFp.contour);
bool test3 = (center == newFp.center);
bool test4 = (depth == newFp.depth);
bool test5 = (shape == newFp.shape);
return test1 && test2 && test3 && test4 && test5;
}
bool Fp::operator!=(Fp newFp) {return !((*this) == newFp);}
string tostr(Fp fp) {return tostr(fp.center);}
Point centroid(vector<Fp> fps) {
#ifdef TEST
cout << "Running centroid(vector<Fp>)..." << endl;
#endif
cnt out;
for (Fp f : fps) {
out.push_back(f.center);
}
return centroid(out);
}
Point centroid(Fp f) {
return f.center;
}
cnt centroids(vector<Fp> fps) {
#ifdef TEST
cout << "Running centroids(vector<Fp>)..." << endl;
#endif
cnt out;
for (Fp fp : fps) {
out.push_back(centroid(fp));
}
return out;
}
bool allInside(cnt poly, vector<Fp> fps) {
#ifdef TEST
cout << "Running allInside..." << endl;
#endif
for (Fp f : fps) {
if (pointPolygonTest(poly, f.center, false) < 0) {
return false;
}
}
return true;
}
//Could be a template
vector<vector<Fp>> hasRectangles(vector<Fp> fps, double angleTol, double distRatio, int n) {
#ifdef TEST
cout << "Running hasRectangles(vector<Fp>...) ..." << endl;
#endif
cout << fps.size() << endl;
if (fps.size()<4 || n < 1) {return vector<vector<Fp>>{{}};}
cout << "here" << endl;
// Check all combinations of poly
vector<vector<Fp>> out;
for (Fp a1 : fps) {
for (Fp a2 : fps) {
for (Fp a3 : fps) {
for (Fp a4 : fps) {
if (a1 != a2 && a1 != a3 && a1 != a4 && a2 != a3 && a2 != a4 && a3 != a4) {
cout << "hasRectangles: " << n << endl;
vector<Fp> found = vector<Fp>{a1, a2, a3, a4};
if (isRectangle(centroids(found), false, angleTol, distRatio)) {
out.push_back(found); n--;
if (n <= 0) {return out;}
}}}}}}
return out;
}
vector<Fp> hasRectangle(vector<Fp> fps, double angleTol, double distRatio) {
return hasRectangles(fps, angleTol, distRatio, 1)[0];
}
vector<double> angs(Point x, vector<Fp> fours) {
return angs(x, centroids(fours));
}
vector<Fp> toFps(cnt cents, vector<Fp> set){
vector<Fp> out; vector<Fp> next = set;
for (Point x : cents) {
for (Fp f : set) {
if (f.center == x) {
out.push_back(f);
break;
}
else {next.push_back(f);}
}
set = next;
}
if (out.size() != cents.size()) {cout<<"ERROR toFps"<<endl;}
return out;
}