43std::size_t parse_size(
const char *text,
const std::size_t fallback)
48 const unsigned long value = std::strtoul(text, &end, 10);
49 if (end == text or *end !=
'\0' or
value == 0
50 or
value > std::numeric_limits<std::size_t>::max())
52 return static_cast<std::size_t
>(
value);
55void seed_sphere(Grid3 &grid)
57 const double cz = 0.5 *
static_cast<double>(grid.size(0) - 1);
58 const double cy = 0.5 *
static_cast<double>(grid.size(1) - 1);
59 const double cx = 0.5 *
static_cast<double>(grid.size(2) - 1);
60 const double radius = 0.28 *
static_cast<double>(
61 std::min(grid.size(0), std::min(grid.size(1), grid.size(2))));
63 for (
ca_size_t z = 0; z < grid.size(0); ++z)
65 for (
ca_size_t x = 0; x < grid.size(2); ++x)
67 const double dz =
static_cast<double>(z) - cz;
68 const double dy =
static_cast<double>(
y) - cy;
69 const double dx =
static_cast<double>(x) - cx;
70 if (std::sqrt(dx * dx + dy * dy + dz * dz) <= radius)
84 if (dz == 0
and dy == 0
and dx == 0)
86 count += grid.at_safe({z + dz,
y + dy, x + dx}) != 0 ? 1 : 0;
91void evolve(Grid3 &cur, Grid3 &
next)
93 for (
ca_size_t z = 0; z < cur.size(0); ++z)
95 for (
ca_size_t x = 0; x < cur.size(2); ++x)
100 const int alive = alive_neighbours(cur, iz, iy, ix);
101 const bool current = cur.at({iz, iy, ix}) != 0;
103 or (not current
and alive == 6) ? 1 : 0;
113 const std::filesystem::path out_dir = argc >= 2 ? argv[1] :
"ca3d_vtk_output";
114 const std::size_t
steps = argc >= 3 ? parse_size(argv[2], 16) : 16;
116 std::filesystem::create_directories(out_dir);
118 Grid3 cur({n, n, n}, 0);
119 Grid3
next({n, n, n}, 0);
123 opts.
title =
"Aleph::CA 3D growth";
126 for (std::size_t step = 0; step <=
steps; ++step)
128 const auto path = out_dir / (
"ca3d_" + std::to_string(step) +
".vtk");
129 std::ofstream
out(path);
135 std::cout <<
"Wrote " << (
steps + 1) <<
" VTK frames to " << out_dir <<
'\n';
136 std::cout <<
"Open ca3d_0.vtk in ParaView and enable file-series loading.\n";
size_t size_t int32_t value
size_t size_t int32_t * out
VTK legacy writer for scientific CA visualisation.
Lattice that adds boundary-aware access on top of a storage.
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.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
decltype(auto) current(It &it) noexcept(current_is_noexcept< It >)
Current item of an iterator that is positioned on one.
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Out-of-range neighbours behave as if the lattice ended.
Options for VTK legacy output.
std::string scalar_name
scalar field name
std::string title
VTK header title.
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).