-
Notifications
You must be signed in to change notification settings - Fork 0
/
symmetryinchaos.cpp
314 lines (277 loc) · 9.99 KB
/
symmetryinchaos.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <SDL/SDL.h>
#include <stdio.h>
#undef main
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>
#include <vector>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#else
#include <cxxopts.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <tuple>
#endif
#include <nlohmann/json.hpp>
using json = nlohmann::json;
class SymmetryGenerator {
public:
SymmetryGenerator(const double &lambda, const double &alpha,
const double &beta, const double &gamma,
const double &delta,
const double &omega, const unsigned int &n, const unsigned int &p)
: _lambda(lambda),
_alpha(alpha),
_beta(beta),
_gamma(gamma),
_delta(delta),
_omega(omega),
_n(n),
_p(p) {}
std::complex<double> iterate(const std::complex<double> &z) {
double zreal = z.real();
double zimag = z.imag();
double znorm = std::norm(z);
double np_factor = 0.0;
// Compute additions power according to book if delta is set
if(_delta != 0.0) {
auto zabs = sqrt(znorm);
double zc = 1.0;
double zd = 0.0;
double ereal = z.real() / zabs;
double eimag = z.imag() / zabs;
for (unsigned int i = 0; i < _n * _p; ++i) {
double za = ereal * zc - eimag * zd;
double zb = ereal * zd + eimag * zc;
zc = za;
zd = zb;
}
np_factor = zc * zabs;
}
for (unsigned int i = 0; i < _n - 2; ++i) {
double za = zreal * z.real() - zimag * z.imag();
double zb = zimag * z.real() + zreal * z.imag();
zreal = za;
zimag = zb;
}
const double zn_real = z.real() * zreal - z.imag() * zimag;
const double p = _lambda + _alpha * znorm + _beta * zn_real + _delta * np_factor;
return std::complex<double>(
p * z.real() + _gamma * zreal - _omega * z.imag(),
p * z.imag() - _gamma * zimag + _omega * z.real());
}
private:
const double _lambda;
const double _alpha;
const double _beta;
const double _gamma;
const double _delta;
const double _omega;
const unsigned int _n;
const unsigned int _p;
};
class SymmetryDrawer {
public:
SymmetryDrawer(SDL_Surface *screen, double extent)
: _surface(screen),
_pixels((Uint32 *)_surface->pixels),
_width(screen->w),
_height(screen->h),
_extent(extent),
_canvas(_width * _height) {
std::fill(_canvas.begin(), _canvas.end(), 0);
}
unsigned short increment(const double &x, const double &y) {
int px = (y / _extent + 0.5) * _width;
int py = (-x / _extent + 0.5) * _height;
if (px >= 0 && px < _width && py >= 0 && py < _height) {
return ++*(_canvas.begin() + py * _width + px);
}
return 0;
}
void colorize(const std::vector<std::array<unsigned char, 3>> palette) {
Uint32 *pix = _pixels;
auto canvas = _canvas.begin();
for (; canvas != _canvas.end(); ++pix, ++canvas) {
unsigned int idx =
std::min(*canvas, (unsigned short)(palette.size() - 1));
*pix = SDL_MapRGB(_surface->format, palette[idx][0],
palette[idx][1], palette[idx][2]);
}
}
void clear() { SDL_FillRect(_surface, NULL, 0x000000); }
private:
SDL_Surface *_surface;
Uint32 *_pixels;
unsigned int _width;
unsigned int _height;
double _extent;
std::vector<unsigned short> _canvas;
};
using TPaletteControlPoint = std::array<double, 4>;
std::vector<std::array<unsigned char, 3>> build_palette(
std::vector<std::array<double, 4>> controlPoints, unsigned int maxval) {
std::vector<std::array<unsigned char, 3>> palette(maxval);
auto controlPoint = controlPoints.begin();
auto color = palette.begin();
double step = 1.0 / maxval;
std::array<double, 4> interval_diff;
std::transform((controlPoint + 1)->begin(), (controlPoint + 1)->end(),
controlPoint->begin(), interval_diff.begin(),
std::minus<double>());
double range_norm = 1.0 / interval_diff[0];
for (double r = 0.0; r < 1.0; r += step, ++color) {
if (r > (*(controlPoint + 1))[0]) {
++controlPoint;
std::transform((controlPoint + 1)->begin(),
(controlPoint + 1)->end(), controlPoint->begin(),
interval_diff.begin(), std::minus<double>());
range_norm = 1.0 / interval_diff[0];
}
double inter = (r - (*controlPoint)[0]) * range_norm;
std::transform(
interval_diff.begin() + 1, interval_diff.end(),
controlPoint->begin() + 1, color->begin(),
[&inter](const double &diff, const double &base) {
return static_cast<unsigned char>(inter * diff + base);
});
}
return palette;
}
class Runner {
public:
Runner(SDL_Surface *screen)
: _screen(screen),
_symmetryGenerator(nullptr),
_symmetryDrawer(nullptr),
_palette(0),
_running(false) {}
void start(
// Generator
const double &lambda, const double &alpha, const double &beta,
const double &gamma, const double &delta, const double &omega, const unsigned int &n, const unsigned int &p,
// Drawer
const double &extent,
// Palette creator
const std::vector<std::array<double, 4>> &controlPoints,
const int maxhit, const long int tick_iterations,
const long int total_iterations) {
_symmetryGenerator = std::make_unique<SymmetryGenerator>(
lambda, alpha, beta, gamma, delta, omega, n, p);
_symmetryDrawer = std::make_unique<SymmetryDrawer>(_screen, extent);
_palette = build_palette(controlPoints, maxhit);
_z = {0.001, 0.002};
// Skip transients
for (unsigned int i = 0; i < 20; ++i) {
_z = _symmetryGenerator->iterate(_z);
}
_tick_iterations = tick_iterations;
_total_iterations = total_iterations;
_running_iterations = 0;
_running = true;
}
void tick() {
for (unsigned long i = 0; i < _tick_iterations; ++i) {
_z = _symmetryGenerator->iterate(_z);
_symmetryDrawer->increment(_z.real(), _z.imag());
}
_symmetryDrawer->colorize(_palette);
SDL_Flip(_screen);
_running_iterations += _tick_iterations;
if (_running_iterations > _total_iterations) {
stop();
}
}
bool running() const { return _running; }
void stop() { _running = false; };
public:
SDL_Surface *_screen;
std::unique_ptr<SymmetryGenerator> _symmetryGenerator;
std::unique_ptr<SymmetryDrawer> _symmetryDrawer;
std::vector<std::array<unsigned char, 3>> _palette;
std::complex<double> _z;
unsigned long int _tick_iterations;
unsigned long int _total_iterations;
unsigned long int _running_iterations;
bool _running;
};
// Global runner
static Runner *runner = nullptr;
#ifdef __EMSCRIPTEN__
void emscripten_mainloop() {
if (SDL_MUSTLOCK(runner->_screen)) SDL_LockSurface(runner->_screen);
if (runner->running()) {
runner->tick();
}
if (SDL_MUSTLOCK(runner->_screen)) SDL_UnlockSurface(runner->_screen);
}
extern "C" {
void launch(const char *json_parameters) {
if (SDL_MUSTLOCK(runner->_screen)) SDL_LockSurface(runner->_screen);
runner->stop();
json dataset = json::parse(std::string(json_parameters));
runner->start(dataset["lambda"].get<double>(), dataset["alpha"].get<double>(),
dataset["beta"].get<double>(), dataset["gamma"].get<double>(),
dataset["delta"].get<double>(),
dataset["omega"].get<double>(), dataset["n"].get<unsigned int>(),
dataset["p"].get<unsigned int>(),
dataset["extent"].get<double>(),
dataset["palette"].get<std::vector<std::array<double, 4>>>(), 1200,
100000, 5E7);
if (SDL_MUSTLOCK(runner->_screen)) SDL_UnlockSurface(runner->_screen);
}
}
#endif
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(1000, 1000, 32, SDL_SWSURFACE);
runner = new Runner(screen);
#ifdef TEST_SDL_LOCK_OPTS
EM_ASM(
"SDL.defaults.copyOnLock = false; SDL.defaults.discardOnLock = true; "
"SDL.defaults.opaqueFrontBuffer = false;");
#endif
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(emscripten_mainloop, 0, true);
#else
cxxopts::Options options("Symmetry in Chaos", "Plots symmetry datasets");
options.add_options()("d,dataset", "Dataset name",
cxxopts::value<std::string>());
auto result = options.parse(argc, argv);
if (result.count("dataset") == 0) {
std::cout << options.help() << std::endl;
return EXIT_FAILURE;
}
std::ifstream f("datasets.json");
json datasets = json::parse(f);
json dataset = datasets[result["dataset"].as<std::string>()];
runner->start(
dataset["lambda"].get<double>(), dataset["alpha"].get<double>(),
dataset["beta"].get<double>(), dataset["gamma"].get<double>(),
dataset["delta"].get<double>(),
dataset["omega"].get<double>(), dataset["n"].get<unsigned int>(),
dataset["p"].get<unsigned int>(),
dataset["extent"].get<double>(),
dataset["palette"].get<std::vector<std::array<double, 4>>>(), 1200,
10000000, 8E7);
if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen);
SDL_Event event;
while (runner->running()) {
runner->tick();
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) {
runner->stop();
}
}
std::cout << "Done" << std::endl;
while (event.type != SDL_QUIT) {
SDL_WaitEvent(&event);
}
if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
SDL_Quit();
#endif
return EXIT_SUCCESS;
}