Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_visualization_gallery_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
27#include <algorithm>
28#include <cstdlib>
29#include <filesystem>
30#include <fstream>
31#include <iostream>
32#include <limits>
33#include <string>
34#include <utility>
35#include <vector>
36
37#include <ca-ascii.H>
38#include <ca-dot.H>
39#include <ca-engine-utils.H>
40#include <ca-ffmpeg-sink.H>
41#include <ca-gif.H>
42#include <ca-html-player.H>
43#include <ca-npy.H>
44#include <ca-png.H>
45#include <ca-svg.H>
46#include <ca-vtk.H>
47#include <ca-x11-viewer.H>
49#include <tpl_ca_lattice.H>
50#include <tpl_ca_storage.H>
51
52using namespace Aleph::CA;
53
54namespace
55{
57
58std::size_t parse_size(const char *text, const std::size_t fallback)
59{
60 if (text == nullptr)
61 return fallback;
62 char *end = nullptr;
63 const unsigned long value = std::strtoul(text, &end, 10);
64 if (end == text or *end != '\0' or value == 0
65 or value > std::numeric_limits<std::size_t>::max())
66 return fallback;
67 return static_cast<std::size_t>(value);
68}
69
70void stamp(Grid &grid,
71 const ca_index_t row,
72 const ca_index_t col,
73 const std::vector<std::pair<int, int>> &cells)
74{
75 const ca_index_t rows = static_cast<ca_index_t>(grid.size(0));
76 const ca_index_t cols = static_cast<ca_index_t>(grid.size(1));
77 for (const auto &[dr, dc] : cells)
78 {
79 const ca_index_t r = ((row + dr) % rows + rows) % rows;
80 const ca_index_t c = ((col + dc) % cols + cols) % cols;
81 grid.set({r, c}, 1);
82 }
83}
84
85std::vector<std::pair<int, int>> gosper_glider_gun()
86{
87 return {
88 {4,0}, {4,1}, {5,0}, {5,1},
89 {2,12}, {2,13}, {3,11}, {3,15}, {4,10}, {4,16},
90 {5,10}, {5,14}, {5,16}, {5,17}, {6,10}, {6,16},
91 {7,11}, {7,15}, {8,12}, {8,13},
92 {0,24}, {1,22}, {1,24}, {2,20}, {2,21}, {3,20},
93 {3,21}, {4,20}, {4,21}, {5,22}, {5,24}, {6,24},
94 {2,34}, {2,35}, {3,34}, {3,35}
95 };
96}
97
99{
100 constexpr std::size_t rows = 5;
101 constexpr std::size_t cols = 5;
103 const ca_size_t r0 = frame.size(0) / 2;
104 const ca_size_t c0 = frame.size(1) / 2;
105 for (std::size_t r = 0; r < rows; ++r)
106 for (std::size_t c = 0; c < cols; ++c)
107 {
108 const int value = frame.at({static_cast<ca_index_t>(r0 + r),
109 static_cast<ca_index_t>(c0 + c)});
110 graph.set_node(r * cols + c, value);
111 }
112 return graph;
113}
114}
115
116int main(int argc, char **argv)
117{
118 const std::filesystem::path out_dir = argc >= 2 ? argv[1] : "ca_gallery_output";
119 const std::size_t steps = argc >= 3 ? parse_size(argv[2], 200) : 200;
120 const bool live = argc >= 4 and std::string(argv[3]) == "--live";
121 std::filesystem::create_directories(out_dir);
122
123 Grid initial({40, 80}, 0);
125 auto engine = make_gol_engine(std::move(initial));
126
127 const auto palette = Binary_RGB_Mapper<int>{0, {248, 250, 252}, {5, 28, 44}};
128 Png_Frame_Sink png(out_dir / "png" / "life_{step}.png", palette, 4);
129 Gif_Frame_Sink gif(out_dir / "life.gif", palette, GIF_Write_Options{5, true});
131 HTML_Player_Options{"Gosper glider gun", 12, 7, false});
132
134 xopts.enabled = live;
135 xopts.title = "Aleph::CA gallery";
136 xopts.cell_size = 8;
138
139 for (std::size_t t = 0; t <= steps; ++t)
140 {
141 if (t % 4 == 0)
142 {
143 png.accept(engine.steps_run(), engine.frame());
144 gif.accept(engine.steps_run(), engine.frame());
145 html.accept(engine.steps_run(), engine.frame());
146 }
147 x11.accept(engine.steps_run(), engine.frame());
148 if (t != steps)
149 engine.step();
150 }
151 gif.flush();
152 html.flush();
153 x11.flush();
154
155 {
156 std::ofstream out(out_dir / "life.svg");
158 opts.cell_size = 10.0;
159 opts.omit_dead_cells = false;
161 }
162 {
163 std::ofstream out(out_dir / "life.npy", std::ios::binary);
164 write_npy(out, engine.frame());
165 }
166 {
167 std::ofstream out(out_dir / "life.vtk");
168 write_vtk_legacy(out, engine.frame(), VTK_Write_Options{"GoL final frame", "alive"});
169 }
170 {
171 std::ofstream out(out_dir / "life.txt");
173 opts.include_border = true;
174 render_ascii(out, engine.frame(), Binary_ASCII_Palette<int>{0, ".", "#"}, opts);
175 }
176 {
177 std::ofstream out(out_dir / "life.dot");
179 graph_sample_from(engine.frame()),
180 Binary_DOT_Palette<int>{0, "white", "black"});
181 }
182 {
184 opts.auto_start = false;
186 std::ofstream out(out_dir / "ffmpeg-command.txt");
187 out << ffmpeg.command() << '\n';
188 }
189
190 std::cout << "Gallery written to " << out_dir << '\n';
191 std::cout << "Open life.html in a browser, life.gif in an image viewer, "
192 << "and life.vtk in ParaView.\n";
193 return 0;
194}
int main()
Terminal and golden-test renderers for CA frames.
size_t steps
Definition ca-c-api.h:126
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t row
Definition ca-c-api.h:115
size_t * rows
Definition ca-c-api.h:112
size_t size_t col
Definition ca-c-api.h:116
size_t cols
Definition ca-c-api.h:105
GraphViz DOT renderer for graph cellular automata.
Convenience builders for the Phase 3 synchronous engine.
Runtime ffmpeg sink for publication-quality CA video.
Animated GIF sink for cellular-automata trajectories.
Self-contained HTML trajectory player for CA frames.
NumPy .npy writer for CA frames.
Dependency-free PNG frame sink for cellular automata.
Native SVG renderer for rectangular, hexagonal and triangular CAs.
VTK legacy writer for scientific CA visualisation.
Optional live X11 viewer sink for CA simulations.
Frame sink that streams PPM frames into ffmpeg.
Sink that buffers frames and writes an animated GIF on flush.
Definition ca-gif.H:337
Graph lattice: one cell per node + precomputed adjacency.
Sink that writes a self-contained HTML playback file.
Lattice that adds boundary-aware access on top of a storage.
Path-pattern sink that writes one PNG file per accepted frame.
Definition ca-png.H:241
Live X11 frame sink.
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
void write_vtk_legacy(std::ostream &out, const Lattice &frame, const VTK_Write_Options &opts={})
Write a 2D or 3D lattice as VTK legacy STRUCTURED_POINTS.
Definition ca-vtk.H:119
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
Array< Array< std::size_t > > make_grid_graph_adjacency(std::size_t rows, std::size_t cols, bool periodic=false)
Build the adjacency of a 2D 4-neighbour grid graph.
void render_ascii(std::ostream &out, const Lattice &frame, Palette &&palette, const Ascii_Render_Options &opts={})
Render a rank-2 frame as text.
Definition ca-ascii.H:168
Game_Of_Life_Engine make_gol_engine(ca_size_t rows, ca_size_t cols)
Build a Conway's Game of Life engine of the given shape.
void render_lattice_svg(std::ostream &out, const Lattice &frame, Palette &&palette, const SVG_Render_Options &opts={}, const typename Lattice::state_type &dead_state=typename Lattice::state_type{})
Render a supported rank-2 CA frame as SVG.
Definition ca-svg.H:205
void render_graph_lattice_dot(std::ostream &out, const Graph &graph, Palette &&palette, const DOT_Render_Options &opts={})
Render a graph CA frame as GraphViz DOT.
Definition ca-dot.H:146
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
void write_npy(std::ostream &out, const Lattice &frame)
Write a lattice frame as a NumPy .npy array.
Definition ca-npy.H:151
and
Check uniqueness with explicit hash + equality functors.
Options for textual CA rendering.
Definition ca-ascii.H:67
bool include_border
draw a simple +---+ border
Definition ca-ascii.H:68
Default binary text palette.
Definition ca-ascii.H:82
Default binary DOT fill-colour palette.
Definition ca-dot.H:75
Default binary RGB mapper.
Definition ca-io.H:245
Options for Ffmpeg_Frame_Sink.
bool auto_start
start ffmpeg in the constructor
Options for animated GIF output.
Definition ca-gif.H:66
Options for generated HTML players.
SVG rendering options.
Definition ca-svg.H:76
double cell_size
logical cell size in SVG units
Definition ca-svg.H:77
The lattice wraps around on every axis.
Definition ca-traits.H:124
Options for VTK legacy output.
Definition ca-vtk.H:62
Options for the live X11 viewer.
bool enabled
disable to force no-op behaviour
static mt19937 engine
gsl_rng * r
CA whose underlying topology is an arbitrary undirected graph.
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).