-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
254 lines (230 loc) · 7.88 KB
/
Copy pathmain.cpp
File metadata and controls
254 lines (230 loc) · 7.88 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "CloudWord.h"
#include "Global.h"
#include "Output.h"
#include "Placer.h"
#include "Sizer.h"
#include <iostream>
#include <time.h>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
#include "Loader.h"
#include "Shuffler.h"
#include <map>
#include "ArgParser.h"
using namespace std;
int main (int argc, char * const argv[]) {
ArgParser::process_arguments(argc, argv);
//okay..here we go
//first, lets just output some nice messages
if (!settings.quiet)
{
cout << "********************************" << endl;
cout << "******* Welcome to Xwall! ******" << endl;
cout << "********************************" << endl;
}
srand(settings.random_seed);
//next, we declare a couple of time related things
double clock_start = clock();
time_t starttime;
struct tm * timeinfo;
//load the startime
time ( &starttime );
timeinfo = localtime (&starttime);
if (!settings.quiet)
{
cout << "Start time: ";
cout << asctime (timeinfo);
//okey dokey then...lets start dealing with the text - first we're gona load it into an array - 1 entry for each line
cout << "Loading Text..." << endl;
}
vector<TextFreq> wordfreqs = Loader::load_file(settings.input);
double highest_freq = 0;
bool normalize = false;
if (settings.frequency_spread > 1)
{
settings.frequency_spread--;
for(vector<TextFreq>::iterator ittf = wordfreqs.begin(); ittf < (wordfreqs.begin() + settings.maximum_words); ++ittf)
{
highest_freq = max((*ittf).freq,highest_freq);
}
normalize = (highest_freq > settings.frequency_spread);
}
//store the average frequency - this will drive the font size of all other words
double frequency_count = 0;
for(vector<TextFreq>::iterator ittf = wordfreqs.begin(); ittf < (wordfreqs.begin() + settings.maximum_words); ++ittf)
{
if ((*ittf).freq >= (highest_freq * (settings.large_word_marker / (double)100))) (*ittf).large_word = true;
if (normalize)
{
(*ittf).freq = ((((*ittf).freq - 1) / (highest_freq -1)) * (settings.frequency_spread - 1)) + 1;
}
frequency_count += (*ittf).freq;
}
double average_frequency = round(frequency_count / settings.maximum_words);
/*
double median_frequency;
if (settings.maximum_words % 2 == 0)
{
//even
frequency_count = 0;
for(vector<TextFreq>::iterator ittf = wordfreqs.begin() + ((settings.maximum_words /2) -1); ittf < (wordfreqs.begin() + ((settings.maximum_words /2) +1)); ++ittf)
{
frequency_count += (*ittf).freq;
}
median_frequency = frequency_count / 2;
}
else
{
//odd
vector<TextFreq>::iterator ittf = wordfreqs.begin() + (settings.maximum_words /2);
median_frequency = (*ittf).freq;
}
*/
//move the words into an vector of cloudword objects
CloudList *words = new CloudList;
words->reserve(wordfreqs.size());
for(vector<TextFreq>::iterator ittf = wordfreqs.begin(); ittf < wordfreqs.end(); ++ittf) {
CloudWord* cloud = new CloudWord((*ittf).text,(*ittf).freq, average_frequency);
cloud->large_word = (*ittf).large_word;
words->push_back(cloud);
}
Sizer *sizer = new Sizer();
double total_size = 0;
double total_width = 0;
double total_height = 0;
if (!settings.quiet) { cout << "Calculating Sizes..." << endl; }
int num_words = 0;
for(itCloudList itcw = words->begin(); itcw < (words->begin() + settings.maximum_words); ++itcw) {
++num_words;
sizer->size_word(*itcw);
total_size+= (*itcw)->area;
total_width += (*itcw)->width();
total_height += (*itcw)->height();
}
settings.h_dictionary_size = ((total_width / settings.maximum_words) + settings.word_border) * 2;
settings.v_dictionary_size = ((total_height / settings.maximum_words) + settings.word_border) * 2;
sort(words->begin(),words->end(), sortbyarea());
double target_area = 0.0f;
for(itCloudList itcw = words->begin(); itcw < (words->begin() + ((settings.maximum_words / 100.f) * settings.pre_placed_words_target)); ++itcw)
{
target_area+= (*itcw)->area;
}
//Shuffler::shuffle(words);
//TESTING REMOVE THIS LINE!!!!!!!!!!!!
//random_shuffle(words->begin(), words->end());
if (!settings.quiet) { cout << "Adding words..." << endl; }
Placer *pic = new Placer(settings.output_width,settings.output_height, target_area);
// //first lets do the preplacement stuff
int i = 0;
bool grew_once = false;
int target_words = trunc((settings.maximum_words / 100.f) * settings.pre_placed_words_target);
bool placed_enough = false;
while (!placed_enough)
{
for(itCloudList itcw = words->begin(); itcw < words->end(); ++itcw)
{
//cout << "Word=" << (*itcw)->text << " letter_count=" << (*itcw)->letters->size() << " letter_1_rects=" << (*itcw)->letters->at(0)->TESTING_letter_rect_count() << endl;
if (pic->_random_placing_mode)
{
i++;
if (i <= settings.maximum_words)
{
if (i > (target_words * 1.05)) break;
if (settings.verbose) { cout << "Preplacing: " << i << ": " << (*itcw)->text << " (freq = " << (*itcw)->occurrences << ", area = " << (*itcw)->area << ")";}
pic->add_word(*itcw);
if (settings.verbose) { cout << endl;}
}
}
else
{
break;
}
}
if (i < (target_words * 0.95))
{
grew_once = true;
settings.size_guestimate *= 1.02;
pic->reset();
i=0;
cout << "upping size estimate to " << settings.size_guestimate << endl;
}
else if ((i > (target_words * 1.05)) && (!grew_once))
{
settings.size_guestimate *= 0.98;
pic->reset();
i=0;
cout << "decreasing size estimate to " << settings.size_guestimate << endl;
}
else
{
placed_enough = true;
}
}
random_shuffle(words->begin()+i, words->begin() + i + ((settings.maximum_words-i)));
for(itCloudList itcw = words->begin()+i; itcw < words->end(); ++itcw)
{
//cout << "Word=" << (*itcw)->text << " letter_count=" << (*itcw)->letters->size() << " letter_1_rects=" << (*itcw)->letters->at(0)->TESTING_letter_rect_count() << endl;
i++;
if (i <= settings.maximum_words)
{
if (settings.verbose) { cout << i << ": " << (*itcw)->text << " (freq = " << (*itcw)->occurrences << ", area = " << (*itcw)->area << ")";}
if ((*itcw)->used) exit(EXIT_FAILURE);
pic->add_word(*itcw);
if (settings.verbose) { cout << endl;}
}
else
{
if (!settings.allow_extra_words) { break; }
//extra words won't have been sized so we need to sort tht out
if (normalize)
{
(*itcw)->occurrences = ((((*itcw)->occurrences - 1) / (highest_freq -1)) * (settings.frequency_spread - 1)) + 1;
}
sizer->size_word(*itcw);
pic->can_grow(false);
if (settings.verbose) { cout << "EXTRA: " << i << ": " << (*itcw)->text << " (" << (*itcw)->occurrences << ")"; }
if (!pic->add_word(*itcw))
{
if (settings.verbose) { cout << "- Full!" << endl; }
break;
}
{
if (settings.verbose) { cout << endl; }
}
}
}
if (!settings.quiet) {
cout << "Drawing Image..." << endl;
}
draw_pic(pic->placed_words());
delete sizer;
if (!settings.quiet)
{
cout << "Finished!" << endl;
cout << "Placed " << pic->placed_words()->words->size() << " words in total." << endl;
}
delete pic;
for (itCloudList word = words->begin(); word != words->end(); word++)
{
delete *word;
}
delete words;
//tell user what time we finished
time_t endtime;
time (&endtime);
timeinfo = localtime(&endtime);
if (!settings.quiet) {
cout << "End time: ";
cout << asctime (timeinfo);
}
//tell user how long they've been waiting
cout << "Time Taken: " << ((clock() - clock_start) / double(CLOCKS_PER_SEC)) << " seconds." << endl;
cout << "Guestimate used: " << settings.size_guestimate << endl;
//uncomment these lines to make the program wait for some input before quitting
//useful when checking for leaks.
//int a;
//cin >> a;
return 0;
}