-
Notifications
You must be signed in to change notification settings - Fork 1
/
attractor.hpp
301 lines (246 loc) · 7.86 KB
/
attractor.hpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#ifndef _ATTRACTOR_HPP_
#define _ATTRACTOR_HPP_
#include <vector>
#include <array>
#include <map>
#include <cmath>
#include <chrono>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <cstdint>
class Attractor {
public:
// leaves domain boundary to default
Attractor(int width, int height)
: n_cols{width}, n_rows{height}
{}
// constructor with user defined domain boundary
Attractor(int width, int height, std::map<std::string,double> edges)
: n_cols{width}, n_rows{height}, xmin{edges["xmin"]}, xmax{edges["xmax"]},
ymin{edges["ymin"]}, ymax{edges["ymax"]}
{}
// run iteration at least fixed number of seconds specified
// by the parameter 'seconds'
void run(std::array<double,2> start, double seconds)
{
double x = start[0];
double y = start[1];
std::size_t xint, yint;
freq = std::vector<double>(n_rows * n_cols);
//auto bar = ProgressIndicator(0, n_iter-1, 100);
std::cout << "Setting counter to: " << seconds << "s" << std::endl;
auto time_start = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds;
while( elapsed_seconds.count() < seconds )
{
for( std::size_t i=0; i<1000; i++)
{
iter(x,y);
xint = static_cast<std::size_t>( (x-xmin)/(xmax-xmin)*n_cols );
yint = static_cast<std::size_t>(
std::abs( (y-ymax)/(ymax-ymin)*n_rows )
);
try
{
freq.at(xint + yint*n_cols) += 1.0;
}
catch(...)
{};
//bar.update(i);
}
auto time_end = std::chrono::system_clock::now();
elapsed_seconds = time_end-time_start;
}
// find maximum and normalize
double maxval = *std::max_element(freq.begin(), freq.end());
for(auto it=freq.begin(); it != freq.end(); it++)
{
// penalize unvisited points
*it = *it/maxval;
/*if(*it == 0.0)
*it = -1.0;
else
*it = *it/maxval;
*/
}
}
void data_export(std::string filename)
{
std::ofstream file(filename, std::ios::binary);
if(file.is_open())
{
std::cout << "Writing in progress..." << std::endl;
// header
file << n_cols << " "; // width
file << n_rows << "\n"; // height
float cast;
for(auto x: freq)
{
cast = static_cast<float>(x);
file.write(reinterpret_cast<char*>(&cast), sizeof(cast));
}
file.close();
}
else
std::cout << "Unable to open the file!" << std::endl;
}
// export data to a simple graphics in grayscale PGM
// precision: "8bit" - 0-255 coding values
// "16bit" - 0-65535 coding value
void pgm_export(std::string filename, std::string precision)
{
if( precision == std::string("8bit") )
{
filename += std::string("8bit");
filename += std::string(".pgm");
std::ofstream file(filename, std::ios::binary);
if(file.is_open())
{
std::cout << "Writing in progress..." << std::endl;
// header
file << "P5" << " ";
file << n_cols << " "; // width
file << n_rows << " "; // height
file << UINT8_MAX << "\n"; // max number
for(auto x: freq)
file << static_cast<uint8_t>( x * UINT8_MAX );
file.close();
}
else
throw("Unable to open the file!");
}
else if( precision == std::string("16bit") )
{
filename += std::string("16bit");
filename += std::string(".pgm");
std::ofstream file(filename, std::ios::binary);
if(file.is_open())
{
std::cout << "Writing in progress..." << std::endl;
// header
file << "P5" << " ";
file << n_cols << " "; // width
file << n_rows << " "; // height
file << UINT16_MAX << "\n"; // max number
uint16_t cast;
for(auto x: freq)
{
cast = static_cast<uint16_t>( x * UINT16_MAX );
file.write(reinterpret_cast<char*>(&cast), sizeof(cast));
}
file.close();
}
else
throw("Unable to open the file!");
}
else
throw("Unrecognized precision format!");
}
// pure virtual
virtual void iter(double& x, double& y) = 0;
int n_cols;
int n_rows;
std::vector<double> freq;
protected:
double xmin;
double xmax;
double ymin;
double ymax;
};
/* ----------------------------------- /
/ Cliford attractor definition /
/ ----------------------------------- */
class Clifford: public Attractor {
public:
// delete default constructor
Clifford() = delete;
// constructor with default domain boundary
Clifford(std::array<double,4> param, int width, int height)
: Attractor(width, height), a{param[0]}, b{param[1]},
c{param[2]}, d{param[3]}
{
xmax = 1.0 + std::abs(c);
xmin = -xmax;
ymax = 1.0 + std::abs(d);
ymin = -ymax;
}
// constructor with user-defined domain boundary
Clifford(std::array<double,4> param, int width, int height,
std::map<std::string,double> edges)
: Attractor(width, height, edges), a{param[0]}, b{param[1]},
c{param[2]}, d{param[3]}
{}
void iter(double& x, double& y)
{
double xx = x;
x = c * std::cos( a * x ) + std::sin( a * y );
y = d * std::cos( b * y ) + std::sin( b * xx );
}
private:
double a, b, c, d;
};
/* ----------------------------------- /
/ Peter DeJong attractor definition /
/ ----------------------------------- */
class PeterDeJong: public Attractor {
public:
// delete default constructor
PeterDeJong() = delete;
// constructor with default domain boundary
PeterDeJong(std::array<double,4> param, int width, int height)
: Attractor(width, height), a{param[0]}, b{param[1]},
c{param[2]}, d{param[3]}
{
xmax = 2.0;
xmin = -xmax;
ymax = 2.0;
ymin = -ymax;
}
// constructor with user-defined domain boundary
PeterDeJong(std::array<double,4> param, int width, int height,
std::map<std::string,double> edges)
: Attractor(width, height, edges), a{param[0]}, b{param[1]},
c{param[2]}, d{param[3]}
{}
void iter(double& x, double& y)
{
double xx = x;
x = std::sin( a * y ) - std::cos( b * x );
y = std::sin( c * xx ) - std::cos( d * y );
}
private:
double a, b, c, d;
};
/* ----------------------------------- /
/ Bedhead attractor definition /
/ ----------------------------------- */
class Bedhead: public Attractor {
public:
// default constructor
Bedhead() = delete;
// default domain boundary (loosely defined)
Bedhead(std::array<double,2> param, int width, int height)
: Attractor(width, height), a{param[0]}, b{param[1]}
{
xmin = -4.0;
xmax = 4.0;
ymin = -4.0;
ymax = 4.0;
}
// uder defined domain
Bedhead(std::array<double,2> param, int width, int height,
std::map<std::string, double> edges)
: Attractor(width, height, edges), a{param[0]}, b{param[1]}
{}
void iter(double& x, double& y)
{
double xx = x;
x = std::sin( x * y / b) * y + std::cos( a * x - y );
y = xx + std::sin( y ) / b;
}
private:
double a, b;
};
#endif