-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.cpp
219 lines (176 loc) · 5.3 KB
/
input.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
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
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <queue>
#include <limits>
#include <map>
#include <utility>
#include <algorithm>
#include <cassert>
#include <sstream>
using namespace std;
const int NOT_SET = -1;
enum Direction{
LEFT,
RIGHT,
NONE
};
struct Point{
double x,y;
Point(double _x, double _y)
: x(_x), y(_y) {}
};
struct SplitInfo{
int left, right, parent;
double lMinDist = NOT_SET, rMinDist = NOT_SET;
Direction dir;
void printSplitInfo(){
cout << "left = " << left << endl;
cout << "right = " << right << endl;
cout << "parent = " << parent << endl;
cout << "dir = " << dir << endl;
cout << "----" << endl << endl;
}
SplitInfo(int _left, int _right, int _parent, Direction _dir)
:left(_left), right(_right), parent(_parent),
lMinDist(double(NOT_SET)), rMinDist(double(NOT_SET)),
dir(_dir) {
//printSplitInfo();
}
string convertToString()
{
vector<double> vec;
vec.push_back((double)left);
vec.push_back((double)right);
vec.push_back((double)parent);
vec.push_back((double)lMinDist);
vec.push_back((double)rMinDist);
vec.push_back((double)dir);
return "hi";
}
};
static vector<Point> pointVector;
static queue<SplitInfo> splitQueue;
static map<int, SplitInfo> parentMap;
static int nextParentId = 0;
double calcDist(Point p1, Point p2){
return sqrt( ((p1.x - p2.x) * (p1.x - p2.x)) +
((p1.y - p2.y) * (p1.y - p2.y)) );
}
//assumes points in pointVector are sorted by x coordinate
void splitter(){
while(!splitQueue.empty()){
SplitInfo splitVec = splitQueue.front();
cout << splitVec.convertToString();
splitQueue.pop();
//base case
if (splitVec.right - splitVec.left < 2){
if (splitVec.right == splitVec.left){
splitVec.lMinDist = splitVec.rMinDist = numeric_limits<double>::max();
} else if (splitVec.right - splitVec.left == 1){
splitVec.lMinDist = splitVec.rMinDist =
calcDist(pointVector[splitVec.left], pointVector[splitVec.right]);
} else {
cerr << "error in splitting indices, right lower than left" << endl;
return;
}
} else {
//split to two vectors and push back into the queue
int mid = (splitVec.right - splitVec.left) / 2;
splitQueue.push(SplitInfo(splitVec.left, splitVec.left + mid, nextParentId, LEFT));
splitQueue.push(SplitInfo(splitVec.left + mid + 1 , splitVec.right, nextParentId, RIGHT));
}
//print info at nextParent Id
//cout << "parent info: " << nextParentId << endl;
//splitVec.printSplitInfo();
parentMap.insert(make_pair(nextParentId++, splitVec));
//parentMap[nextParentId] = splitVec;
}
}
double minDist(SplitInfo splitVec){
double upperBound = min(splitVec.lMinDist, splitVec.rMinDist);
vector<Point> candidates;
int mid = (splitVec.left + splitVec.right) / 2;
for(int i = splitVec.left; i < splitVec.right + 1; i++){
if(abs(pointVector[i].x - pointVector[mid].x) < upperBound){
candidates.push_back(pointVector[i]);
}
}
sort(candidates.begin(), candidates.end(), [](Point p1, Point p2){ return p1.y < p2.y; });
double candMin = upperBound;
for (int i = 0; i < candidates.size(); i++){
for (int j = i + 1; j < candidates.size() && candidates[j].y - candidates[i].y < candMin; j++){
if (calcDist(candidates[i], candidates[j]) < candMin)
candMin = calcDist(candidates[i], candidates[j]);
}
}
return min(upperBound, candMin);
}
double combiner(){
while (parentMap.size() > 1){
//if left and right are not filled out, push to end
auto itr = parentMap.begin();
while(itr != parentMap.end()){
if(itr->second.lMinDist != NOT_SET && itr->second.rMinDist != NOT_SET
&& itr->second.parent != 0){
//calculate minDist
double min = minDist(itr->second);
//update parent
int parentNum = itr->second.parent;
if(itr->second.dir == LEFT){
parentMap.at(parentNum).lMinDist = min;
} else if (itr->second.dir == RIGHT){
parentMap.at(parentNum).rMinDist = min;
}
parentMap.erase(itr++);
} else {
++itr;
}
}
}
//last node should be the parent
auto itr = parentMap.begin();
assert(itr->second.lMinDist != NOT_SET && itr->second.rMinDist != NOT_SET);
return minDist(itr->second);
}
//#define shift argc--,argv++
int main(int argc, char** argv)
{
//get vector points from command line
string filename;
cout << argv[0] << endl;
cout << argv[1] << endl;
cout << argc << endl;
while(argc != 2){
cout << "give only a single filename as input" << endl;
}
filename = argv[1];
double x,y;
ifstream in;
in.open(filename);
while(in >> x >> y){
cout << x << y << endl;
pointVector.push_back(Point(x, y));
}
//debugging, viewing output
for(int i = 0; i != pointVector.size(); i++){
cout << pointVector[i].x << " " << pointVector[i].y << endl;
}
//remember to sort nodes by x coordinate
sort(pointVector.begin(), pointVector.end(), [](Point p1, Point p2){ return p1.x < p2.x; });
//put first node into split queue
SplitInfo start(0, pointVector.size() - 1, nextParentId++, NONE);
splitQueue.push(start);
splitter();
//print parent map
for(auto it = parentMap.begin(); it != parentMap.end(); it++){
cout << "parent id #: " << it->first << endl;
it->second.printSplitInfo();
cout << endl;
}
//TODO: test combiner
double minDistance = combiner();
cout << minDistance << endl;
return 0;
}