Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_paraview_3d_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
24#include <cmath>
25#include <algorithm>
26#include <cstdlib>
27#include <filesystem>
28#include <fstream>
29#include <iostream>
30#include <limits>
31#include <string>
32
33#include <ca-vtk.H>
34#include <tpl_ca_lattice.H>
35#include <tpl_ca_storage.H>
36
37using namespace Aleph::CA;
38
39namespace
40{
42
43std::size_t parse_size(const char *text, const std::size_t fallback)
44{
45 if (text == nullptr)
46 return fallback;
47 char *end = nullptr;
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())
51 return fallback;
52 return static_cast<std::size_t>(value);
53}
54
56{
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))));
62
63 for (ca_size_t z = 0; z < grid.size(0); ++z)
64 for (ca_size_t y = 0; y < grid.size(1); ++y)
65 for (ca_size_t x = 0; x < grid.size(2); ++x)
66 {
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)
71 grid.set({static_cast<ca_index_t>(z),
72 static_cast<ca_index_t>(y),
73 static_cast<ca_index_t>(x)}, 1);
74 }
75}
76
77int alive_neighbours(const Grid3 &grid, const ca_index_t z, const ca_index_t y, const ca_index_t x)
78{
79 int count = 0;
80 for (ca_index_t dz = -1; dz <= 1; ++dz)
81 for (ca_index_t dy = -1; dy <= 1; ++dy)
82 for (ca_index_t dx = -1; dx <= 1; ++dx)
83 {
84 if (dz == 0 and dy == 0 and dx == 0)
85 continue;
86 count += grid.at_safe({z + dz, y + dy, x + dx}) != 0 ? 1 : 0;
87 }
88 return count;
89}
90
91void evolve(Grid3 &cur, Grid3 &next)
92{
93 for (ca_size_t z = 0; z < cur.size(0); ++z)
94 for (ca_size_t y = 0; y < cur.size(1); ++y)
95 for (ca_size_t x = 0; x < cur.size(2); ++x)
96 {
97 const ca_index_t iz = static_cast<ca_index_t>(z);
98 const ca_index_t iy = static_cast<ca_index_t>(y);
99 const ca_index_t ix = static_cast<ca_index_t>(x);
100 const int alive = alive_neighbours(cur, iz, iy, ix);
101 const bool current = cur.at({iz, iy, ix}) != 0;
102 const int value = (current and alive >= 5 and alive <= 10)
103 or (not current and alive == 6) ? 1 : 0;
104 next.set({iz, iy, ix}, value);
105 }
106 cur.swap(next);
107 next.fill(0);
108}
109}
110
111int main(int argc, char **argv)
112{
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;
115 const ca_size_t n = static_cast<ca_size_t>(argc >= 4 ? parse_size(argv[3], 32) : 32);
116 std::filesystem::create_directories(out_dir);
117
118 Grid3 cur({n, n, n}, 0);
119 Grid3 next({n, n, n}, 0);
121
123 opts.title = "Aleph::CA 3D growth";
124 opts.scalar_name = "alive";
125
126 for (std::size_t step = 0; step <= steps; ++step)
127 {
128 const auto path = out_dir / ("ca3d_" + std::to_string(step) + ".vtk");
129 std::ofstream out(path);
131 if (step != steps)
132 evolve(cur, next);
133 }
134
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";
137 return 0;
138}
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
VTK legacy writer for scientific CA visualisation.
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
static mpfr_t y
Definition mpfr_mul_d.c:3
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
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:171
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Out-of-range neighbours behave as if the lattice ended.
Definition ca-traits.H:119
Options for VTK legacy output.
Definition ca-vtk.H:62
std::string title
VTK header title.
Definition ca-vtk.H:63
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).