-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorization.cpp
More file actions
executable file
·201 lines (174 loc) · 4.83 KB
/
vectorization.cpp
File metadata and controls
executable file
·201 lines (174 loc) · 4.83 KB
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
//============================================================================
// Name : vector_new.cpp
// Author : Dingkang Wang
// Version : 1.0.0
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <string.h>
#include <sys/uio.h>
#include <dirent.h>
#include <stdlib.h>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <cassert>
using namespace std;
/*
*get all file names with path from {path}. and save them to {files}. exd is the file extension (.txt, .swc)
*/
void getFiles( string path, string exd, vector<string>& files )
{
//long hFile = 0;
DIR * pDir;
struct dirent* ptr;
string pathName, exdName;
if (0 != strcmp(exd.c_str(), ""))
{
exdName = "/*." + exd;
}
else
{
exdName = "/*";
}
if (!(pDir = opendir(path.c_str()))){
cout << " Folder doesn't Exist! " << endl;
return ;
}
while ((ptr = readdir(pDir))!= 0 ) {
string thefname = ptr->d_name;
if (strcmp(ptr->d_name, "." ) != 0 && strcmp(ptr->d_name, ".." ) != 0 && thefname.at(0) != ' '){
files.push_back(path + "/" + ptr-> d_name);
}
}
closedir(pDir);
}
vector<string> readFiles(string filePath){
vector<string> files;
getFiles(filePath, "pdg", files);
return files;
}
int getIndex(string fileName){
int slashIndex = fileName.find_last_of('/');
string name = fileName.substr(slashIndex+1);
int underscoreIndex = name.find_first_of('_');
int index = atoi(name.substr(0,underscoreIndex).c_str());
return index;
}
vector<string> getFixIndexFiles(vector<string> files){
vector<string> filesFixIndex;
filesFixIndex.resize(files.size());
for(unsigned int i=0;i<files.size();i++){
string next = files[i];
int index = getIndex(next);
filesFixIndex[index-1] = next;
}
return filesFixIndex;
}
vector<vector<double> > readDiagram(string file){
vector<vector<double> > diagram;
std::ifstream ifs;
ifs.open(file.c_str());
string line;
double x, y;
vector<double> X; // x coordinate
vector<double> Y; // y coordinate
while(getline(ifs, line)){
istringstream iss(line);
if(iss >> x >> y){
X.push_back(abs(x));
Y.push_back(abs(y));
}
}
diagram.push_back(X);
diagram.push_back(Y);
ifs.close();
ifs.clear();
return diagram;
}
double get_max_range(vector<vector<double> > diagram) {
double max_range = -1e8;
for(auto x : diagram[0]){
max_range = max(max_range, x);
}
return max_range;
}
double get_min_range(vector<vector<double> > diagram) {
double min_range = 1e8;
for(auto x : diagram[0]) {
min_range = min(min_range, x);
}
return min_range;
}
vector<double> diagram_to_vec(vector<vector<double> > diagram, int vec_len, double sigma, double min_range, double max_range ) {
vector<double> vec;
double upBound = max_range;
double lowBound = min_range;
double interval = (upBound - lowBound) / (vec_len-1);
sigma = 2 * sigma * sigma;
int j = 0;
for(double pos = lowBound; j < vec_len; pos+=interval, ++j){
double value = 0;
for(size_t i = 0; i < diagram[0].size(); i++){
double alpha = fabs(diagram[0][i] - diagram[1][i]);
double dist = diagram[0][i] - pos;
value += alpha * exp(-(dist * dist) / sigma);
}
vec.push_back(value);
}
return vec;
}
void print_vec(vector<double> vec, double min_range, double max_range, string filepath) {
ofstream fout;
fout.open(filepath.c_str());
fout << min_range << " " << max_range << endl;
for (auto v : vec) {
fout << v << " ";
}
fout.close();
}
string get_filename(const string& s) {
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
string res = "";
size_t i = s.rfind(sep, s.length());
if (i == string::npos) {
res = s;
} else {
res = s.substr(i + 1);
}
size_t j = res.rfind('.', res.length());
if (j == string::npos) {
return res;
} else {
return res.substr(0, j);
}
}
void calculate(string diagrams_folder, string vector_folder, int vec_len, double sigma){
vector<string> files = readFiles(diagrams_folder);
for(auto file : files) {
vector<vector<double> > diagram = readDiagram(file);
double min_range = get_min_range(diagram);
double max_range = get_max_range(diagram);
cout << "Processing " << file << ": min range = " << min_range << ", max range = " << max_range << endl;
vector<double> vector = diagram_to_vec(diagram, vec_len, sigma, min_range, max_range);
string output_path = vector_folder + '/' + get_filename(file) + ".pvec";
print_vec(vector, min_range, max_range, output_path);
}
}
int main(int argc, char **argv){
string diagrams_folder = "temp";
string vector_folder = "Vectors";
double sigma = 50;
int vec_len = 100;
string inputFolder(argv[1]);
string outputFolder(argv[2]);
cout << inputFolder;
inputFolder = inputFolder;
calculate(inputFolder, outputFolder, vec_len, sigma);
}