Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_html_demo_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11*/
12
22#include <algorithm>
23#include <cstdint>
24#include <cstdlib>
25#include <iostream>
26#include <limits>
27#include <string>
28
29#include <ca-html-player.H>
30#include <ca-kernels.H>
31#include <ca-rng.H>
33#include <tpl_ca_engine.H>
34#include <tpl_ca_lattice.H>
35#include <tpl_ca_neighborhood.H>
36#include <tpl_ca_storage.H>
37
38using namespace Aleph::CA;
39
40namespace
41{
44
45std::size_t parse_size(const char *text, const std::size_t fallback)
46{
47 if (text == nullptr)
48 return fallback;
49 char *end = nullptr;
50 const unsigned long value = std::strtoul(text, &end, 10);
51 if (end == text or *end != '\0' or value == 0
52 or value > std::numeric_limits<std::size_t>::max())
53 return fallback;
54 return static_cast<std::size_t>(value);
55}
56
57double seeded_noise(const std::uint64_t master, const Coord_Vec<2> &coord)
58{
59 const std::uint64_t h = cell_seed<2>(master, 0, coord);
60 return static_cast<double>(h >> 11) * (1.0 / 9007199254740992.0) - 0.5;
61}
62
64{
65 constexpr std::uint64_t seed = 0x5eed1234abcddcbaull;
66 const ca_size_t rows = lat.size(0);
67 const ca_size_t cols = lat.size(1);
68 // Clamp the seed patch to fit the lattice so r0/c0 cannot underflow
69 // when the user passes dimensions smaller than the default 8.
70 ca_size_t side = std::max<ca_size_t>(8, std::min(rows, cols) / 4);
71 side = std::min(side, std::min(rows, cols));
72 const ca_size_t r0 = rows / 2 - side / 2;
73 const ca_size_t c0 = cols / 2 - side / 2;
74
75 for (ca_size_t r = r0; r < r0 + side; ++r)
76 for (ca_size_t c = c0; c < c0 + side; ++c)
77 {
78 const Coord_Vec<2> coord{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)};
79 const double z = 0.03 * seeded_noise(seed, coord);
80 lat.set(coord, Cell{0.50 + z, 0.25 - z});
81 }
82}
83
84std::uint8_t byte(const double value)
85{
86 const double x = std::clamp(value, 0.0, 1.0);
87 return static_cast<std::uint8_t>(255.0 * x + 0.5);
88}
89
90struct Gray_Scott_Colour
91{
92 [[nodiscard]] RGB8 operator()(const Cell &cell) const noexcept
93 {
94 return RGB8{byte(2.3 * cell.v),
95 byte(1.35 * (1.0 - cell.u)),
96 byte(0.25 + 1.7 * (cell.u - cell.v))};
97 }
98};
99}
100
101int main(int argc, char **argv)
102{
103 const std::string output = argc >= 2 ? argv[1] : "gray_scott.html";
104 const std::size_t steps = argc >= 3 ? parse_size(argv[2], 360) : 360;
105 const ca_size_t n = static_cast<ca_size_t>(argc >= 4 ? parse_size(argv[3], 72) : 72);
106
107 Gray_Lattice lat({n, n}, Cell{1.0, 0.0});
109
110 Gray_Scott_Rule<double> rule(/*feed=*/0.0367,
111 /*kill=*/0.0649,
112 /*du=*/0.16,
113 /*dv=*/0.08);
115 engine(lat, rule);
116
118 opts.title = "Gray-Scott reaction diffusion";
119 opts.fps = 18;
120 opts.cell_size = n <= 80 ? 7 : 4;
121 HTML_Player_Frame_Sink html(output, Gray_Scott_Colour{}, opts);
122
123 const std::size_t stride = std::max<std::size_t>(1, steps / 48);
124 for (std::size_t step = 0; step <= steps; ++step)
125 {
126 if (step % stride == 0)
127 html.accept(engine.steps_run(), engine.frame());
128 if (step != steps)
129 engine.step();
130 }
131 html.flush();
132
133 std::cout << "Wrote " << html.size() << " Gray-Scott frames to " << output << '\n';
134 return 0;
135}
int main()
long double h
Definition btreepic.C:154
static int cell(const aleph_ca_engine_t *e, size_t r, size_t c)
Definition c_abi_smoke.c:53
size_t steps
Definition ca-c-api.h:126
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t * rows
Definition ca-c-api.h:112
size_t cols
Definition ca-c-api.h:105
Self-contained HTML trajectory player for CA frames.
Fixed 2-D convolution kernels for continuous cellular automata.
Reproducible random-number support for stochastic CA rules (Phase 8).
Gray-Scott reaction-diffusion rule.
Sink that writes a self-contained HTML playback file.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Synchronous double-buffered engine.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Definition ca-traits.H:69
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
Options for generated HTML players.
std::string title
document title
Zero-gradient (Neumann) boundary.
Definition ca-traits.H:152
RGB byte triplet used by PPM exporters.
Definition ca-io.H:85
Two-field state for reaction-diffusion cellular automata.
ValueArg< size_t > seed
Definition testHash.C:53
static mt19937 engine
gsl_rng * r
Continuous and memory-bearing CA rules (Phase 9).
Synchronous double-buffered engine for cellular automata.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).
ofstream output
Definition writeHeap.C:215