Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_export_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 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
50#include <cerrno>
51#include <cstdlib>
52#include <filesystem>
53#include <fstream>
54#include <iostream>
55#include <limits>
56#include <string>
57#include <utility>
58#include <vector>
59
60#include <ca-engine-utils.H>
61#include <ca-frame-stream.H>
62#include <ca-io.H>
63#include <ca-tikz.H>
64#include <ca-traits.H>
65#include <tpl_ca_lattice.H>
66#include <tpl_ca_storage.H>
67
68using namespace Aleph::CA;
69
70namespace
71{
73
74 bool parse_steps(const char *text, std::size_t &out)
75 {
76 if (text == nullptr or text[0] == '-')
77 return false;
78 errno = 0;
79 char *end = nullptr;
80 const unsigned long long value = std::strtoull(text, &end, 10);
81 if (errno == ERANGE or end == text or *end != '\0'
82 or value > std::numeric_limits<std::size_t>::max())
83 return false;
84 out = static_cast<std::size_t>(value);
85 return true;
86 }
87
88 std::vector<std::pair<int, int>> pattern_for(const std::string &name)
89 {
90 if (name == "blinker")
91 return {{0, -1}, {0, 0}, {0, 1}};
92 if (name == "r-pentomino")
93 return {{0, 1}, {0, 2}, {1, 0}, {1, 1}, {2, 1}};
94 if (name == "gosper")
95 return {
96 {4,0}, {4,1}, {5,0}, {5,1},
97 {2,12}, {2,13}, {3,11}, {3,15}, {4,10}, {4,16},
98 {5,10}, {5,14}, {5,16}, {5,17}, {6,10}, {6,16},
99 {7,11}, {7,15}, {8,12}, {8,13},
100 {0,24}, {1,22}, {1,24}, {2,20}, {2,21}, {3,20},
101 {3,21}, {4,20}, {4,21}, {5,22}, {5,24}, {6,24},
102 {2,34}, {2,35}, {3,34}, {3,35}
103 };
104 return {{0, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};
105 }
106
107 void stamp(Grid &grid,
110 const std::vector<std::pair<int, int>> &cells)
111 {
112 const auto rows = static_cast<ca_index_t>(grid.size(0));
113 const auto cols = static_cast<ca_index_t>(grid.size(1));
114 for (const auto &[dr, dc] : cells)
115 {
116 const ca_index_t r = ((row + dr) % rows + rows) % rows;
117 const ca_index_t c = ((col + dc) % cols + cols) % cols;
118 grid.set({r, c}, 1);
119 }
120 }
121
122 void write_final_exports(const std::filesystem::path &dir,
123 const Grid &frame,
124 const std::string &pattern,
125 std::size_t steps)
126 {
127 {
128 std::ofstream out(dir / "final.rle");
130 opts.name = pattern + " after " + std::to_string(steps) + " steps";
131 opts.comment = "Generated by Aleph::CA ca_export_example";
132 opts.trim_dead_border = true;
133 write_rle(out, frame, opts);
134 }
135 {
136 std::ofstream out(dir / "final.csv");
137 write_csv(out, frame);
138 }
139 {
140 std::ofstream out(dir / "final.json");
142 opts.pretty = true;
143 opts.rule = "B3/S23";
144 write_json(out, frame, opts);
145 }
146 {
147 std::ofstream out(dir / "final.tex");
149 opts.cell_size = 0.18;
150 opts.grid_colour = "black!25";
152 frame,
153 Binary_Tikz_Palette<int>{0, "white", "black"},
154 opts);
155 }
156 }
157}
158
159int main(int argc, char **argv)
160{
161 std::size_t steps = 32;
162 if (argc >= 2 and not parse_steps(argv[1], steps))
163 {
164 std::cerr << "Invalid steps argument\n";
165 return 1;
166 }
167
168 const std::filesystem::path out_dir = argc >= 3 ? argv[2] : "ca_export_output";
169 const std::string pattern = argc >= 4 ? argv[3] : "glider";
170
171 ca_size_t rows = 32;
172 ca_size_t cols = pattern == "gosper" ? 80 : 48;
173 if (pattern == "gosper")
174 rows = 40;
175
176 Grid grid({rows, cols}, 0);
177 stamp(grid,
178 static_cast<ca_index_t>(rows / 3),
179 static_cast<ca_index_t>(cols / 4),
180 pattern_for(pattern));
181
182 auto engine = make_gol_engine(std::move(grid));
183
184 const auto frames_dir = out_dir / "frames";
187 stream_opts.prefix = "frame";
188 stream_opts.format = Frame_Output_Format::PPM;
189 stream_opts.zero_pad = 4;
190
192 for (std::size_t t = 0; t <= steps; ++t)
193 {
194 frames.accept(engine.steps_run(), engine.frame());
195 if (t != steps)
196 engine.step();
197 }
198 frames.flush();
199
200 std::filesystem::create_directories(out_dir);
201 write_final_exports(out_dir, engine.frame(), pattern, steps);
202
203 std::cout << "Exported " << frames.written_frames().size()
204 << " frames to " << frames_dir << '\n';
205 std::cout << "Final snapshots: " << (out_dir / "final.rle") << ", "
206 << (out_dir / "final.csv") << ", "
207 << (out_dir / "final.json") << ", "
208 << (out_dir / "final.tex") << '\n';
209 std::cout << "Video hint: " << frames.montage_command_hint("life.mp4", 24)
210 << '\n';
211
212 return 0;
213}
int main()
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
Convenience builders for the Phase 3 synchronous engine.
Directory-backed frame sinks for CA trajectories.
File-format readers and writers for cellular-automata frames.
TikZ renderers for cellular-automata frames.
Common typedefs and tag types for the Cellular Automata module.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:365
Directory-backed sink for CA frames.
std::string montage_command_hint(const std::string &output_file="ca.mp4", unsigned fps=24) const
Return an ffmpeg command hint for the current frame sequence.
void accept(std::size_t step, const Lattice &frame)
Accept a frame and write it if it passes the stride filter.
const Array< std::pair< std::size_t, std::filesystem::path > > & written_frames() const noexcept
Return the frame files written so far.
void flush() const
Write the optional manifest.
Lattice that adds boundary-aware access on top of a storage.
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
void write_rle(std::ostream &out, const Lattice &frame, const RLE_Write_Options &opts={}, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Write a 2D frame in Conway RLE format.
Definition ca-io.H:575
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 write_csv(std::ostream &out, const Lattice &frame, const CSV_Options &opts={})
Write a numeric CSV snapshot.
Definition ca-io.H:1207
void write_json(std::ostream &out, const Lattice &frame, const JSON_Write_Options &opts={})
Write a JSON snapshot.
Definition ca-io.H:1314
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
void render_lattice_tikz(std::ostream &out, const Lattice &frame, Palette &&palette, const Rect_Tikz_Options &opts={})
Render any supported 2D CA lattice as TikZ.
Definition ca-tikz.H:225
and
Check uniqueness with explicit hash + equality functors.
Default binary TikZ palette.
Definition ca-tikz.H:92
Options for directory-backed frame streams.
std::filesystem::path directory
destination directory
Options controlling JSON snapshot output.
Definition ca-io.H:196
bool pretty
pretty-print with indentation
Definition ca-io.H:197
Options controlling Conway RLE output.
Definition ca-io.H:159
std::string name
optional #N line
Definition ca-io.H:161
TikZ rendering options for rectangular frames.
Definition ca-tikz.H:74
double cell_size
cell side length in centimetres
Definition ca-tikz.H:75
The lattice wraps around on every axis.
Definition ca-traits.H:124
static mt19937 engine
gsl_rng * r
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).