-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
153 lines (135 loc) · 4.26 KB
/
main.cpp
File metadata and controls
153 lines (135 loc) · 4.26 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
// reading an entire binary file
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <unistd.h>
#include "layout.h"
using namespace std;
void save(const std::string &filename, std::vector<Body> *bodies) {
std::ofstream outfile(filename, std::ofstream::binary);
char block[3 * 4];
int *triplet = (int *)█
for (vector<Body>::iterator body = bodies->begin(); body != bodies->end();
++body) {
triplet[0] = floor(body->pos.x + 0.5);
triplet[1] = floor(body->pos.y + 0.5);
triplet[2] = floor(body->pos.z + 0.5);
// cout << triplet[0] << "," << triplet[1] << "," << triplet[2] << " <-
// node" << endl;
outfile.write(block, 3 * 4);
}
outfile.close();
}
typedef struct {
int *content;
long size;
} FileContent;
FileContent *readFile(const char *fileName) {
streampos size;
char *memblock;
ifstream file(fileName, ios::in | ios::binary | ios::ate);
if (file.is_open()) {
size = file.tellg();
memblock = new char[size];
file.seekg(0, ios::beg);
file.read(memblock, size);
file.close();
FileContent *result = new FileContent();
result->size = size / 4;
result->content = (int *)memblock;
return result;
} else {
return nullptr;
}
}
int getIterationNumberFromPositionFileName(const char *positionFileName) {
cmatch match;
regex pattern(".*?(\\d+)\\.bin$");
regex_match(positionFileName, match, pattern);
if (match.size() == 2) {
try {
return stoi(match[1]) + 1;
} catch (...) {
return 0;
}
}
return 0;
}
int main(int argc, const char *argv[]) {
if (argc < 2) {
cout
<< "Usage: " << endl
<< " " << argv[0] << " links.bin [positions.bin]" << endl
<< "Where" << endl
<< " `links.bin` is a path to the serialized graph. See " << endl
<< " https://github.com/anvaka/ngraph.tobinary for format "
"description"
<< endl
<< " `positions.bin` is optional file with previously saved "
"positions. "
<< endl
<< " This file should match `links.bin` graph, otherwise bad things "
<< endl
<< " will happen" << endl;
return -1;
}
const char *graphFileName = argv[1];
srand(42);
char cwd[1024];
if (getcwd(cwd, 1024) != NULL) {
cout << cwd << endl;
} else {
cout << errno;
}
cout << "Loading links from " << graphFileName << "... " << endl;
FileContent *graphFilePtr = readFile(graphFileName);
if (graphFilePtr == nullptr) {
throw "Could not read links file";
}
FileContent graphFile = *graphFilePtr;
Layout graphLayout;
int startFrom = 0;
if (argc < 3) {
graphLayout.init(graphFile.content, graphFile.size);
cout << "Done. " << endl;
cout << "Loaded " << graphLayout.getBodiesCount() << " bodies;" << endl;
} else {
const char *posFileName = argv[2];
startFrom = getIterationNumberFromPositionFileName(posFileName);
cout << "Loading positions from " << posFileName << "... ";
FileContent *positions = readFile(posFileName);
if (positions == nullptr)
throw "Positions file could not be read";
cout << "Done." << endl;
graphLayout.init(graphFile.content, graphFile.size, positions->content,
positions->size);
cout << "Loaded " << graphLayout.getBodiesCount() << " bodies;" << endl;
}
// TODO: This should be done via arguments, but doing it inline now:
// If current folder containsfil 'weights.bin' we will try to assign
// nodes weights from this file
FileContent *weights = readFile("weights.bin");
if (weights != nullptr) {
cout << "Detected weights.bin file in the current folder." << endl;
cout << "Assuming each node has assigned body weight. Reading weights..."
<< endl;
cout << "Size: " << weights->size;
graphLayout.setBodiesWeight(weights->content);
}
cout << "Starting layout from " << startFrom << " iteration;" << endl;
for (int i = startFrom; i < 10000; ++i) {
cout << "Step " << i << endl;
bool done = graphLayout.step();
if (done) {
cout << "Done!" << endl;
break;
}
if (i % 5 == 0) {
save(std::to_string(i) + ".bin", graphLayout.getBodies());
}
}
save("positions.bin", graphLayout.getBodies());
delete[] graphFile.content;
}