Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_ca_hex_lattice_test.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 ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
47#include <array>
48#include <cstdint>
49#include <set>
50#include <string>
51
52#include <gtest/gtest.h>
53
54#include <ca-traits.H>
55#include <tpl_ca_concepts.H>
56#include <tpl_ca_engine.H>
57#include <tpl_ca_hex_lattice.H>
58#include <tpl_ca_neighborhood.H>
59#include <tpl_ca_rule.H>
60#include <tpl_ca_storage.H>
61
62using namespace Aleph;
63using namespace Aleph::CA;
64
65// ---------------------------------------------------------------------------
66// Concept conformance.
67// ---------------------------------------------------------------------------
68
70static_assert(LatticeLike<Hex_2D>);
71
72// ---------------------------------------------------------------------------
73// Coord conversions.
74// ---------------------------------------------------------------------------
75
77{
78 for (ca_index_t q = -5; q <= 5; ++q)
79 for (ca_index_t r = -5; r <= 5; ++r)
80 {
81 const auto cube = axial_to_cube(Hex_Axial{q, r});
82 EXPECT_EQ(cube.x + cube.y + cube.z, 0);
84 }
85}
86
88{
89 for (ca_index_t q = -4; q <= 4; ++q)
90 for (ca_index_t r = -4; r <= 4; ++r)
91 {
92 const Hex_Axial a{q, r};
94 }
95}
96
98{
99 for (ca_index_t q = -4; q <= 4; ++q)
100 for (ca_index_t r = -4; r <= 4; ++r)
101 {
102 const Hex_Axial a{q, r};
104 }
105}
106
108{
109 const Hex_Axial origin{0, 0};
110 // Each of the six neighbours is at distance 1.
111 for (const auto &off : Hex_Neighborhood::offsets)
112 {
113 const Hex_Axial n{origin.q + off[0], origin.r + off[1]};
114 EXPECT_EQ(hex_distance(origin, n), 1u);
115 }
116 // Distance is symmetric and zero for self.
117 EXPECT_EQ(hex_distance(origin, origin), 0u);
118 EXPECT_EQ(hex_distance(Hex_Axial{2, -1}, Hex_Axial{-1, 2}), 3u);
119}
120
122{
123 std::set<std::pair<long long, long long>> seen;
124 for (ca_index_t r = 0; r < 6; ++r)
125 for (ca_index_t q = 0; q < 6; ++q)
126 {
127 const auto px = axial_to_pixel_pointy(Hex_Axial{q, r}, 1.0);
128 const auto key = std::pair<long long, long long>(
129 static_cast<long long>(px[0] * 1e6),
130 static_cast<long long>(px[1] * 1e6));
131 EXPECT_TRUE(seen.insert(key).second)
132 << "duplicate pixel for (q=" << q << ", r=" << r << ")";
133 }
134}
135
136// ---------------------------------------------------------------------------
137// Hex_Lattice basics.
138// ---------------------------------------------------------------------------
139
141{
142 Hex_2D lat({4, 5}, 0);
143 for (ca_index_t r = 0; r < 4; ++r)
144 for (ca_index_t q = 0; q < 5; ++q)
145 lat.set_axial(Hex_Axial{q, r}, static_cast<int>(q + r * 10));
146
147 for (ca_index_t r = 0; r < 4; ++r)
148 for (ca_index_t q = 0; q < 5; ++q)
149 EXPECT_EQ(lat.at_axial(Hex_Axial{q, r}), q + r * 10);
150}
151
153{
154 Hex_2D lat({4, 5}, 0);
155 // Use the offset accessor to write, axial to read back.
157 // Even-r: (col=2, row=1) -> (q = 2 - (1 + 1)/2 = 1, r = 1)
158 EXPECT_EQ(lat.at_axial(Hex_Axial{1, 1}), 7);
159}
160
162{
163 Hex_2D lat({3, 3}, 5);
164 EXPECT_EQ(lat.at_axial_safe(Hex_Axial{-1, 0}), 0);
165 EXPECT_EQ(lat.at_axial_safe(Hex_Axial{0, 0}), 5);
166}
167
168// ---------------------------------------------------------------------------
169// Engine integration: Bays' B2/S34 hex life on a small toroidal grid.
170// ---------------------------------------------------------------------------
171
172namespace {
173
174struct Bays_B2_S34_Functor
175{
176 template <typename State>
177 [[nodiscard]] constexpr State operator()(const State &current,
178 const std::size_t alive) const noexcept
179 {
180 const bool alive_now = current != State{};
181 const bool next_alive = (alive_now and (alive == 3 or alive == 4))
182 or (not alive_now and alive == 2);
183 return next_alive ? static_cast<State>(1) : static_cast<State>(0);
184 }
185};
186
187} // namespace
188
190{
192 L seed({6, 6}, 0);
193 seed.set_axial(Hex_Axial{2, 2}, 1);
194
197 seed, Rule{Bays_B2_S34_Functor{}}, Hex_Neighborhood{});
198
199 eng.run(1);
200 // Cell with 0 alive neighbours dies.
201 EXPECT_EQ(eng.frame().at_axial(Hex_Axial{2, 2}), 0);
202 // No neighbour had two alive neighbours, so nothing was born either.
203 for (ca_index_t r = 0; r < 6; ++r)
204 for (ca_index_t q = 0; q < 6; ++q)
205 EXPECT_EQ(eng.frame().at_axial(Hex_Axial{q, r}), 0);
206}
207
209{
210 // With B2 and two adjacent cells, the cells that touch *both* are
211 // exactly the two common neighbours of those two cells. They are
212 // born; meanwhile the original two cells have only 1 alive
213 // neighbour each (each other), so under S34 they die. After one
214 // step we therefore have exactly the two common-neighbour cells.
216 L seed({6, 6}, 0);
217 seed.set_axial(Hex_Axial{2, 2}, 1);
218 seed.set_axial(Hex_Axial{3, 2}, 1); // (+1, 0) neighbour of (2, 2)
219
222 seed, Rule{Bays_B2_S34_Functor{}}, Hex_Neighborhood{});
223
224 eng.run(1);
225 // The two starter cells must be dead (S34, only 1 neighbour).
226 EXPECT_EQ(eng.frame().at_axial(Hex_Axial{2, 2}), 0);
227 EXPECT_EQ(eng.frame().at_axial(Hex_Axial{3, 2}), 0);
228 // The two common neighbours of (2,2) and (3,2) are (3,1) and
229 // (2,3); they must have been born.
230 EXPECT_EQ(eng.frame().at_axial(Hex_Axial{3, 1}), 1);
231 EXPECT_EQ(eng.frame().at_axial(Hex_Axial{2, 3}), 1);
232}
233
234// ---------------------------------------------------------------------------
235// TikZ export sanity.
236// ---------------------------------------------------------------------------
237
239{
240 Hex_2D lat({2, 2}, 0);
241 lat.set_axial(Hex_Axial{0, 0}, 1);
242 const std::string out = render_hex_lattice_tikz(
243 lat, [](int v) { return v != 0 ? std::string("black") : std::string("white"); });
244 EXPECT_NE(out.find("\\begin{tikzpicture}"), std::string::npos);
245 EXPECT_NE(out.find("\\end{tikzpicture}"), std::string::npos);
246 EXPECT_NE(out.find("regular polygon sides=6"), std::string::npos);
247 EXPECT_NE(out.find("fill=black"), std::string::npos);
248 EXPECT_NE(out.find("fill=white"), std::string::npos);
249}
size_t size_t int32_t * out
Definition ca-c-api.h:120
Common typedefs and tag types for the Cellular Automata module.
Hexagonal lattice with axial accessors over a 2D storage.
void set_axial(Hex_Axial a, const state_type &v)
Write cell value at axial coords (q, r) (strict).
void set_offset_even_r(Hex_Offset o, const state_type &v)
Convenience: write using the even-r offset convention.
Six-neighbour hex pattern in axial coordinates over a 2D lattice.
static constexpr std::array< Offset_Vec< 2 >, 6 > offsets
The six axial-coordinate offsets of the hex neighborhood.
Rule whose next state depends on (current, alive_count).
Definition tpl_ca_rule.H:99
Synchronous double-buffered engine.
void run(const std::size_t steps)
Run several synchronous steps.
#define TEST(name)
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
constexpr Hex_Offset axial_to_offset_even_r(Hex_Axial a) noexcept
Axial -> offset (even-r convention: even rows are shifted right).
constexpr Hex_Axial cube_to_axial(Hex_Cube c) noexcept
Cube -> axial.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
std::array< double, 2 > axial_to_pixel_pointy(Hex_Axial a, double radius) noexcept
Pointy-top pixel coordinates of an axial cell.
void render_hex_lattice_tikz(std::ostream &os, const Lattice &lat, Palette &&palette, double radius=0.5, bool pointy_top=true)
Render a hex frame as a stand-alone TikZ picture.
constexpr Hex_Offset axial_to_offset_odd_r(Hex_Axial a) noexcept
Axial -> offset (odd-r convention: odd rows are shifted right).
constexpr Hex_Axial offset_to_axial_even_r(Hex_Offset o) noexcept
Offset (even-r) -> axial.
constexpr ca_size_t hex_distance(Hex_Axial a, Hex_Axial b) noexcept
Hex distance between two axial coordinates.
constexpr Hex_Cube axial_to_cube(Hex_Axial a) noexcept
Axial -> cube.
constexpr Hex_Axial offset_to_axial_odd_r(Hex_Offset o) noexcept
Offset (odd-r) -> axial.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
Axial integer coordinates (q, r) of a hex cell.
ca_index_t q
column index
Offset coordinates (col, row) for visualisation.
Out-of-range neighbours behave as if the lattice ended.
Definition ca-traits.H:119
The lattice wraps around on every axis.
Definition ca-traits.H:124
ValueArg< size_t > seed
Definition testHash.C:53
gsl_rng * r
C++20 concepts for the Cellular Automata module.
Synchronous double-buffered engine for cellular automata.
Hexagonal lattice with axial / offset / cube coordinate conversions and a TikZ-compatible pixel mappi...
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).